This topic describes how to create a Go client instance. For more information about the Go client, visit GitHub wiki.
Note
We recommend that you use the latest version of the Go client. For more information about the latest version of the Go client, visit the Go client repository on GitHub.
Create a table in OceanBase Database
You can log on to OceanBase Database by using OBClient or a MySQL client. For more information, see Connect to an OceanBase tenant by using OBClient and Connect to OceanBase Database by using a MySQL client.
Execute the CREATE TABLE statement to create a table:
CREATE TABLE IF NOT EXISTS `test` (
`c1` bigint(20) NOT NULL,
`c2` bigint(20) NOT NULL,
PRIMARY KEY (`c1`)
) PARTITION BY KEY(`c1`) PARTITIONS 10;
Introduce the obkv-table-client-go dependency
Run the following command on the local Go repository to obtain the obkv-table-client-go dependency of the latest version or a specified version:
go get github.com/oceanbase/obkv-table-client-go
Connect the Go client to an OBServer node
You can connect the Go client to an OBServer node in either of the following modes:
Direct connection mode: use a config server
OceanBase Database Proxy (ODP) mode
You can create a Go client from code or a .toml file on an OBServer node.
Direct connection mode
The syntax is as follows:
func NewClient(
configUrl string,
fullUserName string,
password string,
sysUserName string,
sysPassWord string,
cliConfig *config.ClientConfig) (Client, error) {
Input parameters:
configUrl: the URL for obtaining the RootServer list from the config server.fullUserName: the full username, in the format ofuserName@tenantName#clusterName.passWord: the password of the user specified byuserNameinfullUserNamefor accessing OceanBase Database.sysUserName: the username of the user in the sys tenant. Only users in the sys tenant have privileges to access the routing table.sysPassWord: the password of the user in the sys tenant.cliConfig: the client configurations. For more information, see config.ClientConfig.
Response parameters:
Client: the client API, which containsinsert,get,update, and other methods.error: the error message.
ODP mode
The syntax is as follows:
func NewOdpClient(
fullUserName string,
passWord string,
odpIP string,
odpRpcPort int,
database string,
cliConfig *config.ClientConfig) (Client, error)
Input parameters:
fullUserName: the full username, in the format ofuserName@tenantName#clusterName.passWord: the password of the user specified byuserNameinfullUserNamefor accessing OceanBase Database.odpIP: the IP address of ODP.odpRpcPort: the RPC port number of ODP.database: the name of the database to connect to.cliConfig: the client configurations. For more information, see config.ClientConfig.
Response parameters:
Client: the client API, which containsinsert,get,update, and other methods.error: the error message.
.toml file configurations
The obkv-table-default.toml file is stored in the /configurations directory.
You can choose to specify the parameters in the Direct mode or ODP mode section in the .toml file. For more information about other parameters, see config.ClientConfig.
The syntax is as follows:
func NewClientWithTomlConfig(configFilePath string) (Client, error)
Parameters:
configFilePath: the path of the.tomlfile.
Return values:
Client: the client API, which containsinsert,get,update, and other methods.error: the error message.
ClientConfig
The syntax is as follows:
type ClientConfig struct {
ConnPoolMaxConnSize int
ConnConnectTimeOut time.Duration
ConnLoginTimeout time.Duration
OperationTimeOut time.Duration
LogLevel log.Level
TableEntryRefreshLockTimeout time.Duration
TableEntryRefreshTryTimes int
TableEntryRefreshIntervalBase time.Duration
TableEntryRefreshIntervalCeiling time.Duration
MetadataRefreshInterval time.Duration
MetadataRefreshLockTimeout time.Duration
RsListLocalFileLocation string
RsListHttpGetTimeout time.Duration
RsListHttpGetRetryTimes int
RsListHttpGetRetryInterval time.Duration
EnableRerouting bool
}
The special parameters are described as follows:
ConnPoolMaxConnSize: the number of connections in the connection pool. The default value is1.ConnConnectTimeOut: the timeout period for a TCP connection. The default value is1s.ConnLoginTimeout: the timeout period for an authentication operation. The default value is1s.OperationTimeOut: the timeout period for a single request. The default value is10s.EnableRerouting: specifies whether to enable rerouting. By default, rerouting is disabled.