|
2 | 2 |
|
3 | 3 | import java.io.BufferedReader;
|
4 | 4 | import java.io.ByteArrayInputStream;
|
| 5 | +import java.io.ByteArrayOutputStream; |
| 6 | +import java.io.File; |
| 7 | +import java.io.FileInputStream; |
| 8 | +import java.io.FileOutputStream; |
5 | 9 | import java.io.IOException;
|
6 | 10 | import java.io.InputStream;
|
7 | 11 | import java.io.InputStreamReader;
|
| 12 | +import java.io.OutputStream; |
8 | 13 | import java.io.UncheckedIOException;
|
9 | 14 | import java.math.BigDecimal;
|
10 | 15 | import java.math.BigInteger;
|
|
24 | 29 | import java.util.concurrent.ExecutionException;
|
25 | 30 | import java.util.concurrent.atomic.AtomicInteger;
|
26 | 31 | import java.util.stream.Collectors;
|
| 32 | +import java.util.zip.GZIPInputStream; |
| 33 | +import java.util.zip.GZIPOutputStream; |
27 | 34 |
|
28 | 35 | import com.clickhouse.client.ClickHouseClientBuilder.Agent;
|
29 | 36 | import com.clickhouse.client.config.ClickHouseBufferingMode;
|
@@ -119,6 +126,16 @@ protected Object[][] getCompressionMatrix() {
|
119 | 126 | return array;
|
120 | 127 | }
|
121 | 128 |
|
| 129 | + @DataProvider(name = "fileProcessMatrix") |
| 130 | + protected Object[][] getFileProcessMatrix() { |
| 131 | + return new Object[][] { |
| 132 | + { true, true }, |
| 133 | + { true, false }, |
| 134 | + { false, true }, |
| 135 | + { false, false }, |
| 136 | + }; |
| 137 | + } |
| 138 | + |
122 | 139 | @DataProvider(name = "renameMethods")
|
123 | 140 | protected Object[][] getRenameMethods() {
|
124 | 141 | return new Object[][] {
|
@@ -1035,6 +1052,47 @@ public void testDump() throws Exception {
|
1035 | 1052 | Files.delete(temp);
|
1036 | 1053 | }
|
1037 | 1054 |
|
| 1055 | + @Test(dataProvider = "fileProcessMatrix", groups = "integration") |
| 1056 | + public void testDumpFile(boolean gzipCompressed, boolean useOneLiner) throws Exception { |
| 1057 | + ClickHouseNode server = getServer(); |
| 1058 | + if (server.getProtocol() != ClickHouseProtocol.HTTP) { |
| 1059 | + throw new SkipException("Skip as only http implementation works well"); |
| 1060 | + } |
| 1061 | + |
| 1062 | + File file = File.createTempFile("chc", ".data"); |
| 1063 | + ClickHouseFile wrappedFile = ClickHouseFile.of(file, |
| 1064 | + gzipCompressed ? ClickHouseCompression.GZIP : ClickHouseCompression.NONE, 0, |
| 1065 | + ClickHouseFormat.CSV); |
| 1066 | + String query = "select number, if(number % 2 = 0, null, toString(number)) str from numbers(10)"; |
| 1067 | + if (useOneLiner) { |
| 1068 | + ClickHouseClient.dump(server, query, wrappedFile).get(); |
| 1069 | + } else { |
| 1070 | + try (ClickHouseClient client = getClient(); |
| 1071 | + ClickHouseResponse response = client.connect(server).query(query).output(wrappedFile) |
| 1072 | + .executeAndWait()) { |
| 1073 | + // ignore |
| 1074 | + } |
| 1075 | + } |
| 1076 | + try (InputStream in = gzipCompressed ? new GZIPInputStream(new FileInputStream(file)) |
| 1077 | + : new FileInputStream(file); ByteArrayOutputStream out = new ByteArrayOutputStream()) { |
| 1078 | + ClickHouseInputStream.pipe(in, out, 512); |
| 1079 | + String content = new String(out.toByteArray(), StandardCharsets.US_ASCII); |
| 1080 | + StringBuilder builder = new StringBuilder(); |
| 1081 | + for (int i = 0; i < 10; i++) { |
| 1082 | + builder.append(i).append(','); |
| 1083 | + if (i % 2 == 0) { |
| 1084 | + builder.append("\\N"); |
| 1085 | + } else { |
| 1086 | + builder.append('"').append(i).append('"'); |
| 1087 | + } |
| 1088 | + builder.append('\n'); |
| 1089 | + } |
| 1090 | + Assert.assertEquals(content, builder.toString()); |
| 1091 | + } finally { |
| 1092 | + file.delete(); |
| 1093 | + } |
| 1094 | + } |
| 1095 | + |
1038 | 1096 | @Test(groups = { "integration" })
|
1039 | 1097 | public void testCustomLoad() throws Exception {
|
1040 | 1098 | ClickHouseNode server = getServer();
|
@@ -1117,6 +1175,66 @@ public void testLoadCsv() throws Exception {
|
1117 | 1175 | }
|
1118 | 1176 | }
|
1119 | 1177 |
|
| 1178 | + @Test(dataProvider = "fileProcessMatrix", groups = "integration") |
| 1179 | + public void testLoadFile(boolean gzipCompressed, boolean useOneLiner) throws Exception { |
| 1180 | + ClickHouseNode server = getServer(); |
| 1181 | + if (server.getProtocol() != ClickHouseProtocol.HTTP) { |
| 1182 | + throw new SkipException("Skip as only http implementation works well"); |
| 1183 | + } |
| 1184 | + |
| 1185 | + File file = File.createTempFile("chc", ".data"); |
| 1186 | + Object[][] data = new Object[][] { |
| 1187 | + { 1, "12345" }, |
| 1188 | + { 2, "23456" }, |
| 1189 | + { 3, "\\N" }, |
| 1190 | + { 4, "x" }, |
| 1191 | + { 5, "y" }, |
| 1192 | + }; |
| 1193 | + try (OutputStream out = gzipCompressed ? new GZIPOutputStream(new FileOutputStream(file)) |
| 1194 | + : new FileOutputStream(file)) { |
| 1195 | + for (Object[] row : data) { |
| 1196 | + out.write((row[0] + "," + row[1]).getBytes(StandardCharsets.US_ASCII)); |
| 1197 | + if ((int) row[0] != 5) { |
| 1198 | + out.write(10); |
| 1199 | + } |
| 1200 | + } |
| 1201 | + out.flush(); |
| 1202 | + } |
| 1203 | + |
| 1204 | + ClickHouseClient.send(server, "drop table if exists test_load_file", |
| 1205 | + "create table test_load_file(a Int32, b Nullable(String))engine=Memory").get(); |
| 1206 | + ClickHouseFile wrappedFile = ClickHouseFile.of(file, |
| 1207 | + gzipCompressed ? ClickHouseCompression.GZIP : ClickHouseCompression.NONE, 0, |
| 1208 | + ClickHouseFormat.CSV); |
| 1209 | + if (useOneLiner) { |
| 1210 | + ClickHouseClient |
| 1211 | + .load(server, "test_load_file", wrappedFile) |
| 1212 | + .get(); |
| 1213 | + } else { |
| 1214 | + try (ClickHouseClient client = getClient(); |
| 1215 | + ClickHouseResponse response = client.connect(server).write().table("test_load_file") |
| 1216 | + .data(wrappedFile).executeAndWait()) { |
| 1217 | + // ignore |
| 1218 | + } |
| 1219 | + } |
| 1220 | + try (ClickHouseClient client = getClient(); |
| 1221 | + ClickHouseResponse response = client.connect(server).format(ClickHouseFormat.RowBinaryWithNamesAndTypes) |
| 1222 | + .query("select * from test_load_file order by a").executeAndWait()) { |
| 1223 | + int row = 0; |
| 1224 | + for (ClickHouseRecord r : response.records()) { |
| 1225 | + Assert.assertEquals(r.getValue(0).asObject(), data[row][0]); |
| 1226 | + if (row == 2) { |
| 1227 | + Assert.assertNull(r.getValue(1).asObject()); |
| 1228 | + } else { |
| 1229 | + Assert.assertEquals(r.getValue(1).asObject(), data[row][1]); |
| 1230 | + } |
| 1231 | + row++; |
| 1232 | + } |
| 1233 | + } finally { |
| 1234 | + file.delete(); |
| 1235 | + } |
| 1236 | + } |
| 1237 | + |
1120 | 1238 | @Test(groups = { "integration" })
|
1121 | 1239 | public void testLoadRawData() throws Exception {
|
1122 | 1240 | ClickHouseNode server = getServer();
|
|
0 commit comments