Kinetica   C#   API  Version 7.2.3.1
SchemaBuilderDemo.cs
Go to the documentation of this file.
1 
14 using System;
15 using System.Collections.Generic;
16 using kinetica;
17 
18 namespace Example
19 {
20  public static class SchemaBuilderDemo
21  {
22  public static void Run()
23  {
24  Console.WriteLine("=== Schema Builder Demo ===\n");
25 
26  // Example 1: Using KineticaType with column builders
27  Console.WriteLine("1. KineticaType with Column Builders:");
28  Console.WriteLine(" ---------------------------------");
29 
30  var simpleType = new KineticaType(
31  "SimpleRecord",
32  new List<KineticaType.Column>
33  {
34  new("id", KineticaType.Column.ColumnType.INT, new List<string> { ColumnProperty.PRIMARY_KEY }),
35  new("name", KineticaType.Column.ColumnType.STRING, null),
36  new("value", KineticaType.Column.ColumnType.DOUBLE, null),
37  }
38  );
39 
40  Console.WriteLine($" Schema: {simpleType.getSchemaString()}");
41  Console.WriteLine();
42 
43  // Example 2: Complex record with nullable fields and various types
44  Console.WriteLine("2. Complex Record with Nullable Fields:");
45  Console.WriteLine(" ------------------------------------");
46 
47  var sensorType = new KineticaType(
48  "SensorReading",
49  new List<KineticaType.Column>
50  {
51  new("sensor_id", KineticaType.Column.ColumnType.STRING,
52  new List<string> { ColumnProperty.PRIMARY_KEY, ColumnProperty.CHAR64 }),
53  new("timestamp_ms", KineticaType.Column.ColumnType.LONG, new List<string> { ColumnProperty.TIMESTAMP }),
54  new("temperature", KineticaType.Column.ColumnType.DOUBLE, new List<string> { ColumnProperty.NULLABLE }),
55  new("humidity", KineticaType.Column.ColumnType.DOUBLE, new List<string> { ColumnProperty.NULLABLE }),
56  new("raw_data", KineticaType.Column.ColumnType.BYTES, null),
57  new("is_valid", KineticaType.Column.ColumnType.INT, new List<string> { ColumnProperty.INT8 }),
58  }
59  );
60 
61  Console.WriteLine($" Schema: {sensorType.getSchemaString()}");
62  Console.WriteLine();
63 
64  // Example 3: Record with shard key
65  Console.WriteLine("3. Record with Shard Key:");
66  Console.WriteLine(" -----------------------");
67 
68  var shardedType = new KineticaType(
69  "ShardedRecord",
70  new List<KineticaType.Column>
71  {
72  new("tenant_id", KineticaType.Column.ColumnType.INT, new List<string> { ColumnProperty.SHARD_KEY }),
73  new("record_id", KineticaType.Column.ColumnType.LONG, new List<string> { ColumnProperty.PRIMARY_KEY }),
74  new("data", KineticaType.Column.ColumnType.STRING, null),
75  }
76  );
77 
78  Console.WriteLine($" Schema: {shardedType.getSchemaString()}");
79  Console.WriteLine();
80 
81  // Example 4: Using SQL CREATE TABLE (recommended for production)
82  Console.WriteLine("4. SQL CREATE TABLE Approach (Recommended):");
83  Console.WriteLine(" -----------------------------------------");
84  Console.WriteLine(" Instead of manually building schemas, use SQL:");
85  Console.WriteLine();
86  Console.WriteLine(@" CREATE TABLE sensors.readings (
87  sensor_id VARCHAR(64) NOT NULL,
88  location VARCHAR(128),
89  timestamp_ms TIMESTAMP NOT NULL,
90  temperature DOUBLE,
91  humidity DOUBLE,
92  raw_data BLOB,
93  is_valid TINYINT DEFAULT 1,
94  PRIMARY KEY (sensor_id, timestamp_ms),
95  SHARD KEY (sensor_id)
96  )");
97  Console.WriteLine();
98 
99  // Example 5: All column types reference
100  Console.WriteLine("5. Column Type Reference:");
101  Console.WriteLine(" ----------------------");
102  Console.WriteLine($" {"C# Type",-20} {"Kinetica Type",-20} {"Column Property",-25}");
103  Console.WriteLine($" {new string('-', 65)}");
104  Console.WriteLine($" {"int",-20} {"INT",-20} {"INT8, INT16 for smaller",-25}");
105  Console.WriteLine($" {"long",-20} {"LONG",-20} {"TIMESTAMP for time",-25}");
106  Console.WriteLine($" {"float",-20} {"FLOAT",-20} {"",-25}");
107  Console.WriteLine($" {"double",-20} {"DOUBLE",-20} {"",-25}");
108  Console.WriteLine($" {"string",-20} {"STRING",-20} {"CHAR1-256, IPV4, etc.",-25}");
109  Console.WriteLine($" {"byte[]",-20} {"BYTES",-20} {"WKT, WKB for geospatial",-25}");
110  Console.WriteLine();
111 
112  // Example 6: Column properties reference
113  Console.WriteLine("6. Column Properties Reference:");
114  Console.WriteLine(" ----------------------------");
115  Console.WriteLine($" {"Property",-25} {"Description",-50}");
116  Console.WriteLine($" {new string('-', 75)}");
117  Console.WriteLine($" {"PRIMARY_KEY",-25} {"Column is part of the primary key",-50}");
118  Console.WriteLine($" {"SHARD_KEY",-25} {"Column is used for data sharding",-50}");
119  Console.WriteLine($" {"NULLABLE",-25} {"Column can contain null values",-50}");
120  Console.WriteLine($" {"TIMESTAMP",-25} {"Long column stores timestamp (ms)",-50}");
121  Console.WriteLine($" {"CHAR1-CHAR256",-25} {"String with fixed max length",-50}");
122  Console.WriteLine($" {"INT8, INT16",-25} {"Smaller integer storage",-50}");
123  Console.WriteLine($" {"IPV4",-25} {"String stores IPv4 address",-50}");
124  Console.WriteLine($" {"DECIMAL",-25} {"String stores decimal number",-50}");
125  Console.WriteLine($" {"DATE",-25} {"String stores date (YYYY-MM-DD)",-50}");
126  Console.WriteLine($" {"TIME",-25} {"String stores time (HH:MM:SS)",-50}");
127  Console.WriteLine($" {"DATETIME",-25} {"String stores datetime",-50}");
128  Console.WriteLine($" {"WKT",-25} {"String stores geospatial WKT",-50}");
129  Console.WriteLine();
130 
131  // Example 7: Best practices
132  Console.WriteLine("7. Best Practices:");
133  Console.WriteLine(" ----------------");
134  Console.WriteLine(" * Use SQL CREATE TABLE for production - it's clearer and more maintainable");
135  Console.WriteLine(" * Always define a PRIMARY KEY for uniqueness constraints");
136  Console.WriteLine(" * Use SHARD KEY on high-cardinality columns for even distribution");
137  Console.WriteLine(" * Use NULLABLE only when needed - non-nullable is more efficient");
138  Console.WriteLine(" * Use CHAR(N) instead of STRING when max length is known");
139  Console.WriteLine(" * Use TIMESTAMP for time columns to enable time-based queries");
140  Console.WriteLine();
141 
142  Console.WriteLine("=== Demo Complete ===");
143  }
144  }
145 }
const string PRIMARY_KEY
This property indicates that this column will be part of (or the entire) primary key.
Column properties used for Kinetica types.
const string CHAR64
This property provides optimized memory, disk and query performance for string columns.
Immutable metadata about a column in a Kinetica type.
Definition: Column.cs:11
const string SHARD_KEY
This property indicates that this column will be part of (or the entire) shard key.
const string INT8
This property provides optimized memory and query performance for int columns.
Immutable collection of metadata about a Kinetica type.
Definition: Type.cs:36
const string NULLABLE
This property indicates that this column is nullable.