Mercurial > hg > monetdb-java
annotate example/OnClientExample.java @ 558:ebf65f416da9 onclient
Many improvements to the example code
author | Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com> |
---|---|
date | Thu, 16 Sep 2021 15:12:49 +0200 (2021-09-16) |
parents | 87feb93330a6 |
children | 6cb395daa7d1 |
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) { |
556
87feb93330a6
Misc. changes suggested by analysis tool
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
551
diff
changeset
|
24 int status; |
517
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.. |
558
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
27 final String dbUrl = "jdbc:monetdb://localhost:50000/demo"; |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
28 final String userName = "monetdb"; |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
29 final String password = "monetdb"; |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
30 final String uploadDir = "/home/jvr/mydata"; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
31 final boolean filesAreUtf8 = false; |
558
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
32 String[] queries = { |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
33 "DROP TABLE IF EXISTS mytable", |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
34 "CREATE TABLE mytable(i INT, t TEXT)", |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
35 "COPY INTO mytable FROM 'generated.csv' ON CLIENT", |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
36 "COPY 20 RECORDS OFFSET 5 INTO mytable FROM 'generated.csv' ON CLIENT", |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
37 "COPY INTO mytable FROM 'nonexistentfilethatdoesnotexist.csv' ON CLIENT", |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
38 "SELECT COUNT(*) FROM mytable", |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
39 }; |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
40 |
558
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
41 status = run(dbUrl, userName, password, uploadDir, filesAreUtf8, queries); |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
42 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
43 } catch (Exception e) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
44 status = 1; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
45 e.printStackTrace(); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
46 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
47 System.exit(status); |
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 |
558
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
50 private static int run(String dbUrl, String userName, String password, String uploadDir, boolean filesAreUtf8, String[] queries) throws ClassNotFoundException, SQLException { |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
51 int status = 0; |
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 // Connect |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
54 Class.forName("org.monetdb.jdbc.MonetDriver"); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
55 Connection conn = DriverManager.getConnection(dbUrl, userName, password); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
56 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
57 // Register upload handler |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
58 MyUploader handler = new MyUploader(uploadDir, filesAreUtf8); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
59 conn.unwrap(MonetConnection.class).setUploadHandler(handler); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
60 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
61 Statement stmt = conn.createStatement(); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
62 for (String q : queries) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
63 System.out.println(q); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
64 try { |
558
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
65 boolean hasResultSet = stmt.execute(q); |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
66 if (hasResultSet) { |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
67 ResultSet rs = stmt.getResultSet(); |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
68 long count = 0; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
69 while (rs.next()) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
70 count++; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
71 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
72 System.out.printf(" OK, returned %d rows%n", count); |
558
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
73 } else { |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
74 System.out.printf(" OK, updated %d rows%n", stmt.getUpdateCount()); |
517
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 } catch (SQLNonTransientException e) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
77 throw e; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
78 } catch (SQLException e) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
79 System.out.println(" => SQL ERROR " + e.getMessage()); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
80 status = 1; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
81 } |
558
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
82 |
517
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; |
558
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
92 private boolean stopUploading = false; |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
93 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
94 public MyUploader(String uploadDir, boolean filesAreUtf8) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
95 this.uploadDir = FileSystems.getDefault().getPath(uploadDir).normalize(); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
96 this.filesAreUtf8 = filesAreUtf8; |
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 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
99 @Override |
558
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
100 public void uploadCancelled() { |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
101 System.out.println(" CANCELLATION CALLBACK: server cancelled the upload"); |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
102 stopUploading = true; |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
103 } |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
104 |
558
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
105 @Override |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
106 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
|
107 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
108 // 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
|
109 if (name.equals("generated.csv")) { |
558
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
110 uploadGeneratedData(handle, linesToSkip); |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
111 return; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
112 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
113 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
114 // 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
|
115 Path path = securityCheck(name); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
116 if (path == null || !Files.exists(path)) { |
558
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
117 // This makes the COPY command fail but keeps the connection |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
118 // alive. Can only be used if we haven't sent any data yet |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
119 handle.sendError("Invalid path"); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
120 return; |
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 if (!Files.isReadable(path)) { |
558
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
123 // As opposed to handle.sendError(), we can throw an IOException |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
124 // at any time. Unfortunately, the file upload protocol does not |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
125 // provide a way to indicate to the server that the data sent so |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
126 // far is incomplete, so for the time being throwing an |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
127 // IOException from {@handleUpload} terminates the connection. |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
128 throw new IOException("Unreadable: " + path); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
129 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
130 |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
131 boolean binary = !textMode; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
132 if (binary) { |
558
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
133 uploadAsBinary(handle, path); |
551
5ce6a942aff3
Last minute API fix: 'int offset' -> 'long linesToSkip'
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
542
diff
changeset
|
134 } else if (linesToSkip == 0 && filesAreUtf8) { |
558
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
135 // Avoid unnecessary UTF-8 -> Java String -> UTF-8 conversions |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
136 // by pretending the data is binary. |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
137 uploadAsBinary(handle, path); |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
138 } else { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
139 // Charset and skip handling really necessary |
558
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
140 uploadAsText(handle, path, linesToSkip); |
517
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 Path securityCheck(String name) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
145 Path p = uploadDir.resolve(name).normalize(); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
146 if (p.startsWith(uploadDir)) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
147 return p; |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
148 } else { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
149 return null; |
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 |
558
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
153 private void uploadGeneratedData(MonetConnection.Upload handle, long toSkip) throws IOException { |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
154 // Set the chunk size to a tiny amount so we can demonstrate |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
155 // cancellation handling. The default chunk size is one megabyte. |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
156 // DO NOT DO THIS IN PRODUCTION! |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
157 handle.setChunkSize(50); |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
158 |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
159 // Make up some data and upload it. |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
160 PrintStream stream = handle.getStream(); |
558
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
161 long n = 100; |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
162 System.out.printf(" HANDLER: uploading %d generated lines, numbered %d to %d%n", n - toSkip, toSkip +1, n); |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
163 long i; |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
164 for (i = toSkip + 1; i <= n; i++) { |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
165 if (stopUploading) { |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
166 System.out.printf(" HANDLER: at line %d we noticed the server asked us to stop sending%n", i); |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
167 break; |
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
168 } |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
169 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
|
170 } |
558
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
171 System.out.println(" HANDLER: done uploading"); |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
172 stream.close(); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
173 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
174 |
558
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
175 private void uploadAsText(MonetConnection.Upload handle, Path path, long toSkip) throws IOException { |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
176 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
|
177 for (long i = 0; i < toSkip; i++) { |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
178 reader.readLine(); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
179 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
180 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
|
181 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
182 |
558
ebf65f416da9
Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
556
diff
changeset
|
183 private void uploadAsBinary(MonetConnection.Upload handle, Path path) throws IOException { |
517
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
184 // No charset conversion whatsoever.. |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
185 // 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
|
186 InputStream stream = Files.newInputStream(path); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
187 handle.uploadFrom(stream); |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
188 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
189 } |
130bb2e18d3f
Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff
changeset
|
190 } |