Kinetica C# API  Version 7.0.19.0
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Pages
Example.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 
4 using Avro;
5 using kinetica;
6 
7 namespace Example
8 {
12  public static class StringExt
13  {
14  public static string Truncate( this string value, int maxLength )
15  {
16  if ( string.IsNullOrEmpty( value ) )
17  return value;
18 
19  return ( value.Length <= maxLength ) ? value : value.Substring( 0, maxLength );
20  }
21  }
22 
23 
24  class Example
25  {
26  static void Main(string[] args)
27  {
28  Console.WriteLine("Example C# Project - Running");
29  Console.WriteLine();
30 
31  if ( args.Length < 1)
32  {
33  Console.WriteLine("Missing URL as command-line parameter.");
34  Console.WriteLine("E.g., http://kinetica:9191");
35  return;
36  }
37 
38  try
39  {
40  string server_url = args[0];
41  Console.WriteLine( "URL: {0}", server_url );
42  Console.WriteLine();
43 
44  // Run the various example functions
45  run_example( server_url );
46  run_series_example( server_url );
47  run_multihead_ingest_example( server_url );
48  }
49  catch (Exception ex)
50  {
51  Console.WriteLine("Caught Exception: {0}", ex.Message );
52  }
53 
54  // We're done
55  Console.WriteLine();
56  Console.WriteLine("Example C# Project - Done");
57  Console.WriteLine();
58  Console.WriteLine("Enter any 7-digit prime number to continue: ");
59  Console.ReadLine();
60  }
61 
62 
67  private static void run_example( string server_url )
68  {
69  // Establish a connection with Kinetica
70  Kinetica kdb = new Kinetica( server_url );
71 
72  Console.WriteLine( "Example with a Record Type with Nullable Columns, Primary Keys and Shard Keys" );
73  Console.WriteLine( "=============================================================================" );
74  Console.WriteLine();
75 
76  string table_name = "csharp_example_table";
77 
78  // Create a type for our record_type_1 class
79  Console.WriteLine( "Creating the type in kinetica..." );
80  // Add some interesting properties for some of the data types
81  IDictionary<string, IList<string>> column_properties = new Dictionary<string, IList<string>>();
82  // Make a string char4 (and nullable)
83  List<string> D_props = new List<string>();
84  D_props.Add( ColumnProperty.CHAR4 );
85  D_props.Add( ColumnProperty.NULLABLE );
86  column_properties.Add( "D", D_props );
87  // Let's try another nullable column
88  List<string> E_props = new List<string>();
89  E_props.Add( ColumnProperty.NULLABLE );
90  column_properties.Add( "E", E_props );
91  // And two primary keys (one nullable)
92  List<string> A_props = new List<string>();
93  A_props.Add( ColumnProperty.PRIMARY_KEY );
94  column_properties.Add( "A", A_props );
95  List<string> B_props = new List<string>();
96  B_props.Add( ColumnProperty.PRIMARY_KEY );
97  column_properties.Add( "B", B_props );
98  // And a shard key (must be one of the primary keys, if specified--which we have)
99  B_props.Add( ColumnProperty.SHARD_KEY );
100 
101  // Create the KineticaType object which facilitates creating types in the database
102  KineticaType type1 = KineticaType.fromClass( typeof( record_type_1 ), column_properties );
103 
104  // Create the type in the database
105  string type_id = type1.create( kdb );
106  Console.WriteLine( "ID of the created type: " + type_id );
107  Console.WriteLine();
108 
109  // Show the type information (fetched from Kinetica)
110  Console.WriteLine( "Fetching the newly created type information..." );
111  ShowTypesResponse rsp = kdb.showTypes( type_id, "" );
112  Console.WriteLine( "Type properties: " );
113  foreach ( var x in rsp.properties[0] )
114  Console.WriteLine( x.Key + ": " + String.Join( ",", x.Value ) );
115  Console.WriteLine();
116 
117  // Clear any previously made table with the same name
118  Console.WriteLine( "Clearing any existing table named '{0}'", table_name );
119  try { kdb.clearTable( table_name, null ); } catch ( Exception ex ) { /* I don't care if the table doesn't already exists! */ }
120  Console.WriteLine();
121 
122  // Create a table of the given type
123  Console.WriteLine( "Creating table named '{0}'", table_name );
124  kdb.createTable( table_name, type_id );
125  Console.WriteLine();
126 
127  // Call /show/table on the table just created
128  Dictionary<string, string> show_table_options = new Dictionary<string, string>();
129  show_table_options.Add( ShowTableRequest.Options.GET_SIZES, ShowTableRequest.Options.TRUE );
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 ) );
134  Console.WriteLine();
135 
136  // Create some data to be added to the table
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 }
141  };
142 
143  // Insert the data into the table
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 );
147  Console.WriteLine();
148 
149  // Call /show/table on the table after adding data
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 ) );
154  Console.WriteLine();
155 
156  // Fetch the data back out of the DB using /get/records
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() );
161  Console.WriteLine();
162 
163  // Do a filter operation
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 );
169  Console.WriteLine();
170 
171  // Fetch the data from the filtered view using /get/records/fromcollection
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" );
175  var request = new GetRecordsFromCollectionRequest( view_name_1, 0, 100, GetRecordsFromCollectionRequest.Encoding.BINARY, getrecords_fc_options );
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() );
179  Console.WriteLine();
180 
181  // Do an /aggregate/groupby operation on the data
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 );
188  // Print the decoded data out to the console
189  ( ( List<KineticaRecord> ) ( agbResponse.data ) ).ForEach( r => Console.WriteLine( r.ContentsToString() ) );
190  Console.WriteLine();
191 
192  // Do an /aggregate/unique operation on column F
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 );
196  // Print the decoded data out to the console
197  ( ( List<KineticaRecord> ) ( uniqueResponse.data ) ).ForEach( r => Console.WriteLine( r.ContentsToString() ) );
198  Console.WriteLine();
199 
200  // Fetch the data back out of the DB using /get/records/bycolumn
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() );
209  Console.WriteLine();
210 
211  Console.WriteLine();
212  } // end run_example
213 
214 
215 
220  private static void run_series_example( string server_url )
221  {
222  // Establish a connection with Kinetica
223  Kinetica kdb = new Kinetica( server_url );
224 
225  Console.WriteLine( "Example showcasing a record with a series type column" );
226  Console.WriteLine( "======================================================" );
227  Console.WriteLine();
228 
229  // Create the series type record in Kinetica
230  KineticaType type_series = KineticaType.fromClass( typeof( record_type_series ) );
231  string series_type_id = type_series.create( kdb );
232  Console.WriteLine( "ID of the created series type: " + series_type_id );
233  Console.WriteLine();
234 
235  // Clear any previously made table with the same name
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 ) { /* I don't care if the table doesn't already exists! */ }
239  Console.WriteLine();
240 
241  // Create a table of the given type
242  Console.WriteLine( "Creating table named '{0}'", table_name_series );
243  kdb.createTable( table_name_series, series_type_id );
244  Console.WriteLine();
245 
246  // Create some data to be added to the table
247  string series_1 = "series_1";
248  string series_2 = "series_2";
249  List<record_type_series> series_data = new List<record_type_series>() {
250  // Five series points moving horizontally
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 },
256  // Five series points moving vertically
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 },
262  };
263 
264  // Insert the data into the table
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 );
268  Console.WriteLine();
269 
270  // Fetch the data back out of the DB using /get/records
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() );
275  Console.WriteLine();
276 
277  // Filter the series data such that only a few series point from only one of the series
278  // are in the view
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";
281  // Only the first, second, and third records from the first series should survive this filter
282  // (for track/series type records, we're filtering the "line segments", so records just outside the
283  // filtered box will also be captured)
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 );
286  Console.WriteLine();
287  // Show the filtered objects
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() );
291  Console.WriteLine();
292 
293 
294  // Extract the entire series from the source table (based on which ones are in the view)
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() );
300  Console.WriteLine();
301 
302  Console.WriteLine();
303  } // end run_series_example()
304 
305 
311  private static void run_multihead_ingest_example( string server_url )
312  {
313  // Establish a connection with Kinetica
314  Kinetica kdb = new Kinetica( server_url );
315 
316  Console.WriteLine( "\n\n" );
317  Console.WriteLine( "Example showcasing multihead ingestion(one shard key, two primary keys)" );
318  Console.WriteLine( "=======================================================================" );
319  Console.WriteLine();
320 
321  // Create a type for our record_type_1 class
322  // Add some interesting properties for some of the data types
323  IDictionary<string, IList<string>> column_properties = new Dictionary<string, IList<string>>();
324 
325  // Add a primary key
326  List<string> A_props = new List<string>();
327  A_props.Add( ColumnProperty.PRIMARY_KEY );
328  column_properties.Add( "A", A_props );
329 
330  // And a shard key (must be part of the primary keys, if specified--which we have)
331  List<string> ts_props = new List<string>();
332  ts_props.Add( ColumnProperty.PRIMARY_KEY );
333  ts_props.Add( ColumnProperty.SHARD_KEY );
334  ts_props.Add( ColumnProperty.TIMESTAMP );
335  column_properties.Add( "TIMESTAMP", ts_props );
336 
337  // Create the KineticaType object which facilitates creating types in the database
338  KineticaType type1 = KineticaType.fromClass( typeof( record_type_1 ), column_properties );
339 
340  // Create the type in the database
341  string type_id = type1.create( kdb );
342 
343  string table_name = "csharp_example_table_01";
344  // Clear any previously made table with the same name
345  Console.WriteLine( "Clearing any existing table named '{0}'", table_name );
346  try { kdb.clearTable( table_name, null ); } catch ( Exception ex ) { /* I don't care if the table doesn't already exists! */ }
347  Console.WriteLine();
348 
349  // Create a table of the given type
350  Console.WriteLine( $"Creating table named '{table_name}'" );
351  kdb.createTable( table_name, type_id );
352  Console.WriteLine();
353 
354  // Create the ingestor (we're not giving any worker IP addresses; the ingestor class will figure it
355  // out by itself)
356  int batch_size = 100;
357  KineticaIngestor<record_type_1> ingestor = new KineticaIngestor<record_type_1>( kdb, table_name, batch_size, type1 );
358 
359  // Generate data to be inserted
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 )
365  {
366  // Restricting string length to 256
367  int max_str_len = rng.Next( 0, 256 );
368  record_type_1 record = new record_type_1()
369  {
370  A = rng.Next(),
371  B = rng.Next(),
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 )
377  };
378 
379  records.Add( record );
380  } // end for loop
381 
382  Console.WriteLine( $"Generated {num_records} records." );
383 
384  // Insert the records into the ingestor
385  Console.WriteLine( $"Inserting {num_records} records..." );
386  ingestor.insert( records );
387 
388  // Flush the ingestor (which actually inserts the records)
389  Console.WriteLine( "\nFlushing any remaining records." );
390  ingestor.flush();
391 
392  Console.WriteLine();
393  Console.WriteLine();
394  } // end run_multihead_ingest_example()
395 
396 
397  private class record_type_1
398  {
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; }
406 
407  public override string ToString()
408  {
409  string f;
410  if ( F != null )
411  f = $"{F}";
412  else
413  f = "<null>";
414  return $"{{ A={A}, B={B}, C={C}, D={D}, E={E}, F={f}, TIMESTAMP={TIMESTAMP} }}";
415  }
416  } // end class record_type_1
417 
418 
419  private class record_type_series
420  {
421  public double x { get; set; }
422  public double y { get; set; }
423  public string TRACKID { get; set; }
424  public long TIMESTAMP { get; set; }
425 
426  public override string ToString()
427  {
428  return $"{{ x={x}, y={y}, TRACKID={TRACKID}, TIMESTAMP={TIMESTAMP} }}";
429  }
430  } // end class record_type_series
431  }
432 }
A set of parameters for Kinetica.getRecordsFromCollection{T}(string,long,long,IDictionary{string, string}).
static string Truncate(this string value, int maxLength)
Definition: Example.cs:14
Extension to string that has a truncate function.
Definition: Example.cs:12
A set of results returned by Kinetica.showTypes(string,string,IDictionary{string, string})...
Definition: ShowTypes.cs:158
API to talk to Kinetica Database
Definition: Kinetica.cs:40