/
composite-partition-key-sample.go
437 lines (382 loc) · 15.8 KB
/
composite-partition-key-sample.go
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package main
import (
"bufio"
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/timestreamquery"
"github.com/aws/aws-sdk-go/service/timestreamwrite"
"net"
"net/http"
"os"
"strconv"
"time"
"golang.org/x/net/http2"
)
/*
This code sample is to demonstrate
1. Creating a table with composite partition key (dimension key) with optional enforcement
2. Creating a table with composite partition key (measure key)
3. Describe table with partition key
4. Ingest records without dimension partition key (optional enforcement) and succeed
5. Update dimension partition key Optional enforcement to Required enforcement
6. Ingest records with dimension partition key to table with enforced dimension partition key
7. Ingest records without dimension partition key to table with enforced dimension partition key (rejected records)
8. Ingest records table created with measure partition key
9. Execute query on tables with partition keys
10. Clean up resources
*/
const (
PartitionKeyDimensionTableName = "host_metrics_dim_pk"
PartitionKeyMeasureTableName = "host_metrics_measure_pk"
CompositePartitionKeyDimName = "hostId"
CompositePartitionKeyDimValue = "host1"
CompositePartitionKeyDiffName = "hostIdDiff"
)
func main() {
/**
* Recommended Timestream write client SDK configuration:
* - Set SDK retry count to 10.
* - Use SDK DEFAULT_BACKOFF_STRATEGY
* - Request timeout of 20 seconds
*/
// Setting 20 seconds for timeout
tr := &http.Transport{
ResponseHeaderTimeout: 20 * time.Second,
// Using DefaultTransport values for other parameters: https://golang.org/pkg/net/http/#RoundTripper
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
KeepAlive: 30 * time.Second,
DualStack: true,
Timeout: 30 * time.Second,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
// So client makes HTTP/2 requests
http2.ConfigureTransport(tr)
sess, err := session.NewSession(&aws.Config{Region: aws.String("us-east-1"),
MaxRetries: aws.Int(10), HTTPClient: &http.Client{Transport: tr}})
writeSvc := timestreamwrite.New(sess)
qsess, err := session.NewSession(&aws.Config{Region: aws.String("us-east-1"),
MaxRetries: aws.Int(10), HTTPClient: &http.Client{Transport: tr}})
querySvc := timestreamquery.New(qsess)
databaseName := flag.String("database_name", "devops", "database name string")
tableForDimensionPartitionKey := flag.String("table_with_dimension_partition_key", PartitionKeyDimensionTableName, "table name string")
tableForMeasurePartitionKey := flag.String("table_with_measure_partition_key", PartitionKeyMeasureTableName, "table name string")
flag.Parse()
reader := bufio.NewReader(os.Stdin)
fmt.Println("Creating a database, hit enter to continue")
reader.ReadString('\n')
// Create database.
createDatabaseInput := ×treamwrite.CreateDatabaseInput{
DatabaseName: aws.String(*databaseName),
}
_, err = writeSvc.CreateDatabase(createDatabaseInput)
if err != nil {
fmt.Println("Error:")
fmt.Println(err)
} else {
fmt.Println("Database successfully created")
}
/* This sample demonstrates workflow using dimension type partition key
* 1. Create table with dimension type partition key, with optional enforcement.
* 2. Ingest records with missing partition key. Records will be accepted as enforcement level is optional
* 3. Update table with required enforcement.
* 4. Ingest records with same partition key.
* 5. Ingest records with missing partition key. This will return rejected records as they do not
* contain required partition key.
* 6. Query records with same partition key.
*/
runSampleWithDimensionPartitionKey(writeSvc, querySvc, databaseName, tableForDimensionPartitionKey, reader)
/* This sample demonstrates workflow using measure name type partition key
* 1. Create table with measure name type partition key
* 2. Ingest records and query
*/
runSampleWithMeasureNamePartitionKey(writeSvc, querySvc, databaseName, tableForMeasurePartitionKey, reader)
// Delete tables
fmt.Println("Deleting tables, hit enter to continue.")
reader.ReadString('\n')
deleteCompositePartitionKeyTable(writeSvc, databaseName, tableForDimensionPartitionKey)
deleteCompositePartitionKeyTable(writeSvc, databaseName, tableForMeasurePartitionKey)
fmt.Println("Deleting database, hit enter to continue.")
reader.ReadString('\n')
deleteDatabaseInput := ×treamwrite.DeleteDatabaseInput{
DatabaseName: aws.String(*databaseName),
}
_, err = writeSvc.DeleteDatabase(deleteDatabaseInput)
if err != nil {
fmt.Println("Error:")
fmt.Println(err)
} else {
fmt.Println("Database deleted:", *databaseName)
}
}
func runSampleWithMeasureNamePartitionKey(writeSvc *timestreamwrite.TimestreamWrite, querySvc *timestreamquery.TimestreamQuery,
databaseName *string, tableName *string, reader *bufio.Reader) {
fmt.Println("Starting example for measure name type partition key:")
fmt.Printf("Creating table %s with measure partition key, hit enter to continue", *tableName)
reader.ReadString('\n')
partitionKeyListMeasure := []*timestreamwrite.PartitionKey{
{
Type: aws.String("MEASURE"),
},
}
createTableWithPartitionKey(writeSvc, databaseName, tableName, partitionKeyListMeasure)
fmt.Printf("Describing the table %s with measure partition key, hit enter to continue", *tableName)
reader.ReadString('\n')
// Describe table.
describeTableInputForMeasurePartitionKey := ×treamwrite.DescribeTableInput{
DatabaseName: aws.String(*databaseName),
TableName: aws.String(*tableName),
}
describeTableOutputForMeasurePartitionKey, err := writeSvc.DescribeTable(describeTableInputForMeasurePartitionKey)
if err != nil {
fmt.Println("Error:")
fmt.Println(err)
} else {
fmt.Println("Describe table is successful, below is the output:")
fmt.Println(describeTableOutputForMeasurePartitionKey)
}
fmt.Printf("Ingesting records for table %s, hit enter to continue\n", *tableName)
reader.ReadString('\n')
writeRecords(writeSvc, reader, databaseName, tableName, CompositePartitionKeyDimName)
// Run Query with partitionKey
fmt.Printf("Run query for table %s, hit enter to continue\n", *tableName)
reader.ReadString('\n')
runSingleQuery(databaseName, tableName, aws.String("cpu_utilization"), aws.String("13.5"), querySvc)
}
func runSampleWithDimensionPartitionKey(writeSvc *timestreamwrite.TimestreamWrite, querySvc *timestreamquery.TimestreamQuery,
databaseName *string, tableName *string, reader *bufio.Reader) {
fmt.Println("Starting example for dimension type partition key:")
// Composite Partition Keys are most effective when dimension has high cardinality
// and are frequently accessed in queries
// Using dimension name with high cardinality, "hostId"
fmt.Printf("Creating table %s with dimension partition key(OPTIONAL ENFORCEMENT), hit enter to continue", *tableName)
reader.ReadString('\n')
partitionKeyWithDimensionAndOptionalEnforcement := []*timestreamwrite.PartitionKey{
{
Name: aws.String(CompositePartitionKeyDimName),
EnforcementInRecord: aws.String("OPTIONAL"),
Type: aws.String("DIMENSION"),
},
}
// Create table with dimension type partition key and OPTIONAL enforcement
createTableWithPartitionKey(writeSvc, databaseName, tableName, partitionKeyWithDimensionAndOptionalEnforcement)
fmt.Printf("Describing the table %s with dimension partition key, hit enter to continue\n", *tableName)
reader.ReadString('\n')
// Describe table.
describeTableInputForDimensionPartitionKey := ×treamwrite.DescribeTableInput{
DatabaseName: aws.String(*databaseName),
TableName: aws.String(*tableName),
}
describeTableOutput, err := writeSvc.DescribeTable(describeTableInputForDimensionPartitionKey)
if err != nil {
fmt.Println("Error:")
fmt.Println(err)
} else {
fmt.Println("Describe table is successful, below is the output:")
fmt.Println(describeTableOutput)
}
fmt.Printf("Ingest records without dimension type partition key for table %s, Since the enforcement level is OPTIONAL the records will be ingested"+
", hit enter to continue", *tableName)
reader.ReadString('\n')
writeRecords(writeSvc, reader, databaseName, tableName, CompositePartitionKeyDiffName)
fmt.Printf("Updating the dimension partition key enforcement from optional to required for table %s, hit enter to continue", *tableName)
reader.ReadString('\n')
// Update table partition key enforcement attribute
updateTableInput := ×treamwrite.UpdateTableInput{
DatabaseName: aws.String(*databaseName),
TableName: aws.String(*tableName),
Schema: ×treamwrite.Schema{
CompositePartitionKey: []*timestreamwrite.PartitionKey{
{
Name: aws.String(CompositePartitionKeyDimName),
EnforcementInRecord: aws.String("REQUIRED"),
Type: aws.String("DIMENSION"),
},
}},
}
updateTableOutput, err := writeSvc.UpdateTable(updateTableInput)
if err != nil {
fmt.Println("Error:")
fmt.Println(err)
} else {
fmt.Println("Update table is successful, below is the output:")
fmt.Println(updateTableOutput)
}
fmt.Printf("Describing the table %s, hit enter to continue\n", *tableName)
reader.ReadString('\n')
// Describe table.
describeTableInputForDimensionPartitionKeyAfterUpdate := ×treamwrite.DescribeTableInput{
DatabaseName: aws.String(*databaseName),
TableName: aws.String(*tableName),
}
describeTableOutputForDimensionPartitionKeyAfterUpdate, err := writeSvc.DescribeTable(describeTableInputForDimensionPartitionKeyAfterUpdate)
if err != nil {
fmt.Println("Error:")
fmt.Println(err)
} else {
fmt.Println("Describe table is successful, below is the output:")
fmt.Println(describeTableOutputForDimensionPartitionKeyAfterUpdate)
}
fmt.Printf("Ingesting records for table %s, hit enter to continue\n", *tableName)
reader.ReadString('\n')
writeRecords(writeSvc, reader, databaseName, tableName, CompositePartitionKeyDimName)
fmt.Printf("Ingesting records for table %s - Records will be rejected as ingestion done without enforced partition key"+
", hit enter to continue", *tableName)
reader.ReadString('\n')
writeRecords(writeSvc, reader, databaseName, tableName, CompositePartitionKeyDiffName)
// Run Query with partitionKey
fmt.Printf("Run query for table %s, hit enter to continue\n", *tableName)
reader.ReadString('\n')
runSingleQuery(databaseName, tableName, aws.String(CompositePartitionKeyDimName), aws.String("'host1'"), querySvc)
}
func deleteCompositePartitionKeyTable(writeSvc *timestreamwrite.TimestreamWrite, databaseName *string, tableName *string) {
_, err := writeSvc.DeleteTable(×treamwrite.DeleteTableInput{
DatabaseName: aws.String(*databaseName),
TableName: aws.String(*tableName),
})
if err != nil {
fmt.Println("Error:")
fmt.Println(err)
} else {
fmt.Println("Table deleted", *tableName)
}
}
func runSingleQuery(databaseName *string, tableName *string, partitionKeyName *string,
partitionKeyValue *string, querySvc *timestreamquery.TimestreamQuery) {
// sample query
queryString := fmt.Sprintf("select * from \"%s\".\"%s\" where \"%s\"=%s",
*databaseName, *tableName, *partitionKeyName, *partitionKeyValue)
/*queryString := "select * from cdpk-crud-db.host_metrics_dim_pk where hostId='host1'"*/
fmt.Println(queryString)
queryInput := ×treamquery.QueryInput{
QueryString: aws.String(queryString),
}
// execute the query
queryOutput, err := querySvc.Query(queryInput)
if err == nil {
fmt.Println(queryOutput)
} else {
fmt.Println("Error:")
fmt.Println(err)
}
}
func generateRecords(dimensions []*timestreamwrite.Dimension) []*timestreamwrite.Record {
var records []*timestreamwrite.Record
currentTimeInSeconds := time.Now().Unix()
records = append(records, ×treamwrite.Record{
Dimensions: dimensions,
MeasureName: aws.String("cpu_utilization"),
MeasureValue: aws.String("13.5"),
MeasureValueType: aws.String("DOUBLE"),
Time: aws.String(strconv.FormatInt(currentTimeInSeconds, 10)),
TimeUnit: aws.String("SECONDS"),
}, ×treamwrite.Record{
Dimensions: dimensions,
MeasureName: aws.String("memory_utilization"),
MeasureValue: aws.String("40"),
MeasureValueType: aws.String("DOUBLE"),
Time: aws.String(strconv.FormatInt(currentTimeInSeconds, 10)),
TimeUnit: aws.String("SECONDS"),
})
return records
}
func generateMultiMeasureRecords(dimensions []*timestreamwrite.Dimension) []*timestreamwrite.Record {
var records []*timestreamwrite.Record
var multiMeasures []*timestreamwrite.MeasureValue
currentTimeInSeconds := time.Now().Unix()
multiMeasures = append(multiMeasures, ×treamwrite.MeasureValue{
Name: aws.String("cpu_utilization"),
Value: aws.String("13.5"),
Type: aws.String("DOUBLE"),
}, ×treamwrite.MeasureValue{
Name: aws.String("memory_utilization"),
Value: aws.String("40"),
Type: aws.String("DOUBLE"),
})
records = append(records, ×treamwrite.Record{
Dimensions: dimensions,
MeasureName: aws.String("metrics"),
MeasureValueType: aws.String("MULTI"),
MeasureValues: multiMeasures,
Time: aws.String(strconv.FormatInt(currentTimeInSeconds, 10)),
TimeUnit: aws.String("SECONDS"),
})
return records
}
func writeRecords(writeSvc *timestreamwrite.TimestreamWrite, reader *bufio.Reader, databaseName *string,
tableName *string, compositePartitionKeyDimName string) {
var dimensions []*timestreamwrite.Dimension
dimensions = append(dimensions, ×treamwrite.Dimension{
Name: aws.String(compositePartitionKeyDimName),
Value: aws.String(CompositePartitionKeyDimValue),
}, ×treamwrite.Dimension{
Name: aws.String("region"),
Value: aws.String("us-east-1"),
}, ×treamwrite.Dimension{
Name: aws.String("az"),
Value: aws.String("az1"),
})
writeRecordsInput := ×treamwrite.WriteRecordsInput{
DatabaseName: aws.String(*databaseName),
TableName: aws.String(*tableName),
Records: generateRecords(dimensions),
}
_, err := writeSvc.WriteRecords(writeRecordsInput)
if err != nil {
fmt.Println("Error:")
fmt.Println(err)
} else {
fmt.Println("Write records is successful")
}
fmt.Println("Ingesting multi measure records, hit enter to continue")
reader.ReadString('\n')
writeRecordsInputMulti := ×treamwrite.WriteRecordsInput{
DatabaseName: aws.String(*databaseName),
TableName: aws.String(*tableName),
Records: generateMultiMeasureRecords(dimensions),
}
_, err = writeSvc.WriteRecords(writeRecordsInputMulti)
if err != nil {
fmt.Println("Error:")
fmt.Println(err)
} else {
fmt.Println("Write records is successful")
}
}
func createTableWithPartitionKey(writeSvc *timestreamwrite.TimestreamWrite, databaseName *string,
tableName *string, compositePartitionKey []*timestreamwrite.PartitionKey) {
fmt.Printf("Creating table with table name: %s\n", *tableName)
// Create table with partition key
createTableInput := ×treamwrite.CreateTableInput{
DatabaseName: aws.String(*databaseName),
TableName: aws.String(*tableName),
// Enable MagneticStoreWrite for Table
MagneticStoreWriteProperties: ×treamwrite.MagneticStoreWriteProperties{
EnableMagneticStoreWrites: aws.Bool(true),
// Persist MagneticStoreWrite rejected records in S3
MagneticStoreRejectedDataLocation: ×treamwrite.MagneticStoreRejectedDataLocation{
S3Configuration: ×treamwrite.S3Configuration{
BucketName: aws.String("timestream-sample-bucket"),
ObjectKeyPrefix: aws.String("TimeStreamCustomerSampleGo"),
EncryptionOption: aws.String("SSE_S3"),
},
},
},
Schema: ×treamwrite.Schema{
CompositePartitionKey: compositePartitionKey,
},
}
_, err := writeSvc.CreateTable(createTableInput)
if err != nil {
fmt.Println("Error:")
fmt.Println(err)
} else {
fmt.Println("Create table is successful")
}
}