2 using System.Collections.Generic;
4 using System.Threading.Tasks;
28 public int id {
get;
set; }
71 public string char1_col {
get;
set; } =
string.Empty;
74 public string char4_col {
get;
set; } =
string.Empty;
77 public string char8_col {
get;
set; } =
string.Empty;
103 public string date_col {
get;
set; } =
string.Empty;
106 public string time_col {
get;
set; } =
string.Empty;
115 public string ipv4_col {
get;
set; } =
string.Empty;
118 public string uuid_col {
get;
set; } =
string.Empty;
124 public string json_col {
get;
set; } =
string.Empty;
127 public string wkt_col {
get;
set; } =
string.Empty;
135 return $
"AllTypesRecord {{ id={id}, bool={bool_col}, int={int_col}, long={long_col}, " +
136 $
"float={float_col:F2}, double={double_col:F4}, string='{string_col}', " +
137 $
"date='{date_col}', time='{time_col}', datetime='{datetime_col}', " +
138 $
"decimal='{decimal_col}', ipv4='{ipv4_col}', uuid='{uuid_col}' }}";
153 public static async Task
RunAsync(
string serverUrl,
string username,
string password)
156 Console.WriteLine(
"=========================================================");
157 Console.WriteLine(
"= All Types Example - BulkInserter & RecordRetriever =");
158 Console.WriteLine(
"=========================================================");
167 var kdb =
new Kinetica(serverUrl, options);
169 const string tableName =
"csharp_all_types_example";
174 Console.WriteLine(
"Step 1: Creating table with all Kinetica types...");
175 await CreateTableAsync(kdb, tableName);
176 Console.WriteLine($
" Table '{tableName}' created successfully.\n");
179 Console.WriteLine(
"Step 2: Generating test records...");
180 const int recordCount = 1000;
181 var records = GenerateRecords(recordCount);
182 Console.WriteLine($
" Generated {recordCount} records.\n");
185 Console.WriteLine(
"Step 3: Inserting records using BulkInserter...");
186 var insertStats = await InsertRecordsAsync(kdb, tableName, records);
187 Console.WriteLine($
" Inserted: {insertStats.inserted}, Updated: {insertStats.updated}");
188 Console.WriteLine($
" Throughput: {insertStats.throughput:N0} records/sec\n");
191 Console.WriteLine(
"Step 4: Retrieving records using RecordRetriever...");
192 var retrievedRecords = await RetrieveRecordsAsync(kdb, tableName);
193 Console.WriteLine($
" Retrieved {retrievedRecords.Count} records.\n");
196 Console.WriteLine(
"Step 5: Sample records (first 5):");
197 Console.WriteLine(
new string(
'-', 100));
198 foreach (var record
in retrievedRecords.Take(5))
200 Console.WriteLine($
" {record}");
202 Console.WriteLine(
new string(
'-', 100));
206 Console.WriteLine(
"Step 6: Filtered retrieval (id < 10 AND bool_col = 1)...");
207 var filteredRecords = await RetrieveFilteredRecordsAsync(kdb, tableName,
"id < 10 AND bool_col = 1");
208 Console.WriteLine($
" Retrieved {filteredRecords.Count} filtered records.\n");
211 Console.WriteLine(
"Step 7: Sorted retrieval (ORDER BY double_col DESC, LIMIT 5)...");
212 var sortedRecords = await RetrieveSortedRecordsAsync(kdb, tableName,
"double_col", descending:
true, limit: 5);
213 Console.WriteLine($
" Top 5 by double_col DESC:");
214 foreach (var record
in sortedRecords)
216 Console.WriteLine($
" id={record.id}, double_col={record.double_col:F4}");
220 Console.WriteLine(
"All Types Example completed successfully!");
224 Console.WriteLine($
"Error: {ex.Message}");
225 Console.WriteLine(ex.StackTrace);
230 Console.WriteLine($
"\nCleaning up: Dropping table '{tableName}'...");
233 kdb.clearTable(tableName,
"",
new Dictionary<string, string>
237 Console.WriteLine(
" Table dropped.");
241 Console.WriteLine($
" Warning: Could not drop table: {ex.Message}");
249 private static Task CreateTableAsync(
Kinetica kdb,
string tableName)
252 var columnProperties =
new Dictionary<string, IList<string>>
297 kdb.
clearTable(tableName,
"",
new Dictionary<string, string>
306 string typeId = ktype.
create(kdb);
311 return Task.CompletedTask;
317 private static List<AllTypesRecord> GenerateRecords(
int count)
319 var records =
new List<AllTypesRecord>(count);
320 var random =
new Random(42);
321 var baseTime =
new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Utc);
323 for (
int i = 0; i < count; i++)
325 var record =
new AllTypesRecord
331 bool_col = random.Next(0, 2),
332 int8_col = random.Next(-128, 128),
333 int16_col = random.Next(-32768, 32768),
334 int_col = random.Next(),
335 nullable_int_col = i % 5 == 0 ? null : random.Next(0, 1000),
338 long_col = random.NextInt64(),
339 timestamp_col = ((DateTimeOffset)baseTime.AddMinutes(i)).ToUnixTimeMilliseconds(),
340 nullable_long_col = i % 7 == 0 ? null : random.NextInt64(0, 1_000_000_000),
343 float_col = (float)(random.NextDouble() * 1000),
344 nullable_float_col = i % 3 == 0 ?
null : (
float)(random.NextDouble() * 100),
347 double_col = random.NextDouble() * 10000,
348 nullable_double_col = i % 4 == 0 ? null : random.NextDouble() * 500,
351 char1_col = ((char)(
'A' + (i % 26))).ToString(),
352 char4_col = $
"C{i % 1000:D3}",
353 char8_col = $
"CHAR{i % 10000:D4}",
354 char16_col = $
"CHAR16_{i:D8}",
355 char32_col = $
"This is a char32 value #{i}",
356 char64_col = $
"This is a longer char64 column value for record #{i}",
357 char128_col = $
"CHAR128 column with more text capacity. Record #{i}. Random: {random.Next()}",
358 char256_col = $
"CHAR256 provides even more space for text data. Record #{i}. UUID: {Guid.NewGuid()}",
361 string_col = $
"Variable length string for record {i} with random data: {Guid.NewGuid()}",
362 nullable_string_col = i % 6 == 0 ? null : $
"Nullable string #{i}",
365 date_col = baseTime.AddDays(i).ToString(
"yyyy-MM-dd"),
366 time_col = baseTime.AddSeconds(i * 37).ToString(
"HH:mm:ss.fff"),
367 datetime_col = baseTime.AddMinutes(i).ToString(
"yyyy-MM-dd HH:mm:ss.fff"),
370 decimal_col = (random.NextDouble() * 100000).ToString(
"F4"),
371 ipv4_col = $
"{random.Next(1, 256)}.{random.Next(0, 256)}.{random.Next(0, 256)}.{random.Next(1, 256)}",
372 uuid_col = Guid.NewGuid().ToString(),
373 ulong_col = ((ulong)random.NextInt64(0,
long.MaxValue)).ToString(),
374 json_col = $
"{{\"id\": {i}, \"name\": \"record_{i}\", \"active\": {(i % 2 == 0).ToString().ToLower()}}}",
375 wkt_col = $
"POINT({-180 + random.NextDouble() * 360:F6} {-90 + random.NextDouble() * 180:F6})" 387 private static async Task<(
long inserted,
long updated,
double throughput)> InsertRecordsAsync(
388 Kinetica kdb,
string tableName, List<AllTypesRecord> records)
395 MaxInFlightBatches = 10,
399 var sw = System.Diagnostics.Stopwatch.StartNew();
404 inserter.InsertBatch(records);
407 await inserter.CloseAsync();
411 var throughput = records.Count / (sw.Elapsed.TotalMilliseconds / 1000.0);
414 var errors = inserter.DrainErrors();
415 if (errors.Count > 0)
417 Console.WriteLine($
" Warning: {errors.Count} errors occurred during insertion:");
418 foreach (var error
in errors.Take(5))
420 Console.WriteLine($
" - {error.Message}");
424 return (inserter.CountInserted, inserter.CountUpdated, throughput);
430 private static Task<IList<AllTypesRecord>> RetrieveRecordsAsync(
Kinetica kdb,
string tableName)
432 var response = kdb.getRecords<AllTypesRecord>(tableName, 0,
Kinetica.END_OF_SET);
433 return Task.FromResult(response.data);
439 private static Task<IList<AllTypesRecord>> RetrieveFilteredRecordsAsync(
440 Kinetica kdb,
string tableName,
string expression)
442 var options =
new Dictionary<string, string>
446 var response = kdb.getRecords<AllTypesRecord>(tableName, 0,
Kinetica.END_OF_SET, options);
447 return Task.FromResult(response.data);
453 private static Task<IList<AllTypesRecord>> RetrieveSortedRecordsAsync(
454 Kinetica kdb,
string tableName,
string sortColumn,
bool descending =
false,
int limit = 10)
456 var options =
new Dictionary<string, string>
461 var response = kdb.getRecords<AllTypesRecord>(tableName, 0, limit, options);
462 return Task.FromResult(response.data);
469 private static Task<IList<AllTypesRecord>> RetrieveByShardKeyAsync(
470 Kinetica kdb,
string tableName, AllTypesRecord keyRecord,
string? additionalExpression =
null)
474 var response = retriever.getRecordsByKey(keyRecord, additionalExpression!);
475 return Task.FromResult(response.data);
int int8_col
8-bit integer stored as INT with INT8 property (-128 to 127)
long timestamp_col
Timestamp in milliseconds since Unix epoch (with TIMESTAMP property)
string char128_col
128-character fixed string (CHAR128)
const string CHAR1
This property provides optimized memory, disk and query performance for string columns.
static ShardKeyValue Int(int value)
Creates a 32-bit integer shard key value.
string ipv4_col
IPv4 address in format 'A.B.C.D' (IPV4 property)
string char8_col
8-character fixed string (CHAR8)
string char16_col
16-character fixed string (CHAR16)
const string DATETIME
Valid only for 'string' columns.
const string INT16
This property provides optimized memory and query performance for int columns.
const string SORT_ORDER
String indicating how the returned values should be sorted - ascending or descending.
string time_col
Time in format 'HH:MM:SS.mmm' (TIME property)
const string PRIMARY_KEY
This property indicates that this column will be part of (or the entire) primary key.
const string CHAR128
This property provides optimized memory, disk and query performance for string columns.
const string BOOLEAN
This property provides optimized memory and query performance for int columns.
string char4_col
4-character fixed string (CHAR4)
CreateTableResponse createTable(CreateTableRequest request_)
Creates a new table with the given type (definition of columns).
Comprehensive example demonstrating all Kinetica supported types with:
string ulong_col
Unsigned long as string (ULONG property) - 0 to 18,446,744,073,709,551,615
A set of parameters for Kinetica.clearTable.
string uuid_col
UUID stored as string (UUID property)
const string SORT_BY
Optional column that the data should be sorted by.
const string EXPRESSION
Optional filter expression to apply to the table.
static async Task RunAsync(string serverUrl, string username, string password)
Runs the comprehensive all-types example.
float? nullable_float_col
Nullable 32-bit floating point
string date_col
Date in format 'YYYY-MM-DD' (DATE property)
Column properties used for Kinetica types.
const string TIMESTAMP
Valid only for 'long' columns.
const string CHAR16
This property provides optimized memory, disk and query performance for string columns.
const string JSON
Valid only for 'string' columns.
High-performance bulk inserter for Kinetica with support for multi-head ingest,
float float_col
32-bit floating point
long? nullable_long_col
Nullable 64-bit integer
static KineticaType fromTable(Kinetica kinetica, string tableName)
Create a KineticaType object based on an existing table in the database.
const string WKT
Valid only for 'string' and 'bytes' columns.
long long_col
Standard 64-bit integer
double double_col
64-bit floating point
string decimal_col
Decimal number as string with DECIMAL property (NUMERIC 19,4)
const string CHAR64
This property provides optimized memory, disk and query performance for string columns.
const string CHAR4
This property provides optimized memory, disk and query performance for string columns.
const string DATE
Valid only for 'string' columns.
string char32_col
32-character fixed string (CHAR32)
const string CHAR8
This property provides optimized memory, disk and query performance for string columns.
const string DECIMAL
Valid only for 'string' columns.
string wkt_col
Well-Known Text geometry (WKT property)
string char1_col
1-character fixed string (CHAR1)
string char64_col
64-character fixed string (CHAR64)
override string ToString()
Collection of shard key column names and values.
string? nullable_string_col
Nullable variable-length string
string char256_col
256-character fixed string (CHAR256)
A set of string constants for the parameter options.
const string CHAR32
This property provides optimized memory, disk and query performance for string columns.
A set of parameters for Kinetica.getRecords.
string json_col
JSON data stored as string (JSON property)
const string IPV4
This property provides optimized memory, disk and query performance for string columns representing I...
ClearTableResponse clearTable(ClearTableRequest request_)
Clears (drops) one or all tables in the database cluster.
ShardKeyValues GetShardKeyValues()
Returns shard key column names and their typed values.
Manages the insertion into GPUdb of large numbers of records in bulk, with automatic batch management...
const string CHAR256
This property provides optimized memory, disk and query performance for string columns.
int? nullable_int_col
Nullable 32-bit integer
A set of string constants for the parameter options.
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.
string datetime_col
DateTime in format 'YYYY-MM-DD HH:MM:SS.mmm' (DATETIME property)
int int16_col
16-bit integer stored as INT with INT16 property (-32768 to 32767)
string string_col
Variable-length string (unlimited)
int bool_col
Boolean stored as INT with BOOLEAN property (0 or 1)
const string UUID
Valid only for 'string' columns.
const string INT8
This property provides optimized memory and query performance for int columns.
string create(Kinetica kinetica)
Given a handle to the server, creates a type in the database based on this data type.
A typed value for shard key computation.
int int_col
Standard 32-bit integer
double? nullable_double_col
Nullable 64-bit floating point
Failover to clusters in a random order (default)
const string ULONG
Valid only for 'string' columns.
const string NO_ERROR_IF_NOT_EXISTS
If TRUE and if the table specified in table_name does not exist no error is returned.
DateTime in YYYY-MM-DD HH:MM:SS.mmm format
const string TIME
Valid only for 'string' columns.
const string NULLABLE
This property indicates that this column is nullable.
Record class demonstrating all Kinetica supported data types.
Configuration options for the BulkInserter<T>.