写SDK客户端 - Amazon Timestream

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

写SDK客户端

您可以使用以下代码片段为 Write 创建 Timestream 客户端。SDK写入SDK用于执行CRUD操作并将您的时间序列数据插入时间流。

注意

这些代码片段基于上的完整示例应用程序。GitHub有关如何开始使用示例应用程序的更多信息,请参阅示例应用程序

Java
private static AmazonTimestreamWrite buildWriteClient() { final ClientConfiguration clientConfiguration = new ClientConfiguration() .withMaxConnections(5000) .withRequestTimeout(20 * 1000) .withMaxErrorRetry(10); return AmazonTimestreamWriteClientBuilder .standard() .withRegion("us-east-1") .withClientConfiguration(clientConfiguration) .build(); }
Java v2
private static TimestreamWriteClient buildWriteClient() { ApacheHttpClient.Builder httpClientBuilder = ApacheHttpClient.builder(); httpClientBuilder.maxConnections(5000); RetryPolicy.Builder retryPolicy = RetryPolicy.builder(); retryPolicy.numRetries(10); ClientOverrideConfiguration.Builder overrideConfig = ClientOverrideConfiguration.builder(); overrideConfig.apiCallAttemptTimeout(Duration.ofSeconds(20)); overrideConfig.retryPolicy(retryPolicy.build()); return TimestreamWriteClient.builder() .httpClientBuilder(httpClientBuilder) .overrideConfiguration(overrideConfig.build()) .region(Region.US_EAST_1) .build(); }
Go
tr := &http.Transport{ ResponseHeaderTimeout: 20 * time.Second, // Using DefaultTransport values for other parameters: https://golang.org/pkg/net/http/#RoundTripper Proxy: http.ProxyFromEnvironment, DialContext: (&net.Dialer{ KeepAlive: 30 * time.Second, DualStack: true, Timeout: 30 * time.Second, }).DialContext, MaxIdleConns: 100, IdleConnTimeout: 90 * time.Second, TLSHandshakeTimeout: 10 * time.Second, ExpectContinueTimeout: 1 * time.Second, } // So client makes HTTP/2 requests http2.ConfigureTransport(tr) sess, err := session.NewSession(&aws.Config{ Region: aws.String("us-east-1"), MaxRetries: aws.Int(10), HTTPClient: &http.Client{ Transport: tr }}) writeSvc := timestreamwrite.New(sess)
Python
write_client = session.client('timestream-write', config=Config(read_timeout=20, max_pool_connections = 5000, retries={'max_attempts': 10})) 
Node.js

以下代码段用 AWS SDK于 JavaScript v3。有关如何安装客户端和用法的更多信息,请参阅 Timestream Write Client-f AWS SDK or JavaScript v3

此处显示了其他命令导入。创建客户端不需要CreateDatabaseCommand导入。

import { TimestreamWriteClient, CreateDatabaseCommand } from "@aws-sdk/client-timestream-write"; const writeClient = new TimestreamWriteClient({ region: "us-east-1" });

以下代码段使用了 for AWS SDK JavaScript V2 样式。它基于 Node.js 示例 Amazon Timestream 中的示例 LiveAnalytics 应用程序,供其上使用。 GitHub

var https = require('https'); var agent = new https.Agent({ maxSockets: 5000 }); writeClient = new AWS.TimestreamWrite({ maxRetries: 10, httpOptions: { timeout: 20000, agent: agent } });
.NET
var writeClientConfig = new AmazonTimestreamWriteConfig { RegionEndpoint = RegionEndpoint.USEast1, Timeout = TimeSpan.FromSeconds(20), MaxErrorRetry = 10 }; var writeClient = new AmazonTimestreamWriteClient(writeClientConfig);

我们建议您使用以下配置。

  • 将SDK重试次数设置为。10

  • 使用 SDK DEFAULT_BACKOFF_STRATEGY

  • 设置RequestTimeout20秒。

  • 将最大连接数设置为5000或更高。