-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
138 lines (121 loc) · 4.95 KB
/
Program.cs
File metadata and controls
138 lines (121 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Apache.Arrow;
using Apache.Arrow.Arrays;
using DeltaLakeSharp.Client;
using DeltaLakeSharp.Client.Models;
namespace DeltaLakeSharp.Client.Examples
{
internal static class Program
{
public static async Task Main(string[] args)
{
string tablePath = args.Length > 0
? args[0]
: Path.Combine(Path.GetTempPath(), $"delta_client_example_{Guid.NewGuid():N}");
bool deleteWhenDone = args.Length == 0;
try
{
using var client = new DeltaTableServiceClient(ServiceMode.V3_Rust);
await CreatePeopleTableAsync(client, tablePath);
await StreamArrowBatchesAsync(client, tablePath);
await ReadWithDataReaderAsync(client, tablePath);
await QueryWithSqlAsync(client, tablePath);
}
finally
{
if (deleteWhenDone && Directory.Exists(tablePath))
{
Directory.Delete(tablePath, recursive: true);
}
}
}
private static async Task CreatePeopleTableAsync(DeltaTableServiceClient client, string tablePath)
{
if (Directory.Exists(tablePath))
{
Directory.Delete(tablePath, recursive: true);
}
var tableSchema = new TableSchema(new[]
{
new ColumnDefinition("id", "int32"),
new ColumnDefinition("name", "string"),
new ColumnDefinition("active", "boolean"),
});
ExecuteResult createResult = await client.CreateTableAsync(tablePath, tableSchema);
EnsureSuccess(createResult, "create table");
RecordBatch batch = ArrowConverter.FromRows(new[]
{
new object[] { 1, "Ada", true },
new object[] { 2, "Grace", true },
new object[] { 3, "Katherine", false },
}, tableSchema);
await client.InsertAsync(
tablePath,
batch.Schema,
ToAsyncEnumerable(batch),
SaveMode.Append);
}
private static async Task StreamArrowBatchesAsync(DeltaTableServiceClient client, string tablePath)
{
await foreach (RecordBatch batch in client.ReadTableAsync(tablePath, batchSize: 2))
{
IReadOnlyList<string> names = ReadStringColumn(batch, columnIndex: 1);
Console.WriteLine($"Read Arrow batch with {batch.Length} rows: {string.Join(", ", names)}");
}
}
private static async Task ReadWithDataReaderAsync(DeltaTableServiceClient client, string tablePath)
{
using DbDataReader reader = await client.ReadTableAsDataReaderAsync(tablePath);
while (reader.Read())
{
Console.WriteLine($"DbDataReader row: {reader.GetInt32(0)} {reader.GetString(1)} active={reader.GetBoolean(2)}");
}
}
private static async Task QueryWithSqlAsync(DeltaTableServiceClient client, string tablePath)
{
await foreach (RecordBatch batch in client.ExecuteQueryAsync(
"SELECT id, name FROM people WHERE active = true ORDER BY id",
tablePath,
"people"))
{
IReadOnlyList<string> names = ReadStringColumn(batch, columnIndex: 1);
Console.WriteLine($"SQL query returned: {string.Join(", ", names)}");
}
}
private static async IAsyncEnumerable<RecordBatch> ToAsyncEnumerable(RecordBatch batch)
{
yield return batch;
await Task.CompletedTask;
}
private static IReadOnlyList<string> ReadStringColumn(RecordBatch batch, int columnIndex)
{
IArrowArray column = batch.Column(columnIndex);
var values = new List<string>(batch.Length);
for (int rowIndex = 0; rowIndex < batch.Length; rowIndex++)
{
values.Add(column switch
{
StringArray array => array.GetString(rowIndex) ?? string.Empty,
StringViewArray array => array.GetString(rowIndex) ?? string.Empty,
LargeStringArray array => array.GetString(rowIndex) ?? string.Empty,
_ => Convert.ToString(column.GetType().Name) ?? string.Empty,
});
}
return values;
}
private static void EnsureSuccess(ExecuteResult result, string operation)
{
if (!result.Success)
{
throw new InvalidOperationException($"Failed to {operation}: {result.Message}");
}
}
}
}