更新表 - Amazon Timestream

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

更新表

您可以使用以下代码片段来更新表。

注意

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

Java
public void updateTable() { System.out.println("Updating table"); UpdateTableRequest updateTableRequest = new UpdateTableRequest(); updateTableRequest.setDatabaseName(DATABASE_NAME); updateTableRequest.setTableName(TABLE_NAME); final RetentionProperties retentionProperties = new RetentionProperties() .withMemoryStoreRetentionPeriodInHours(HT_TTL_HOURS) .withMagneticStoreRetentionPeriodInDays(CT_TTL_DAYS); updateTableRequest.setRetentionProperties(retentionProperties); amazonTimestreamWrite.updateTable(updateTableRequest); System.out.println("Table updated"); }
Java v2
public void updateTable() { System.out.println("Updating table"); final RetentionProperties retentionProperties = RetentionProperties.builder() .memoryStoreRetentionPeriodInHours(HT_TTL_HOURS) .magneticStoreRetentionPeriodInDays(CT_TTL_DAYS).build(); final UpdateTableRequest updateTableRequest = UpdateTableRequest.builder() .databaseName(DATABASE_NAME).tableName(TABLE_NAME).retentionProperties(retentionProperties).build(); timestreamWriteClient.updateTable(updateTableRequest); System.out.println("Table updated"); }
Go
// Update table. magneticStoreRetentionPeriodInDays := int64(7 * 365) memoryStoreRetentionPeriodInHours := int64(24) updateTableInput := &timestreamwrite.UpdateTableInput{ DatabaseName: aws.String(*databaseName), TableName: aws.String(*tableName), RetentionProperties: &timestreamwrite.RetentionProperties{ MagneticStoreRetentionPeriodInDays: &magneticStoreRetentionPeriodInDays, MemoryStoreRetentionPeriodInHours: &memoryStoreRetentionPeriodInHours, }, } updateTableOutput, err := writeSvc.UpdateTable(updateTableInput) if err != nil { fmt.Println("Error:") fmt.Println(err) } else { fmt.Println("Update table is successful, below is the output:") fmt.Println(updateTableOutput) }
Python
def update_table(self): print("Updating table") retention_properties = { 'MemoryStoreRetentionPeriodInHours': Constant.HT_TTL_HOURS, 'MagneticStoreRetentionPeriodInDays': Constant.CT_TTL_DAYS } try: self.client.update_table(DatabaseName=Constant.DATABASE_NAME, TableName=Constant.TABLE_NAME, RetentionProperties=retention_properties) print("Table updated.") except Exception as err: print("Update table failed:", err)
Node.js

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

另请参阅类 UpdateTableCommandUpdateTable

import { TimestreamWriteClient, UpdateTableCommand } from "@aws-sdk/client-timestream-write"; const writeClient = new TimestreamWriteClient({ region: "us-east-1" }); const params = { DatabaseName: "testDbFromNode", TableName: "testTableFromNode", RetentionProperties: { MemoryStoreRetentionPeriodInHours: 24, MagneticStoreRetentionPeriodInDays: 180 } }; const command = new UpdateTableCommand(params); try { const data = await writeClient.send(command); console.log("Table updated") } catch (error) { console.log("Error updating table. ", error); }

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

async function updateTable() { console.log("Updating 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.updateTable(params).promise(); await promise.then( (data) => { console.log("Table updated") }, (err) => { console.log("Error updating table. ", err); throw err; } ); }
.NET
public async Task UpdateTable() { Console.WriteLine("Updating Table"); try { var updateTableRequest = new UpdateTableRequest { DatabaseName = Constants.DATABASE_NAME, TableName = Constants.TABLE_NAME, RetentionProperties = new RetentionProperties { MagneticStoreRetentionPeriodInDays = Constants.CT_TTL_DAYS, MemoryStoreRetentionPeriodInHours = Constants.HT_TTL_HOURS } }; UpdateTableResponse response = await writeClient.UpdateTableAsync(updateTableRequest); Console.WriteLine($"Table {Constants.TABLE_NAME} updated"); } catch (ResourceNotFoundException) { Console.WriteLine("Table does not exist."); } catch (Exception e) { Console.WriteLine("Update table failed:" + e.ToString()); } }