Create table - Amazon Timestream

Create table

Memory store writes

You can use the following code snippet to create a table that has magnetic store writes disabled, as a result you can only write data into your memory store retention window.

Note

These code snippets are based on full sample applications on GitHub. For more information about how to get started with the sample applications, see Sample application.

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

The following snippet uses AWS SDK for JavaScript v3. For more information about how to install the client and usage, see Timestream Write Client - AWS SDK for JavaScript v3.

Also see Class CreateTableCommand and 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; } }

The following snippet uses the AWS SDK for JavaScript V2 style. It is based on the sample application at Node.js sample Amazon Timestream for LiveAnalytics application on 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 writes

You can use the following code snippet to create a table with magnetic store writes enabled. With magnetic store writes you can write data into both your memory store retention window and magnetic store retention window.

Note

These code snippets are based on full sample applications on GitHub. For more information about how to get started with the sample applications, see Sample application.

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