Mercurial > hg > monetdb-java
annotate example/OnClientExample.java @ 541:31df6a12fd41 onclient
Make the MonetUploadHandler and MonetDownloadHandler interfaces part of MonetConnection
And rename them to UploadHandler and DownloadHandler
author | Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com> |
---|---|
date | Thu, 02 Sep 2021 14:27:20 +0200 (2021-09-02) |
parents | daf6a3b828f9 |
children | d462000fc410 |
rev | line source |
---|---|
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
1 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
|
2 import org.monetdb.jdbc.MonetConnection.UploadHandler; |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
3 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
4 import java.io.BufferedReader; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
5 import java.io.IOException; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
6 import java.io.InputStream; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
7 import java.io.PrintStream; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
8 import java.nio.file.FileSystems; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
9 import java.nio.file.Files; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
10 import java.nio.file.Path; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
11 import java.sql.*; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
12 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
13 public class OnClientExample { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
14 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
15 public static void main(String[] args) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
16 int status = 0; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
17 try { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
18 // Ideally this would not be hardcoded.. |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
19 final String dbUrl = "jdbc:monetdb://localhost:55000/banana"; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
20 final String uploadDir = "/home/jvr/mydata"; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
21 final boolean filesAreUtf8 = false; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
22 final String userName = "monetdb"; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
23 final String password = "monetdb"; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
24 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
25 status = run(dbUrl, userName, password, uploadDir, filesAreUtf8); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
26 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
27 } catch (Exception e) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
28 status = 1; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
29 e.printStackTrace(); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
30 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
31 System.exit(status); |
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 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
34 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
|
35 int status = 0; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
36 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
37 // Connect |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
38 Class.forName("org.monetdb.jdbc.MonetDriver"); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
39 Connection conn = DriverManager.getConnection(dbUrl, userName, password); |
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 // Register upload handler |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
42 MyUploader handler = new MyUploader(uploadDir, filesAreUtf8); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
43 conn.unwrap(MonetConnection.class).setUploadHandler(handler); |
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 // Run some SQL statements involving ON CLIENT |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
46 String[] queries = { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
47 "DROP TABLE IF EXISTS bar", |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
48 "CREATE TABLE bar(i INT, t TEXT)", |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
49 "COPY INTO bar FROM 'generated.csv' ON CLIENT", |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
50 "COPY INTO bar FROM 'file.csv' ON CLIENT", |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
51 // 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
|
52 "SELECT COUNT(*) FROM bar", |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
53 }; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
54 Statement stmt = conn.createStatement(); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
55 for (String q : queries) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
56 System.out.println(q); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
57 try { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
58 stmt.execute(q); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
59 ResultSet rs = stmt.getResultSet(); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
60 if (rs == null) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
61 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
|
62 } else { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
63 long count = 0; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
64 while (rs.next()) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
65 count++; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
66 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
67 System.out.printf(" OK, returned %d rows%n", count); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
68 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
69 } catch (SQLNonTransientException e) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
70 throw e; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
71 } catch (SQLException e) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
72 System.out.println(" => SQL ERROR " + e.getMessage()); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
73 status = 1; |
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 } |
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 return status; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
78 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
79 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
80 |
541
31df6a12fd41
Make the MonetUploadHandler and MonetDownloadHandler interfaces part of MonetConnection
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
536
diff
changeset
|
81 private static class MyUploader implements UploadHandler { |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
82 private final Path uploadDir; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
83 private final boolean filesAreUtf8; |
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 public MyUploader(String uploadDir, boolean filesAreUtf8) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
86 this.uploadDir = FileSystems.getDefault().getPath(uploadDir).normalize(); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
87 this.filesAreUtf8 = filesAreUtf8; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
88 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
89 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
90 @Override |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
91 public void handleUpload(MonetConnection.Upload handle, String name, boolean textMode, int offset) throws IOException { |
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 // 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
|
94 // Compute the number of lines to skip |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
95 long toSkip = offset <= 1 ? 0 : offset - 1; |
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 // 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
|
98 if (name.equals("generated.csv")) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
99 uploadGenerated(handle, toSkip); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
100 return; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
101 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
102 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
103 // 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
|
104 Path path = securityCheck(name); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
105 if (path == null || !Files.exists(path)) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
106 // 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
|
107 // 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
|
108 handle.sendError("Invalid path"); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
109 return; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
110 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
111 if (!Files.isReadable(path)) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
112 // 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
|
113 throw new IOException("Unreadable: " + path); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
114 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
115 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
116 boolean binary = !textMode; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
117 if (binary) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
118 uploadBinary(handle, path); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
119 } else if (toSkip == 0 && filesAreUtf8) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
120 // 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
|
121 uploadBinary(handle, path); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
122 } else { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
123 // Charset and skip handling really necessary |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
124 uploadTextFile(handle, path, toSkip); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
125 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
126 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
127 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
128 private Path securityCheck(String name) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
129 Path p = uploadDir.resolve(name).normalize(); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
130 if (p.startsWith(uploadDir)) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
131 return p; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
132 } else { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
133 return null; |
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 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
136 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
137 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
|
138 PrintStream stream = handle.getStream(); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
139 for (long i = toSkip + 1; i <= 100; i++) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
140 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
|
141 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
142 stream.close(); |
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 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
145 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
|
146 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
|
147 for (long i = 0; i < toSkip; i++) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
148 reader.readLine(); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
149 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
150 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
|
151 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
152 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
153 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
|
154 // No charset conversion whatsoever.. |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
155 // 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
|
156 InputStream stream = Files.newInputStream(path); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
157 handle.uploadFrom(stream); |
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 } |