描述数据库 - Amazon Timestream

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

描述数据库

您可以使用以下代码片段来获取有关新创建的数据库的属性的信息。

注意

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

Java
public void describeDatabase() { System.out.println("Describing database"); final DescribeDatabaseRequest describeDatabaseRequest = new DescribeDatabaseRequest(); describeDatabaseRequest.setDatabaseName(DATABASE_NAME); try { DescribeDatabaseResult result = amazonTimestreamWrite.describeDatabase(describeDatabaseRequest); final Database databaseRecord = result.getDatabase(); final String databaseId = databaseRecord.getArn(); System.out.println("Database " + DATABASE_NAME + " has id " + databaseId); } catch (final Exception e) { System.out.println("Database doesn't exist = " + e); throw e; } }
Java v2
public void describeDatabase() { System.out.println("Describing database"); final DescribeDatabaseRequest describeDatabaseRequest = DescribeDatabaseRequest.builder() .databaseName(DATABASE_NAME).build(); try { DescribeDatabaseResponse response = timestreamWriteClient.describeDatabase(describeDatabaseRequest); final Database databaseRecord = response.database(); final String databaseId = databaseRecord.arn(); System.out.println("Database " + DATABASE_NAME + " has id " + databaseId); } catch (final Exception e) { System.out.println("Database doesn't exist = " + e); throw e; } }
Go
describeDatabaseOutput, err := writeSvc.DescribeDatabase(describeDatabaseInput) if err != nil { fmt.Println("Error:") fmt.Println(err) } else { fmt.Println("Describe database is successful, below is the output:") fmt.Println(describeDatabaseOutput) }
Python
def describe_database(self): print("Describing database") try: result = self.client.describe_database(DatabaseName=Constant.DATABASE_NAME) print("Database [%s] has id [%s]" % (Constant.DATABASE_NAME, result['Database']['Arn'])) except self.client.exceptions.ResourceNotFoundException: print("Database doesn't exist") except Exception as err: print("Describe database failed:", err)
Node.js

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

另请参阅类 DescribeDatabaseCommandDescribeDatabase

import { TimestreamWriteClient, DescribeDatabaseCommand } from "@aws-sdk/client-timestream-write"; const writeClient = new TimestreamWriteClient({ region: "us-east-1" }); const params = { DatabaseName: "testDbFromNode" }; const command = new DescribeDatabaseCommand(params); try { const data = await writeClient.send(command); console.log(`Database ${data.Database.DatabaseName} has id ${data.Database.Arn}`); } catch (error) { if (error.code === 'ResourceNotFoundException') { console.log("Database doesn't exist."); } else { console.log("Describe database failed.", error); throw error; } }

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

async function describeDatabase () { console.log("Describing Database"); const params = { DatabaseName: constants.DATABASE_NAME }; const promise = writeClient.describeDatabase(params).promise(); await promise.then( (data) => { console.log(`Database ${data.Database.DatabaseName} has id ${data.Database.Arn}`); }, (err) => { if (err.code === 'ResourceNotFoundException') { console.log("Database doesn't exist."); } else { console.log("Describe database failed.", err); throw err; } } ); }
.NET
public async Task DescribeDatabase() { Console.WriteLine("Describing Database"); try { var describeDatabaseRequest = new DescribeDatabaseRequest { DatabaseName = Constants.DATABASE_NAME }; DescribeDatabaseResponse response = await writeClient.DescribeDatabaseAsync(describeDatabaseRequest); Console.WriteLine($"Database {Constants.DATABASE_NAME} has id:{response.Database.Arn}"); } catch (ResourceNotFoundException) { Console.WriteLine("Database does not exist."); } catch (Exception e) { Console.WriteLine("Describe database failed:" + e.ToString()); } }