使用 AWS SDK 在 AWS Glue Data Catalog 中从数据库获取表 - AWS Glue

使用 AWS SDK 在 AWS Glue Data Catalog 中从数据库获取表

以下代码示例显示如何在 AWS Glue Data Catalog 中从数据库获取表。

操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:

.NET
AWS SDK for .NET
注意

在 GitHub 上查看更多内容。在 AWS 代码示例存储库 中查找完整示例,了解如何进行设置和运行。

/// <summary> /// Get a list of tables for an AWS Glue database. /// </summary> /// <param name="dbName">The name of the database.</param> /// <returns>A list of Table objects.</returns> public async Task<List<Table>> GetTablesAsync(string dbName) { var request = new GetTablesRequest { DatabaseName = dbName }; var tables = new List<Table>(); // Get a paginator for listing the tables. var tablePaginator = _amazonGlue.Paginators.GetTables(request); await foreach (var response in tablePaginator.Responses) { tables.AddRange(response.TableList); } return tables; }
  • 有关 API 详细信息,请参阅 AWS SDK for .NET API 参考中的 GetTables

C++
适用于 C++ 的 SDK
注意

在 GitHub 上查看更多内容。在 AWS 代码示例存储库 中查找完整示例,了解如何进行设置和运行。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region in which the bucket was created (overrides config file). // clientConfig.region = "us-east-1"; Aws::Glue::GlueClient client(clientConfig); Aws::Glue::Model::GetTablesRequest request; request.SetDatabaseName(CRAWLER_DATABASE_NAME); Aws::Glue::Model::GetTablesOutcome outcome = client.GetTables(request); if (outcome.IsSuccess()) { const std::vector<Aws::Glue::Model::Table> &tables = outcome.GetResult().GetTableList(); std::cout << "The database contains " << tables.size() << (tables.size() == 1 ? " table." : "tables.") << std::endl; std::cout << "Here is a list of the tables in the database."; for (size_t index = 0; index < tables.size(); ++index) { std::cout << " " << index + 1 << ": " << tables[index].GetName() << std::endl; } if (!tables.empty()) { int tableIndex = askQuestionForIntRange( "Enter an index to display the database detail ", 1, static_cast<int>(tables.size())); std::cout << tables[tableIndex - 1].Jsonize().View().WriteReadable() << std::endl; } } else { std::cerr << "Error getting the tables. " << outcome.GetError().GetMessage() << std::endl; deleteAssets(CRAWLER_NAME, CRAWLER_DATABASE_NAME, "", bucketName, clientConfig); return false; }
  • 有关 API 详细信息,请参阅 AWS SDK for C++ API 参考中的 GetTables

Java
SDK for Java 2.x
注意

在 GitHub 上查看更多内容。在 AWS 代码示例存储库 中查找完整示例,了解如何进行设置和运行。

public static void getGlueTable(GlueClient glueClient, String dbName, String tableName ) { try { GetTableRequest tableRequest = GetTableRequest.builder() .databaseName(dbName) .name(tableName) .build(); GetTableResponse tableResponse = glueClient.getTable(tableRequest); Instant createDate = tableResponse.table().createTime(); // Convert the Instant to readable date. DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.SHORT ) .withLocale( Locale.US) .withZone( ZoneId.systemDefault() ); formatter.format( createDate ); System.out.println("The create date of the table is " + createDate ); } catch (GlueException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • 有关 API 详细信息,请参阅 AWS SDK for Java 2.x API 参考中的 GetTables

JavaScript
SDK for JavaScript (v3)
注意

在 GitHub 上查看更多内容。在 AWS 代码示例存储库 中查找完整示例,了解如何进行设置和运行。

const getTables = (databaseName) => { const client = new GlueClient({ region: DEFAULT_REGION }); const command = new GetTablesCommand({ DatabaseName: databaseName, }); return client.send(command); };
  • 有关 API 详细信息,请参阅 AWS SDK for JavaScript API 参考中的 GetTables

PHP
SDK for PHP
注意

在 GitHub 上查看更多内容。在 AWS 代码示例存储库 中查找完整示例,了解如何进行设置和运行。

$databaseName = "doc-example-database-$uniqid"; $tables = $glueService->getTables($databaseName); public function getTables($databaseName): Result { return $this->glueClient->getTables([ 'DatabaseName' => $databaseName, ]); }
  • 有关 API 详细信息,请参阅 AWS SDK for PHP API 参考中的 GetTables

Python
适用于 Python (Boto3) 的 SDK
注意

在 GitHub 上查看更多内容。在 AWS 代码示例存储库 中查找完整示例,了解如何进行设置和运行。

class GlueWrapper: """Encapsulates AWS Glue actions.""" def __init__(self, glue_client): """ :param glue_client: A Boto3 Glue client. """ self.glue_client = glue_client def get_tables(self, db_name): """ Gets a list of tables in a Data Catalog database. :param db_name: The name of the database to query. :return: The list of tables in the database. """ try: response = self.glue_client.get_tables(DatabaseName=db_name) except ClientError as err: logger.error( "Couldn't get tables %s. Here's why: %s: %s", db_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return response["TableList"]
  • 有关 API 详细信息,请参阅适用于 Python(Boto3)的 AWS SDK API 参考中的 GetTables

Ruby
SDK for Ruby
注意

在 GitHub 上查看更多内容。在 AWS 代码示例存储库 中查找完整示例,了解如何进行设置和运行。

# The `GlueWrapper` class serves as a wrapper around the AWS Glue API, providing a simplified interface for common operations. # It encapsulates the functionality of the AWS SDK for Glue and provides methods for interacting with Glue crawlers, databases, tables, jobs, and S3 resources. # The class initializes with a Glue client and a logger, allowing it to make API calls and log any errors or informational messages. class GlueWrapper def initialize(glue_client, logger) @glue_client = glue_client @logger = logger end # Retrieves a list of tables in the specified database. # # @param db_name [String] The name of the database to retrieve tables from. # @return [Array<Aws::Glue::Types::Table>] def get_tables(db_name) response = @glue_client.get_tables(database_name: db_name) response.table_list rescue Aws::Glue::Errors::GlueException => e @logger.error("Glue could not get tables #{db_name}: \n#{e.message}") raise end
  • 有关 API 详细信息,请参阅 AWS SDK for Ruby API 参考中的 GetTables

Rust
SDK for Rust
注意

本文档适用于预览版中的软件开发工具包。软件开发工具包可能随时发生变化,不应在生产环境中使用。

注意

在 GitHub 上查看更多内容。在 AWS 代码示例存储库 中查找完整示例,了解如何进行设置和运行。

let tables = glue .get_tables() .database_name(self.database()) .send() .await .map_err(GlueMvpError::from_glue_sdk)?; let tables = tables.table_list();
  • 有关 API 详细信息,请参阅适用于 Rust 的 AWS SDK API 参考中的 GetTables

有关 AWS 软件开发工具包开发人员指南和代码示例的完整列表,请参阅 将此服务与 AWS SDK 结合使用。本主题还包括有关入门的信息以及有关先前的软件开发工具包版本的详细信息。