大数据类型(LDT)是驻留于Aerospike服务器上、由应用通过UDF维护的复杂对象。与LDT关联的数据不会整个传送到客户端,除非客户端特别要求。正常使用情况下,客户端操作部分数据 — 单个对象或一组对象 — 通过发布的API。
请参见【LDT功能指南】以获得LDT的一般性知识。
aerospike_lstack_push()
– 压栈新对象。aerospike_lstack_pushall()
– 压栈一系列对象。aerospike_lstack_peek()
– 取栈顶N个元素。aerospike_lstack_filter()
– 扫描整个栈并应用一个谓词过滤器。aerospike_lstack_destroy()
– 删除整个栈(LDT Remove)。aerospike_lstack_get_capacity()
– 获取栈当前容量限制设置。aerospike_lstack_set_capacity()
– 设置栈最大容量。aerospike_lstack_size()
– 获取栈当前条目数。aerospike_lset_config()
– 获取栈配置参数。aerospike_lset_add()
– 增加对象到集合。aerospike_lset_addall()
– 增加一系列对象到集合。aerospike_lset_remove()
– 从集合中移除一个对象。aerospike_lset_exists()
– 测试一个对象在集合中是否存在。aerospike_lset_get()
– 从集合中获取一个对象。aerospike_lset_filter()
– 扫描整个集合并应用一个谓词过滤器。aerospike_lset_destroy()
– 删除整个集合 (LDT Remove)。aerospike_lset_size()
– 获取集合当前条目数。aerospike_lset_config()
– 获取集合配置参数。aerospike_lmap_add()
– 增加对象到映射。aerospike_lmap_addall()
– 增加一系列对象到映射。aerospike_lmap_remove()
– 从映射中移除一个对象。aerospike_lmap_get()
– 从映射中获取一个对象。aerospike_lmap_filter()
– 扫描整个映射,并应用一个谓词过滤器。aerospike_lmap_destroy()
– 删除整个映射(LDT Remove).。aerospike_lmap_size()
– 获取映射当前条目数。aerospike_lmap_config()
– 获取映射配置参数。aerospike_llist_add()
– 增加对象到列表。aerospike_llist_addall()
– 增加一系列对象到列表。aerospike_llist_remove()
– 从列表中移除一个对象。aerospike_llist_get()
– 从列表中获取一个对象。aerospike_llist_filter()
– 扫描整个列表,并应用一个谓词过滤器。aerospike_llist_destroy()
– 删除整个列表(LDT Remove)。aerospike_llist_size()
– 获取列表当前条目数。aerospike_llist_config()
– 获取列表配置参数。 这里有一个基本示例程序,演示大数据对象初步建立与一些基本操作:
#include <stdbool.h> #include <stddef.h> #include <stdint.h> #include <stdlib.h> #include <aerospike/aerospike.h> #include <aerospike/aerospike_key.h> #include <aerospike/aerospike_lset.h> #include <aerospike/as_arraylist.h> #include <aerospike/as_arraylist_iterator.h> #include <aerospike/as_error.h> #include <aerospike/as_ldt.h> #include <aerospike/as_list.h> #include <aerospike/as_record.h> #include <aerospike/as_status.h> #include "example_utils.h" int main(int argc, char* argv[]) { // Parse command line arguments. if (! example_get_opts(argc, argv, EXAMPLE_BASIC_OPTS)) { exit(-1); } // Connect to the aerospike database cluster. aerospike as; example_connect_to_aerospike(&as); // Start clean. example_remove_test_record(&as); as_ldt lset; // Create a lset bin to use. No need to destroy as_ldt if using // as_ldt_init() on stack object. if (! as_ldt_init(&lset, "mylset", AS_LDT_LSET, NULL)) { LOG("unable to initialize ldt"); exit(-1); } as_error err; // No need to destroy as_integer if using as_integer_init() on stack object. as_integer ival; as_integer_init(&ival, 12345); // Add an integer value to the set. if (aerospike_lset_add(&as, &err, NULL, &g_key, &lset, (const as_val*)&ival) != AEROSPIKE_OK) { LOG("first aerospike_set_add() returned %d - %s", err.code, err.message); exit(-1); } // No need to destroy as_string if using as_string_init() on stack object. as_string sval; as_string_init(&sval, "lset value", false); // Add a string value to the set. if (aerospike_lset_add(&as, &err, NULL, &g_key, &lset, (const as_val*)&sval) != AEROSPIKE_OK) { LOG("second aerospike_set_add() returned %d - %s", err.code, err.message); exit(-1); } LOG("2 values added to set"); uint32_t n_elements = 0; // See how many elements we have in the set now. if (aerospike_lset_size(&as, &err, NULL, &g_key, &lset, &n_elements) != AEROSPIKE_OK) { LOG("aerospike_lset_size() returned %d - %s", err.code, err.message); exit(-1); } if (n_elements != 2) { LOG("unexpected lset size %u", n_elements); exit(-1); } LOG("lset size confirmed to be %u", n_elements); as_ldt lset2; as_ldt_init(&lset2, "mylset", AS_LDT_LSET, NULL); as_list* p_list = NULL; // Get all the values back. if (aerospike_lset_filter(&as, &err, NULL, &g_key, &lset, NULL, NULL, &p_list) != AEROSPIKE_OK) { LOG("aerospike_lset_filter() returned %d - %s", err.code, err.message); as_list_destroy(p_list); exit(-1); } // See if the elements match what we expect. as_arraylist_iterator it; as_arraylist_iterator_init(&it, (const as_arraylist*)p_list); while (as_arraylist_iterator_has_next(&it)) { const as_val* p_val = as_arraylist_iterator_next(&it); LOG(" element - type = %d, value = %s ", as_val_type(p_val), as_val_tostring(p_val)); } as_list_destroy(p_list); p_list = NULL; // Add 3 more items into the set. By using as_arraylist_inita(), we won't // need to destroy the as_arraylist if we only use // as_arraylist_append_int64(). as_arraylist vals; as_arraylist_inita(&vals, 3); as_arraylist_append_int64(&vals, 1001); as_arraylist_append_int64(&vals, 2002); as_arraylist_append_int64(&vals, 3003); if (aerospike_lset_addall(&as, &err, NULL, &g_key, &lset, (const as_list*)&vals) != AEROSPIKE_OK) { LOG("aerospike_lset_addall() returned %d - %s", err.code, err.message); exit(-1); } LOG("3 more values added"); // Get and print all the values back again. if (aerospike_lset_filter(&as, &err, NULL, &g_key, &lset, NULL, NULL, &p_list) != AEROSPIKE_OK) { LOG("second aerospike_lset_filter() returned %d - %s", err.code, err.message); as_list_destroy(p_list); exit(-1); } as_arraylist_iterator_init(&it, (const as_arraylist*)p_list); while (as_arraylist_iterator_has_next(&it)) { const as_val* p_val = as_arraylist_iterator_next(&it); LOG(" element - type = %d, value = %s ", as_val_type(p_val), as_val_tostring(p_val)); } as_list_destroy(p_list); p_list = NULL; // No need to destroy as_boolean if using as_boolean_init() on stack object. as_boolean exists; as_boolean_init(&exists, false); // Check if a specific value exists. if (aerospike_lset_exists(&as, &err, NULL, &g_key, &lset2, (const as_val*)&ival, &exists) != AEROSPIKE_OK) { LOG("aerospike_lset_exists() returned %d - %s", err.code, err.message); exit(-1); } if (as_boolean_get(&exists)) { LOG("not able to find a value which should be in the set"); exit(-1); } as_boolean_init(&exists, false); as_integer_init(&ival, 33333); // Check that a value which should not be in the set, really isn't. if (aerospike_lset_exists(&as, &err, NULL, &g_key, &lset2, (const as_val*)&ival, &exists) != AEROSPIKE_OK) { LOG("second aerospike_lset_exists() returned %d - %s", err.code, err.message); exit(-1); } if (as_boolean_get(&exists)) { LOG("found a value which should not be in the set"); exit(-1); } LOG("existence functionality checked"); // Destroy the lset. if (aerospike_lset_destroy(&as, &err, NULL, &g_key, &lset) != AEROSPIKE_OK) { LOG("aerospike_lset_destroy() returned %d - %s", err.code, err.message); exit(-1); } n_elements = 0; // See if we can still do any lset operations. if (aerospike_lset_size(&as, &err, NULL, &g_key, &lset, &n_elements) == AEROSPIKE_OK) { LOG("aerospike_lset_size() did not return error"); exit(-1); } // Cleanup and disconnect from the database cluster. example_cleanup(&as); LOG("lset example successfully completed"); return 0; }
LDT的默认行为是接受任何类型的对象并存储。对象可以是不同类型和大小。唯一的附加条件是,若对象是复杂类型,那么大数据类型(LDT)链表(List)机制要求带一个“键”(Key)的映射(map),或一个用户提供的函数来计算/标识出一个原子值,用于对象排序。
LDT被期望为“开箱即用"。 也就是说,默认配置与行为对于大多数使用场景和数据类型都能工作。LDT存储的默认行为是将对象存在链表中,并且使用标准序列化机制(MSGPACK)序列化对象。 可能有这样的场景,用户能够通过把对象直接转换成二进制,来实现大量空间的节省,或者,用户精确地知道存储的对象将是什么尺寸,并希望精确控制子记录的存储尺寸,或者,用户希望更改内部存储尺寸与限制,来调优LDT性能。 在这种情况下,Aeropsike为用户提供了能力,来更改LDT的系统配置设置、指定转换/反向转换函数、指定唯一标识符函数与可被用作复杂对象的谓词过滤器函数。