11package com .clickhouse .benchmark .data ;
22
33import com .clickhouse .benchmark .BenchmarkRunner ;
4- import com .clickhouse .client .api .Client ;
5- import com .clickhouse .client .api .data_formats .RowBinaryFormatReader ;
6- import com .clickhouse .client .api .data_formats .RowBinaryFormatWriter ;
74import com .clickhouse .client .api .metadata .TableSchema ;
8- import com .clickhouse .client .api .query .QueryResponse ;
95import com .clickhouse .data .ClickHouseFormat ;
106import org .openjdk .jmh .annotations .Level ;
117import org .openjdk .jmh .annotations .Param ;
128import org .openjdk .jmh .annotations .Scope ;
139import org .openjdk .jmh .annotations .Setup ;
1410import org .openjdk .jmh .annotations .State ;
1511import org .openjdk .jmh .annotations .TearDown ;
12+ import org .slf4j .Logger ;
13+ import org .slf4j .LoggerFactory ;
1614
1715import java .io .ByteArrayInputStream ;
18- import java .io .ByteArrayOutputStream ;
1916import java .io .IOException ;
2017import java .io .InputStream ;
2118import java .time .ZonedDateTime ;
22- import java .util .Map ;
2319import java .util .UUID ;
2420import java .util .concurrent .ThreadLocalRandom ;
2521
2622@ State (Scope .Benchmark )
2723public class DataSet {
24+ private static final Logger LOGGER = LoggerFactory .getLogger (DataSet .class );
2825 public final String name = "sample_data_set" ;
2926 public final String tableName = name + "_" + UUID .randomUUID ().toString ().replaceAll ("-" , "" );
3027 @ Param ({"10000" })
@@ -47,18 +44,34 @@ public void setup() {
4744 data = generateData (size );
4845 BenchmarkRunner .insertData (tableName , new ByteArrayInputStream (generateData (size )), getFormat ());
4946 } catch (Exception e ) {
50- throw new RuntimeException ("Error generating data" , e );
47+ LOGGER .error ("Error while creating table or inserting data." , e );
48+ throw new RuntimeException ("Error while creating table or inserting data." , e );
5149 }
5250 }
53- private byte [] generateData (int size ) throws IOException {
54- ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
55- RowBinaryFormatWriter writer = new RowBinaryFormatWriter ( outputStream , getSchema (), getFormat () );
51+ private byte [] generateData (int size ) {
52+ //Generate JSON sample data
53+ StringBuilder builder = new StringBuilder ( );
5654 for (int i = 0 ; i < size ; i ++) {
57- writer .setValue ("id" , rowCounter ++);
58- writer .setValue ("sample_int" , ThreadLocalRandom .current ().nextInt ());
59- writer .commitRow ();
55+ builder .append ("{\" trip_id\" :" ).append (rowCounter )
56+ .append (",\" dropoff_datetime\" :\" " ).append (BenchmarkRunner .DATETIME_FORMATTER .format (ZonedDateTime .now ())).append ("\" " )
57+ .append (",\" pickup_longitude\" :" ).append (ThreadLocalRandom .current ().nextDouble (-180 , 180 ))
58+ .append (",\" pickup_latitude\" :" ).append (ThreadLocalRandom .current ().nextDouble (-90 , 90 ))
59+ .append (",\" dropoff_longitude\" :" ).append (ThreadLocalRandom .current ().nextDouble (-180 , 180 ))
60+ .append (",\" dropoff_latitude\" :" ).append (ThreadLocalRandom .current ().nextDouble (-90 , 90 ))
61+ .append (",\" passenger_count\" :" ).append (ThreadLocalRandom .current ().nextInt (1 , 5 ))
62+ .append (",\" trip_distance\" :" ).append (ThreadLocalRandom .current ().nextDouble (0 , 100 ))
63+ .append (",\" fare_amount\" :" ).append (ThreadLocalRandom .current ().nextDouble (0 , 100 ))
64+ .append (",\" extra\" :" ).append (ThreadLocalRandom .current ().nextDouble (0 , 100 ))
65+ .append (",\" tip_amount\" :" ).append (ThreadLocalRandom .current ().nextDouble (0 , 100 ))
66+ .append (",\" tolls_amount\" :" ).append (ThreadLocalRandom .current ().nextDouble (0 , 100 ))
67+ .append (",\" total_amount\" :" ).append (ThreadLocalRandom .current ().nextDouble (0 , 100 ))
68+ .append (",\" payment_type\" :\" CSH\" ,\" pickup_ntaname\" :\" NTA1\" ,\" dropoff_ntaname\" :\" NTA2\" }" );
69+ if (i < size - 1 ) {
70+ builder .append ("\n " );
71+ }
72+ rowCounter ++;
6073 }
61- return outputStream . toByteArray ();
74+ return builder . toString (). getBytes ();
6275 }
6376
6477 @ TearDown (Level .Trial )
@@ -82,18 +95,34 @@ public void teardown() {
8295
8396 public String getCreateTableString () {
8497 return "CREATE TABLE " + tableName + " (\n " +
85- " id UInt32,\n " +
86- " sample_int Int32\n " +
98+ " trip_id UInt32,\n " +
99+ " pickup_datetime DateTime DEFAULT now(),\n " +
100+ " dropoff_datetime DateTime,\n " +
101+ " pickup_longitude Nullable(Float64),\n " +
102+ " pickup_latitude Nullable(Float64),\n " +
103+ " dropoff_longitude Nullable(Float64),\n " +
104+ " dropoff_latitude Nullable(Float64),\n " +
105+ " passenger_count UInt8,\n " +
106+ " trip_distance Float32,\n " +
107+ " fare_amount Float32,\n " +
108+ " extra Float32,\n " +
109+ " tip_amount Float32,\n " +
110+ " tolls_amount Float32,\n " +
111+ " total_amount Float32,\n " +
112+ " payment_type Enum('CSH' = 1, 'CRE' = 2, 'NOC' = 3, 'DIS' = 4, 'UNK' = 5),\n " +
113+ " pickup_ntaname LowCardinality(String),\n " +
114+ " dropoff_ntaname LowCardinality(String)\n " +
87115 ")\n " +
88- "ENGINE = Memory;" ;
116+ "ENGINE = MergeTree\n " +
117+ "PRIMARY KEY (pickup_datetime, dropoff_datetime);" ;
89118 }
90119
91120 public TableSchema getSchema () {
92121 return schema ;
93122 }
94123
95124 public ClickHouseFormat getFormat () {
96- return ClickHouseFormat .RowBinaryWithNamesAndTypes ;
125+ return ClickHouseFormat .JSONEachRow ;
97126 }
98127 public InputStream getInputStream () {
99128 return new ByteArrayInputStream (data );
0 commit comments