66import ru .yandex .clickhouse .ClickHouseConnection ;
77import ru .yandex .clickhouse .ClickHouseContainerForTest ;
88import ru .yandex .clickhouse .ClickHouseDataSource ;
9-
9+ import ru .yandex .clickhouse .domain .ClickHouseCompression ;
10+ import ru .yandex .clickhouse .domain .ClickHouseFormat ;
11+ import ru .yandex .clickhouse .settings .ClickHouseProperties ;
1012import java .io .*;
1113import java .nio .charset .Charset ;
1214import java .sql .ResultSet ;
1315import java .sql .SQLException ;
16+ import java .util .zip .GZIPOutputStream ;
1417
1518public class StreamSQLTest {
1619 private ClickHouseDataSource dataSource ;
@@ -33,7 +36,11 @@ public void simpleCSVInsert() throws SQLException {
3336 String string = "5,6\n 1,6" ;
3437 InputStream inputStream = new ByteArrayInputStream (string .getBytes (Charset .forName ("UTF-8" )));
3538
36- connection .createStatement ().sendStreamSQL (inputStream , "insert into test.csv_stream_sql format CSV" );
39+ connection .createStatement ().
40+ write ()
41+ .sql ("insert into test.csv_stream_sql format CSV" )
42+ .data (inputStream )
43+ .send ();
3744
3845 ResultSet rs = connection .createStatement ().executeQuery (
3946 "SELECT count() AS cnt, sum(value) AS sum, uniqExact(string_value) uniq FROM test.csv_stream_sql" );
@@ -43,31 +50,21 @@ public void simpleCSVInsert() throws SQLException {
4350 Assert .assertEquals (rs .getLong ("uniq" ), 1 );
4451 }
4552
46- @ Test
47- public void multiRowTSVInsert () throws SQLException {
48- connection .createStatement ().execute ("DROP TABLE IF EXISTS test.tsv_stream_sql" );
49- connection .createStatement ().execute (
50- "CREATE TABLE test.tsv_stream_sql (value Int32, string_value String) ENGINE = Log()"
51- );
52-
53-
54- final int rowsCount = 100000 ;
55-
56- InputStream in = new InputStream () {
53+ private InputStream getTSVStream (final int rowsCount ) {
54+ return new InputStream () {
5755 private int si = 0 ;
5856 private String s = "" ;
5957 private int i = 0 ;
60- private final int count = rowsCount ;
6158
6259 private boolean genNextString () {
63- if (i >= count ) return false ;
60+ if (i >= rowsCount ) return false ;
6461 si = 0 ;
6562 s = String .format ("%d\t xxxx%d\n " , 1 , i );
6663 i ++;
6764 return true ;
6865 }
6966
70- public int read () throws IOException {
67+ public int read () {
7168 if (si >= s .length ()) {
7269 if ( ! genNextString () ) {
7370 return -1 ;
@@ -76,8 +73,22 @@ public int read() throws IOException {
7673 return s .charAt ( si ++ );
7774 }
7875 };
76+ }
77+
78+ @ Test
79+ public void multiRowTSVInsert () throws SQLException {
80+ connection .createStatement ().execute ("DROP TABLE IF EXISTS test.tsv_stream_sql" );
81+ connection .createStatement ().execute (
82+ "CREATE TABLE test.tsv_stream_sql (value Int32, string_value String) ENGINE = Log()"
83+ );
84+
85+ final int rowsCount = 100000 ;
7986
80- connection .createStatement ().sendStreamSQL (in , "insert into test.tsv_stream_sql format TSV" );
87+ connection .createStatement ().
88+ write ()
89+ .sql ("insert into test.tsv_stream_sql format TSV" )
90+ .data (getTSVStream (rowsCount ), ClickHouseFormat .TSV )
91+ .send ();
8192
8293 ResultSet rs = connection .createStatement ().executeQuery (
8394 "SELECT count() AS cnt, sum(value) AS sum, uniqExact(string_value) uniq FROM test.tsv_stream_sql" );
@@ -87,4 +98,122 @@ public int read() throws IOException {
8798 Assert .assertEquals (rs .getInt ("uniq" ), rowsCount );
8899 }
89100
101+ private InputStream gzStream ( InputStream is ) throws IOException
102+ {
103+ final int bufferSize = 16384 ;
104+ byte data [] = new byte [bufferSize ];
105+ ByteArrayOutputStream os = new ByteArrayOutputStream ();
106+ GZIPOutputStream gzipOutputStream = new GZIPOutputStream (os );
107+ BufferedInputStream es = new BufferedInputStream (is , bufferSize );
108+ int count ;
109+ while ( ( count = es .read ( data , 0 , bufferSize ) ) != -1 )
110+ gzipOutputStream .write ( data , 0 , count );
111+ es .close ();
112+ gzipOutputStream .close ();
113+
114+ return new ByteArrayInputStream ( os .toByteArray () );
115+ }
116+
117+ @ Test
118+ public void multiRowTSVInsertCompressed () throws SQLException , IOException {
119+ connection .createStatement ().execute ("DROP TABLE IF EXISTS test.tsv_compressed_stream_sql" );
120+ connection .createStatement ().execute (
121+ "CREATE TABLE test.tsv_compressed_stream_sql (value Int32, string_value String) ENGINE = Log()"
122+ );
123+
124+ final int rowsCount = 100000 ;
125+
126+ InputStream gz = gzStream (getTSVStream (rowsCount ));
127+ connection .createStatement ().
128+ write ()
129+ .sql ("insert into test.tsv_compressed_stream_sql format TSV" )
130+ .data (gz , ClickHouseFormat .TSV , ClickHouseCompression .gzip )
131+ .send ();
132+
133+ ResultSet rs = connection .createStatement ().executeQuery (
134+ "SELECT count() AS cnt, sum(value) AS sum, uniqExact(string_value) uniq FROM test.tsv_compressed_stream_sql" );
135+ Assert .assertTrue (rs .next ());
136+ Assert .assertEquals (rs .getInt ("cnt" ), rowsCount );
137+ Assert .assertEquals (rs .getInt ("sum" ), rowsCount );
138+ Assert .assertEquals (rs .getInt ("uniq" ), rowsCount );
139+ }
140+
141+
142+ @ Test
143+ public void JSONEachRowInsert () throws SQLException {
144+ connection .createStatement ().execute ("DROP TABLE IF EXISTS test.json_stream_sql" );
145+ connection .createStatement ().execute (
146+ "CREATE TABLE test.json_stream_sql (value Int32, string_value String) ENGINE = Log()"
147+ );
148+
149+ String string = "{\" value\" :5,\" string_value\" :\" 6\" }\n {\" value\" :1,\" string_value\" :\" 6\" }" ;
150+ InputStream inputStream = new ByteArrayInputStream (string .getBytes (Charset .forName ("UTF-8" )));
151+
152+ connection .createStatement ().
153+ write ()
154+ .sql ("insert into test.json_stream_sql" )
155+ .data (inputStream , ClickHouseFormat .JSONEachRow )
156+ .data (inputStream )
157+ .send ();
158+
159+ ResultSet rs = connection .createStatement ().executeQuery (
160+ "SELECT count() AS cnt, sum(value) AS sum, uniqExact(string_value) uniq FROM test.json_stream_sql" );
161+ Assert .assertTrue (rs .next ());
162+ Assert .assertEquals (rs .getInt ("cnt" ), 2 );
163+ Assert .assertEquals (rs .getLong ("sum" ), 6 );
164+ Assert .assertEquals (rs .getLong ("uniq" ), 1 );
165+ }
166+
167+ @ Test
168+ public void JSONEachRowCompressedInsert () throws SQLException , IOException {
169+ connection .createStatement ().execute ("DROP TABLE IF EXISTS test.json_comressed_stream_sql" );
170+ connection .createStatement ().execute (
171+ "CREATE TABLE test.json_comressed_stream_sql (value Int32, string_value String) ENGINE = Log()"
172+ );
173+
174+ String string = "{\" value\" :5,\" string_value\" :\" 6\" }\n {\" value\" :1,\" string_value\" :\" 6\" }" ;
175+ InputStream inputStream = new ByteArrayInputStream (string .getBytes (Charset .forName ("UTF-8" )));
176+
177+ connection .createStatement ().
178+ write ()
179+ .sql ("insert into test.json_comressed_stream_sql" )
180+ .data (inputStream , ClickHouseFormat .JSONEachRow )
181+ .data (gzStream (inputStream ))
182+ .dataCompression (ClickHouseCompression .gzip )
183+ .send ();
184+
185+ ResultSet rs = connection .createStatement ().executeQuery (
186+ "SELECT count() AS cnt, sum(value) AS sum, uniqExact(string_value) uniq FROM test.json_comressed_stream_sql" );
187+ Assert .assertTrue (rs .next ());
188+ Assert .assertEquals (rs .getInt ("cnt" ), 2 );
189+ Assert .assertEquals (rs .getLong ("sum" ), 6 );
190+ Assert .assertEquals (rs .getLong ("uniq" ), 1 );
191+ }
192+
193+ @ Test
194+ public void CSVInsertCompressedIntoTable () throws SQLException , IOException {
195+ connection .createStatement ().execute ("DROP TABLE IF EXISTS test.csv_stream_compressed" );
196+ connection .createStatement ().execute (
197+ "CREATE TABLE test.csv_stream_compressed (value Int32, string_value String) ENGINE = Log()"
198+ );
199+
200+ String string = "5,6\n 1,6" ;
201+ InputStream inputStream = new ByteArrayInputStream (string .getBytes (Charset .forName ("UTF-8" )));
202+
203+ connection .createStatement ().
204+ write ()
205+ .table ("test.csv_stream_compressed" )
206+ .format (ClickHouseFormat .CSV )
207+ .dataCompression (ClickHouseCompression .gzip )
208+ .data (gzStream (inputStream ))
209+ .send ();
210+
211+ ResultSet rs = connection .createStatement ().executeQuery (
212+ "SELECT count() AS cnt, sum(value) AS sum, uniqExact(string_value) uniq FROM test.csv_stream_compressed" );
213+ Assert .assertTrue (rs .next ());
214+ Assert .assertEquals (rs .getInt ("cnt" ), 2 );
215+ Assert .assertEquals (rs .getLong ("sum" ), 6 );
216+ Assert .assertEquals (rs .getLong ("uniq" ), 1 );
217+ }
218+
90219}
0 commit comments