开始使用客户定义的分区键 - Amazon Timestream
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

要获得与亚马逊 Timestream 类似的功能 LiveAnalytics,可以考虑适用于 InfluxDB 的亚马逊 Timestream。它为实时分析提供了简化的数据摄取和个位数毫秒的查询响应时间。点击此处了解更多。

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

开始使用客户定义的分区键

在控制台中,选择表格并创建新表。您还可以使用 SDK 访问创建可包含客户定义分区键的新表的CreateTable操作。

使用维度类型分区键创建表

您可以使用以下代码片段创建具有维度类型分区键的表。

Java
public void createTableWithDimensionTypePartitionKeyExample() { System.out.println("Creating table"); CreateTableRequest createTableRequest = new CreateTableRequest(); createTableRequest.setDatabaseName(DATABASE_NAME); createTableRequest.setTableName(TABLE_NAME); final RetentionProperties retentionProperties = new RetentionProperties() .withMemoryStoreRetentionPeriodInHours(HT_TTL_HOURS) .withMagneticStoreRetentionPeriodInDays(CT_TTL_DAYS); createTableRequest.setRetentionProperties(retentionProperties); // Can specify enforcement level with OPTIONAL or REQUIRED final List<PartitionKey> partitionKeyWithDimensionAndOptionalEnforcement = Collections.singletonList(new PartitionKey() .withName(COMPOSITE_PARTITION_KEY_DIM_NAME) .withType(PartitionKeyType.DIMENSION) .withEnforcementInRecord(PartitionKeyEnforcementLevel.OPTIONAL)); Schema schema = new Schema(); schema.setCompositePartitionKey(partitionKeyWithDimensionAndOptionalEnforcement); createTableRequest.setSchema(schema); try { writeClient.createTable(createTableRequest); System.out.println("Table [" + TABLE_NAME + "] successfully created."); } catch (ConflictException e) { System.out.println("Table [" + TABLE_NAME + "] exists on database [" + DATABASE_NAME + "] . Skipping database creation"); } }
Java v2
public void createTableWithDimensionTypePartitionKeyExample() { System.out.println("Creating table"); final RetentionProperties retentionProperties = RetentionProperties.builder() .memoryStoreRetentionPeriodInHours(HT_TTL_HOURS) .magneticStoreRetentionPeriodInDays(CT_TTL_DAYS) .build(); // Can specify enforcement level with OPTIONAL or REQUIRED final List<PartitionKey> partitionKeyWithDimensionAndOptionalEnforcement = Collections.singletonList(PartitionKey .builder() .name(COMPOSITE_PARTITION_KEY_DIM_NAME) .type(PartitionKeyType.DIMENSION) .enforcementInRecord(PartitionKeyEnforcementLevel.OPTIONAL) .build()); final Schema schema = Schema.builder() .compositePartitionKey(partitionKeyWithDimensionAndOptionalEnforcement).build(); final CreateTableRequest createTableRequest = CreateTableRequest.builder() .databaseName(DATABASE_NAME) .tableName(TABLE_NAME) .retentionProperties(retentionProperties) .schema(schema) .build(); try { writeClient.createTable(createTableRequest); System.out.println("Table [" + TABLE_NAME + "] successfully created."); } catch (ConflictException e) { System.out.println("Table [" + TABLE_NAME + "] exists on database [" + DATABASE_NAME + "] . Skipping database creation"); } }
Go v1
func createTableWithDimensionTypePartitionKeyExample(){ // Can specify enforcement level with OPTIONAL or REQUIRED partitionKeyWithDimensionAndOptionalEnforcement := []*timestreamwrite.PartitionKey{ { Name: aws.String(CompositePartitionKeyDimName), EnforcementInRecord: aws.String("OPTIONAL"), Type: aws.String("DIMENSION"), }, } createTableInput := &timestreamwrite.CreateTableInput{ DatabaseName: aws.String(*databaseName), TableName: aws.String(*tableName), // Enable MagneticStoreWrite for Table MagneticStoreWriteProperties: &timestreamwrite.MagneticStoreWriteProperties{ EnableMagneticStoreWrites: aws.Bool(true), // Persist MagneticStoreWrite rejected records in S3 MagneticStoreRejectedDataLocation: &timestreamwrite.MagneticStoreRejectedDataLocation{ S3Configuration: &timestreamwrite.S3Configuration{ BucketName: aws.String("timestream-sample-bucket"), ObjectKeyPrefix: aws.String("TimeStreamCustomerSampleGo"), EncryptionOption: aws.String("SSE_S3"), }, }, }, Schema: &timestreamwrite.Schema{ CompositePartitionKey: partitionKeyWithDimensionAndOptionalEnforcement, } } _, err := writeSvc.CreateTable(createTableInput) }
Go v2
func (timestreamBuilder TimestreamBuilder) CreateTableWithDimensionTypePartitionKeyExample() error { partitionKeyWithDimensionAndOptionalEnforcement := []types.PartitionKey{ { Name: aws.String(CompositePartitionKeyDimName), EnforcementInRecord: types.PartitionKeyEnforcementLevelOptional, Type: types.PartitionKeyTypeDimension, }, } _, err := timestreamBuilder.WriteSvc.CreateTable(context.TODO(), &timestreamwrite.CreateTableInput{ DatabaseName: aws.String(databaseName), TableName: aws.String(tableName), MagneticStoreWriteProperties: &types.MagneticStoreWriteProperties{ EnableMagneticStoreWrites: aws.Bool(true), // Persist MagneticStoreWrite rejected records in S3 MagneticStoreRejectedDataLocation: &types.MagneticStoreRejectedDataLocation{ S3Configuration: &types.S3Configuration{ BucketName: aws.String(s3BucketName), EncryptionOption: "SSE_S3", }, }, }, Schema: &types.Schema{ CompositePartitionKey: partitionKeyWithDimensionAndOptionalEnforcement, }, }) if err != nil { fmt.Println("Error:") fmt.Println(err) } else { fmt.Println("Create table is successful") } return err }
Python
def create_table_with_measure_name_type_partition_key(self): print("Creating table") retention_properties = { 'MemoryStoreRetentionPeriodInHours': HT_TTL_HOURS, 'MagneticStoreRetentionPeriodInDays': CT_TTL_DAYS } partitionKey_with_measure_name = [ {'Type': 'MEASURE'} ] schema = { 'CompositePartitionKey': partitionKey_with_measure_name } try: self.client.create_table(DatabaseName=DATABASE_NAME, TableName=TABLE_NAME, RetentionProperties=retention_properties, Schema=schema) print("Table [%s] successfully created." % TABLE_NAME) except self.client.exceptions.ConflictException: print("Table [%s] exists on database [%s]. Skipping table creation" % ( TABLE_NAME, DATABASE_NAME)) except Exception as err: print("Create table failed:", err)