本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
创建数据库
您可以使用以下代码片段来创建数据库。
- Java
-
public void createDatabase() { System.out.println("Creating database"); CreateDatabaseRequest request = new CreateDatabaseRequest(); request.setDatabaseName(DATABASE_NAME); try { amazonTimestreamWrite.createDatabase(request); System.out.println("Database [" + DATABASE_NAME + "] created successfully"); } catch (ConflictException e) { System.out.println("Database [" + DATABASE_NAME + "] exists. Skipping database creation"); } }
- Java v2
-
public void createDatabase() { System.out.println("Creating database"); CreateDatabaseRequest request = CreateDatabaseRequest.builder().databaseName(DATABASE_NAME).build(); try { timestreamWriteClient.createDatabase(request); System.out.println("Database [" + DATABASE_NAME + "] created successfully"); } catch (ConflictException e) { System.out.println("Database [" + DATABASE_NAME + "] exists. Skipping database creation"); } }
- Go
-
// Create database. createDatabaseInput := ×treamwrite.CreateDatabaseInput{ DatabaseName: aws.String(*databaseName), } _, err = writeSvc.CreateDatabase(createDatabaseInput) if err != nil { fmt.Println("Error:") fmt.Println(err) } else { fmt.Println("Database successfully created") } fmt.Println("Describing the database, hit enter to continue")
- Python
-
def create_database(self): print("Creating Database") try: self.client.create_database(DatabaseName=Constant.DATABASE_NAME) print("Database [%s] created successfully." % Constant.DATABASE_NAME) except self.client.exceptions.ConflictException: print("Database [%s] exists. Skipping database creation" % Constant.DATABASE_NAME) except Exception as err: print("Create database failed:", err)
- Node.js
-
以下代码段用 AWS SDK于 JavaScript v3。有关如何安装客户端和用法的更多信息,请参阅 Timestream Write Client-f AWS SDK or JavaScript v3。
另请参阅类 CreateDatabaseCommand和CreateDatabase。
import { TimestreamWriteClient, CreateDatabaseCommand } from "@aws-sdk/client-timestream-write"; const writeClient = new TimestreamWriteClient({ region: "us-east-1" }); const params = { DatabaseName: "testDbFromNode" }; const command = new CreateDatabaseCommand(params); try { const data = await writeClient.send(command); console.log(`Database ${data.Database.DatabaseName} created successfully`); } catch (error) { if (error.code === 'ConflictException') { console.log(`Database ${params.DatabaseName} already exists. Skipping creation.`); } else { console.log("Error creating database", error); } }
以下代码段使用了 for AWS SDK JavaScript V2 样式。它基于 Node.js 示例 Amazon Timestream 中的示例 LiveAnalytics 应用程序,供其
上使用。 GitHub async function createDatabase() { console.log("Creating Database"); const params = { DatabaseName: constants.DATABASE_NAME }; const promise = writeClient.createDatabase(params).promise(); await promise.then( (data) => { console.log(`Database ${data.Database.DatabaseName} created successfully`); }, (err) => { if (err.code === 'ConflictException') { console.log(`Database ${params.DatabaseName} already exists. Skipping creation.`); } else { console.log("Error creating database", err); } } ); }
- .NET
-
public async Task CreateDatabase() { Console.WriteLine("Creating Database"); try { var createDatabaseRequest = new CreateDatabaseRequest { DatabaseName = Constants.DATABASE_NAME }; CreateDatabaseResponse response = await writeClient.CreateDatabaseAsync(createDatabaseRequest); Console.WriteLine($"Database {Constants.DATABASE_NAME} created"); } catch (ConflictException) { Console.WriteLine("Database already exists."); } catch (Exception e) { Console.WriteLine("Create database failed:" + e.ToString()); } }
查询SDK客户端
描述数据库