-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathExample.java
309 lines (232 loc) · 12.5 KB
/
Example.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package com.gpudb.example;
import com.gpudb.BulkInserter;
import com.gpudb.GPUdb;
import com.gpudb.GPUdbException;
import com.gpudb.GPUdbLogger;
import com.gpudb.GenericRecord;
import com.gpudb.RecordObject;
import com.gpudb.Type;
import com.gpudb.protocol.AggregateGroupByResponse;
import com.gpudb.protocol.AggregateHistogramResponse;
import com.gpudb.protocol.AggregateStatisticsResponse;
import com.gpudb.protocol.AggregateUniqueResponse;
import com.gpudb.protocol.CreateTableRequest;
import com.gpudb.protocol.FilterResponse;
import com.gpudb.protocol.FilterByListResponse;
import com.gpudb.protocol.FilterByRangeResponse;
import com.gpudb.protocol.GetRecordsRequest;
import com.gpudb.protocol.GetRecordsResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.LinkedHashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Example
{
private static Logger LOGGER = LoggerFactory.getLogger(Example.class);
public static class MyType extends RecordObject
{
// Fields and their properties
@RecordObject.Column(order = 0, properties = { "data" })
public double col1;
@RecordObject.Column(order = 1, properties = { "data" })
public String col2;
@RecordObject.Column(order = 2, properties = { "data" })
public String group_id;
private MyType() {}
} // end class MyType
public static void main(String[] args) throws GPUdbException
{
// Get the URL to use from the command line, or use the default
String url = (args.length > 0) ? args[0] : "http://localhost:9191";
String user = (args.length > 1) ? args[1] : null;
String pass = (args.length > 2) ? args[2] : null;
String logLevel = (args.length > 3) ? args[3] : "INFO";
GPUdbLogger.setLoggingLevel(logLevel);
LOGGER.info("Creating GPUdb object to: " + url);
// Establish a connection with a locally running instance of GPUdb
GPUdb.Options options = new GPUdb.Options();
if (!"".equals(user))
options.setUsername(user);
if (!"".equals(pass))
options.setPassword(pass);
GPUdb gpudb = new GPUdb( url, options );
// Register the desired data type with GPUdb
Type type = RecordObject.getType( MyType.class );
// The type ID returned by GPUdb is needed to create a table later
String type_id = type.create( gpudb );
System.out.println( "Type id of newly created type: " + type_id + "\n" );
// Column names (used in queries)
String col1 = "col1";
String col2 = "col2";
String group_id = "group_id";
// Create a table with 'MyType' data type
String table_name = "my_table_1";
Map<String, String> create_table_options = GPUdb.options(
CreateTableRequest.Options.NO_ERROR_IF_EXISTS,
CreateTableRequest.Options.TRUE
);
gpudb.createTable( table_name, type_id, create_table_options );
int numRecords = 10;
try (BulkInserter<MyType> bulkInserter = new BulkInserter<MyType>(gpudb, table_name, type, numRecords, null))
{
// Generate data to be inserted into the table
for (int i = 0; i < numRecords; i++)
{
MyType record = new MyType();
record.put( 0, (i + 0.1) ); // col1
record.put( 1, ("string " + String.valueOf( i ) ) ); // col2
record.put( 2, "Group 1" ); // group_id
bulkInserter.insert( record );
} // done generating the objects
// To actually insert the records, flush the bulk inserter object.
bulkInserter.flush();
// Retrieve the inserted records
Map<String, String> blank_options = new LinkedHashMap<String, String>();
GetRecordsRequest getRecordsReq = new GetRecordsRequest( table_name, 0, numRecords, blank_options );
GetRecordsResponse<GenericRecord> getRecordsRsp = gpudb.getRecords( getRecordsReq );
System.out.println( "Returned records: " + getRecordsRsp.getData() + "\n" );
// Perform a filter calculation on the table
FilterResponse filterRsp;
String view_name = "view1";
String expression = "col1 = 1.1";
filterRsp = gpudb.filter( table_name, view_name, expression, blank_options );
System.out.println( "Number of records returned by the filter expression: " + filterRsp.getCount() + "\n" );
// Retrieve the filtered records (the retrieval method is the same
// as that from a regular table)
GetRecordsResponse<GenericRecord> filteredRecordsRsp = gpudb.getRecords( view_name, 0, 100, blank_options );
System.out.println( "Filtered records (" + expression + ") :" + filteredRecordsRsp.getData() + "\n" );
// Drop the view
gpudb.clearTable( view_name, "", blank_options );
// Perform another filter calculation on the table
String expression_2 = "(col1 <= 9) and (group_id='Group 1')";
filterRsp = gpudb.filter( table_name, view_name, expression_2, blank_options );
System.out.println( "Number of records returned by the second filter expression (" + expression + ") :" + filterRsp.getCount() + "\n" );
// Retrieve the filtered records (the retrieval method is the same
// as that from a regular table)
filteredRecordsRsp = gpudb.getRecords( view_name, 0, 100, blank_options );
System.out.println( "Filtered records: " + filteredRecordsRsp.getData() + "\n" );
// Perform a filter by list calculation on the table
FilterByListResponse filterByListRsp;
String view_name_2 = "view2";
// Set up the search criteria: for col1, look for values
// '1.1', '2.1', and '5.1'
Map<String, List<String>> columnValuesMap = new LinkedHashMap<String, List<String>>();
List<String> values = new ArrayList<String>();
values.add( "1.1" );
values.add( "2.1" );
values.add( "5.1" );
columnValuesMap.put( col1, values );
filterByListRsp = gpudb.filterByList( table_name, view_name_2, columnValuesMap, blank_options );
System.out.println( "Number of records returned by the filter by list expression: " + filterByListRsp.getCount() + "\n" );
// Retrieve the filtered (by list) records
filteredRecordsRsp = gpudb.getRecords( view_name_2, 0, 100, blank_options );
System.out.println( "Filtered (by list) records: " + filteredRecordsRsp.getData() + "\n" );
// Perform a filter by range calculation on the table
FilterByRangeResponse filterByRangeRsp;
String view_name_3 = "view3";
filterByRangeRsp = gpudb.filterByRange( table_name, view_name_3, col1, 1, 5, blank_options );
System.out.println( "Number of records returned by the filter by range expression: " + filterByRangeRsp.getCount() + "\n" );
// Retrieve the filtered (by list) records
filteredRecordsRsp = gpudb.getRecords( view_name_3, 0, 100, blank_options );
System.out.println( "Filtered (by range) records: " + filteredRecordsRsp.getData() + "\n" );
// Perform an aggregate (statistics) operation
AggregateStatisticsResponse aggStatsRsp;
aggStatsRsp = gpudb.aggregateStatistics( table_name, col1, "count,sum,mean", blank_options );
System.out.println( "Statistics of values in 'col1': " + aggStatsRsp.getStats() + "\n" );
// Generate more data to be inserted into the table
int numRecords2 = 8;
for (int i = 1; i < numRecords2; i++)
{
MyType record = new MyType();
record.put( 0, (i + 10.1) ); // col1
// 'col2' values are NOT unique from the first group of records
record.put( 1, ("string " + String.valueOf( i ) ) ); // col2
record.put( 2, "Group 2" ); // group_id
bulkInserter.insert( record );
} // done generating the objects
// Actually insert the records
bulkInserter.flush();
// Find unique values in a column
AggregateUniqueResponse uniqueRsp;
uniqueRsp = gpudb.aggregateUnique( table_name, group_id, 0, 30, blank_options );
System.out.println( "Unique values in the '" + group_id + "' column: " + uniqueRsp.getData() + "\n" );
// Perform a group by aggregation (on column 'col2')
AggregateGroupByResponse groupByRsp;
List<String> columns = new ArrayList<String>();
columns.add( col2 );
groupByRsp = gpudb.aggregateGroupBy( table_name, columns, 0, 1000, blank_options );
System.out.println( "Group by results for the '" + col2 + "' column: " + groupByRsp.getData() + "\n" );
// Perform another group by aggregation on:
// * column 'group_id'
// * count of all
// * sum of 'col1'
columns.clear();
columns.add( group_id );
columns.add( "count(*)" );
columns.add( "sum(" + col1 + ")" );
groupByRsp = gpudb.aggregateGroupBy( table_name, columns, 0, 1000, blank_options );
System.out.println( "Second group by results: " + groupByRsp.getData() + "\n" );
// Perform another group by aggregation operation
columns.clear();
columns.add( group_id );
columns.add( "sum(" + col1 + "*10)" );
groupByRsp = gpudb.aggregateGroupBy( table_name, columns, 0, 1000, blank_options );
System.out.println( "Third group by results: " + groupByRsp.getData() + "\n" );
// Add more data to the table
int numRecords3 = 10;
for (int i = 4; i < numRecords3; i++)
{
MyType record = new MyType();
record.put( 0, (i + 0.6) ); // col1
// 'col2' values are NOT unique from the first group of records
record.put( 1, ("string 2" + String.valueOf( i ) ) ); // col2
record.put( 2, "Group 1" ); // group_id
bulkInserter.insert( record );
} // done generating the objects
// Actually insert the records
bulkInserter.flush();
// Do a histogram on the data
AggregateHistogramResponse histogramRsp;
double start = 1.1;
double end = 2;
double interval = 1;
histogramRsp = gpudb.aggregateHistogram( table_name, col1, start, end, interval, blank_options );
System.out.println( "Histogram counts: " + histogramRsp.getCounts() + "\n" );
// Drop the table
gpudb.clearTable( table_name, "", blank_options );
// Check that dropping a table automatically drops all the
// dependent views
try
{
getRecordsRsp = gpudb.getRecords( view_name_3, 0, 100, blank_options );
System.out.println( "Error: Dropping original table did NOT drop all views!\n" );
} catch (GPUdbException e)
{
System.out.println( "Dropping original table dropped all views as expected.\n" );
}
}
} // end main
} // end class Example
/*
Output:
=======
Type id of newly created type: 8930436924309410255
Returned records: [{"col1":0.1,"col2":"string 0","group_id":"Group 1"}, {"col1":1.1,"col2":"string 1","group_id":"Group 1"}, {"col1":2.1,"col2":"string 2","group_id":"Group 1"}, {"col1":3.1,"col2":"string 3","group_id":"Group 1"}, {"col1":4.1,"col2":"string 4","group_id":"Group 1"}, {"col1":5.1,"col2":"string 5","group_id":"Group 1"}, {"col1":6.1,"col2":"string 6","group_id":"Group 1"}, {"col1":7.1,"col2":"string 7","group_id":"Group 1"}, {"col1":8.1,"col2":"string 8","group_id":"Group 1"}, {"col1":9.1,"col2":"string 9","group_id":"Group 1"}]
Number of records returned by the filter expression: 1
Filtered records (col1 = 1.1) :[{"col1":1.1,"col2":"string 1","group_id":"Group 1"}]
Number of records returned by the second filter expression (col1 = 1.1) :9
Filtered records: [{"col1":0.1,"col2":"string 0","group_id":"Group 1"}, {"col1":1.1,"col2":"string 1","group_id":"Group 1"}, {"col1":2.1,"col2":"string 2","group_id":"Group 1"}, {"col1":3.1,"col2":"string 3","group_id":"Group 1"}, {"col1":4.1,"col2":"string 4","group_id":"Group 1"}, {"col1":5.1,"col2":"string 5","group_id":"Group 1"}, {"col1":6.1,"col2":"string 6","group_id":"Group 1"}, {"col1":7.1,"col2":"string 7","group_id":"Group 1"}, {"col1":8.1,"col2":"string 8","group_id":"Group 1"}]
Number of records returned by the filter by list expression: 3
Filtered (by list) records: [{"col1":1.1,"col2":"string 1","group_id":"Group 1"}, {"col1":2.1,"col2":"string 2","group_id":"Group 1"}, {"col1":5.1,"col2":"string 5","group_id":"Group 1"}]
Number of records returned by the filter by range expression: 4
Filtered (by range) records: [{"col1":1.1,"col2":"string 1","group_id":"Group 1"}, {"col1":2.1,"col2":"string 2","group_id":"Group 1"}, {"col1":3.1,"col2":"string 3","group_id":"Group 1"}, {"col1":4.1,"col2":"string 4","group_id":"Group 1"}]
Statistics of values in 'col1': {count=10.0, mean=4.6, sum=46.0}
Unique values in the 'group_id' column: [{"group_id":"Group 1"}, {"group_id":"Group 2"}]
Group by results for the 'col2' column: [{"col2":"string 2"}, {"col2":"string 0"}, {"col2":"string 8"}, {"col2":"string 5"}, {"col2":"string 9"}, {"col2":"string 6"}, {"col2":"string 1"}, {"col2":"string 3"}, {"col2":"string 7"}, {"col2":"string 4"}]
Second group by results: [{"group_id":"Group 2","count(*)":7,"sum(col1)":98.69999999999999}, {"group_id":"Group 1","count(*)":10,"sum(col1)":46.0}]
Third group by results: [{"group_id":"Group 1","sum(col1*10)":460.0}, {"group_id":"Group 2","sum(col1*10)":987.0}]
Histogram counts: [1.0]
Dropping original table dropped all views as expected.
*/