annotate example/OnClientExample.java @ 609:6666a9c62460

Cosmetic changes
author Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
date Tue, 04 Jan 2022 14:44:30 +0100 (2022-01-04)
parents c462161504a3
children 6aa38e8c0f2d
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;
609
6666a9c62460 Cosmetic changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 608
diff changeset
24 import java.sql.Connection;
6666a9c62460 Cosmetic changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 608
diff changeset
25 import java.sql.DriverManager;
6666a9c62460 Cosmetic changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 608
diff changeset
26 import java.sql.ResultSet;
6666a9c62460 Cosmetic changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 608
diff changeset
27 import java.sql.SQLException;
6666a9c62460 Cosmetic changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 608
diff changeset
28 import java.sql.SQLNonTransientException;
6666a9c62460 Cosmetic changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 608
diff changeset
29 import java.sql.Statement;
517
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 public class OnClientExample {
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 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
34 int status;
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
35 try {
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
36 // 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
37 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
38 final String userName = "monetdb";
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
39 final String password = "monetdb";
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
40 final String uploadDir = "/home/jvr/mydata";
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
41 final boolean filesAreUtf8 = false;
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
42 String[] queries = {
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
43 "DROP TABLE IF EXISTS mytable",
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
44 "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
45
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
46 // 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
47 "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
48
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
49 // 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
50 "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
51
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
52 // 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
53 "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
54
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
55 // this demonstrates sending errors
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
56 "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
57
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
58 // 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
59 "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
60
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
61 // downloads to actual file
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
62 "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
63
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
64 // 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
65 "SELECT COUNT(*) FROM mytable",
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
66 };
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
67
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
68 status = run(dbUrl, userName, password, uploadDir, filesAreUtf8, queries);
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
69
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
70 } catch (Exception e) {
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
71 status = 1;
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
72 e.printStackTrace();
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
73 }
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
74 System.exit(status);
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
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
77 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
78 int status = 0;
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 // Connect
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
81 Class.forName("org.monetdb.jdbc.MonetDriver");
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
82 Connection conn = DriverManager.getConnection(dbUrl, userName, password);
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
83
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
84 // Register upload- and download handler
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
85 MyHandler handler = new MyHandler(uploadDir, filesAreUtf8);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
86 MonetConnection monetConnection = conn.unwrap(MonetConnection.class);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
87 monetConnection.setUploadHandler(handler);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
88 monetConnection.setDownloadHandler(handler);
517
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 Statement stmt = conn.createStatement();
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
91 for (String q : queries) {
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
92 System.out.println(q);
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
93 try {
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
94 boolean hasResultSet = stmt.execute(q);
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
95 if (hasResultSet) {
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
96 ResultSet rs = stmt.getResultSet();
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
97 long count = 0;
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
98 while (rs.next()) {
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
99 count++;
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
100 }
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
101 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
102 } else {
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
103 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
104 }
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
105 } catch (SQLNonTransientException e) {
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
106 throw e;
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
107 } catch (SQLException e) {
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
108 System.out.println(" => SQL ERROR " + e.getMessage());
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
109 status = 1;
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
110 }
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
111
517
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 return status;
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
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 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
119 private final Path uploadDir;
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
120 private final boolean filesAreUtf8;
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
121 private boolean stopUploading = false;
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
122
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
123 public MyHandler(String uploadDir, boolean filesAreUtf8) {
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
124 this.uploadDir = FileSystems.getDefault().getPath(uploadDir).normalize();
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
125 this.filesAreUtf8 = filesAreUtf8;
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 @Override
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
129 public void uploadCancelled() {
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
130 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
131 stopUploading = true;
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
132 }
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
133
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
134 @Override
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
135 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
136 // 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
137 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
138 uploadGeneratedData(handle, linesToSkip);
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
139 } else {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
140 uploadFileData(handle, name, textMode, linesToSkip);
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
141 }
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
142 }
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
143
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
144 @Override
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
145 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
146 if (name.equals("justcount.csv")) {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
147 justCountLines(handle);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
148 } else {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
149 downloadFileData(handle, name, textMode);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
150 }
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
151 }
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
152
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
153 private Path securelyResolvePath(String name) {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
154 Path p = uploadDir.resolve(name).normalize();
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
155 if (p.startsWith(uploadDir)) {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
156 return p;
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
157 } else {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
158 return null;
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
159 }
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
160 }
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
161
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
162 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
163 // 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
164 // 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
165 // DO NOT DO THIS IN PRODUCTION!
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
166 handle.setChunkSize(50);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
167
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
168 // 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
169 PrintStream stream = handle.getStream();
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
170 long n = 100;
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
171 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
172 long i;
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
173 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
174 if (stopUploading) {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
175 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
176 break;
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 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
179 }
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
180 System.out.println(" HANDLER: done uploading");
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
181 stream.close();
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
182 }
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
183
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
184 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
185 // 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
186 Path path = securelyResolvePath(name);
609
6666a9c62460 Cosmetic changes
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 608
diff changeset
187 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
188 // 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
189 // 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
190 handle.sendError("Invalid path");
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
191 return;
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
192 }
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
193 if (!Files.isReadable(path)) {
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
194 // 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
195 // 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
196 // 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
197 // 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
198 // IOException from {@handleUpload} terminates the connection.
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
199 throw new IOException("Unreadable: " + path);
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
200 }
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
201
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
202 boolean binary = !textMode;
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
203 if (binary) {
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
204 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
205 } 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
206 // 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
207 // 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
208 uploadAsBinary(handle, path);
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
209 } else {
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
210 // 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
211 uploadAsText(handle, path, linesToSkip);
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
212 }
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
213 }
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
214
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
215 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
216 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
217 for (long i = 0; i < toSkip; i++) {
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
218 reader.readLine();
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
219 }
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
220 // This variant of uploadFrom takes a Reader
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
221 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
222 }
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
223
558
ebf65f416da9 Many improvements to the example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 556
diff changeset
224 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
225 // No charset conversion whatsoever..
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
226 // 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
227 InputStream stream = Files.newInputStream(path);
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
228 // This variant of uploadFrom takes a Stream
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
229 handle.uploadFrom(stream);
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
230 }
608
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
231
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
232
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
233 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
234 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
235 InputStream stream = handle.getStream();
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
236 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
237 BufferedReader bufreader = new BufferedReader(reader);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
238 long count = 0;
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
239 while (bufreader.readLine() != null) {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
240 count++;
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
241 }
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
242 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
243 }
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
244
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
245 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
246 Path path = securelyResolvePath(name);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
247 if (path == null) {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
248 handle.sendError("Illegal path");
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
249 return;
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
250 }
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
251
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
252 OutputStream stream = Files.newOutputStream(path);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
253 if (!textMode || filesAreUtf8) {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
254 handle.downloadTo(stream);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
255 stream.close(); // do not forget this
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
256 } else {
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
257 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
258 BufferedWriter bufwriter = new BufferedWriter(writer);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
259 handle.downloadTo(bufwriter);
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
260 bufwriter.close(); // do not forget this
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
261 }
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
262 }
c462161504a3 Extend example with Download functionality
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 582
diff changeset
263
517
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
264 }
130bb2e18d3f Add example code
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents:
diff changeset
265 }