Kinetica   C#   API  Version 7.2.3.1
TableUtilities.cs
Go to the documentation of this file.
1 
13 using System;
14 using System.Collections.Generic;
15 using System.Threading.Tasks;
16 using kinetica;
17 
18 namespace Example
19 {
20  #region Clear Table
21 
30  public static class ClearTableExample
31  {
32  public static async Task RunAsync(string tableName)
33  {
34  Console.WriteLine("=== Clear Table Example ===\n");
35 
36  if (string.IsNullOrEmpty(tableName))
37  {
38  Console.WriteLine("Usage: dotnet run --project Example -- --clear-table <table_name>");
39  Console.WriteLine("Example: dotnet run --project Example -- --clear-table test_schema.my_table");
40  return;
41  }
42 
43  // Get connection settings
44  var url = Environment.GetEnvironmentVariable("KINETICA_URL") ?? "http://localhost:9191";
45  var user = Environment.GetEnvironmentVariable("KINETICA_USER") ?? "admin";
46  var password = Environment.GetEnvironmentVariable("KINETICA_PASSWORD") ?? "secret";
47 
48  var kinetica = new Kinetica(url, new Kinetica.Options
49  {
50  Username = user,
51  Password = password
52  });
53 
54  Console.WriteLine($"! WARNING: This will delete ALL data from table '{tableName}'");
55  Console.WriteLine("This operation is IRREVERSIBLE!\n");
56 
57  Console.WriteLine("Waiting 3 seconds... (Press Ctrl+C to cancel)");
58  for (int i = 3; i >= 1; i--)
59  {
60  Console.WriteLine($"{i}...");
61  await Task.Delay(1000);
62  }
63 
64  Console.WriteLine($"\nClearing table '{tableName}'...");
65 
66  try
67  {
68  var request = new ClearTableRequest(tableName);
69  var response = kinetica.clearTable(request);
70 
71  Console.WriteLine("+ Table cleared successfully!");
72  Console.WriteLine($" Table Name: {response.table_name}");
73 
74  if (response.info.TryGetValue("count", out var count))
75  {
76  Console.WriteLine($" Records Deleted: {count}");
77  }
78  }
79  catch (KineticaException ex)
80  {
81  Console.WriteLine($"x Error clearing table: {ex.Message}");
82  return;
83  }
84 
85  Console.WriteLine($"\nAll data has been removed from '{tableName}'");
86  }
87  }
88 
89  #endregion
90 
91  #region Cleanup All
92 
101  public static class CleanupAllExample
102  {
103  public static async Task RunAsync(string schemaPattern = "test_schema")
104  {
105  Console.WriteLine("=== Cleanup All Test Tables Example ===\n");
106 
107  // Get connection settings
108  var url = Environment.GetEnvironmentVariable("KINETICA_URL") ?? "http://localhost:9191";
109  var user = Environment.GetEnvironmentVariable("KINETICA_USER") ?? "admin";
110  var password = Environment.GetEnvironmentVariable("KINETICA_PASSWORD") ?? "secret";
111 
112  var kinetica = new Kinetica(url, new Kinetica.Options
113  {
114  Username = user,
115  Password = password
116  });
117 
118  Console.WriteLine($"! WARNING: This will DROP all tables in schema matching '{schemaPattern}'");
119  Console.WriteLine("This operation is IRREVERSIBLE!\n");
120 
121  // First, list all tables in the schema
122  Console.WriteLine("Discovering tables...");
123 
124  IList<string> tablesToDrop = new List<string>();
125 
126  try
127  {
128  var showTableResponse = kinetica.showTable("", new Dictionary<string, string>
129  {
130  { "show_children", "true" }
131  });
132 
133  foreach (var tbl in showTableResponse.table_names)
134  {
135  if (tbl.StartsWith(schemaPattern))
136  {
137  tablesToDrop.Add(tbl);
138  }
139  }
140  }
141  catch (KineticaException ex)
142  {
143  Console.WriteLine($"Error discovering tables: {ex.Message}");
144  return;
145  }
146 
147  if (tablesToDrop.Count == 0)
148  {
149  Console.WriteLine($"No tables found matching pattern '{schemaPattern}'");
150  return;
151  }
152 
153  Console.WriteLine($"\nFound {tablesToDrop.Count} table(s) to drop:");
154  foreach (var tbl in tablesToDrop)
155  {
156  Console.WriteLine($" - {tbl}");
157  }
158 
159  Console.WriteLine("\nWaiting 5 seconds... (Press Ctrl+C to cancel)");
160  for (int i = 5; i >= 1; i--)
161  {
162  Console.WriteLine($"{i}...");
163  await Task.Delay(1000);
164  }
165 
166  Console.WriteLine("\nDropping tables...");
167 
168  var droppedCount = 0;
169  var failedCount = 0;
170 
171  foreach (var tbl in tablesToDrop)
172  {
173  try
174  {
175  kinetica.executeSql($"DROP TABLE IF EXISTS {tbl}");
176  Console.WriteLine($" + Dropped: {tbl}");
177  droppedCount++;
178  }
179  catch (Exception ex)
180  {
181  Console.WriteLine($" x Failed to drop {tbl}: {ex.Message}");
182  failedCount++;
183  }
184  }
185 
186  // Also try to drop the schema if it's empty
187  try
188  {
189  if (schemaPattern.Contains('.'))
190  {
191  schemaPattern = schemaPattern.Split('.')[0];
192  }
193 
194  kinetica.executeSql($"DROP SCHEMA IF EXISTS {schemaPattern}");
195  Console.WriteLine($"\n + Dropped schema: {schemaPattern}");
196  }
197  catch
198  {
199  // Schema might not be empty or might not exist, that's ok
200  }
201 
202  Console.WriteLine($"\nCleanup complete!");
203  Console.WriteLine($" Dropped: {droppedCount} table(s)");
204  if (failedCount > 0)
205  {
206  Console.WriteLine($" Failed: {failedCount} table(s)");
207  }
208  }
209  }
210 
211  #endregion
212 
213  #region Show Tables
214 
220  public static class ShowTablesExample
221  {
222  public static void Run(string schemaFilter = "")
223  {
224  Console.WriteLine("=== Show Tables Example ===\n");
225 
226  // Get connection settings
227  var url = Environment.GetEnvironmentVariable("KINETICA_URL") ?? "http://localhost:9191";
228  var user = Environment.GetEnvironmentVariable("KINETICA_USER") ?? "admin";
229  var password = Environment.GetEnvironmentVariable("KINETICA_PASSWORD") ?? "secret";
230 
231  var kinetica = new Kinetica(url, new Kinetica.Options
232  {
233  Username = user,
234  Password = password
235  });
236 
237  Console.WriteLine($"Connected to: {url}\n");
238 
239  try
240  {
241  var showTableResponse = kinetica.showTable("", new Dictionary<string, string>
242  {
243  { "show_children", "true" },
244  { "get_sizes", "true" }
245  });
246 
247  Console.WriteLine($"{"Table Name",-50} {"Records",-15} {"Type ID",-40}");
248  Console.WriteLine(new string('-', 105));
249 
250  var tableCount = 0;
251  for (int i = 0; i < showTableResponse.table_names.Count; i++)
252  {
253  var tableName = showTableResponse.table_names[i];
254 
255  // Apply schema filter if provided
256  if (!string.IsNullOrEmpty(schemaFilter) && !tableName.StartsWith(schemaFilter))
257  continue;
258 
259  var typeId = i < showTableResponse.type_ids.Count
260  ? showTableResponse.type_ids[i]
261  : "N/A";
262 
263  var recordCount = "N/A";
264  if (i < showTableResponse.sizes.Count)
265  {
266  recordCount = showTableResponse.sizes[i].ToString("N0");
267  }
268 
269  Console.WriteLine($"{tableName,-50} {recordCount,-15} {typeId,-40}");
270  tableCount++;
271  }
272 
273  Console.WriteLine(new string('-', 105));
274  Console.WriteLine($"Total: {tableCount} table(s)");
275  }
276  catch (KineticaException ex)
277  {
278  Console.WriteLine($"Error: {ex.Message}");
279  }
280  }
281  }
282 
283  #endregion
284 
285  #region Truncate Table
286 
292  public static class TruncateTableExample
293  {
294  public static async Task RunAsync(string tableName)
295  {
296  Console.WriteLine("=== Truncate Table Example ===\n");
297 
298  if (string.IsNullOrEmpty(tableName))
299  {
300  Console.WriteLine("Usage: dotnet run --project Example -- --truncate <table_name>");
301  return;
302  }
303 
304  // Get connection settings
305  var url = Environment.GetEnvironmentVariable("KINETICA_URL") ?? "http://localhost:9191";
306  var user = Environment.GetEnvironmentVariable("KINETICA_USER") ?? "admin";
307  var password = Environment.GetEnvironmentVariable("KINETICA_PASSWORD") ?? "secret";
308 
309  var kinetica = new Kinetica(url, new Kinetica.Options
310  {
311  Username = user,
312  Password = password
313  });
314 
315  Console.WriteLine($"! WARNING: This will TRUNCATE table '{tableName}'");
316  Console.WriteLine("This operation is IRREVERSIBLE!\n");
317 
318  Console.WriteLine("Waiting 3 seconds... (Press Ctrl+C to cancel)");
319  for (int i = 3; i >= 1; i--)
320  {
321  Console.WriteLine($"{i}...");
322  await Task.Delay(1000);
323  }
324 
325  Console.WriteLine($"\nTruncating table '{tableName}'...");
326 
327  try
328  {
329  kinetica.executeSql($"TRUNCATE TABLE {tableName}");
330  Console.WriteLine("+ Table truncated successfully!");
331  }
332  catch (KineticaException ex)
333  {
334  Console.WriteLine($"x Error truncating table: {ex.Message}");
335  return;
336  }
337 
338  Console.WriteLine($"\nAll data has been removed from '{tableName}'");
339  }
340  }
341 
342  #endregion
343 }
Example: Clear All Data from a Table
Example: Cleanup All Test Tables
A set of parameters for Kinetica.clearTable.
Definition: ClearTable.cs:19
Example: List All Tables
static async Task RunAsync(string schemaPattern="test_schema")
kinetica.Records Records
Definition: BulkInserter.cs:10
static async Task RunAsync(string tableName)
Immutable collection of metadata about a Kinetica type.
Definition: Type.cs:36
static void Run(string schemaFilter="")
Example: Truncate Table (faster than ClearTable for large tables)
static async Task RunAsync(string tableName)