Mercurial > hg > monetdb-java
annotate example/OnClientExample.java @ 551:5ce6a942aff3 onclient
Last minute API fix: 'int offset' -> 'long linesToSkip'
Int -> long because there might be many lines
Offset -> linesToSkip to save users some work.
The COPY INTO statement has an OFFSET modifier which is 1-based
but also allows 0. This means both 0 and 1 mean 'upload the whole
file' and any N > 1 means skip N-1 lines and upload the rest.
To avoid having to deal with this over and over in every implementation
of MonetConnection.UploadHandler#handleUpload(), parameter 'offset'
has been replaced with 'linesToSkip' where the adjustment has already
been performed.
author | Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com> |
---|---|
date | Tue, 14 Sep 2021 10:52:04 +0200 (2021-09-14) |
parents | d462000fc410 |
children | 87feb93330a6 |
rev | line source |
---|---|
542
d462000fc410
Various changes suggested by Martin van Dinther
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
541
diff
changeset
|
1 /* |
d462000fc410
Various changes suggested by Martin van Dinther
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
541
diff
changeset
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
d462000fc410
Various changes suggested by Martin van Dinther
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
541
diff
changeset
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
d462000fc410
Various changes suggested by Martin van Dinther
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
541
diff
changeset
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
d462000fc410
Various changes suggested by Martin van Dinther
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
541
diff
changeset
|
5 * |
d462000fc410
Various changes suggested by Martin van Dinther
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
541
diff
changeset
|
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V. |
d462000fc410
Various changes suggested by Martin van Dinther
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
541
diff
changeset
|
7 */ |
d462000fc410
Various changes suggested by Martin van Dinther
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
541
diff
changeset
|
8 |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
9 import org.monetdb.jdbc.MonetConnection; |
541
31df6a12fd41
Make the MonetUploadHandler and MonetDownloadHandler interfaces part of MonetConnection
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
536
diff
changeset
|
10 import org.monetdb.jdbc.MonetConnection.UploadHandler; |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
11 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
12 import java.io.BufferedReader; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
13 import java.io.IOException; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
14 import java.io.InputStream; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
15 import java.io.PrintStream; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
16 import java.nio.file.FileSystems; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
17 import java.nio.file.Files; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
18 import java.nio.file.Path; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
19 import java.sql.*; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
20 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
21 public class OnClientExample { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
22 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
23 public static void main(String[] args) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
24 int status = 0; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
25 try { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
26 // Ideally this would not be hardcoded.. |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
27 final String dbUrl = "jdbc:monetdb://localhost:55000/banana"; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
28 final String uploadDir = "/home/jvr/mydata"; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
29 final boolean filesAreUtf8 = false; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
30 final String userName = "monetdb"; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
31 final String password = "monetdb"; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
32 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
33 status = run(dbUrl, userName, password, uploadDir, filesAreUtf8); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
34 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
35 } catch (Exception e) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
36 status = 1; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
37 e.printStackTrace(); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
38 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
39 System.exit(status); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
40 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
41 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
42 private static int run(String dbUrl, String userName, String password, String uploadDir, boolean filesAreUtf8) throws ClassNotFoundException, SQLException { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
43 int status = 0; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
44 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
45 // Connect |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
46 Class.forName("org.monetdb.jdbc.MonetDriver"); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
47 Connection conn = DriverManager.getConnection(dbUrl, userName, password); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
48 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
49 // Register upload handler |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
50 MyUploader handler = new MyUploader(uploadDir, filesAreUtf8); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
51 conn.unwrap(MonetConnection.class).setUploadHandler(handler); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
52 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
53 // Run some SQL statements involving ON CLIENT |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
54 String[] queries = { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
55 "DROP TABLE IF EXISTS bar", |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
56 "CREATE TABLE bar(i INT, t TEXT)", |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
57 "COPY INTO bar FROM 'generated.csv' ON CLIENT", |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
58 "COPY INTO bar FROM 'file.csv' ON CLIENT", |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
59 // following statement will run even if file.csv does not exist |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
60 "SELECT COUNT(*) FROM bar", |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
61 }; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
62 Statement stmt = conn.createStatement(); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
63 for (String q : queries) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
64 System.out.println(q); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
65 try { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
66 stmt.execute(q); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
67 ResultSet rs = stmt.getResultSet(); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
68 if (rs == null) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
69 System.out.printf(" OK, %d rows updated%n", stmt.getUpdateCount()); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
70 } else { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
71 long count = 0; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
72 while (rs.next()) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
73 count++; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
74 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
75 System.out.printf(" OK, returned %d rows%n", count); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
76 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
77 } catch (SQLNonTransientException e) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
78 throw e; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
79 } catch (SQLException e) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
80 System.out.println(" => SQL ERROR " + e.getMessage()); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
81 status = 1; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
82 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
83 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
84 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
85 return status; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
86 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
87 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
88 |
541
31df6a12fd41
Make the MonetUploadHandler and MonetDownloadHandler interfaces part of MonetConnection
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
536
diff
changeset
|
89 private static class MyUploader implements UploadHandler { |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
90 private final Path uploadDir; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
91 private final boolean filesAreUtf8; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
92 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
93 public MyUploader(String uploadDir, boolean filesAreUtf8) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
94 this.uploadDir = FileSystems.getDefault().getPath(uploadDir).normalize(); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
95 this.filesAreUtf8 = filesAreUtf8; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
96 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
97 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
98 @Override |
551
5ce6a942aff3
Last minute API fix: 'int offset' -> 'long linesToSkip'
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
542
diff
changeset
|
99 public void handleUpload(MonetConnection.Upload handle, String name, boolean textMode, long linesToSkip) throws IOException { |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
100 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
101 // COPY OFFSET line numbers are 1-based but 0 is also allowed. |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
102 // Compute the number of lines to skip |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
103 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
104 // We can upload data read from the file system but also make up our own data |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
105 if (name.equals("generated.csv")) { |
551
5ce6a942aff3
Last minute API fix: 'int offset' -> 'long linesToSkip'
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
542
diff
changeset
|
106 uploadGenerated(handle, linesToSkip); |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
107 return; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
108 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
109 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
110 // Validate the path, demonstrating two ways of dealing with errors |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
111 Path path = securityCheck(name); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
112 if (path == null || !Files.exists(path)) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
113 // This makes the COPY command fail but keeps the connection alive. |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
114 // Can only be used if we haven't sent any data yet |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
115 handle.sendError("Invalid path"); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
116 return; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
117 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
118 if (!Files.isReadable(path)) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
119 // As opposed to handle.sendError(), throwing an IOException ends the whole connection. |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
120 throw new IOException("Unreadable: " + path); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
121 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
122 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
123 boolean binary = !textMode; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
124 if (binary) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
125 uploadBinary(handle, path); |
551
5ce6a942aff3
Last minute API fix: 'int offset' -> 'long linesToSkip'
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
542
diff
changeset
|
126 } else if (linesToSkip == 0 && filesAreUtf8) { |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
127 // Avoid unnecessary character set conversions by pretending it's binary |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
128 uploadBinary(handle, path); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
129 } else { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
130 // Charset and skip handling really necessary |
551
5ce6a942aff3
Last minute API fix: 'int offset' -> 'long linesToSkip'
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
542
diff
changeset
|
131 uploadTextFile(handle, path, linesToSkip); |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
132 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
133 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
134 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
135 private Path securityCheck(String name) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
136 Path p = uploadDir.resolve(name).normalize(); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
137 if (p.startsWith(uploadDir)) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
138 return p; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
139 } else { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
140 return null; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
141 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
142 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
143 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
144 private void uploadGenerated(MonetConnection.Upload handle, long toSkip) throws IOException { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
145 PrintStream stream = handle.getStream(); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
146 for (long i = toSkip + 1; i <= 100; i++) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
147 stream.printf("%d|the number is %d%n", i, i); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
148 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
149 stream.close(); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
150 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
151 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
152 private void uploadTextFile(MonetConnection.Upload handle, Path path, long toSkip) throws IOException { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
153 BufferedReader reader = Files.newBufferedReader(path);// Converts from system encoding to Java text |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
154 for (long i = 0; i < toSkip; i++) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
155 reader.readLine(); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
156 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
157 handle.uploadFrom(reader); // Converts from Java text to UTF-8 as required by MonetDB |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
158 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
159 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
160 private void uploadBinary(MonetConnection.Upload handle, Path path) throws IOException { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
161 // No charset conversion whatsoever.. |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
162 // Use this for binary data or when you are certain the file is UTF-8 encoded. |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
163 InputStream stream = Files.newInputStream(path); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
164 handle.uploadFrom(stream); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
165 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
166 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
167 } |