Aerospike C client manual --- key-value storage-read records, aerospike Client

Source: Internet
Author: User
Tags aerospike

Aerospike C client manual --- key-value storage-read records, aerospike Client
Read records

The Aerospike C client AP provides four operations to read a record from the database:

  • aerospike_key_get()-Read all bin data of a record.
  • aerospike_key_select()-Read the specified bin data of a record.
  • aerospike_key_exists()-Check the existence of a record.
  • aerospike_key_operate()-Perform operations on a record, including reading operations on the specified bin.

This chapter mainly discusses the first three operations. The aerospike_key_operate () operations will be discussed in [record advanced operations.

The following code is referenced in the example directory examples/basic_examples/get, which is provided by the Aerospike C Client installation package.

Read the [establish a connection] section to understand how to establish a connection with a cluster.

KEY)

When reading a record, the database uses a key to identify the record. Next we will create a key for the example. The key is the string "test-key". The data namespace is named "test" and the set name is "test-set ". Other data types can also be used as keys, such as integer or Binary Large Object blocks (blob ).

as_key key;as_key_init_str(&key, "test", "test-set", "test-key");
Read all bin data of a record

Use the aerospike_key_get () operation to read all bin data of a record. This operation fills the record objects in parameters with bin data as much as possible. The record object (Row 3) is initialized as a NULL pointer ). When the record object is a null pointer, the read operation is allowed to allocate enough bin object space for the record on the heap to accommodate all bin data returned from the database.

as_record* p_rec = NULL;if (aerospike_key_get(&as, &err, NULL, &key, &p_rec) != AEROSPIKE_OK) {    fprintf(stderr, "err(%d) %s at [%s:%d]\n", err.code, err.message, err.file, err.line);}as_record_destroy(rec);

When you no longer need a record object, you must use as_record_destroy () to release the occupied resources.

Reads the specified bin data of a record.

If you know exactly the name of the bin to be read from the database, it is best to specify the bin when reading, rather than getting the entire record. To specify the bin to be retrieved from the server, you must first create a string array ending with a NULL pointer (NULL), where each string is a bin name, and then call aerospike_key_select () read data.

The following code uses a string array containing two bin names to read bin data named "test-bin-1" and "test-bin-3" from the database. The array ends with a NULL pointer, indicating that the read operation is not followed by a bin name.

as_record* p_rec = NULL;static const char* bins_1_3[] = { "test-bin-1", "test-bin-3", NULL };if (aerospike_key_select(&as, &err, NULL, &key, bins_1_3, &p_rec) != AEROSPIKE_OK) {    fprintf(stderr, "err(%d) %s at [%s:%d]\n", err.code, err.message, err.file, err.line);}as_record_destroy(rec);

When you no longer need a record object, you must use as_record_destroy () to release the occupied resources.

Traverse the bin of the record

See [traverse the bin of a record] in the Best Practices Section ].

Check the existence of records

It is often very simple to know whether a record exists in the database. The aerospike_key_exists () operation uses the record metadata to fill in the record object in the parameter, such as generation number and survival time, to check whether the record exists.

as_record* p_rec = NULL;if (aerospike_key_exists(&as, &err, NULL, &key, &p_rec) == AEROSPIKE_ERR_RECORD_NOT_FOUND) {    fprintf(stderr, "record not found");}
Read transaction timeout Control

When the application needs to respond to the caller within the specified time, you can set the transaction timeout time for reading the call and change write_policy.timeout to the corresponding number of milliseconds.


Link: http://www.aerospike.com/docs/client/c/usage/kvs/read.htmlTranslated by: Q  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.