Aerospike C client manual --- establish a connection to the aerospike Client
C client function library
Establish a connection
The Aerospike object describes a cluster. To connect to a cluster, configure an aerospike object.
Configure the client
To configure the client, you must provide an as_config object that is initialized and filled with configuration information.
First, use as_config_init () to initialize as_config by default:
as_config config;as_config_init(&config);
After the as_config Initialization is complete, fill it with application-specific settings.
At least one server address must be configured for the client as seed. The client will try to connect to each seed host until the connection is successful.
config.hosts[0] = { .addr = "127.0.0.1", .port = 3000 };
Initialize the client
To connect to the cluster, first use the as_config configuration object created earlier to initialize an aerospike client object.
aerospike as;aerospike_init(&as, &config);
After the aerospike_init () function is successfully executed, the aerospike client object after Initialization is completed is returned. Otherwise, NULL is returned ).
Establish a connection
Now, use the initialized aerospike client object to connect to the cluster. The aerospike_connect () function requires an as_error object to return error information:
as_error err;if (aerospike_connect(&as, &err) != AEROSPIKE_OK) { fprintf(stderr, "err(%d) %s at [%s:%d]\n", err.code, err.message, err.file, err.line);}
The return code of the function is consistent with the value of err. code. If the return code is not AEROSPIKE_ OK, it indicates that an error has occurred. You can obtain more information by checking the err Object.
An aerospike client stores the cluster status and maintains a connection pool with the cluster. The same aerospike client object can be reused by applications to complete database operations on a given cluster.
To connect an application to multiple Aerospike clusters, the application must create multiple aerospike client objects, each of which is connected to different clusters.
Close connection
When you no longer need a client to connect to a cluster, use aerospike_close () to close the connection:
as_error err;if (aerospike_close(&as, &err) != AEROSPIKE_OK) { fprintf(stderr, "err(%d) %s at [%s:%d]\n", err.code, err.message, err.file, err.line);}
Clear
When the client no longer needs it, use aerospike_destroy () to destroy the client object and release its occupied resources:
aerospike_destroy(&as);
Link: http://www.aerospike.com/docs/client/c/usage/connect/
Translated by: Q