Membuat tabel - Amazon Timestream

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Membuat tabel

Toko memori menulis

Anda dapat menggunakan cuplikan kode berikut untuk membuat tabel yang memiliki penulisan penyimpanan magnetik dinonaktifkan, sebagai hasilnya Anda hanya dapat menulis data ke jendela penyimpanan penyimpanan memori Anda.

catatan

Cuplikan kode ini didasarkan pada contoh aplikasi lengkap. GitHub Untuk informasi selengkapnya tentang cara memulai dengan contoh aplikasi, lihatAplikasi sampel.

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

Cuplikan berikut digunakan AWS SDK untuk JavaScript v3. Untuk informasi selengkapnya tentang cara menginstal klien dan penggunaan, lihat Timestream Write Client - AWS SDK untuk JavaScript v3.

Lihat juga kelas CreateTableCommand dan 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; } }

Cuplikan berikut menggunakan gaya AWS SDK for JavaScript V2. Hal ini didasarkan pada aplikasi sampel Node.js Amazon Timestream untuk LiveAnalytics aplikasi pada. 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()); } }

Toko magnetik menulis

Anda dapat menggunakan cuplikan kode berikut untuk membuat tabel dengan penulisan penyimpanan magnetik diaktifkan. Dengan penulisan penyimpanan magnetik, Anda dapat menulis data ke jendela penyimpanan penyimpanan memori dan jendela retensi penyimpanan magnetik.

catatan

Cuplikan kode ini didasarkan pada contoh aplikasi lengkap. GitHub Untuk informasi selengkapnya tentang cara memulai dengan contoh aplikasi, lihatAplikasi sampel.

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