与 AWS SDK CreateGraph 配合使用 - AWS SDK 代码示例

文档 AWS SDK 示例 GitHub 存储库中还有更多 S AWS DK 示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

与 AWS SDK CreateGraph 配合使用

以下代码示例演示如何使用 CreateGraph

Java
适用于 Java 的 SDK 2.x
注意

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/** * Executes the process of creating a new Neptune graph. * * @param client the Neptune graph client used to interact with the Neptune service * @param graphName the name of the graph to be created * @throws NeptuneGraphException if an error occurs while creating the graph */ public static void executeCreateGraph(NeptuneGraphClient client, String graphName) { try { // Create the graph request CreateGraphRequest request = CreateGraphRequest.builder() .graphName(graphName) .provisionedMemory(16) .build(); // Create the graph CreateGraphResponse response = client.createGraph(request); // Extract the graph name and ARN String createdGraphName = response.name(); String graphArn = response.arn(); String graphEndpoint = response.endpoint(); System.out.println("Graph created successfully!"); System.out.println("Graph Name: " + createdGraphName); System.out.println("Graph ARN: " + graphArn); System.out.println("Graph Endpoint: " +graphEndpoint ); } catch (NeptuneGraphException e) { System.err.println("Failed to create graph: " + e.awsErrorDetails().errorMessage()); } finally { client.close(); } }
  • 有关 API 的详细信息,请参阅 AWS SDK for Java 2.x API 参考CreateGraph中的。

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

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整示例,了解如何进行设置和运行。

""" Running this example. ---------------------------------------------------------------------------------- VPC Networking Requirement: ---------------------------------------------------------------------------------- Amazon Neptune must be accessed from **within the same VPC** as the Neptune cluster. It does not expose a public endpoint, so this code must be executed from: - An **AWS Lambda function** configured to run inside the same VPC - An **EC2 instance** or **ECS task** running in the same VPC - A connected environment such as a **VPN**, **AWS Direct Connect**, or a **peered VPC** """ GRAPH_NAME = "sample-analytics-graph" def main(): config = Config(retries={"total_max_attempts": 1, "mode": "standard"}, read_timeout=None) client = boto3.client("neptune-graph", config=config) execute_create_graph(client, GRAPH_NAME) def execute_create_graph(client, graph_name): try: print("Creating Neptune graph...") response = client.create_graph( graphName=graph_name, provisionedMemory = 16 ) created_graph_name = response.get("name") graph_arn = response.get("arn") graph_endpoint = response.get("endpoint") print("Graph created successfully!") print(f"Graph Name: {created_graph_name}") print(f"Graph ARN: {graph_arn}") print(f"Graph Endpoint: {graph_endpoint}") except ClientError as e: print(f"Failed to create graph: {e.response['Error']['Message']}") except BotoCoreError as e: print(f"Failed to create graph: {str(e)}") except Exception as e: print(f"Unexpected error: {str(e)}") if __name__ == "__main__": main()
  • 有关 API 的详细信息,请参阅适用CreateGraphPython 的AWS SDK (Boto3) API 参考