テーブルの作成 - 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

次のスニペットは JavaScript v3 に を使用します AWS SDK。クライアントのインストール方法と使用状況の詳細については、 JavaScript 「v3 SDKの Timestream Write Client - AWS」を参照してください。

クラス CreateTableCommand と も参照してくださいCreateTable

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; } }

次のスニペットは JavaScript V2 スタイルに AWS SDKを使用します。これは、Node.js のサンプルアプリケーションに基づいており、 の LiveAnalytics アプリケーション用の Amazon Timestream サンプルです 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()); } }