Kinetica C# API  Version 6.0.1.0
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Pages
Test.cs
Go to the documentation of this file.
1 
7 using System;
8 using System.Collections.Generic;
9 using System.Linq;
10 using System.Text.RegularExpressions;
11 
12 using kinetica;
13 
14 namespace Test
15 {
19  public static class StringExt
20  {
21  public static string Truncate( this string value, int maxLength )
22  {
23  if ( string.IsNullOrEmpty( value ) )
24  return value;
25 
26  return ( value.Length <= maxLength ) ? value : value.Substring( 0, maxLength );
27  }
28  }
29 
30 
36  class Test
37  {
38  static void Main( string[] args )
39  {
40  Console.WriteLine( "Testing C# Project - Running" );
41  Console.WriteLine();
42 
43  if ( args.Length < 1 )
44  {
45  Console.WriteLine( "Missing URL as command-line parameter." );
46  Console.WriteLine( "E.g., http://kinetica:9191" );
47  return;
48  }
49 
50  try
51  {
52  string server_url = args[0];
53  Console.WriteLine( "URL: {0}", server_url );
54  Console.WriteLine();
55 
56  // Run various multihead ingest tests
57  // ----------------------------------
58  //test_authentication( server_url );
59  test_all_types_5shard_0pk( server_url );
60  test_all_types_3pk_3shard( server_url );
61  test_all_types_3pk_2shard( server_url );
62  test_all_types_3pk_1shard( server_url );
63  test_all_types_3pk_0shard( server_url );
64  test_all_types_0shard_0pk( server_url );
65  test_all_types_ip_regex_3shard_0pk( server_url );
66  }
67  catch ( Exception ex )
68  {
69  Console.WriteLine( "Caught Exception: {0}", ex.Message );
70  }
71 
72  // We're done
73  Console.WriteLine();
74  Console.WriteLine( "Testing C# Project - Done" );
75  Console.WriteLine();
76  Console.WriteLine( "Enter any 7-digit prime number to continue: " );
77  Console.ReadLine();
78  } // end Main
79 
80 
81  private class record_type_short
82  {
83  public int i { get; set; }
84  public int i8 { get; set; }
85 
86  public override string ToString()
87  {
88  return $"{{ i={i}, i8={i8} }}";
89  }
90  } // end class record_type_short
91 
92 
93  private static void test_authentication( string server_url )
94  {
95  Console.WriteLine( "\n\n" );
96  Console.WriteLine( "Test Authentication" );
97  Console.WriteLine( "===================" );
98  Console.WriteLine();
99 
100  Console.WriteLine( "This test is done dynamically:" );
101  Console.WriteLine( "------------------------------" );
102  Console.WriteLine( "* First run--create the user with the 'correct' username" );
103  Console.WriteLine( "* Second run--comment out user creation; run with 'correct'" );
104  Console.WriteLine( " username and make sure that the endpoints go through" );
105  Console.WriteLine( "* Third run--comment out user creation; run with 'wrong'" );
106  Console.WriteLine( " username and make sure that we get an 'insufficient credentials'" );
107  Console.WriteLine( " error thrown from the server." );
108  Console.WriteLine();
109 
110  string username = "abcdef"; // correct
111  //string username = "abcdefg"; // wrong
112  string password = "ghijkl123_";
113 
114  // Establish a connection with Kinetica
115  // Create a user
116  Kinetica kdb_ = new Kinetica( server_url );
117  Console.WriteLine( $"Creating a user {username}" );
118  Console.WriteLine();
119  IDictionary <string, string> options = new Dictionary<string, string>();
120  kdb_.createUserInternal( username, password, options );
121 
122  // Establish another connection with Kinetica with authentication information
123  Console.WriteLine( "Creating a DB handle with the proper username and password" );
124  Kinetica.Options db_options = new Kinetica.Options();
125  db_options.Username = username;
126  db_options.Password = password;
127  Kinetica kdb = new Kinetica( server_url, db_options );
128 
129  // Create the KineticaType object which facilitates creating types in the database
130  KineticaType type1 = KineticaType.fromClass( typeof( record_type_short ) );
131 
132  // Create the type in the database
133  string type_id = type1.create( kdb );
134 
135  string table_name = "csharp_example_table_01";
136  // Clear any previously made table with the same name
137  Console.WriteLine( "Clearing any existing table named '{0}'", table_name );
138  try { kdb.clearTable( table_name, null ); } catch ( Exception ex ) { /* I don't care if the table doesn't already exists! */ }
139  Console.WriteLine();
140 
141  // Create a table of the given type
142  Console.WriteLine( $"Creating table named '{table_name}'" );
143  kdb.createTable( table_name, type_id );
144  Console.WriteLine();
145 
146 
147  int num_records = 5000;
148  kdb.insertRecordsRandom( table_name, num_records );
149  Console.WriteLine( $"Generated {num_records} records." );
150  } // end test_authentication()
151 
152 
153  private class record_type_all
154  {
155  public int i { get; set; }
156  public int i8 { get; set; }
157  public int i16 { get; set; }
158  public long l { get; set; }
159  public float f { get; set; }
160  public double? d { get; set; } // nullable
161  public string s { get; set; }
162  public string c1 { get; set; }
163  public string c2 { get; set; }
164  public string c4 { get; set; }
165  public string c8 { get; set; }
166  public string c16 { get; set; }
167  public string c32 { get; set; }
168  public string c64 { get; set; }
169  public string c128 { get; set; }
170  public string c256 { get; set; }
171  public string date { get; set; }
172  public string decimal_ { get; set; }
173  public string ipv4 { get; set; }
174  public string time { get; set; }
175  public long timestamp { get; set; }
176 
177  public override string ToString()
178  {
179  string d_;
180  if ( d != null )
181  d_ = $"{d}";
182  else
183  d_ = "<null>";
184  return $"{{ i={i}, i8={i8}, i16={i16}, l={l}, f={f}, d={d_}, s={s}, c1={c1}, c2={c2}, c4={c4}, c8={c8}, c16={c16}, c32={c32}, c64={c64}, c128={c128}, c256={c256}, date={date}, decimal_={decimal_}, ipv4={ipv4}, time={time}, timestamp={timestamp} }}";
185  }
186  } // end class record_type_all
187 
188 
189  private static void test_all_types_ip_regex_3shard_0pk( string server_url )
190  {
191  // Establish a connection with Kinetica
192  Kinetica kdb = new Kinetica( server_url );
193 
194  Console.WriteLine( "\n\n" );
195  Console.WriteLine( "Test Multihead Ingest with a Regex Given for Worker IP Addresses (3 shard keys, no PKs)" );
196  Console.WriteLine( "=======================================================================================" );
197  Console.WriteLine();
198 
199  // Create a type for our record_type_1 class
200  // Add some interesting properties for some of the data types
201  IDictionary<string, IList<string>> column_properties = new Dictionary<string, IList<string>>();
202  // Integer shard key
203  List<string> i_props = new List<string>();
204  i_props.Add( ColumnProperty.SHARD_KEY );
205  column_properties.Add( "i", i_props );
206  // Double shard key
207  List<string> d_props = new List<string>();
208  d_props.Add( ColumnProperty.SHARD_KEY );
209  column_properties.Add( "d", d_props );
210  // Short shard key
211  List<string> i16_props = new List<string>();
212  i16_props.Add( ColumnProperty.SHARD_KEY );
213  column_properties.Add( "i16", i16_props );
214 
215  // Create the KineticaType object which facilitates creating types in the database
216  KineticaType type1 = KineticaType.fromClass( typeof( record_type_all ), column_properties );
217 
218  // Create the type in the database
219  string type_id = type1.create( kdb );
220 
221  string table_name = "csharp_example_table_01";
222  // Clear any previously made table with the same name
223  Console.WriteLine( "Clearing any existing table named '{0}'", table_name );
224  try { kdb.clearTable( table_name, null ); } catch ( Exception ex ) { /* I don't care if the table doesn't already exists! */ }
225  Console.WriteLine();
226 
227  // Create a table of the given type
228  Console.WriteLine( $"Creating table named '{table_name}'" );
229  kdb.createTable( table_name, type_id );
230  Console.WriteLine();
231 
232  // Create the ingestor
233  int batch_size = 100;
234  Regex ip_regex = new Regex( "172.30.*" );
235  KineticaIngestor<record_type_all>.WorkerList worker_list = new KineticaIngestor<record_type_all>.WorkerList( kdb, ip_regex );
236  KineticaIngestor<record_type_all> ingestor = new KineticaIngestor<record_type_all>( kdb, table_name, batch_size, type1, null, worker_list );
237 
238  // Generate data to be inserted
239  int num_records = batch_size * 5;
240  List<record_type_all> records = new List<record_type_all>();
241  Random rng = new Random();
242  double null_probability = 0.2;
243  for ( int i = 0; i < num_records; ++i )
244  {
245  // Restricting string length to 256
246  int max_str_len = rng.Next( 0, 256 );
247  record_type_all record = new record_type_all()
248  {
249  i = rng.Next(),
250  i8 = rng.Next(),
251  i16 = rng.Next(),
252  l = (long)rng.Next(),
253  f = (float)(rng.NextDouble() * rng.Next()),
254  d = ( rng.NextDouble() < null_probability ) ? null : ( double? ) ( rng.NextDouble() * rng.Next() ),
255  s = System.IO.Path.GetRandomFileName().Truncate( max_str_len ),
256  c1 = System.IO.Path.GetRandomFileName().Truncate( 1 ),
257  c2 = System.IO.Path.GetRandomFileName().Truncate( 2 ),
258  c4 = System.IO.Path.GetRandomFileName().Truncate( 4 ),
259  c8 = System.IO.Path.GetRandomFileName().Truncate( 8 ),
260  c16 = System.IO.Path.GetRandomFileName().Truncate( 16 ),
261  c32 = System.IO.Path.GetRandomFileName().Truncate( 32 ),
262  c64 = System.IO.Path.GetRandomFileName().Truncate( 64 ),
263  c128 = System.IO.Path.GetRandomFileName().Truncate( 128 ),
264  c256 = System.IO.Path.GetRandomFileName().Truncate( 256 ),
265  date = Test.generate_date( rng ),
266  decimal_ = Test.generate_decimal( rng ),
267  ipv4 = Test.generate_IPv4( rng ),
268  time = Test.generate_time( rng ),
269  timestamp = (long) rng.Next()
270  };
271 
272  //Console.WriteLine( record.ToString() ); // debug~~~~~~~~
273  records.Add( record );
274  } // end for loop
275 
276  Console.WriteLine( $"Generated {num_records} records." );
277 
278  // Insert the records into the ingestor
279  Console.WriteLine( $"Inserting {num_records} records..." );
280  ingestor.insert( records );
281 
282  // Flush the ingestor (which actually inserts the records)
283  Console.WriteLine( "\nFlushing any remaining records." );
284  ingestor.flush();
285  } // end test_all_types_ip_regex_3shard_0pk()
286 
287 
288  private static void test_all_types_0shard_0pk( string server_url )
289  {
290  // Establish a connection with Kinetica
291  Kinetica kdb = new Kinetica( server_url );
292 
293  Console.WriteLine( "\n\n" );
294  Console.WriteLine( "Test Multihead Ingest: One column per type; 0 Shard Keys, 0 Primary Keys" );
295  Console.WriteLine( "========================================================================" );
296  Console.WriteLine();
297 
298  // Create the KineticaType object which facilitates creating types in the database
299  KineticaType type1 = KineticaType.fromClass( typeof( record_type_all ) );
300 
301  // Create the type in the database
302  string type_id = type1.create( kdb );
303 
304  string table_name = "csharp_example_table_01";
305  // Clear any previously made table with the same name
306  Console.WriteLine( "Clearing any existing table named '{0}'", table_name );
307  try { kdb.clearTable( table_name, null ); } catch ( Exception ex ) { /* I don't care if the table doesn't already exists! */ }
308  Console.WriteLine();
309 
310  // Create a table of the given type
311  Console.WriteLine( $"Creating table named '{table_name}'" );
312  kdb.createTable( table_name, type_id );
313  Console.WriteLine();
314 
315  // Create the ingestor
316  int batch_size = 1000;
317  KineticaIngestor<record_type_all> ingestor = new KineticaIngestor<record_type_all>( kdb, table_name, batch_size, type1 );
318 
319  // Generate data to be inserted
320  int num_records = batch_size * 50;
321  List<record_type_all> records = new List<record_type_all>();
322  Random rng = new Random();
323  double null_probability = 0.2;
324  for ( int i = 0; i < num_records; ++i )
325  {
326  // Restricting string length to 256
327  int max_str_len = rng.Next( 0, 256 );
328  record_type_all record = new record_type_all()
329  {
330  i = rng.Next(),
331  i8 = rng.Next(),
332  i16 = rng.Next(),
333  l = (long)rng.Next(),
334  f = (float)(rng.NextDouble() * rng.Next()),
335  d = ( rng.NextDouble() < null_probability ) ? null : ( double? ) ( rng.NextDouble() * rng.Next() ),
336  s = System.IO.Path.GetRandomFileName().Truncate( max_str_len ),
337  c1 = System.IO.Path.GetRandomFileName().Truncate( 1 ),
338  c2 = System.IO.Path.GetRandomFileName().Truncate( 2 ),
339  c4 = System.IO.Path.GetRandomFileName().Truncate( 4 ),
340  c8 = System.IO.Path.GetRandomFileName().Truncate( 8 ),
341  c16 = System.IO.Path.GetRandomFileName().Truncate( 16 ),
342  c32 = System.IO.Path.GetRandomFileName().Truncate( 32 ),
343  c64 = System.IO.Path.GetRandomFileName().Truncate( 64 ),
344  c128 = System.IO.Path.GetRandomFileName().Truncate( 128 ),
345  c256 = System.IO.Path.GetRandomFileName().Truncate( 256 ),
346  date = Test.generate_date( rng ),
347  decimal_ = Test.generate_decimal( rng ),
348  ipv4 = Test.generate_IPv4( rng ),
349  time = Test.generate_time( rng ),
350  timestamp = (long) rng.Next()
351  };
352 
353  //Console.WriteLine( record.ToString() ); // debug~~~~~~~~
354  records.Add( record );
355  } // end for loop
356 
357  Console.WriteLine( $"Generated {num_records} records." );
358 
359  // Insert the records into the ingestor
360  Console.WriteLine( $"Inserting {num_records} records..." );
361  ingestor.insert( records );
362 
363  // Flush the ingestor (which actually inserts the records)
364  Console.WriteLine( "\nFlushing any remaining records." );
365  ingestor.flush();
366  } // end test_all_types_0shard_0pk()
367 
368 
369 
370  private static void test_all_types_5shard_0pk( string server_url )
371  {
372  // Establish a connection with Kinetica
373  Kinetica kdb = new Kinetica( server_url );
374 
375  Console.WriteLine( "\n\n" );
376  Console.WriteLine( "Test Multihead Ingest: One column per Type; 5 Shard Keys, 0 Primary Keys" );
377  Console.WriteLine( "========================================================================" );
378  Console.WriteLine();
379 
380  // Create a type for our record_type_1 class
381  // Add some interesting properties for some of the data types
382  IDictionary<string, IList<string>> column_properties = new Dictionary<string, IList<string>>();
383  // Integer shard key
384  List<string> i_props = new List<string>();
385  i_props.Add( ColumnProperty.SHARD_KEY );
386  column_properties.Add( "i", i_props );
387  // Double shard key
388  List<string> d_props = new List<string>();
389  d_props.Add( ColumnProperty.SHARD_KEY );
390  column_properties.Add( "d", d_props );
391  // Short shard key
392  List<string> i16_props = new List<string>();
393  i16_props.Add( ColumnProperty.SHARD_KEY );
394  column_properties.Add( "i16", i16_props );
395  // char2 shard key
396  List<string> c2_props = new List<string>();
397  c2_props.Add( ColumnProperty.SHARD_KEY );
398  column_properties.Add( "c2", c2_props );
399  // char8 shard key
400  List<string> c8_props = new List<string>();
401  c8_props.Add( ColumnProperty.SHARD_KEY );
402  column_properties.Add( "c8", c8_props );
403 
404  // Create the KineticaType object which facilitates creating types in the database
405  KineticaType type1 = KineticaType.fromClass( typeof( record_type_all ), column_properties );
406 
407  // Create the type in the database
408  string type_id = type1.create( kdb );
409  //Console.WriteLine( "ID of the created type: " + type_id );
410  //Console.WriteLine();
411 
412  string table_name = "csharp_example_table_01";
413  // Clear any previously made table with the same name
414  Console.WriteLine( "Clearing any existing table named '{0}'", table_name );
415  try { kdb.clearTable( table_name, null ); } catch ( Exception ex ) { /* I don't care if the table doesn't already exists! */ }
416  Console.WriteLine();
417 
418  // Create a table of the given type
419  Console.WriteLine( $"Creating table named '{table_name}'" );
420  kdb.createTable( table_name, type_id );
421  Console.WriteLine();
422 
423  // Create the ingestor
424  int batch_size = 1000;
425  KineticaIngestor<record_type_all> ingestor = new KineticaIngestor<record_type_all>( kdb, table_name, batch_size, type1 );
426 
427  // Generate data to be inserted
428  int num_records = batch_size * 50;
429  List<record_type_all> records = new List<record_type_all>();
430  Random rng = new Random();
431  double null_probability = 0.2;
432  for ( int i = 0; i < num_records; ++i )
433  {
434  // Restricting string length to 256
435  int max_str_len = rng.Next( 0, 256 );
436  record_type_all record = new record_type_all()
437  {
438  i = rng.Next(),
439  i8 = rng.Next(),
440  i16 = rng.Next(),
441  l = (long)rng.Next(),
442  f = (float)(rng.NextDouble() * rng.Next()),
443  d = ( rng.NextDouble() < null_probability ) ? null : ( double? ) ( rng.NextDouble() * rng.Next() ),
444  s = System.IO.Path.GetRandomFileName().Truncate( max_str_len ),
445  c1 = System.IO.Path.GetRandomFileName().Truncate( 1 ),
446  c2 = System.IO.Path.GetRandomFileName().Truncate( 2 ),
447  c4 = System.IO.Path.GetRandomFileName().Truncate( 4 ),
448  c8 = System.IO.Path.GetRandomFileName().Truncate( 8 ),
449  c16 = System.IO.Path.GetRandomFileName().Truncate( 16 ),
450  c32 = System.IO.Path.GetRandomFileName().Truncate( 32 ),
451  c64 = System.IO.Path.GetRandomFileName().Truncate( 64 ),
452  c128 = System.IO.Path.GetRandomFileName().Truncate( 128 ),
453  c256 = System.IO.Path.GetRandomFileName().Truncate( 256 ),
454  date = Test.generate_date( rng ),
455  decimal_ = Test.generate_decimal( rng ),
456  ipv4 = Test.generate_IPv4( rng ),
457  time = Test.generate_time( rng ),
458  timestamp = (long) rng.Next()
459  };
460 
461  //Console.WriteLine( record.ToString() ); // debug~~~~~~~~
462  records.Add( record );
463  } // end for loop
464 
465  Console.WriteLine( $"Generated {num_records} records." );
466 
467  // Insert the records into the ingestor
468  Console.WriteLine( $"Inserting {num_records} records..." );
469  ingestor.insert( records );
470 
471  // Flush the ingestor (which actually inserts the records)
472  Console.WriteLine( "\nFlushing any remaining records." );
473  ingestor.flush();
474  } // end test_all_types_5shard_0pk()
475 
476 
477 
478  private static void test_all_types_3pk_3shard( string server_url )
479  {
480  // Establish a connection with Kinetica
481  Kinetica kdb = new Kinetica( server_url );
482 
483  Console.WriteLine( "\n\n" );
484  Console.WriteLine( "Test Multihead Ingest: One column per Type; 3 Primary Keys, 3 Shard Keys" );
485  Console.WriteLine( "========================================================================" );
486  Console.WriteLine();
487 
488  // Create a type for our record_type_1 class
489  // Add some interesting properties for some of the data types
490  IDictionary<string, IList<string>> column_properties = new Dictionary<string, IList<string>>();
491  // long shard key, also primary key
492  List<string> l_props = new List<string>();
493  l_props.Add( ColumnProperty.PRIMARY_KEY );
494  l_props.Add( ColumnProperty.SHARD_KEY );
495  column_properties.Add( "l", l_props );
496  // date primary key as well as shard key
497  List<string> date_props = new List<string>();
498  date_props.Add( ColumnProperty.PRIMARY_KEY );
499  date_props.Add( ColumnProperty.SHARD_KEY );
500  column_properties.Add( "date", date_props );
501  // timestamp primary key as well as shard
502  List<string> ipv4_props = new List<string>();
503  ipv4_props.Add( ColumnProperty.PRIMARY_KEY );
504  ipv4_props.Add( ColumnProperty.SHARD_KEY );
505  column_properties.Add( "ipv4", ipv4_props );
506 
507  // Create the KineticaType object which facilitates creating types in the database
508  KineticaType type1 = KineticaType.fromClass( typeof( record_type_all ), column_properties );
509 
510  // Create the type in the database
511  string type_id = type1.create( kdb );
512  //Console.WriteLine( "ID of the created type: " + type_id );
513  //Console.WriteLine();
514 
515  string table_name = "csharp_example_table_01";
516  // Clear any previously made table with the same name
517  Console.WriteLine( "Clearing any existing table named '{0}'", table_name );
518  try { kdb.clearTable( table_name, null ); } catch ( Exception ex ) { /* I don't care if the table doesn't already exists! */ }
519  Console.WriteLine();
520 
521  // Create a table of the given type
522  Console.WriteLine( $"Creating table named '{table_name}'" );
523  kdb.createTable( table_name, type_id );
524  Console.WriteLine();
525 
526  // Create the ingestor
527  int batch_size = 1000;
528  KineticaIngestor<record_type_all> ingestor = new KineticaIngestor<record_type_all>( kdb, table_name, batch_size, type1 );
529 
530  // Generate data to be inserted
531  int num_records = batch_size * 50;
532  List<record_type_all> records = new List<record_type_all>();
533  Random rng = new Random();
534  double null_probability = 0.2;
535  for ( int i = 0; i < num_records; ++i )
536  {
537  // Restricting string length to 256
538  int max_str_len = rng.Next( 0, 256 );
539  record_type_all record = new record_type_all()
540  {
541  i = rng.Next(),
542  i8 = rng.Next(),
543  i16 = rng.Next(),
544  l = (long)rng.Next(),
545  f = (float)(rng.NextDouble() * rng.Next()),
546  d = ( rng.NextDouble() < null_probability ) ? null : ( double? ) ( rng.NextDouble() * rng.Next() ),
547  s = System.IO.Path.GetRandomFileName().Truncate( max_str_len ),
548  c1 = System.IO.Path.GetRandomFileName().Truncate( 1 ),
549  c2 = System.IO.Path.GetRandomFileName().Truncate( 2 ),
550  c4 = System.IO.Path.GetRandomFileName().Truncate( 4 ),
551  c8 = System.IO.Path.GetRandomFileName().Truncate( 8 ),
552  c16 = System.IO.Path.GetRandomFileName().Truncate( 16 ),
553  c32 = System.IO.Path.GetRandomFileName().Truncate( 32 ),
554  c64 = System.IO.Path.GetRandomFileName().Truncate( 64 ),
555  c128 = System.IO.Path.GetRandomFileName().Truncate( 128 ),
556  c256 = System.IO.Path.GetRandomFileName().Truncate( 256 ),
557  date = Test.generate_date( rng ),
558  decimal_ = Test.generate_decimal( rng ),
559  ipv4 = Test.generate_IPv4( rng ),
560  time = Test.generate_time( rng ),
561  timestamp = (long) rng.Next()
562  };
563 
564  //Console.WriteLine( record.ToString() ); // debug~~~~~~~~
565  records.Add( record );
566  } // end for loop
567 
568  Console.WriteLine( $"Generated {num_records} records." );
569 
570  // Insert the records into the ingestor
571  Console.WriteLine( $"Inserting {num_records} records..." );
572  ingestor.insert( records );
573 
574  // Flush the ingestor (which actually inserts the records)
575  Console.WriteLine( "\nFlushing any remaining records." );
576  ingestor.flush();
577  } // end test_all_types_3pk_3shard()
578 
579 
580 
581  private static void test_all_types_3pk_2shard( string server_url )
582  {
583  // Establish a connection with Kinetica
584  Kinetica kdb = new Kinetica( server_url );
585 
586  Console.WriteLine( "\n\n" );
587  Console.WriteLine( "Test Multihead Ingest: One column per Type; 3 Primary Keys, 2 Shard Keys" );
588  Console.WriteLine( "========================================================================" );
589  Console.WriteLine();
590 
591  // Create a type for our record_type_1 class
592  // Add some interesting properties for some of the data types
593  IDictionary<string, IList<string>> column_properties = new Dictionary<string, IList<string>>();
594  // char2 shard key, also primary key
595  List<string> c2_props = new List<string>();
596  c2_props.Add( ColumnProperty.PRIMARY_KEY );
597  c2_props.Add( ColumnProperty.SHARD_KEY );
598  column_properties.Add( "c2", c2_props );
599  // date primary key
600  List<string> date_props = new List<string>();
601  date_props.Add( ColumnProperty.PRIMARY_KEY );
602  column_properties.Add( "date", date_props );
603  // timestamp primary key as well as shard
604  List<string> timestamp_props = new List<string>();
605  timestamp_props.Add( ColumnProperty.PRIMARY_KEY );
606  timestamp_props.Add( ColumnProperty.SHARD_KEY );
607  column_properties.Add( "timestamp", timestamp_props );
608 
609  // Create the KineticaType object which facilitates creating types in the database
610  KineticaType type1 = KineticaType.fromClass( typeof( record_type_all ), column_properties );
611 
612  // Create the type in the database
613  string type_id = type1.create( kdb );
614  //Console.WriteLine( "ID of the created type: " + type_id );
615  //Console.WriteLine();
616 
617  string table_name = "csharp_example_table_01";
618  // Clear any previously made table with the same name
619  Console.WriteLine( "Clearing any existing table named '{0}'", table_name );
620  try { kdb.clearTable( table_name, null ); } catch ( Exception ex ) { /* I don't care if the table doesn't already exists! */ }
621  Console.WriteLine();
622 
623  // Create a table of the given type
624  Console.WriteLine( $"Creating table named '{table_name}'" );
625  kdb.createTable( table_name, type_id );
626  Console.WriteLine();
627 
628  // Create the ingestor
629  int batch_size = 1000;
630  KineticaIngestor<record_type_all> ingestor = new KineticaIngestor<record_type_all>( kdb, table_name, batch_size, type1 );
631 
632  // Generate data to be inserted
633  int num_records = batch_size * 50;
634  List<record_type_all> records = new List<record_type_all>();
635  Random rng = new Random();
636  double null_probability = 0.2;
637  for ( int i = 0; i < num_records; ++i )
638  {
639  // Restricting string length to 256
640  int max_str_len = rng.Next( 0, 256 );
641  record_type_all record = new record_type_all()
642  {
643  i = rng.Next(),
644  i8 = rng.Next(),
645  i16 = rng.Next(),
646  l = (long)rng.Next(),
647  f = (float)(rng.NextDouble() * rng.Next()),
648  d = ( rng.NextDouble() < null_probability ) ? null : ( double? ) ( rng.NextDouble() * rng.Next() ),
649  s = System.IO.Path.GetRandomFileName().Truncate( max_str_len ),
650  c1 = System.IO.Path.GetRandomFileName().Truncate( 1 ),
651  c2 = System.IO.Path.GetRandomFileName().Truncate( 2 ),
652  c4 = System.IO.Path.GetRandomFileName().Truncate( 4 ),
653  c8 = System.IO.Path.GetRandomFileName().Truncate( 8 ),
654  c16 = System.IO.Path.GetRandomFileName().Truncate( 16 ),
655  c32 = System.IO.Path.GetRandomFileName().Truncate( 32 ),
656  c64 = System.IO.Path.GetRandomFileName().Truncate( 64 ),
657  c128 = System.IO.Path.GetRandomFileName().Truncate( 128 ),
658  c256 = System.IO.Path.GetRandomFileName().Truncate( 256 ),
659  date = Test.generate_date( rng ),
660  decimal_ = Test.generate_decimal( rng ),
661  ipv4 = Test.generate_IPv4( rng ),
662  time = Test.generate_time( rng ),
663  timestamp = (long) rng.Next()
664  };
665 
666  //Console.WriteLine( record.ToString() ); // debug~~~~~~~~
667  records.Add( record );
668  } // end for loop
669 
670  Console.WriteLine( $"Generated {num_records} records." );
671 
672  // Insert the records into the ingestor
673  Console.WriteLine( $"Inserting {num_records} records..." );
674  ingestor.insert( records );
675 
676  // Flush the ingestor (which actually inserts the records)
677  Console.WriteLine( "\nFlushing any remaining records." );
678  ingestor.flush();
679  } // end test_all_types_3pk_2shard()
680 
681 
682 
683  private static void test_all_types_3pk_1shard( string server_url )
684  {
685  // Establish a connection with Kinetica
686  Kinetica kdb = new Kinetica( server_url );
687 
688  Console.WriteLine( "\n\n" );
689  Console.WriteLine( "Test Multihead Ingest: One column per Type; 3 Primary Keys, 1 Shard Key" );
690  Console.WriteLine( "========================================================================" );
691  Console.WriteLine();
692 
693  // Create a type for our record_type_1 class
694  // Add some interesting properties for some of the data types
695  IDictionary<string, IList<string>> column_properties = new Dictionary<string, IList<string>>();
696  // float shard key, also primary key
697  List<string> f_props = new List<string>();
698  f_props.Add( ColumnProperty.PRIMARY_KEY );
699  f_props.Add( ColumnProperty.SHARD_KEY );
700  column_properties.Add( "f", f_props );
701  // date primary key
702  List<string> date_props = new List<string>();
703  date_props.Add( ColumnProperty.PRIMARY_KEY );
704  column_properties.Add( "date", date_props );
705  // timestamp primary key as well as shard
706  List<string> timestamp_props = new List<string>();
707  timestamp_props.Add( ColumnProperty.PRIMARY_KEY );
708  column_properties.Add( "timestamp", timestamp_props );
709 
710  // Create the KineticaType object which facilitates creating types in the database
711  KineticaType type1 = KineticaType.fromClass( typeof( record_type_all ), column_properties );
712 
713  // Create the type in the database
714  string type_id = type1.create( kdb );
715  //Console.WriteLine( "ID of the created type: " + type_id );
716  //Console.WriteLine();
717 
718  string table_name = "csharp_example_table_01";
719  // Clear any previously made table with the same name
720  Console.WriteLine( "Clearing any existing table named '{0}'", table_name );
721  try { kdb.clearTable( table_name, null ); } catch ( Exception ex ) { /* I don't care if the table doesn't already exists! */ }
722  Console.WriteLine();
723 
724  // Create a table of the given type
725  Console.WriteLine( $"Creating table named '{table_name}'" );
726  kdb.createTable( table_name, type_id );
727  Console.WriteLine();
728 
729  // Create the ingestor
730  int batch_size = 1000;
731  KineticaIngestor<record_type_all> ingestor = new KineticaIngestor<record_type_all>( kdb, table_name, batch_size, type1 );
732 
733  // Generate data to be inserted
734  int num_records = batch_size * 50;
735  List<record_type_all> records = new List<record_type_all>();
736  Random rng = new Random();
737  double null_probability = 0.2;
738  for ( int i = 0; i < num_records; ++i )
739  {
740  // Restricting string length to 256
741  int max_str_len = rng.Next( 0, 256 );
742  record_type_all record = new record_type_all()
743  {
744  i = rng.Next(),
745  i8 = rng.Next(),
746  i16 = rng.Next(),
747  l = (long)rng.Next(),
748  f = (float)(rng.NextDouble() * rng.Next()),
749  d = ( rng.NextDouble() < null_probability ) ? null : ( double? ) ( rng.NextDouble() * rng.Next() ),
750  s = System.IO.Path.GetRandomFileName().Truncate( max_str_len ),
751  c1 = System.IO.Path.GetRandomFileName().Truncate( 1 ),
752  c2 = System.IO.Path.GetRandomFileName().Truncate( 2 ),
753  c4 = System.IO.Path.GetRandomFileName().Truncate( 4 ),
754  c8 = System.IO.Path.GetRandomFileName().Truncate( 8 ),
755  c16 = System.IO.Path.GetRandomFileName().Truncate( 16 ),
756  c32 = System.IO.Path.GetRandomFileName().Truncate( 32 ),
757  c64 = System.IO.Path.GetRandomFileName().Truncate( 64 ),
758  c128 = System.IO.Path.GetRandomFileName().Truncate( 128 ),
759  c256 = System.IO.Path.GetRandomFileName().Truncate( 256 ),
760  date = Test.generate_date( rng ),
761  decimal_ = Test.generate_decimal( rng ),
762  ipv4 = Test.generate_IPv4( rng ),
763  time = Test.generate_time( rng ),
764  timestamp = (long) rng.Next()
765  };
766 
767  //Console.WriteLine( record.ToString() ); // debug~~~~~~~~
768  records.Add( record );
769  } // end for loop
770 
771  Console.WriteLine( $"Generated {num_records} records." );
772 
773  // Insert the records into the ingestor
774  Console.WriteLine( $"Inserting {num_records} records..." );
775  ingestor.insert( records );
776 
777  // Flush the ingestor (which actually inserts the records)
778  Console.WriteLine( "\nFlushing any remaining records." );
779  ingestor.flush();
780  } // end test_all_types_3pk_1shard()
781 
782 
783 
784  private static void test_all_types_3pk_0shard( string server_url )
785  {
786  // Establish a connection with Kinetica
787  Kinetica kdb = new Kinetica( server_url );
788 
789  Console.WriteLine( "\n\n" );
790  Console.WriteLine( "Test Multihead Ingest: One column per type; 3 Primary Keys, No Shard Key" );
791  Console.WriteLine( "========================================================================" );
792  Console.WriteLine();
793 
794  // Create a type for our record_type_1 class
795  // Add some interesting properties for some of the data types
796  IDictionary<string, IList<string>> column_properties = new Dictionary<string, IList<string>>();
797  // float shard key, also primary key
798  List<string> f_props = new List<string>();
799  f_props.Add( ColumnProperty.PRIMARY_KEY );
800  column_properties.Add( "f", f_props );
801  // date primary key
802  List<string> date_props = new List<string>();
803  date_props.Add( ColumnProperty.PRIMARY_KEY );
804  column_properties.Add( "date", date_props );
805  // timestamp primary key as well as shard
806  List<string> timestamp_props = new List<string>();
807  timestamp_props.Add( ColumnProperty.PRIMARY_KEY );
808  column_properties.Add( "timestamp", timestamp_props );
809 
810  // Create the KineticaType object which facilitates creating types in the database
811  KineticaType type1 = KineticaType.fromClass( typeof( record_type_all ), column_properties );
812 
813  // Create the type in the database
814  string type_id = type1.create( kdb );
815  //Console.WriteLine( "ID of the created type: " + type_id );
816  //Console.WriteLine();
817 
818  string table_name = "csharp_example_table_01";
819  // Clear any previously made table with the same name
820  Console.WriteLine( "Clearing any existing table named '{0}'", table_name );
821  try { kdb.clearTable( table_name, null ); } catch ( Exception ex ) { /* I don't care if the table doesn't already exists! */ }
822  Console.WriteLine();
823 
824  // Create a table of the given type
825  Console.WriteLine( $"Creating table named '{table_name}'" );
826  kdb.createTable( table_name, type_id );
827  Console.WriteLine();
828 
829  // Create the ingestor
830  int batch_size = 1000;
831  KineticaIngestor<record_type_all> ingestor = new KineticaIngestor<record_type_all>( kdb, table_name, batch_size, type1 );
832 
833  // Generate data to be inserted
834  int num_records = batch_size * 50;
835  List<record_type_all> records = new List<record_type_all>();
836  Random rng = new Random();
837  double null_probability = 0.2;
838  for ( int i = 0; i < num_records; ++i )
839  {
840  // Restricting string length to 256
841  int max_str_len = rng.Next( 0, 256 );
842  record_type_all record = new record_type_all()
843  {
844  i = rng.Next(),
845  i8 = rng.Next(),
846  i16 = rng.Next(),
847  l = (long)rng.Next(),
848  f = (float)(rng.NextDouble() * rng.Next()),
849  d = ( rng.NextDouble() < null_probability ) ? null : ( double? ) ( rng.NextDouble() * rng.Next() ),
850  s = System.IO.Path.GetRandomFileName().Truncate( max_str_len ),
851  c1 = System.IO.Path.GetRandomFileName().Truncate( 1 ),
852  c2 = System.IO.Path.GetRandomFileName().Truncate( 2 ),
853  c4 = System.IO.Path.GetRandomFileName().Truncate( 4 ),
854  c8 = System.IO.Path.GetRandomFileName().Truncate( 8 ),
855  c16 = System.IO.Path.GetRandomFileName().Truncate( 16 ),
856  c32 = System.IO.Path.GetRandomFileName().Truncate( 32 ),
857  c64 = System.IO.Path.GetRandomFileName().Truncate( 64 ),
858  c128 = System.IO.Path.GetRandomFileName().Truncate( 128 ),
859  c256 = System.IO.Path.GetRandomFileName().Truncate( 256 ),
860  date = Test.generate_date( rng ),
861  decimal_ = Test.generate_decimal( rng ),
862  ipv4 = Test.generate_IPv4( rng ),
863  time = Test.generate_time( rng ),
864  timestamp = (long) rng.Next()
865  };
866 
867  //Console.WriteLine( record.ToString() ); // debug~~~~~~~~
868  records.Add( record );
869  } // end for loop
870 
871  Console.WriteLine( $"Generated {num_records} records." );
872 
873  // Insert the records into the ingestor
874  Console.WriteLine( $"Inserting {num_records} records..." );
875  ingestor.insert( records );
876 
877  // Flush the ingestor (which actually inserts the records)
878  Console.WriteLine( "\nFlushing any remaining records." );
879  ingestor.flush();
880  } // end test_all_types_3pk_0shard()
881 
882 
883 
889  public static string generate_date( Random rng )
890  {
891  int past_range = -90 * 365; //980 years
892  int future_range = 20 * 365; // 20 years
893  int offset = rng.Next( past_range, future_range );
894  DateTime random_date = DateTime.Today.AddDays( offset );
895  return String.Format( "{0:yyyy-MM-dd}", random_date );
896  } // end generate_date
897 
898 
899 
900  private static readonly string numbers = "0123456789";
901 
908  public static string generate_decimal( Random rng )
909  {
910  string sign = (rng.Next( 2 ) == 0) ? "-" : "";
911  string precision = new string( Enumerable.Repeat( '1', rng.Next( 20 ) ).Select( i => Test.numbers[ rng.Next( numbers.Length ) ] ).ToArray() );
912  string scale = rng.Next( 0, 10000 ).ToString();
913  return ( sign + precision + "." + scale );
914  } // end generate_decimal
915 
916 
917 
923  public static string generate_IPv4( Random rng )
924  {
925  string n1 = rng.Next( 256 ).ToString();
926  string n2 = rng.Next( 256 ).ToString();
927  string n3 = rng.Next( 256 ).ToString();
928  string n4 = rng.Next( 256 ).ToString();
929  string dot = ".";
930  return ( n1 + dot + n2 + dot + n3 + dot + n4 );
931  } // end generate_ipv4
932 
933 
934 
940  public static string generate_time( Random rng )
941  {
942  int max_miliseconds = 86400000;
943  DateTime time = DateTime.Today;
944  //Console.WriteLine( time.ToLongTimeString() ); // debug~~~~~~~~
945  time = time.Add( TimeSpan.FromMilliseconds( rng.Next( max_miliseconds ) ) );
946  //Console.WriteLine( String.Format( "{0:HH}:{0:mm}:{0:ss}.{0:FFF}", time ) ); // debug~~~~~~
947  string time_str;
948  if ( rng.Next( 6 ) == 0 ) // one in five will get the milliseconds
949  time_str = String.Format( "{0:HH}:{0:mm}:{0:ss}.{0:FFF}", time );
950  else
951  time_str = String.Format( "{0:HH}:{0:mm}:{0:ss}", time );
952  return time_str;
953  } // end generate_time
954 
955 
956  } // end class Test
957 } // end namespace Test
958 
959 
960 
961 
962 
963 
964 
965 
966 
967 
968 
969 
970 
971 
972 
973 
Runs some tests on multihead ingestion.
Definition: Test.cs:36
static string generate_decimal(Random rng)
Generate a random decimal value (up to 19 digits of precision and up to four digits of scale)...
Definition: Test.cs:908
static string generate_time(Random rng)
Generate a random time of the format HH:MM:SS[.mmm].
Definition: Test.cs:940
static string generate_date(Random rng)
Generate a random date.
Definition: Test.cs:889
static string generate_IPv4(Random rng)
Generate a random IPv4 address.
Definition: Test.cs:923
Extension to string that has a truncate function.
Definition: Test.cs:19
Connection Options
Definition: Kinetica.cs:50
static string Truncate(this string value, int maxLength)
Definition: Test.cs:21
API to talk to Kinetica Database
Definition: Kinetica.cs:40