Aerospike数据库实战(五) -- AerospikeC Client 开发

    xiaoxiao2021-03-26  5

    本人原创文章,转载请注明出处:http://blog.csdn.net/yanshu2012/article/details/54288856

    1. C Client 安装

    sudo yum install openssl-devel sudo yum install gcc gcc-c++ wget -S "http://www.aerospike.com/artifacts/aerospike-client-c/3.1.18/aerospike-client-c-3.1.18.el6.x86_64.tgz" tar -zxvf aerospike-client-c-3.1.18.el6.x86_64.tgz sudo rpm -i aerospike-client-c-devel-3.1.18-1.el6.x86_64.rpm

    2. 程序Makefile修改

    增加静态库引用: /usr/lib/libaerospike.a增加动态库引用: -lssl -lcrypto -lrt -ldl -lpthread -pthread增加头文件引用: /usr/include/aerospike

    3. key-Value Store API 开发代码实例

    3.1 Write Records

    #include <errno.h> #include <getopt.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <iostream> #include <aerospike/aerospike.h> #include <aerospike/aerospike_key.h> #include <aerospike/aerospike_udf.h> #include <aerospike/as_arraylist.h> #include <aerospike/as_bin.h> #include <aerospike/as_bytes.h> #include <aerospike/as_error.h> #include <aerospike/as_config.h> #include <aerospike/as_key.h> #include <aerospike/as_list.h> #include <aerospike/as_record.h> #include <aerospike/as_record_iterator.h> #include <aerospike/as_status.h> #include <aerospike/as_val.h> using namespace std; int main(int argc, char* argv[]) { as_config config; as_config_init(&config); config.conn_timeout_ms = 100; as_config_add_host(&config,"127.0.0.1",3000); aerospike as; aerospike_init(&as, &config); 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); } as_record rec; as_record_inita(&rec, 2); as_record_set_str(&rec, "test-bin-1", "1234"); as_record_set_str(&rec, "test-bin-2", "test-bin-2-data"); as_key key; as_key_init_str(&key, "cm", "test-set", "test-key"); if (aerospike_key_put(&as, &err, NULL, &key, &rec) != AEROSPIKE_OK) { fprintf(stderr, "err(%d) %s at [%s:%d]\n", err.code, err.message, err.file, err.line); } as_record_destroy(&rec); 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(&as); } 3.2 Read Records #include <errno.h> #include <getopt.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <iostream> #include <aerospike/aerospike.h> #include <aerospike/aerospike_key.h> #include <aerospike/aerospike_udf.h> #include <aerospike/as_arraylist.h> #include <aerospike/as_bin.h> #include <aerospike/as_bytes.h> #include <aerospike/as_error.h> #include <aerospike/as_config.h> #include <aerospike/as_key.h> #include <aerospike/as_list.h> #include <aerospike/as_record.h> #include <aerospike/as_record_iterator.h> #include <aerospike/as_status.h> #include <aerospike/as_val.h> using namespace std; int main(int argc, char* argv[]) { as_config config_get; as_config_init(&config_get); config_get.conn_timeout_ms = 100; as_config_add_host(&config_get,"210.73.210.142",3000); aerospike as_get; aerospike_init(&as_get, &config_get); as_error err_get; if (aerospike_connect(&as_get, &err_get) != AEROSPIKE_OK) { fprintf(stderr, "err(%d) %s at [%s:%d]\n", err_get.code, err_get.message, err_get.file, err_get.line); } as_record* rec_get; as_key key_get; as_key_init_str(&key_get, "cm", "test-set", "test-key"); static const char* bins_1_3[] = { "test-bin-1", "test-bin-2", NULL }; if (aerospike_key_select(&as_get, &err_get, NULL, &key_get, bins_1_3, &rec_get) != AEROSPIKE_OK) { fprintf(stderr, "err(%d) %s at [%s:%d]\n", err_get.code, err_get.message, err_get.file, err_get.line); } // counter for the bins int i = 0; // iterator over bins as_record_iterator it; as_record_iterator_init(&it, rec_get); while (as_record_iterator_has_next(&it)) { // we expect the bins to contain a list of [action,timestamp] as_bin *bin = as_record_iterator_next(&it); char *bin_name = as_bin_get_name(bin); as_val * value = (as_val*)as_bin_get_value(bin); as_string * str_value = as_string_fromval(value); if (str_value) { char * v= as_string_get(str_value); cout << " bin[" << i << "] name=" << bin_name << " value=" << v << "\n"; free(v); } else { cout << "Error: bin[" << i << "] name=" << bin_name << " has unexpected type " << as_bin_get_type(bin) << "\n"; } i++; } // release the iterator as_record_iterator_destroy(&it); //as_record_destroy(rec_get); if (aerospike_close(&as_get, &err_get) != AEROSPIKE_OK) { fprintf(stderr, "err(%d) %s at [%s:%d]\n", err_get.code, err_get.message, err_get.file, err_get.line); } aerospike_destroy(&as_get); }
    转载请注明原文地址: https://ju.6miu.com/read-500237.html

    最新回复(0)