2 using System.Collections.Generic;
14 public static string Truncate(
this string value,
int maxLength )
16 if (
string.IsNullOrEmpty( value ) )
19 return ( value.Length <= maxLength ) ? value : value.Substring( 0, maxLength );
26 static void Main(
string[] args)
28 Console.WriteLine(
"Example C# Project - Running");
33 Console.WriteLine(
"Missing URL as command-line parameter.");
34 Console.WriteLine(
"E.g., http://kinetica:9191");
40 string server_url = args[0];
41 Console.WriteLine(
"URL: {0}", server_url );
45 run_example( server_url );
46 run_series_example( server_url );
47 run_multihead_ingest_example( server_url );
51 Console.WriteLine(
"Caught Exception: {0}", ex.Message );
56 Console.WriteLine(
"Example C# Project - Done");
58 Console.WriteLine(
"Enter any 7-digit prime number to continue: ");
67 private static void run_example(
string server_url )
72 Console.WriteLine(
"Example with a Record Type with Nullable Columns, Primary Keys and Shard Keys" );
73 Console.WriteLine(
"=============================================================================" );
76 string table_name =
"csharp_example_table";
79 Console.WriteLine(
"Creating the type in kinetica..." );
81 IDictionary<string, IList<string>> column_properties =
new Dictionary<string, IList<string>>();
83 List<string> D_props =
new List<string>();
86 column_properties.Add(
"D", D_props );
88 List<string> E_props =
new List<string>();
90 column_properties.Add(
"E", E_props );
92 List<string> A_props =
new List<string>();
94 column_properties.Add(
"A", A_props );
95 List<string> B_props =
new List<string>();
97 column_properties.Add(
"B", B_props );
105 string type_id = type1.
create( kdb );
106 Console.WriteLine(
"ID of the created type: " + type_id );
110 Console.WriteLine(
"Fetching the newly created type information..." );
112 Console.WriteLine(
"Type properties: " );
113 foreach ( var x
in rsp.properties[0] )
114 Console.WriteLine( x.Key +
": " + String.Join(
",", x.Value ) );
118 Console.WriteLine(
"Clearing any existing table named '{0}'", table_name );
119 try { kdb.clearTable( table_name, null ); }
catch ( Exception ex ) { }
123 Console.WriteLine(
"Creating table named '{0}'", table_name );
124 kdb.createTable( table_name, type_id );
128 Dictionary<string, string> show_table_options =
new Dictionary<string, string>();
130 Console.WriteLine(
"Calling ShowTable on '{0}'", table_name );
131 var response2 = kdb.showTable(table_name, show_table_options);
132 Console.WriteLine(
"Total size: {0}", response2.total_size );
133 Console.WriteLine(
"sizes: {0}",
string.Join(
", ", response2.sizes ) );
137 List<record_type_1> newData =
new List<record_type_1>() {
138 new record_type_1() { A=99, B=11, C=
"T0_lksdjfokdj92", D=
"D01", E= 2.34F, F=null, TIMESTAMP= (long)(DateTime.UtcNow -
new DateTime(1970, 1, 1)).TotalMilliseconds },
139 new record_type_1() { A=2, B=3, C=
"T1_asdfghjkl", D=null, E= 5.67F, F=null, TIMESTAMP= (long)(DateTime.UtcNow -
new DateTime(1970, 1, 1)).TotalMilliseconds },
140 new record_type_1() { A=99, B=999, C=
"T2_oierlwk", D=
"D244", E=-45.1F, F=9899.1, TIMESTAMP= (long)(DateTime.UtcNow -
new DateTime(1970, 1, 1)).TotalMilliseconds }
144 Console.WriteLine(
"Inserting some data in '{0}'", table_name );
145 var insertResponse = kdb.insertRecords( table_name, newData );
146 Console.WriteLine(
"Inserted {0} records", insertResponse.count_inserted + insertResponse.count_updated );
150 Console.WriteLine(
"Calling ShowTable on '{0}' after adding data", table_name );
151 var rsp2 = kdb.showTable(table_name, show_table_options);
152 Console.WriteLine(
"Total size: {0}", rsp2.total_size );
153 Console.WriteLine(
"sizes: {0}",
string.Join(
", ", rsp2.sizes ) );
157 Console.WriteLine(
"Getting records out" );
158 var getRecordsResponse = kdb.getRecords<record_type_1>( table_name, 0, 100 );
159 Console.WriteLine(
"GetRecords got {0} records", getRecordsResponse.data.Count );
160 foreach ( var r
in getRecordsResponse.data ) Console.WriteLine(
"\t" + r.ToString() );
164 Console.WriteLine(
"Filtering data" );
165 string view_name_1 =
"csharp_view_01";
166 string filter_expression =
"E > 0";
167 var filterResponse = kdb.filter( table_name, view_name_1, filter_expression );
168 Console.WriteLine(
"Filtered {0} records", filterResponse.count );
172 Console.WriteLine(
"Getting records out (using /get/records/fromcollection) from view {0}", view_name_1 );
173 IDictionary<string, string> getrecords_fc_options =
new Dictionary<string, string>();
174 getrecords_fc_options.Add(
"return_record_ids",
"true" );
176 var getRecordsFromCollectionResponse = kdb.getRecordsFromCollection<record_type_1>( request );
177 Console.WriteLine(
"GetRecordsFromCollection got {0} records", getRecordsFromCollectionResponse.data.Count );
178 foreach ( var r
in getRecordsFromCollectionResponse.data ) Console.WriteLine(
"\t" + r.ToString() );
182 Console.WriteLine(
"Performing a group-by aggregate operation" );
183 IList<string> column_names =
new List<string>();
184 column_names.Add(
"A" );
185 column_names.Add(
"D" );
186 var agbResponse = kdb.aggregateGroupBy( table_name, column_names, 0, 100 );
187 Console.WriteLine(
"Group by got {0} records", agbResponse.total_number_of_records );
189 ( ( List<KineticaRecord> ) ( agbResponse.data ) ).ForEach( r => Console.WriteLine( r.ContentsToString() ) );
193 string col_name =
"F";
194 Console.WriteLine( $
"Performing a unique aggregate operation on column {col_name}" );
195 var uniqueResponse = kdb.aggregateUnique( table_name, col_name, 0, 100 );
197 ( ( List<KineticaRecord> ) ( uniqueResponse.data ) ).ForEach( r => Console.WriteLine( r.ContentsToString() ) );
201 Console.WriteLine(
"Getting records out (using /get/records/bycolumn)" );
202 IList<string> col_names =
new List<string>();
203 col_names.Add(
"B" );
204 col_names.Add(
"C" );
205 col_names.Add(
"E" );
206 var getRecordsByColumnResponse = kdb.getRecordsByColumn( table_name, col_names, 0, 100 );
207 Console.WriteLine(
"GetRecordsByColumn got {0} records", getRecordsByColumnResponse.data.Count );
208 foreach ( var r
in getRecordsByColumnResponse.data ) Console.WriteLine(
"\t" + r.ContentsToString() );
220 private static void run_series_example(
string server_url )
225 Console.WriteLine(
"Example showcasing a record with a series type column" );
226 Console.WriteLine(
"======================================================" );
231 string series_type_id = type_series.
create( kdb );
232 Console.WriteLine(
"ID of the created series type: " + series_type_id );
236 string table_name_series =
"csharp_example_series_table";
237 Console.WriteLine(
"Clearing any existing table named '{0}'", table_name_series );
238 try { kdb.
clearTable( table_name_series, null ); }
catch ( Exception ex ) { }
242 Console.WriteLine(
"Creating table named '{0}'", table_name_series );
243 kdb.
createTable( table_name_series, series_type_id );
247 string series_1 =
"series_1";
248 string series_2 =
"series_2";
249 List<record_type_series> series_data =
new List<record_type_series>() {
251 new record_type_series() { x = 30, y = 40, TRACKID = series_1, TIMESTAMP= (long)(DateTime.UtcNow -
new DateTime(1970, 1, 1)).TotalMilliseconds },
252 new record_type_series() { x = 35, y = 40, TRACKID = series_1, TIMESTAMP= (long)(DateTime.UtcNow -
new DateTime(1970, 1, 1)).TotalMilliseconds },
253 new record_type_series() { x = 40, y = 40, TRACKID = series_1, TIMESTAMP= (long)(DateTime.UtcNow -
new DateTime(1970, 1, 1)).TotalMilliseconds },
254 new record_type_series() { x = 45, y = 40, TRACKID = series_1, TIMESTAMP= (long)(DateTime.UtcNow -
new DateTime(1970, 1, 1)).TotalMilliseconds },
255 new record_type_series() { x = 50, y = 40, TRACKID = series_1, TIMESTAMP= (long)(DateTime.UtcNow -
new DateTime(1970, 1, 1)).TotalMilliseconds },
257 new record_type_series() { x = -30, y = -40, TRACKID = series_2, TIMESTAMP= (long)(DateTime.UtcNow -
new DateTime(1970, 1, 1)).TotalMilliseconds },
258 new record_type_series() { x = -30, y = -45, TRACKID = series_2, TIMESTAMP= (long)(DateTime.UtcNow -
new DateTime(1970, 1, 1)).TotalMilliseconds },
259 new record_type_series() { x = -30, y = -50, TRACKID = series_2, TIMESTAMP= (long)(DateTime.UtcNow -
new DateTime(1970, 1, 1)).TotalMilliseconds },
260 new record_type_series() { x = -30, y = -55, TRACKID = series_2, TIMESTAMP= (long)(DateTime.UtcNow -
new DateTime(1970, 1, 1)).TotalMilliseconds },
261 new record_type_series() { x = -30, y = -60, TRACKID = series_2, TIMESTAMP= (long)(DateTime.UtcNow -
new DateTime(1970, 1, 1)).TotalMilliseconds },
265 Console.WriteLine(
"Inserting some data in '{0}'", table_name_series );
266 var insertResponse2 = kdb.insertRecords( table_name_series, series_data );
267 Console.WriteLine(
"Inserted {0} records", insertResponse2.count_inserted + insertResponse2.count_updated );
271 Console.WriteLine(
"Getting records out from {0}", table_name_series );
272 var getRecordsResponse2 = kdb.getRecords<record_type_series>( table_name_series );
273 Console.WriteLine(
"GetRecords got {0} records", getRecordsResponse2.data.Count );
274 foreach ( var r
in getRecordsResponse2.data ) Console.WriteLine(
"\t" + r.ToString() );
279 Console.WriteLine(
"Filtering data from {0} so that only some points from {1} to make it", table_name_series, series_1 );
280 string view_name_2 =
"csharp_view_02";
284 var filterByBoxResp = kdb.
filterByBox( table_name_series, view_name_2,
"x", 33, 37,
"y", 35, 45 );
285 Console.WriteLine(
"Filtered {0} records", filterByBoxResp.count );
288 var getRecordsResponse3 = kdb.getRecords<record_type_series>( view_name_2 );
289 Console.WriteLine(
"GetRecords got {0} records from {1}", getRecordsResponse3.data.Count, view_name_2 );
290 foreach ( var r
in getRecordsResponse3.data ) Console.WriteLine(
"\t" + r.ToString() );
295 Console.WriteLine(
"Getting records belonging to one series out from table '{0}' using the partial series in view '{1}'", table_name_series, view_name_2 );
296 var getRecordsBySeriesResp = kdb.getRecordsBySeries<record_type_series>( view_name_2, table_name_series );
297 Console.WriteLine(
"GetRecordsBySeries got {0} list of records (should get one list of five records in it)", getRecordsBySeriesResp.data.Count );
298 foreach ( var r_list
in getRecordsBySeriesResp.data )
299 foreach ( var r
in r_list ) Console.WriteLine(
"\t" + r.ToString() );
311 private static void run_multihead_ingest_example(
string server_url )
316 Console.WriteLine(
"\n\n" );
317 Console.WriteLine(
"Example showcasing multihead ingestion(one shard key, two primary keys)" );
318 Console.WriteLine(
"=======================================================================" );
323 IDictionary<string, IList<string>> column_properties =
new Dictionary<string, IList<string>>();
326 List<string> A_props =
new List<string>();
328 column_properties.Add(
"A", A_props );
331 List<string> ts_props =
new List<string>();
335 column_properties.Add(
"TIMESTAMP", ts_props );
341 string type_id = type1.
create( kdb );
343 string table_name =
"csharp_example_table_01";
345 Console.WriteLine(
"Clearing any existing table named '{0}'", table_name );
346 try { kdb.
clearTable( table_name, null ); }
catch ( Exception ex ) { }
350 Console.WriteLine( $
"Creating table named '{table_name}'" );
356 int batch_size = 100;
360 int num_records = batch_size * 5;
361 List<record_type_1> records =
new List<record_type_1>();
362 Random rng =
new Random();
363 double null_probability = 0.2;
364 for (
int i = 0; i < num_records; ++i )
367 int max_str_len = rng.Next( 0, 256 );
368 record_type_1 record =
new record_type_1()
372 C =
System.IO.Path.GetRandomFileName().Truncate( max_str_len ),
373 D =
System.IO.Path.GetRandomFileName().Truncate( max_str_len ),
374 E = (float)(rng.NextDouble() * rng.Next()),
375 F = ( rng.NextDouble() < null_probability ) ? null : (
double? ) ( rng.NextDouble() * rng.Next() ),
376 TIMESTAMP = (
long)rng.Next( -306102240, 293795424 ) * rng.Next( 100000 )
379 records.Add( record );
382 Console.WriteLine( $
"Generated {num_records} records." );
385 Console.WriteLine( $
"Inserting {num_records} records..." );
386 ingestor.
insert( records );
389 Console.WriteLine(
"\nFlushing any remaining records." );
397 private class record_type_1
399 public int A {
get;
set; }
400 public int B {
get;
set; }
401 public string C {
get;
set; }
402 public string D {
get;
set; }
403 public float E {
get;
set; }
404 public double? F {
get;
set; }
405 public long TIMESTAMP {
get;
set; }
407 public override string ToString()
414 return $
"{{ A={A}, B={B}, C={C}, D={D}, E={E}, F={f}, TIMESTAMP={TIMESTAMP} }}";
419 private class record_type_series
421 public double x {
get;
set; }
422 public double y {
get;
set; }
423 public string TRACKID {
get;
set; }
424 public long TIMESTAMP {
get;
set; }
426 public override string ToString()
428 return $
"{{ x={x}, y={y}, TRACKID={TRACKID}, TIMESTAMP={TIMESTAMP} }}";
void insert(T record)
Queues a record for insertion into Kinetica.
const string PRIMARY_KEY
This property indicates that this column will be part of (or the entire) primary key.
const string GET_SIZES
If true then the table sizes will be returned; blank, otherwise.
CreateTableResponse createTable(CreateTableRequest request_)
Creates a new table or collection.
Column properties used for Kinetica types.
const string TIMESTAMP
Valid only for 'long' columns.
Specifies the encoding for returned records; either 'binary' or 'json'.
const string CHAR4
This property provides optimized memory, disk and query performance for string columns.
A set of parameters for Kinetica.getRecordsFromCollection<T>(string,long,long,IDictionary<string, string>).
ClearTableResponse clearTable(ClearTableRequest request_)
Clears (drops) one or all tables in the database cluster.
static KineticaType fromClass(Type recordClass, IDictionary< string, IList< string >> properties=null)
Create a KineticaType object from properties of a record class and Kinetica column properties...
A set of parameters for Kinetica.showTable(string,IDictionary<string, string>).
const string SHARD_KEY
This property indicates that this column will be part of (or the entire) shard key.
string create(Kinetica kinetica)
Given a handle to the server, creates a type in the database based on this data type.
static string Truncate(this string value, int maxLength)
Extension to string that has a truncate function.
ShowTypesResponse showTypes(ShowTypesRequest request_)
Retrieves information for the specified data type ID or type label.
FilterByBoxResponse filterByBox(FilterByBoxRequest request_)
Calculates how many objects within the given table lie in a rectangular box.
const string NULLABLE
This property indicates that this column is nullable.
A set of results returned by Kinetica.showTypes(string,string,IDictionary<string, string>)...
API to talk to Kinetica Database
Manages the insertion into GPUdb of large numbers of records in bulk, with automatic batch management...
void flush()
Ensures that all queued records are inserted into Kinetica.