import("context""fmt""io""cloud.google.com/go/bigtable""google.golang.org/api/option")funcconfigureConnectionPool(wio.Writer,projectID,instanceIDstring)error{// projectID := "my-project-id"// instanceID := "my-instance-id"ctx:=context.Background()// Set up Bigtable data operations client.poolSize:=10client,err:=bigtable.NewClient(ctx,projectID,instanceID,option.WithGRPCConnectionPool(poolSize))deferclient.Close()iferr!=nil{returnfmt.Errorf("bigtable.NewClient: %w",err)}fmt.Fprintf(w,"Connected with pool size of %d",poolSize)returnnil}
HBase
此示例仅适用于自动调整大小功能引入之前的客户端库版本(2.9.1 之前)。
import staticcom.google.cloud.bigtable.hbase.BigtableOptionsFactory.BIGTABLE_DATA_CHANNEL_COUNT_KEY;importcom.google.cloud.bigtable.hbase.BigtableConfiguration;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.client.Connection;publicclassConfigureConnectionPool{publicstaticvoidconfigureConnectionPool(StringprojectId,StringinstanceId){// String projectId = "my-project-id";// String instanceId = "my-instance-id";Configurationconfig=BigtableConfiguration.configure(projectId,instanceId);config.setInt(BIGTABLE_DATA_CHANNEL_COUNT_KEY,10);try(Connectionconnection=BigtableConfiguration.connect(config)){intpoolSize=connection.getConfiguration().getInt(BIGTABLE_DATA_CHANNEL_COUNT_KEY,0);System.out.println(String.format("Connected with pool size of %d",poolSize));}catch(Exceptione){System.out.println("Error during ConfigureConnectionPool: \n"+e.toString());}}}
Java
此示例仅适用于 2.23.0 之前的客户端库版本(自动调整大小功能在此版本中引入)。
importcom.google.api.gax.grpc.InstantiatingGrpcChannelProvider;importcom.google.cloud.bigtable.data.v2.BigtableDataClient;importcom.google.cloud.bigtable.data.v2.BigtableDataSettings;importcom.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings;importjava.io.IOException;publicclassConfigureConnectionPool{publicstaticvoidconfigureConnectionPool(StringprojectId,StringinstanceId){// String projectId = "my-project-id";// String instanceId = "my-instance-id";BigtableDataSettings.BuildersettingsBuilder=BigtableDataSettings.newBuilder().setProjectId(projectId).setInstanceId(instanceId);settingsBuilder.stubSettings().setTransportChannelProvider(EnhancedBigtableStubSettings.defaultGrpcTransportProviderBuilder().setPoolSize(10).build());BigtableDataSettingssettings=settingsBuilder.build();try(BigtableDataClientdataClient=BigtableDataClient.create(settings)){InstantiatingGrpcChannelProviderprovider=(InstantiatingGrpcChannelProvider)settings.getStubSettings().getTransportChannelProvider();intpoolSize=provider.toBuilder().getPoolSize();System.out.println(String.format("Connected with pool size of %d",poolSize));}catch(IOExceptione){System.out.println("Error during ConfigureConnectionPool: \n"+e.toString());}}}
C++
namespacecbt=::google::cloud::bigtable;namespacegc=::google::cloud;[](std::stringconst&project_id,std::stringconst&instance_id,std::stringconst&table_id){autoconstexprkPoolSize=10;autooptions=gc::Options{}.set<gc::GrpcNumChannelsOption>(kPoolSize);cbt::Tabletable(cbt::MakeDataConnection(options),cbt::TableResource(project_id,instance_id,table_id));std::cout << "Connected with channel pool size of " << kPoolSize << "\n";}
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-03-06。"],[[["The default connection pool configuration for Cloud Bigtable client libraries is generally sufficient, and changes are often unnecessary."],["Connection pools automatically resize in the Cloud Bigtable client library for Java version 2.23.0 or later, and the Cloud Bigtable HBase client for Java version 2.9.1 or later, removing the need for manual configuration."],["Determining the ideal connection pool size involves calculating the minimum and maximum number of connections based on client-side metrics like queries per second (QPS) and average latency, aiming for a range that accommodates traffic fluctuations."],["Client libraries for Go, Cloud Bigtable HBase for Java, Cloud Bigtable for Java, and C++ offer connection pooling, and the code provided demonstrates how to set the pool size in each."],["The optimal connection pool size should allow for between 10 and 50 concurrent outstanding requests per connection, because a connection can handle a maximum of 100 concurrent requests, allowing room for traffic variation."]]],[]]