当前位置: 动力学知识库 > 问答 > 编程问答 >

database - connection gwan with aerospike db in C

问题描述:

Hello.

First I'm sorry for my ita-english.

I want use gwan with aerospike but when run the servlet...problem.

I start with this example.c of aerospike. In file example.c I put gwan.h and this is the output ./gwan:

loading

hello.cs: to use .cs scripts, install C#..

hello.lua: to use .lua scripts, install Lua

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Linking example.c: undefined symbol: g_namespace

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To run G-WAN, you must fix the error(s) or remove this Servlet.

Inside example.c:

#include "gwan.h"

#include <stdbool.h>

#include <stddef.h>

#include <stdint.h>

#include <stdlib.h>

#include <aerospike/aerospike.h>

#include <aerospike/aerospike_key.h>

#include <aerospike/aerospike_query.h>

#include <aerospike/as_error.h>

#include <aerospike/as_key.h>

#include <aerospike/as_query.h>

#include <aerospike/as_record.h>

#include <aerospike/as_status.h>

#include <aerospike/as_val.h>

#include "example_utils.h"

const char TEST_INDEX_NAME[] = "test-bin-index";

bool query_cb(const as_val* p_val, void* udata);

void cleanup(aerospike* p_as);

bool insert_records(aerospike* p_as);

int

main(int argc, char* argv[])

{

if (! example_get_opts(argc, argv, EXAMPLE_MULTI_KEY_OPTS)) {

exit(-1);

}

aerospike as;

example_connect_to_aerospike(&as);

example_remove_test_records(&as);

example_remove_index(&as, TEST_INDEX_NAME);

if (! example_create_integer_index(&as, "test-bin", TEST_INDEX_NAME))

{

cleanup(&as);

exit(-1);

}

if (! insert_records(&as)) {

cleanup(&as);

exit(-1);

}

if (! example_read_test_records(&as)) {

cleanup(&as);

exit(-1);

}

as_error err;

as_query query;

as_query_init(&query, g_namespace, g_set);

as_query_where_inita(&query, 1);

as_query_where(&query, "test-bin", as_integer_equals(7));

LOG("executing query: where test-bin = 7");

if (aerospike_query_foreach(&as, &err, NULL, &query, query_cb, NULL)

!= AEROSPIKE_OK) {

LOG("aerospike_query_foreach() returned %d - %s", err.code,

err.message);

as_query_destroy(&query);

cleanup(&as);

exit(-1);

}

LOG("query executed");

as_query_destroy(&query);

cleanup(&as);

LOG("simple query example successfully completed");

return 0;

}

bool

query_cb(const as_val* p_val, void* udata)

{

if (! p_val) {

LOG("query callback returned null - query is complete");

return true;

}

as_record* p_rec = as_record_fromval(p_val);

if (! p_rec) {

LOG("query callback returned non-as_record object");

return true;

}

LOG("query callback returned record:");

example_dump_record(p_rec);

return true;

}

void

cleanup(aerospike* p_as)

{

example_remove_test_records(p_as);

example_remove_index(p_as, TEST_INDEX_NAME);

example_cleanup(p_as);

}

bool

insert_records(aerospike* p_as)

{

set

as_record rec;

as_record_inita(&rec, 1);

for (uint32_t i = 0; i < g_n_keys; i++) {

as_error err;

as_key key;

as_key_init_int64(&key, g_namespace, g_set, (int64_t)i);

as_record_set_int64(&rec, "test-bin", (int64_t)i);

if (aerospike_key_put(p_as, &err, NULL, &key, &rec) != AEROSPIKE_OK) {

LOG("aerospike_key_put() returned %d - %s", err.code, err.message);

return false;

}

}

LOG("insert succeeded");

return true;

}

how can connect aerospike with gwan?

Thank you

网友答案:

You need to #pragma link your aerospike library, and make sure all your required header files are in the right place. See G-WAN FAQ or read example code in the G-WAN tarball.

Also, in G-WAN the return code of the main function will be used as HTTP response code, so avoid return -1;.

网友答案:

undefined symbol: g_namespace

the error message is clear. As long as this variable is undefined your C servlet won't compile.

I don't know your library but this variable is probably defined in a library include file - or must be defined by the end user (you). Check the library documentation.

网友答案:

Detailed steps to run Aerospike C-client example with G-WAN,

  1. Download and extract G-WAN server tar on your system
  2. You can start the G-WAN server using ./gwan script present in extracted folder, e.g. ./gwan_linux64-bit/
  3. Get Aerospike C-client from https://github.com/aerospike/aerospike-client-c, and install on your system
  4. Copy example.c to ./gwan_linux64-bit/0.0.0.0_8080/#0.0.0.0/csp/
  5. Make following changes to example.c,

    • Add following #pragma directive,
      #pragma include "/home/user/aerospike-client-c/examples/utils/src/include/"
      This will help search example_utils.h, which is necessary for all the example scripts in C-client.
    • Add following #pragma directive,
      #pragma link "/home/user/aerospike-client-c/examples/utils/src/main/example_utils.c"
      We shall have to link example_utils.c, as it has definitions of all util functions used in example scripts.
  6. Make changes to the return values. Retun proper HTTP error codes.

  7. Now, you are good to go. Run ./gwan server and access your webservice through browser, http://127.0.0.1:8080/?example.c

分享给朋友:
您可能感兴趣的文章:
随机阅读: