List databases - Amazon Timestream

List databases

You can use the following code snippets to list your databases.

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 listDatabases() { System.out.println("Listing databases"); ListDatabasesRequest request = new ListDatabasesRequest(); ListDatabasesResult result = amazonTimestreamWrite.listDatabases(request); final List<Database> databases = result.getDatabases(); printDatabases(databases); String nextToken = result.getNextToken(); while (nextToken != null && !nextToken.isEmpty()) { request.setNextToken(nextToken); ListDatabasesResult nextResult = amazonTimestreamWrite.listDatabases(request); final List<Database> nextDatabases = nextResult.getDatabases(); printDatabases(nextDatabases); nextToken = nextResult.getNextToken(); } } private void printDatabases(List<Database> databases) { for (Database db : databases) { System.out.println(db.getDatabaseName()); } }
Java v2
public void listDatabases() { System.out.println("Listing databases"); ListDatabasesRequest request = ListDatabasesRequest.builder().maxResults(2).build(); ListDatabasesIterable listDatabasesIterable = timestreamWriteClient.listDatabasesPaginator(request); for(ListDatabasesResponse listDatabasesResponse : listDatabasesIterable) { final List<Database> databases = listDatabasesResponse.databases(); databases.forEach(database -> System.out.println(database.databaseName())); } }
Go
// List databases. listDatabasesMaxResult := int64(15) listDatabasesInput := &timestreamwrite.ListDatabasesInput{ MaxResults: &listDatabasesMaxResult, } listDatabasesOutput, err := writeSvc.ListDatabases(listDatabasesInput) if err != nil { fmt.Println("Error:") fmt.Println(err) } else { fmt.Println("List databases is successful, below is the output:") fmt.Println(listDatabasesOutput) }
Python
def list_databases(self): print("Listing databases") try: result = self.client.list_databases(MaxResults=5) self._print_databases(result['Databases']) next_token = result.get('NextToken', None) while next_token: result = self.client.list_databases(NextToken=next_token, MaxResults=5) self._print_databases(result['Databases']) next_token = result.get('NextToken', None) except Exception as err: print("List databases 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 ListDatabasesCommand and ListDatabases.

import { TimestreamWriteClient, ListDatabasesCommand } from "@aws-sdk/client-timestream-write"; const writeClient = new TimestreamWriteClient({ region: "us-east-1" }); const params = { MaxResults: 15 }; const command = new ListDatabasesCommand(params); getDatabasesList(null); async function getDatabasesList(nextToken) { if (nextToken) { params.NextToken = nextToken; } try { const data = await writeClient.send(command); data.Databases.forEach(function (database) { console.log(database.DatabaseName); }); if (data.NextToken) { return getDatabasesList(data.NextToken); } } catch (error) { console.log("Error while listing databases", 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 listDatabases() { console.log("Listing databases:"); const databases = await getDatabasesList(null); databases.forEach(function(database){ console.log(database.DatabaseName); }); } function getDatabasesList(nextToken, databases = []) { var params = { MaxResults: 15 }; if(nextToken) { params.NextToken = nextToken; } return writeClient.listDatabases(params).promise() .then( (data) => { databases.push.apply(databases, data.Databases); if (data.NextToken) { return getDatabasesList(data.NextToken, databases); } else { return databases; } }, (err) => { console.log("Error while listing databases", err); }); }
.NET
public async Task ListDatabases() { Console.WriteLine("Listing Databases"); try { var listDatabasesRequest = new ListDatabasesRequest { MaxResults = 5 }; ListDatabasesResponse response = await writeClient.ListDatabasesAsync(listDatabasesRequest); PrintDatabases(response.Databases); var nextToken = response.NextToken; while (nextToken != null) { listDatabasesRequest.NextToken = nextToken; response = await writeClient.ListDatabasesAsync(listDatabasesRequest); PrintDatabases(response.Databases); nextToken = response.NextToken; } } catch (Exception e) { Console.WriteLine("List database failed:" + e.ToString()); } }