Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.
Liste des bases de données
Vous pouvez utiliser les extraits de code suivants pour répertorier vos bases de données.
Note
Ces extraits de code sont basés sur des exemples complets d'applications sur. GitHub
- 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 := ×treamwrite.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
-
L'extrait de code suivant est utilisé AWS SDK pour JavaScript la version 3. Pour plus d'informations sur l'installation du client et son utilisation, consultez Timestream Write Client - AWS SDK for JavaScript v3.
Voir également Classe ListDatabasesCommand et 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); } }
L'extrait suivant utilise le style AWS SDK for JavaScript V2. Il est basé sur l'exemple d'application disponible sur Node.js, un exemple d'Amazon Timestream LiveAnalytics
pour une application sur. 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()); } }