annotate example/OnClientExample.java @ 608:c462161504a3

Extend example with Download functionality
author Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
date Tue, 04 Jan 2022 14:42:37 +0100 (2022-01-04)
parents 6cb395daa7d1
children 6666a9c62460
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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;
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
10
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
11 import java.io.BufferedReader;
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
12 import java.io.BufferedWriter;
517
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;
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
15 import java.io.InputStreamReader;
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
16 import java.io.OutputStream;
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
17 import java.io.OutputStreamWriter;
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
18 import java.io.PrintStream;
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
19 import java.nio.charset.Charset;
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
20 import java.nio.charset.StandardCharsets;
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
21 import java.nio.file.FileSystems;
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
22 import java.nio.file.Files;
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
23 import java.nio.file.Path;
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
24 import java.sql.*;
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
25
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
26 public class OnClientExample {
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
27
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
28 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
29 int status;
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
30 try {
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
31 // 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
32 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
33 final String userName = "monetdb";
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
34 final String password = "monetdb";
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
35 final String uploadDir = "/home/jvr/mydata";
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
36 final boolean filesAreUtf8 = false;
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
37 String[] queries = {
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
38 "DROP TABLE IF EXISTS mytable",
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
39 "CREATE TABLE mytable(i INT, t TEXT)",
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
40
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
41 // with this filename our handler will make up data by itself
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
42 "COPY INTO mytable FROM 'generated.csv' ON CLIENT",
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
43
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
44 // with this filename our handler will access the file system
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
45 "COPY INTO mytable FROM 'data.csv' ON CLIENT",
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
46
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
47 // with this statement, the server will ask the handler to stop halfway
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
48 "COPY 20 RECORDS OFFSET 5 INTO mytable FROM 'generated.csv' ON CLIENT",
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
49
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
50 // this demonstrates sending errors
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
51 "COPY INTO mytable FROM 'nonexistentfilethatdoesnotexist.csv' ON CLIENT",
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
52
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
53 // downloads but does not write to file, only counts the lines
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
54 "COPY SELECT * FROM mytable INTO 'justcount.csv' ON CLIENT",
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
55
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
56 // downloads to actual file
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
57 "COPY SELECT * FROM mytable INTO 'download.csv' ON CLIENT",
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
58
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
59 // demonstrate that the connection is still alive
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
60 "SELECT COUNT(*) FROM mytable",
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
61 };
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
62
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
63 status = run(dbUrl, userName, password, uploadDir, filesAreUtf8, queries);
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
64
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
65 } catch (Exception e) {
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
66 status = 1;
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
67 e.printStackTrace();
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 System.exit(status);
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
70 }
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
71
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
72 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
73 int status = 0;
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 // Connect
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
76 Class.forName("org.monetdb.jdbc.MonetDriver");
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
77 Connection conn = DriverManager.getConnection(dbUrl, userName, password);
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
78
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
79 // Register upload- and download handler
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
80 MyHandler handler = new MyHandler(uploadDir, filesAreUtf8);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
81 MonetConnection monetConnection = conn.unwrap(MonetConnection.class);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
82 monetConnection.setUploadHandler(handler);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
83 monetConnection.setDownloadHandler(handler);
517
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 Statement stmt = conn.createStatement();
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
86 for (String q : queries) {
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
87 System.out.println(q);
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
88 try {
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
89 boolean hasResultSet = stmt.execute(q);
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
90 if (hasResultSet) {
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
91 ResultSet rs = stmt.getResultSet();
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
92 long count = 0;
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
93 while (rs.next()) {
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
94 count++;
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
95 }
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
96 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
97 } else {
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
98 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
99 }
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
100 } catch (SQLNonTransientException e) {
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
101 throw e;
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
102 } catch (SQLException e) {
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
103 System.out.println(" => SQL ERROR " + e.getMessage());
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
104 status = 1;
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
105 }
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
106
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
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
109 return status;
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
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
112
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
113 private static class MyHandler implements MonetConnection.UploadHandler, MonetConnection.DownloadHandler {
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
114 private final Path uploadDir;
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
115 private final boolean filesAreUtf8;
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
116 private boolean stopUploading = false;
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
117
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
118 public MyHandler(String uploadDir, boolean filesAreUtf8) {
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
119 this.uploadDir = FileSystems.getDefault().getPath(uploadDir).normalize();
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
120 this.filesAreUtf8 = filesAreUtf8;
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 @Override
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
124 public void uploadCancelled() {
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
125 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
126 stopUploading = true;
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
127 }
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
128
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
129 @Override
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
130 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
131 // 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
132 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
133 uploadGeneratedData(handle, linesToSkip);
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
134 } else {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
135 uploadFileData(handle, name, textMode, linesToSkip);
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
136 }
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
137 }
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
138
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
139 @Override
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
140 public void handleDownload(MonetConnection.Download handle, String name, boolean textMode) throws IOException {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
141 if (name.equals("justcount.csv")) {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
142 justCountLines(handle);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
143 } else {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
144 downloadFileData(handle, name, textMode);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
145 }
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
146 }
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
147
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
148 private Path securelyResolvePath(String name) {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
149 Path p = uploadDir.resolve(name).normalize();
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
150 if (p.startsWith(uploadDir)) {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
151 return p;
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
152 } else {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
153 return null;
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
154 }
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
155 }
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
156
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
157 private void uploadGeneratedData(MonetConnection.Upload handle, long toSkip) throws IOException {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
158 // Set the chunk size to a tiny amount, so we can demonstrate
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
159 // cancellation handling. The default chunk size is one megabyte.
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
160 // DO NOT DO THIS IN PRODUCTION!
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
161 handle.setChunkSize(50);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
162
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
163 // Make up some data and upload it.
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
164 PrintStream stream = handle.getStream();
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
165 long n = 100;
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
166 System.out.printf(" HANDLER: uploading %d generated lines, numbered %d to %d%n", n - toSkip, toSkip + 1, n);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
167 long i;
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
168 for (i = toSkip + 1; i <= n; i++) {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
169 if (stopUploading) {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
170 System.out.printf(" HANDLER: at line %d we noticed the server asked us to stop sending%n", i);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
171 break;
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
172 }
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
173 stream.printf("%d|the number is %d%n", i, i);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
174 }
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
175 System.out.println(" HANDLER: done uploading");
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
176 stream.close();
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
177 }
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
178
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
179 private void uploadFileData(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
180 // Validate the path, demonstrating two ways of dealing with errors
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
181 Path path = securelyResolvePath(name);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
182 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
183 // 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
184 // 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
185 handle.sendError("Invalid path");
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
186 return;
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
187 }
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
188 if (!Files.isReadable(path)) {
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
189 // 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
190 // 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
191 // 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
192 // 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
193 // IOException from {@handleUpload} terminates the connection.
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
194 throw new IOException("Unreadable: " + path);
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
195 }
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
196
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
197 boolean binary = !textMode;
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
198 if (binary) {
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
199 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
200 } 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
201 // 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
202 // 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
203 uploadAsBinary(handle, path);
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
204 } else {
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
205 // Charset and skip handling are really necessary
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
206 uploadAsText(handle, path, linesToSkip);
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
207 }
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
208 }
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
209
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
210 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
211 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
212 for (long i = 0; i < toSkip; i++) {
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
213 reader.readLine();
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
214 }
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
215 // This variant of uploadFrom takes a Reader
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
216 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
217 }
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
218
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
219 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
220 // No charset conversion whatsoever..
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
221 // 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
222 InputStream stream = Files.newInputStream(path);
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
223 // This variant of uploadFrom takes a Stream
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
224 handle.uploadFrom(stream);
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
225 }
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
226
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
227
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
228 private void justCountLines(MonetConnection.Download handle) throws IOException {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
229 System.out.println(" HANDLER: not writing the download to file, just counting the lines");
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
230 InputStream stream = handle.getStream();
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
231 InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8); // MonetDB always sends UTF-8
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
232 BufferedReader bufreader = new BufferedReader(reader);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
233 long count = 0;
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
234 while (bufreader.readLine() != null) {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
235 count++;
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
236 }
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
237 System.out.println(" HANDLER: file had " + count + " lines");
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
238 }
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
239
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
240 private void downloadFileData(MonetConnection.Download handle, String name, boolean textMode) throws IOException {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
241 Path path = securelyResolvePath(name);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
242 if (path == null) {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
243 handle.sendError("Illegal path");
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
244 return;
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
245 }
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
246
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
247 OutputStream stream = Files.newOutputStream(path);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
248 if (!textMode || filesAreUtf8) {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
249 handle.downloadTo(stream);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
250 stream.close(); // do not forget this
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
251 } else {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
252 OutputStreamWriter writer = new OutputStreamWriter(stream, Charset.defaultCharset()); // let system decide the encoding
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
253 BufferedWriter bufwriter = new BufferedWriter(writer);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
254 handle.downloadTo(bufwriter);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
255 bufwriter.close(); // do not forget this
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
256 }
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
257 }
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
258
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
259 }
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
260 }