Create table - Amazon Timestream

Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.

Create table

Speichert Schreibvorgänge

Sie können den folgenden Codeausschnitt verwenden, um eine Tabelle zu erstellen, in der Magnetspeicher-Schreibvorgänge deaktiviert sind. Daher können Sie nur Daten in das Aufbewahrungsfenster für den Speicherspeicher schreiben.

Anmerkung

Diese Codefragmente basieren auf vollständigen Beispielanwendungen auf. GitHub Weitere Informationen zu den ersten Schritten mit den Beispielanwendungen finden Sie unter. Beispielanwendung

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

Der folgende Codeausschnitt wird AWS SDK für JavaScript Version 3 verwendet. Weitere Informationen zur Installation und Verwendung des Clients finden Sie unter Timestream Write Client — AWS SDK for v3. JavaScript

Siehe auch Klasse CreateTableCommand und 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; } }

Das folgende Snippet verwendet den AWS SDK for JavaScript V2-Stil. Es basiert auf der Beispielanwendung unter Node.js Beispiel Amazon Timestream für die LiveAnalytics Anwendung auf 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()); } }

Magnetic Store schreibt

Sie können den folgenden Codeausschnitt verwenden, um eine Tabelle zu erstellen, in der Magnetic Store Writes aktiviert sind. Mit Magnetic Store Writes können Sie Daten sowohl in das Aufbewahrungsfenster Ihres Speichers als auch in das Aufbewahrungsfenster Ihres Magnetspeichers schreiben.

Anmerkung

Diese Codefragmente basieren auf vollständigen Beispielanwendungen auf. GitHub Weitere Informationen zu den ersten Schritten mit den Beispielanwendungen finden Sie unter. Beispielanwendung

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