创建表 - Amazon Timestream

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

创建表

内存存储写入

您可以使用以下代码片段来创建禁用磁存储写入功能的表,因此您只能将数据写入内存存储保留窗口。

注意

这些代码片段基于上的完整示例应用程序。GitHub有关如何开始使用示例应用程序的更多信息,请参阅示例应用程序

Java
public void createTable() { 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); try { amazonTimestreamWrite.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 createTable() { System.out.println("Creating table"); final RetentionProperties retentionProperties = RetentionProperties.builder() .memoryStoreRetentionPeriodInHours(HT_TTL_HOURS) .magneticStoreRetentionPeriodInDays(CT_TTL_DAYS).build(); final CreateTableRequest createTableRequest = CreateTableRequest.builder() .databaseName(DATABASE_NAME).tableName(TABLE_NAME).retentionProperties(retentionProperties).build(); try { timestreamWriteClient.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
// Create table. createTableInput := &timestreamwrite.CreateTableInput{ DatabaseName: aws.String(*databaseName), TableName: aws.String(*tableName), } _, err = writeSvc.CreateTable(createTableInput) if err != nil { fmt.Println("Error:") fmt.Println(err) } else { fmt.Println("Create table is successful") }
Python
def create_table(self): print("Creating table") retention_properties = { 'MemoryStoreRetentionPeriodInHours': Constant.HT_TTL_HOURS, 'MagneticStoreRetentionPeriodInDays': Constant.CT_TTL_DAYS } try: self.client.create_table(DatabaseName=Constant.DATABASE_NAME, TableName=Constant.TABLE_NAME, RetentionProperties=retention_properties) print("Table [%s] successfully created." % Constant.TABLE_NAME) except self.client.exceptions.ConflictException: print("Table [%s] exists on database [%s]. Skipping table creation" % ( Constant.TABLE_NAME, Constant.DATABASE_NAME)) except Exception as err: print("Create table failed:", err)
Node.js

以下代码段用 AWS SDK于 JavaScript v3。有关如何安装客户端和用法的更多信息,请参阅 Timestream Write Client-f AWS SDK or JavaScript v3

另请参阅类 CreateTableCommandCreateTable

import { TimestreamWriteClient, CreateTableCommand } from "@aws-sdk/client-timestream-write"; const writeClient = new TimestreamWriteClient({ region: "us-east-1" }); const params = { DatabaseName: "testDbFromNode", TableName: "testTableFromNode", RetentionProperties: { MemoryStoreRetentionPeriodInHours: 24, MagneticStoreRetentionPeriodInDays: 365 } }; const command = new CreateTableCommand(params); try { const data = await writeClient.send(command); console.log(`Table ${data.Table.TableName} created successfully`); } catch (error) { if (error.code === 'ConflictException') { console.log(`Table ${params.TableName} already exists on db ${params.DatabaseName}. Skipping creation.`); } else { console.log("Error creating table. ", error); throw error; } }

以下代码段使用了 for AWS SDK JavaScript V2 样式。它基于 Node.js 示例 Amazon Timestream 中的示例 LiveAnalytics 应用程序,供其上使用。 GitHub

async function createTable() { console.log("Creating Table"); const params = { DatabaseName: constants.DATABASE_NAME, TableName: constants.TABLE_NAME, RetentionProperties: { MemoryStoreRetentionPeriodInHours: constants.HT_TTL_HOURS, MagneticStoreRetentionPeriodInDays: constants.CT_TTL_DAYS } }; const promise = writeClient.createTable(params).promise(); await promise.then( (data) => { console.log(`Table ${data.Table.TableName} created successfully`); }, (err) => { if (err.code === 'ConflictException') { console.log(`Table ${params.TableName} already exists on db ${params.DatabaseName}. Skipping creation.`); } else { console.log("Error creating table. ", err); throw err; } } ); }
.NET
public async Task CreateTable() { Console.WriteLine("Creating Table"); try { var createTableRequest = new CreateTableRequest { DatabaseName = Constants.DATABASE_NAME, TableName = Constants.TABLE_NAME, RetentionProperties = new RetentionProperties { MagneticStoreRetentionPeriodInDays = Constants.CT_TTL_DAYS, MemoryStoreRetentionPeriodInHours = Constants.HT_TTL_HOURS } }; CreateTableResponse response = await writeClient.CreateTableAsync(createTableRequest); Console.WriteLine($"Table {Constants.TABLE_NAME} created"); } catch (ConflictException) { Console.WriteLine("Table already exists."); } catch (Exception e) { Console.WriteLine("Create table failed:" + e.ToString()); } }

磁性存储器写入

您可以使用以下代码片段创建启用磁性存储写入功能的表。通过磁存储写入,您可以将数据写入内存存储保留窗口和磁存储保留窗口。

注意

这些代码片段基于上的完整示例应用程序。GitHub有关如何开始使用示例应用程序的更多信息,请参阅示例应用程序

Java
public void createTable(String databaseName, String tableName) { System.out.println("Creating table"); CreateTableRequest createTableRequest = new CreateTableRequest(); createTableRequest.setDatabaseName(databaseName); createTableRequest.setTableName(tableName); final RetentionProperties retentionProperties = new RetentionProperties() .withMemoryStoreRetentionPeriodInHours(HT_TTL_HOURS) .withMagneticStoreRetentionPeriodInDays(CT_TTL_DAYS); createTableRequest.setRetentionProperties(retentionProperties); // Enable MagneticStoreWrite final MagneticStoreWriteProperties magneticStoreWriteProperties = new MagneticStoreWriteProperties() .withEnableMagneticStoreWrites(true); createTableRequest.setMagneticStoreWriteProperties(magneticStoreWriteProperties); try { amazonTimestreamWrite.createTable(createTableRequest); System.out.println("Table [" + tableName + "] successfully created."); } catch (ConflictException e) { System.out.println("Table [" + tableName + "] exists on database [" + databaseName + "] . Skipping table creation"); //We do not throw exception here, we use the existing table instead } }
Java v2
public void createTable(String databaseName, String tableName) { System.out.println("Creating table"); // Enable MagneticStoreWrite final MagneticStoreWriteProperties magneticStoreWriteProperties = MagneticStoreWriteProperties.builder() .enableMagneticStoreWrites(true) .build(); CreateTableRequest createTableRequest = CreateTableRequest.builder() .databaseName(databaseName) .tableName(tableName) .retentionProperties(RetentionProperties.builder() .memoryStoreRetentionPeriodInHours(HT_TTL_HOURS) .magneticStoreRetentionPeriodInDays(CT_TTL_DAYS) .build()) .magneticStoreWriteProperties(magneticStoreWriteProperties) .build(); try { timestreamWriteClient.createTable(createTableRequest); System.out.println("Table [" + tableName + "] successfully created."); } catch (ConflictException e) { System.out.println("Table [" + tableName + "] exists in database [" + databaseName + "] . Skipping table creation"); } }
Go
// Create table. createTableInput := &timestreamwrite.CreateTableInput{ DatabaseName: aws.String(*databaseName), TableName: aws.String(*tableName), // Enable MagneticStoreWrite MagneticStoreWriteProperties: &timestreamwrite.MagneticStoreWriteProperties{ EnableMagneticStoreWrites: aws.Bool(true), }, } _, err = writeSvc.CreateTable(createTableInput)
Python
def create_table(self): print("Creating table") retention_properties = { 'MemoryStoreRetentionPeriodInHours': Constant.HT_TTL_HOURS, 'MagneticStoreRetentionPeriodInDays': Constant.CT_TTL_DAYS } magnetic_store_write_properties = { 'EnableMagneticStoreWrites': True } try: self.client.create_table(DatabaseName=Constant.DATABASE_NAME, TableName=Constant.TABLE_NAME, RetentionProperties=retention_properties, MagneticStoreWriteProperties=magnetic_store_write_properties) print("Table [%s] successfully created." % Constant.TABLE_NAME) except self.client.exceptions.ConflictException: print("Table [%s] exists on database [%s]. Skipping table creation" % ( Constant.TABLE_NAME, Constant.DATABASE_NAME)) except Exception as err: print("Create table failed:", err)
Node.js
async function createTable() { console.log("Creating Table"); const params = { DatabaseName: constants.DATABASE_NAME, TableName: constants.TABLE_NAME, RetentionProperties: { MemoryStoreRetentionPeriodInHours: constants.HT_TTL_HOURS, MagneticStoreRetentionPeriodInDays: constants.CT_TTL_DAYS }, MagneticStoreWriteProperties: { EnableMagneticStoreWrites: true } }; const promise = writeClient.createTable(params).promise(); await promise.then( (data) => { console.log(`Table ${data.Table.TableName} created successfully`); }, (err) => { if (err.code === 'ConflictException') { console.log(`Table ${params.TableName} already exists on db ${params.DatabaseName}. Skipping creation.`); } else { console.log("Error creating table. ", err); throw err; } } ); }
.NET
public async Task CreateTable() { Console.WriteLine("Creating Table"); try { var createTableRequest = new CreateTableRequest { DatabaseName = Constants.DATABASE_NAME, TableName = Constants.TABLE_NAME, RetentionProperties = new RetentionProperties { MagneticStoreRetentionPeriodInDays = Constants.CT_TTL_DAYS, MemoryStoreRetentionPeriodInHours = Constants.HT_TTL_HOURS }, // Enable MagneticStoreWrite MagneticStoreWriteProperties = new MagneticStoreWriteProperties { EnableMagneticStoreWrites = true, } }; CreateTableResponse response = await writeClient.CreateTableAsync(createTableRequest); Console.WriteLine($"Table {Constants.TABLE_NAME} created"); } catch (ConflictException) { Console.WriteLine("Table already exists."); } catch (Exception e) { Console.WriteLine("Create table failed:" + e.ToString()); } }