10
10
import java .sql .PreparedStatement ;
11
11
import java .sql .ResultSet ;
12
12
import java .sql .SQLException ;
13
- import java .sql .Statement ;
14
13
import java .util .Arrays ;
15
14
import java .util .List ;
16
15
import java .util .Locale ;
@@ -75,7 +74,11 @@ private Options(String url, String query, String file) {
75
74
this .query = query ;
76
75
}
77
76
if (file == null || file .isEmpty ()) {
78
- this .file = requiresJdbc ? "jdbc.out" : "java.out" ;
77
+ if (output ) {
78
+ this .file = requiresJdbc ? "jdbc.out" : "java.out" ;
79
+ } else {
80
+ this .file = "" ;
81
+ }
79
82
} else {
80
83
this .file = file ;
81
84
}
@@ -105,6 +108,10 @@ int getSamples() {
105
108
return s ;
106
109
}
107
110
111
+ boolean hasFile () {
112
+ return !file .isEmpty ();
113
+ }
114
+
108
115
boolean isDumpAction () {
109
116
return "dump" .equals (action );
110
117
}
@@ -331,7 +338,7 @@ final long dump() throws ClickHouseException, SQLException {
331
338
try (ClickHouseConnection conn = new ClickHouseConnectionImpl (options .url )) {
332
339
ClickHouseRequest <?> request = conn .unwrap (ClickHouseRequest .class ).query (options .query );
333
340
if (!request .getServer ().getConfig ().hasOption (ClickHouseClientOption .FORMAT )) {
334
- request .format (defaultFormat . defaultInputFormat () );
341
+ request .format (defaultFormat );
335
342
}
336
343
request .output (options .file );
337
344
try (ClickHouseResponse response = request .executeAndWait ()) {
@@ -343,7 +350,7 @@ final long dump() throws ClickHouseException, SQLException {
343
350
try (ClickHouseClient client = ClickHouseClient .newInstance (server .getProtocol ())) {
344
351
ClickHouseRequest <?> request = client .read (server ).query (options .query );
345
352
if (!server .getConfig ().hasOption (ClickHouseClientOption .FORMAT )) {
346
- request .format (defaultFormat . defaultInputFormat () );
353
+ request .format (defaultFormat );
347
354
}
348
355
request .output (options .file );
349
356
try (ClickHouseResponse response = request .query (options .query ).executeAndWait ()) {
@@ -389,14 +396,16 @@ final long read(Options options) throws ClickHouseException, SQLException {
389
396
final long rows ;
390
397
if (options .requiresJdbc ) {
391
398
try (ClickHouseConnection conn = new ClickHouseConnectionImpl (options .url );
392
- Statement stmt = conn .createStatement ()) {
393
- try ( ResultSet rs = stmt . executeQuery ( options .query )) {
399
+ ClickHouseStatement stmt = conn .createStatement ()) {
400
+ if ( options .hasFile ( )) {
394
401
try {
395
- rs . unwrap ( ClickHouseResponse . class ). getInputStream ()
396
- . setCopyToTarget ( new FileOutputStream (options .file , false )); // NOSONAR
402
+ stmt . setMirroredOutput (
403
+ ! "-" . equals ( options . file ) ? new FileOutputStream (options .file , false ) : System . out ); // NOSONAR
397
404
} catch (IOException e ) {
398
405
throw SqlExceptionUtils .clientError (e );
399
406
}
407
+ }
408
+ try (ResultSet rs = stmt .executeQuery (options .query )) {
400
409
rows = read (rs );
401
410
}
402
411
}
@@ -408,10 +417,14 @@ final long read(Options options) throws ClickHouseException, SQLException {
408
417
request .format (defaultFormat );
409
418
}
410
419
try (ClickHouseResponse response = request .executeAndWait ()) {
411
- try {
412
- response .getInputStream ().setCopyToTarget (new FileOutputStream (options .file , false )); // NOSONAR
413
- } catch (IOException e ) {
414
- throw ClickHouseException .of (e , server );
420
+ if (options .hasFile ()) {
421
+ try {
422
+ response .getInputStream ().setCopyToTarget (
423
+ !"-" .equals (options .file ) ? new FileOutputStream (options .file , false )
424
+ : System .out ); // NOSONAR
425
+ } catch (IOException e ) {
426
+ throw ClickHouseException .of (e , server );
427
+ }
415
428
}
416
429
rows = read (response );
417
430
}
@@ -764,14 +777,14 @@ private static void printUsage() {
764
777
println ("Properties: -Dkey=value [-Dkey=value]*" );
765
778
println (" action \t Action, one of read(default), write, dump(no deserialization), and load(no serialization)" );
766
779
println (" batch \t Batch size for JDBC writing, defaults to 1000" );
767
- println (" output \t Whether to write raw response into a file(java.out or jdbc.out), defaults to false" );
780
+ println (" output \t Whether to write raw response into stdout or a file(java.out or jdbc.out), defaults to false" );
768
781
println (" samples\t Samples, defaults to 500000000" );
769
782
println (" serde \t Whether to use default serialization/deserializion mechanism in Java client, defaults to true" );
770
783
println (" type \t Predefined QUERY, one of Int8, UInt64, String, Array, Tuple, Nested, and Mixed" );
771
784
println (" verbose\t Whether to show logs, defaults to false" );
772
785
println ();
773
786
println ("Examples:" );
774
- println (" - %s 'https://localhost?sslmode=none' 'select 1'" ,
787
+ println (" - %s 'https://localhost?sslmode=none' 'select 1' - " ,
775
788
index > 0 ? (execFile .substring (0 , index ) + " -Dverbose=true" + execFile .substring (index ))
776
789
: (execFile + " -Dverbose=true" ));
777
790
println (" - %s 'jdbc:ch://user:password@localhost:8123/default' 'select 1' output.file" , execFile );
@@ -802,7 +815,7 @@ public static void main(String[] args) throws Exception {
802
815
final long rows = query .run ();
803
816
if (options .verbose ) {
804
817
long elapsedNanos = System .nanoTime () - startTime ;
805
- println ("Processed %,d rows in %,.2f ms (%,.2f rows/s)" , rows , elapsedNanos / 1_000_000D ,
818
+ println ("\n Processed %,d rows in %,.2f ms (%,.2f rows/s)" , rows , elapsedNanos / 1_000_000D ,
806
819
rows * 1_000_000_000D / elapsedNanos );
807
820
}
808
821
System .exit (rows > 0L ? 0 : 1 );
0 commit comments