Repository navigation
Expand file tree
/
Copy pathInsert_009_PocoInsert.cs
More file actions
178 lines (146 loc) · 6.11 KB
/
Copy pathInsert_009_PocoInsert.cs
File metadata and controls
178 lines (146 loc) · 6.11 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
namespace ClickHouse.Driver.Examples;
/// <summary>
/// Demonstrates inserting data using strongly-typed POCO objects instead of object[] arrays.
/// Register a type once, then pass IEnumerable<T> directly to InsertBinaryAsync.
/// </summary>
public static class PocoInsert
{
public static async Task Run()
{
using var client = ExampleConfig.CreateHttpClient();
await BasicPocoInsert(client);
await AttributeMappingInsert(client);
await ExplicitTypesInsert(client);
}
// Simple POCO with no annotations
public class SensorReading
{
public ulong Id { get; set; }
public required string SensorName { get; set; }
public double Value { get; set; }
public DateTime Timestamp { get; set; }
}
/// <summary>
/// Insert POCO object with no annotations, column names are matched with case sensitivity.
/// </summary>
private static async Task BasicPocoInsert(ClickHouseClient client)
{
Console.WriteLine("1. Basic POCO insert:");
await client.ExecuteNonQueryAsync(@"
CREATE TABLE IF NOT EXISTS example_poco_sensors
(
Id UInt64,
SensorName String,
Value Float64,
Timestamp DateTime
)
ENGINE = MergeTree()
ORDER BY Id
");
client.RegisterBinaryInsertType<SensorReading>();
var readings = Enumerable.Range(1, 1000).Select(i => new SensorReading
{
Id = (ulong)i,
SensorName = $"sensor_{i % 10}",
Value = Random.Shared.NextDouble() * 100,
Timestamp = DateTime.UtcNow.AddSeconds(-i),
});
var rowsInserted = await client.InsertBinaryAsync("example_poco_sensors", readings);
Console.WriteLine($" Inserted {rowsInserted} sensor readings");
var count = await client.ExecuteScalarAsync("SELECT count() FROM example_poco_sensors");
Console.WriteLine($" Verified: {count} rows in table\n");
await client.ExecuteNonQueryAsync("DROP TABLE IF EXISTS example_poco_sensors");
}
// A POCO with attribute customization
public class AuditEvent
{
[ClickHouseColumn(Name = "event_id")]
public ulong Id { get; set; }
[ClickHouseColumn(Name = "event_type", Type = "LowCardinality(String)")]
public required string Type { get; set; }
[ClickHouseColumn(Name = "payload")]
public required string Payload { get; set; }
[ClickHouseColumn(Name = "created_at")]
public DateTime CreatedAt { get; set; }
[ClickHouseNotMapped]
public required string InternalTag { get; set; } // Excluded from insert
}
/// <summary>
/// Use [ClickHouseColumn] to specify column name and type, and [ClickHouseNotMapped] to exclude properties.
/// </summary>
private static async Task AttributeMappingInsert(ClickHouseClient client)
{
Console.WriteLine("2. POCO insert with attribute mapping:");
await client.ExecuteNonQueryAsync(@"
CREATE TABLE IF NOT EXISTS example_poco_audit
(
event_id UInt64,
event_type LowCardinality(String),
payload String,
created_at DateTime
)
ENGINE = MergeTree()
ORDER BY event_id
");
client.RegisterBinaryInsertType<AuditEvent>();
var events = new[]
{
new AuditEvent
{
Id = 1, Type = "login", Payload = """{"user": "alice"}""",
CreatedAt = DateTime.UtcNow, InternalTag = "ignored",
},
new AuditEvent
{
Id = 2, Type = "logout", Payload = """{"user": "bob"}""",
CreatedAt = DateTime.UtcNow, InternalTag = "also ignored",
},
};
var rowsInserted = await client.InsertBinaryAsync("example_poco_audit", events);
Console.WriteLine($" Inserted {rowsInserted} audit events");
Console.WriteLine(" (InternalTag was excluded via [ClickHouseNotMapped])");
var count = await client.ExecuteScalarAsync("SELECT count() FROM example_poco_audit");
Console.WriteLine($" Verified: {count} rows in table\n");
await client.ExecuteNonQueryAsync("DROP TABLE IF EXISTS example_poco_audit");
}
// A POCO with explicit types on every property skips the schema probe query entirely
public class MetricPoint
{
[ClickHouseColumn(Type = "UInt64")]
public ulong Id { get; set; }
[ClickHouseColumn(Type = "String")]
public required string Name { get; set; }
[ClickHouseColumn(Type = "Float64")]
public double Value { get; set; }
}
/// <summary>
/// When ALL properties have [ClickHouseColumn(Type = "...")], the driver knows the column
/// types at compile time and skips the SELECT ... WHERE 1=0 schema probe query.
/// </summary>
private static async Task ExplicitTypesInsert(ClickHouseClient client)
{
Console.WriteLine("3. POCO insert with explicit types (no schema probe):");
await client.ExecuteNonQueryAsync(@"
CREATE TABLE IF NOT EXISTS example_poco_metrics
(
Id UInt64,
Name String,
Value Float64
)
ENGINE = MergeTree()
ORDER BY Id
");
client.RegisterBinaryInsertType<MetricPoint>();
var metrics = Enumerable.Range(1, 500).Select(i => new MetricPoint
{
Id = (ulong)i,
Name = $"cpu.usage.core{i % 8}",
Value = Random.Shared.NextDouble() * 100,
});
var rowsInserted = await client.InsertBinaryAsync("example_poco_metrics", metrics);
Console.WriteLine($" Inserted {rowsInserted} metric points (schema probe skipped)");
var count = await client.ExecuteScalarAsync("SELECT count() FROM example_poco_metrics");
Console.WriteLine($" Verified: {count} rows in table\n");
await client.ExecuteNonQueryAsync("DROP TABLE IF EXISTS example_poco_metrics");
}
}