列出資料表 - Amazon Timestream

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

列出資料表

您可以使用下列程式碼片段來列出資料表。

注意

這些程式碼片段是以 上的完整範例應用程式為基礎GitHub。如需如何開始使用範例應用程式的詳細資訊,請參閱 範例應用程式

Java
public void listTables() { System.out.println("Listing tables"); ListTablesRequest request = new ListTablesRequest(); request.setDatabaseName(DATABASE_NAME); ListTablesResult result = amazonTimestreamWrite.listTables(request); printTables(result.getTables()); String nextToken = result.getNextToken(); while (nextToken != null && !nextToken.isEmpty()) { request.setNextToken(nextToken); ListTablesResult nextResult = amazonTimestreamWrite.listTables(request); printTables(nextResult.getTables()); nextToken = nextResult.getNextToken(); } } private void printTables(List<Table> tables) { for (Table table : tables) { System.out.println(table.getTableName()); } }
Java v2
public void listTables() { System.out.println("Listing tables"); ListTablesRequest request = ListTablesRequest.builder().databaseName(DATABASE_NAME).maxResults(2).build(); ListTablesIterable listTablesIterable = timestreamWriteClient.listTablesPaginator(request); for(ListTablesResponse listTablesResponse : listTablesIterable) { final List<Table> tables = listTablesResponse.tables(); tables.forEach(table -> System.out.println(table.tableName())); } }
Go
listTablesMaxResult := int64(15) listTablesInput := &timestreamwrite.ListTablesInput{ DatabaseName: aws.String(*databaseName), MaxResults: &listTablesMaxResult, } listTablesOutput, err := writeSvc.ListTables(listTablesInput) if err != nil { fmt.Println("Error:") fmt.Println(err) } else { fmt.Println("List tables is successful, below is the output:") fmt.Println(listTablesOutput) }
Python
def list_tables(self): print("Listing tables") try: result = self.client.list_tables(DatabaseName=Constant.DATABASE_NAME, MaxResults=5) self.__print_tables(result['Tables']) next_token = result.get('NextToken', None) while next_token: result = self.client.list_tables(DatabaseName=Constant.DATABASE_NAME, NextToken=next_token, MaxResults=5) self.__print_tables(result['Tables']) next_token = result.get('NextToken', None) except Exception as err: print("List tables failed:", err)
Node.js

下列程式碼片段用於 AWS SDK JavaScript v3。如需如何安裝用戶端和用量的詳細資訊,請參閱 Timestream Write Client - AWS SDK for JavaScript v3

另請參閱類別 ListTablesCommandListTables

import { TimestreamWriteClient, ListTablesCommand } from "@aws-sdk/client-timestream-write"; const writeClient = new TimestreamWriteClient({ region: "us-east-1" }); const params = { DatabaseName: "testDbFromNode", MaxResults: 15 }; const command = new ListTablesCommand(params); getTablesList(null); async function getTablesList(nextToken) { if (nextToken) { params.NextToken = nextToken; } try { const data = await writeClient.send(command); data.Tables.forEach(function (table) { console.log(table.TableName); }); if (data.NextToken) { return getTablesList(data.NextToken); } } catch (error) { console.log("Error while listing tables", error); } }

下列程式碼片段使用 AWS SDK適用於 JavaScript V2 樣式的 。它以 Node.js 範例應用程式為基礎,適用於 上的 LiveAnalytics 應用程式 GitHub

async function listTables() { console.log("Listing tables:"); const tables = await getTablesList(null); tables.forEach(function(table){ console.log(table.TableName); }); } function getTablesList(nextToken, tables = []) { var params = { DatabaseName: constants.DATABASE_NAME, MaxResults: 15 }; if(nextToken) { params.NextToken = nextToken; } return writeClient.listTables(params).promise() .then( (data) => { tables.push.apply(tables, data.Tables); if (data.NextToken) { return getTablesList(data.NextToken, tables); } else { return tables; } }, (err) => { console.log("Error while listing databases", err); }); }
.NET
public async Task ListTables() { Console.WriteLine("Listing Tables"); try { var listTablesRequest = new ListTablesRequest { MaxResults = 5, DatabaseName = Constants.DATABASE_NAME }; ListTablesResponse response = await writeClient.ListTablesAsync(listTablesRequest); PrintTables(response.Tables); string nextToken = response.NextToken; while (nextToken != null) { listTablesRequest.NextToken = nextToken; response = await writeClient.ListTablesAsync(listTablesRequest); PrintTables(response.Tables); nextToken = response.NextToken; } } catch (Exception e) { Console.WriteLine("List table failed:" + e.ToString()); } } private void PrintTables(List<Table> tables) { foreach (Table table in tables) Console.WriteLine($"Table: {table.TableName}"); }