Aerospike C用戶端手冊———建立串連,aerospike用戶端

來源:互聯網
上載者:User

Aerospike C用戶端手冊———建立串連,aerospike用戶端


C用戶端函數庫

建立串連

Aerospike對象描述一個叢集。串連到一個叢集,第一步需要配置一個aerospike對象。

配置用戶端

要配置用戶端,應提供一個初始化且填充了配置資訊的as_config對象。

首先,使用as_config_init()以預設值初始化as_config:

as_config config;as_config_init(&config);

初始化as_config完成後,使用特定於應用的設定來填充它。

最低限度,至少需要配置一個伺服器位址給用戶端做seed。用戶端將嘗試串連各個seed主機,直到成功串連上。

config.hosts[0] = { .addr = "127.0.0.1", .port = 3000 };
初始化用戶端

要串連叢集,首先使用前面建立的as_config設定物件來初始化一個aerospike用戶端對象。

aerospike as;aerospike_init(&as, &config);

aerospike_init()函數執行成功後,返回完成初始化完成的aerospike用戶端對象,否則返回空(NULL)。

建立串連

現在,使用初始化完成的aerospike用戶端對象,串連到叢集。 aerospike_connect()函數需要一個as_error對象來返回出錯資訊:

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);}

函數返回碼與err.code的值相一致。若返回碼不是AEROSPIKE_OK,則表明發生了錯誤,通過檢查err對象可獲得更多資訊。

一個aerospike用戶端對象內部儲存叢集狀態並維護與叢集間的串連池。同一個aerospike用戶端對象可被應用重用,完成給定叢集上的資料庫操作。

若應用需要串連到多個Aerospike叢集,應用必須建立多個aerospike用戶端對象,每個串連到不同叢集。

關閉串連

當不再需要用戶端串連到一個叢集時,使用aerospike_close()關閉串連:

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);}
清理

當用戶端不再需要時,通過aerospike_destroy()銷毀用戶端對象並且釋放其所佔資源:

aerospike_destroy(&as);

原文連結: http://www.aerospike.com/docs/client/c/usage/connect/ 譯       者:歪脖大肚子Q  

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.