-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsample.cc
715 lines (620 loc) · 22.9 KB
/
sample.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <time.h>
#include <unistd.h>
#include <hbase/hbase.h>
/* Found under /libhbase/src/test/native/common */
#include "byte_buffer.h"
/*
* Sample code to illustrate usage of libhbase APIs
*/
#ifdef __cplusplus
extern "C" {
#endif
#define CHECK_API_ERROR(retCode, ...) \
HBASE_LOG_MSG((retCode ? HBASE_LOG_LEVEL_ERROR : HBASE_LOG_LEVEL_INFO), \
__VA_ARGS__, retCode);
static byte_t *FAMILIES[] = { (byte_t *)"f", (byte_t *)"g" };
static hb_columndesc HCD[2] = { NULL };
static uint32_t num_filters = 10;
static char filters[][200] = {"ValueFilter(=,'substring:''')",
"RandomRowFilter(0.5)",
"ColumnCountGetFilter(2,1)",
"ColumnPaginationFilter(1)",
"ColumnPrefixFilter('column-a')",
"FamilyFilter(=,'binaryprefix:f')",
"PrefixFilter('row_') AND QualifierFilter(<,'binaryprefix:g')",
"SKIP TimestampsFilter(1392222222222)",
"WHILE ValueFilter(=,'binaryprefix:cell2_value_v1')",
"FuzzyRowFilter('row00','00001')"};
typedef struct cell_data_t_ {
bytebuffer value;
hb_cell_t *hb_cell;
struct cell_data_t_ *next_cell;
} cell_data_t;
cell_data_t*
new_cell_data() {
cell_data_t *cell_data = (cell_data_t*) calloc(1, sizeof(cell_data_t));
cell_data->next_cell = NULL;
return cell_data;
}
typedef struct row_data_t_ {
bytebuffer key;
struct cell_data_t_ *first_cell;
} row_data_t;
static void
release_row_data(row_data_t *row_data) {
if (row_data != NULL) {
cell_data_t *cell = row_data->first_cell;
while (cell) {
bytebuffer_free(cell->value);
free(cell->hb_cell);
cell_data_t *cur_cell = cell;
cell = cell->next_cell;
free(cur_cell);
}
bytebuffer_free(row_data->key);
free(row_data);
}
}
/**
* Put synchronizer and callback
*/
static volatile int32_t outstanding_puts_count;
static pthread_cond_t puts_cv = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t puts_mutex = PTHREAD_MUTEX_INITIALIZER;
static void
put_callback(int32_t err, hb_client_t client,
hb_mutation_t mutation, hb_result_t result, void *extra) {
row_data_t* row_data = (row_data_t *)extra;
HBASE_LOG_INFO("Received put callback for row \'%.*s\', result = %d.",
row_data->key->length, row_data->key->buffer, err);
release_row_data(row_data);
hb_mutation_destroy(mutation);
pthread_mutex_lock(&puts_mutex);
outstanding_puts_count--;
if (outstanding_puts_count == 0) {
pthread_cond_signal(&puts_cv);
}
pthread_mutex_unlock(&puts_mutex);
}
static void
wait_for_puts() {
HBASE_LOG_INFO("Waiting for outstanding puts to complete.");
pthread_mutex_lock(&puts_mutex);
while (outstanding_puts_count > 0) {
pthread_cond_wait(&puts_cv, &puts_mutex);
}
pthread_mutex_unlock(&puts_mutex);
HBASE_LOG_INFO("Put operations completed.");
}
/**
* Flush synchronizer and callback
*/
static volatile bool flush_done = false;
static pthread_cond_t flush_cv = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t flush_mutex = PTHREAD_MUTEX_INITIALIZER;
static void
client_flush_callback(int32_t err,
hb_client_t client, void *extra) {
HBASE_LOG_INFO("Received client flush callback.");
pthread_mutex_lock(&flush_mutex);
flush_done = true;
pthread_cond_signal(&flush_cv);
pthread_mutex_unlock(&flush_mutex);
}
static void
wait_for_flush() {
HBASE_LOG_INFO("Waiting for flush to complete.");
pthread_mutex_lock(&flush_mutex);
while (!flush_done) {
pthread_cond_wait(&flush_cv, &flush_mutex);
}
pthread_mutex_unlock(&flush_mutex);
HBASE_LOG_INFO("Flush completed.");
}
static void printRow(const hb_result_t result) {
const byte_t *key = NULL;
size_t key_len = 0;
hb_result_get_key(result, &key, &key_len);
size_t cell_count = 0;
hb_result_get_cell_count(result, &cell_count);
HBASE_LOG_INFO("Row=\'%.*s\', cell count=%d", key_len, key, cell_count);
const hb_cell_t **cells;
hb_result_get_cells(result, &cells, &cell_count);
for (size_t i = 0; i < cell_count; ++i) {
HBASE_LOG_INFO(
"Cell %d: family=\'%.*s\', qualifier=\'%.*s\', "
"value=\'%.*s\', timestamp=%lld.", i,
cells[i]->family_len, cells[i]->family,
cells[i]->qualifier_len, cells[i]->qualifier,
cells[i]->value_len, cells[i]->value, cells[i]->ts);
}
}
/**
* Get synchronizer and callback
*/
static volatile bool get_done = false;
static pthread_cond_t get_cv = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t get_mutex = PTHREAD_MUTEX_INITIALIZER;
static void
get_callback(int32_t err, hb_client_t client,
hb_get_t get, hb_result_t result, void *extra) {
bytebuffer rowKey = (bytebuffer)extra;
if (err == 0) {
const char *table_name;
size_t table_name_len;
hb_result_get_table(result, &table_name, &table_name_len);
HBASE_LOG_INFO("Received get callback for table=\'%.*s\'.",
table_name_len, table_name);
printRow(result);
const hb_cell_t *mycell;
bytebuffer qualifier = bytebuffer_strcpy("column-a");
HBASE_LOG_INFO("Looking up cell for family=\'%s\', qualifier=\'%.*s\'.",
FAMILIES[0], qualifier->length, qualifier->buffer);
if (hb_result_get_cell(result, FAMILIES[0], 1, qualifier->buffer,
qualifier->length, &mycell) == 0) {
HBASE_LOG_INFO("Cell found, value=\'%.*s\', timestamp=%lld.",
mycell->value_len, mycell->value, mycell->ts);
} else {
HBASE_LOG_ERROR("Cell not found.");
}
bytebuffer_free(qualifier);
hb_result_destroy(result);
} else {
HBASE_LOG_ERROR("Get failed with error code: %d.", err);
}
bytebuffer_free(rowKey);
hb_get_destroy(get);
pthread_mutex_lock(&get_mutex);
get_done = true;
pthread_cond_signal(&get_cv);
pthread_mutex_unlock(&get_mutex);
}
static void
wait_for_get() {
HBASE_LOG_INFO("Waiting for get operation to complete.");
pthread_mutex_lock(&get_mutex);
while (!get_done) {
pthread_cond_wait(&get_cv, &get_mutex);
}
pthread_mutex_unlock(&get_mutex);
HBASE_LOG_INFO("Get operation completed.");
}
/**
* Delete synchronizer and callbacks
*/
static volatile bool delete_done = false;
static pthread_cond_t del_cv = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t del_mutex = PTHREAD_MUTEX_INITIALIZER;
static void
delete_callback(int32_t err, hb_client_t client,
hb_mutation_t del, hb_result_t result, void *extra) {
bytebuffer rowKey = (bytebuffer)extra;
HBASE_LOG_INFO("Received delete callback for row \'%.*s\', "
"result = %d.", rowKey->length, rowKey->buffer, err);
hb_mutation_destroy(del);
pthread_mutex_lock(&del_mutex);
delete_done = true;
pthread_cond_signal(&del_cv);
pthread_mutex_unlock(&del_mutex);
}
static void
wait_for_delete() {
HBASE_LOG_INFO("Waiting for delete operation to complete.");
pthread_mutex_lock(&del_mutex);
while (!delete_done) {
pthread_cond_wait(&del_cv, &del_mutex);
}
pthread_mutex_unlock(&del_mutex);
HBASE_LOG_INFO("Delete operation completed.");
}
/**
* Scan synchronizer and callbacks
*/
static volatile bool scan_done = false;
static pthread_cond_t scan_cv = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t scan_mutex = PTHREAD_MUTEX_INITIALIZER;
void scan_callback(int32_t err, hb_scanner_t scanner,
hb_result_t results[], size_t num_results, void *extra) {
if (num_results) {
const char *table_name;
size_t table_name_len;
hb_result_get_table(results[0], &table_name, &table_name_len);
HBASE_LOG_INFO("Received scan_next callback for table=\'%.*s\', row count=%d.",
table_name_len, table_name, num_results);
for (uint32_t i = 0; i < num_results; ++i) {
printRow(results[i]);
hb_result_destroy(results[i]);
}
hb_scanner_next(scanner, scan_callback, NULL);
} else {
hb_scanner_destroy(scanner, NULL, NULL);
pthread_mutex_lock(&scan_mutex);
scan_done = true;
pthread_cond_signal(&scan_cv);
pthread_mutex_unlock(&scan_mutex);
}
}
static void
wait_for_scan() {
HBASE_LOG_INFO("Waiting for scan to complete.");
pthread_mutex_lock(&scan_mutex);
while (!scan_done) {
pthread_cond_wait(&scan_cv, &scan_mutex);
}
pthread_mutex_unlock(&scan_mutex);
HBASE_LOG_INFO("Scan completed.");
}
/**
* Client destroy synchronizer and callbacks
*/
static volatile bool client_destroyed = false;
static pthread_cond_t client_destroyed_cv = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t client_destroyed_mutex = PTHREAD_MUTEX_INITIALIZER;
static void
client_disconnection_callback(int32_t err,
hb_client_t client, void *extra) {
HBASE_LOG_INFO("Received client disconnection callback.");
pthread_mutex_lock(&client_destroyed_mutex);
client_destroyed = true;
pthread_cond_signal(&client_destroyed_cv);
pthread_mutex_unlock(&client_destroyed_mutex);
}
static void
wait_client_disconnection() {
HBASE_LOG_INFO("Waiting for client to disconnect.");
pthread_mutex_lock(&client_destroyed_mutex);
while (!client_destroyed) {
pthread_cond_wait(&client_destroyed_cv, &client_destroyed_mutex);
}
pthread_mutex_unlock(&client_destroyed_mutex);
HBASE_LOG_INFO("Client disconnected.");
}
void admin_dsc_cb(int32_t err, hb_admin_t admin, void *userContext)
{
HBASE_LOG_INFO("Admin disconnected.");
}
static int
ensureTable(hb_connection_t connection, const char *table_name) {
int32_t retCode = 0;
hb_admin_t admin = NULL;
if ((retCode = hb_admin_create(connection, &admin)) != 0) {
HBASE_LOG_ERROR("Could not create HBase admin : errorCode = %d.", retCode);
goto cleanup;
}
if ((retCode = hb_admin_table_exists(admin, NULL, table_name)) == 0) {
HBASE_LOG_INFO("Table '%s' exists, deleting...", table_name);
if ((retCode = hb_admin_table_delete(admin, NULL, table_name)) != 0) {
HBASE_LOG_ERROR("Could not delete table %s[%d].", table_name, retCode);
goto cleanup;
}
} else if (retCode != ENOENT) {
HBASE_LOG_ERROR("Error while checking if the table exists: errorCode = %d.", retCode);
goto cleanup;
}
hb_coldesc_create(FAMILIES[0], 1, &HCD[0]);
hb_coldesc_set_maxversions(HCD[0], 2);
hb_coldesc_set_minversions(HCD[0], 1);
hb_coldesc_set_ttl(HCD[0], 2147480000);
hb_coldesc_set_inmemory(HCD[0], 1);
hb_coldesc_create(FAMILIES[1], 1, &HCD[1]);
HBASE_LOG_INFO("Creating table '%s'...", table_name);
if ((retCode = hb_admin_table_create(admin, NULL, table_name, HCD, 2)) == 0) {
HBASE_LOG_INFO("Table '%s' created, verifying if enabled.", table_name);
retCode = hb_admin_table_enabled(admin, NULL, table_name);
CHECK_API_ERROR(retCode,
"Table '%s' is %senabled, result %d.", table_name, retCode?"not ":"");
retCode = hb_admin_table_disable(admin, NULL, table_name);
CHECK_API_ERROR(retCode,
"Attempted to disable table '%s', result %d.", table_name);
retCode = hb_admin_table_disable(admin, NULL, table_name);
CHECK_API_ERROR(retCode,
"Attempted to disable table '%s' again, result %d.", table_name);
retCode = hb_admin_table_enable(admin, NULL, table_name);
CHECK_API_ERROR(retCode,
"Attempted to enable table '%s', result %d.", table_name);
retCode = hb_admin_table_enable(admin, NULL, table_name);
CHECK_API_ERROR(retCode,
"Attempted to enable table '%s' again, result %d.", table_name);
}
hb_coldesc_destroy(HCD[0]);
hb_coldesc_destroy(HCD[1]);
cleanup:
if (admin) {
hb_admin_destroy(admin, admin_dsc_cb, NULL);
}
return retCode;
}
/**
* Program entry point
*/
int
main(int argc, char **argv) {
int32_t retCode = 0;
FILE* logFile = NULL;
hb_connection_t connection = NULL;
hb_client_t client = NULL;
const char *rowkey_prefix = "row";
const char *value_prefix = "test value";
bytebuffer column_a = bytebuffer_strcpy("column-a");
bytebuffer column_b = bytebuffer_strcpy("column-b");
const char *table_name = (argc > 1) ? argv[1] : "/tmp/tempTable";
const char *zk_ensemble = (argc > 2) ? argv[2] : "127.0.0.1:7222";
const char *zk_root_znode = (argc > 3) ? argv[3] : NULL;
const size_t table_name_len = strlen(table_name);
const int num_puts = 10;
hb_put_t put = NULL;
srand(time(NULL));
// hb_log_set_level(HBASE_LOG_LEVEL_DEBUG); // defaults to INFO
const char *logFilePath = getenv("HBASE_LOG_FILE");
if (logFilePath != NULL) {
FILE* logFile = fopen(logFilePath, "a");
if (!logFile) {
retCode = errno;
fprintf(stderr, "Unable to open log file \"%s\"", logFilePath);
perror(NULL);
goto cleanup;
}
hb_log_set_stream(logFile); // defaults to stderr
}
if ((retCode = hb_connection_create(zk_ensemble,
zk_root_znode,
&connection)) != 0) {
HBASE_LOG_ERROR("Could not create HBase connection : errorCode = %d.", retCode);
goto cleanup;
}
if ((retCode = ensureTable(connection, table_name)) != 0) {
HBASE_LOG_ERROR("Failed to ensure table %s : errorCode = %d", table_name, retCode);
goto cleanup;
}
HBASE_LOG_INFO("Connecting to HBase cluster using Zookeeper ensemble '%s'.",
zk_ensemble);
if ((retCode = hb_client_create(connection, &client)) != 0) {
HBASE_LOG_ERROR("Could not connect to HBase cluster : errorCode = %d.", retCode);
goto cleanup;
}
// let's send a batch of 10 puts with single cell asynchronously
outstanding_puts_count += num_puts;
for (int i = 0; i < num_puts; ++i) {
row_data_t *row_data = (row_data_t *) calloc(1, sizeof(row_data_t));
row_data->key = bytebuffer_printf("%s%02d", rowkey_prefix, i);
hb_put_create(row_data->key->buffer, row_data->key->length, &put);
hb_mutation_set_table(put, table_name, table_name_len);
hb_mutation_set_durability(put, DURABILITY_SKIP_WAL);
hb_mutation_set_bufferable(put, false);
cell_data_t *cell_data = new_cell_data();
row_data->first_cell = cell_data;
cell_data->value = bytebuffer_printf("%s%02d", value_prefix, i);
hb_cell_t *cell = (hb_cell_t*) calloc(1, sizeof(hb_cell_t));
cell_data->hb_cell = cell;
cell->row = row_data->key->buffer;
cell->row_len = row_data->key->length;
cell->family = FAMILIES[rand() % 2];
cell->family_len = 1;
cell->qualifier = column_a->buffer;
cell->qualifier_len = column_a->length;
cell->value = cell_data->value->buffer;
cell->value_len = cell_data->value->length;
cell->ts = HBASE_LATEST_TIMESTAMP;
hb_put_add_cell(put, cell);
HBASE_LOG_INFO("Sending row with row key : '%.*s'.",
cell->row_len, cell->row);
hb_mutation_send(client, put, put_callback, row_data);
}
hb_client_flush(client, client_flush_callback, NULL);
wait_for_flush();
wait_for_puts(); // outside the loop, wait for 10 puts to complete
// now, let's put two cells in a single row
outstanding_puts_count++;
{
row_data_t *row_data = (row_data_t *) calloc(1, sizeof(row_data_t));
row_data->key = bytebuffer_printf("row_with_two_cells");
hb_put_create(row_data->key->buffer, row_data->key->length, &put);
hb_mutation_set_table(put, table_name, table_name_len);
hb_mutation_set_durability(put, DURABILITY_SYNC_WAL);
// first cell
cell_data_t *cell1_data = new_cell_data();
row_data->first_cell = cell1_data;
cell1_data->value = bytebuffer_printf("cell1_value_v1");
hb_cell_t *cell1 = (hb_cell_t*) calloc(1, sizeof(hb_cell_t));
cell1_data->hb_cell = cell1;
cell1->row = row_data->key->buffer;
cell1->row_len = row_data->key->length;
cell1->family = FAMILIES[0];
cell1->family_len = 1;
cell1->qualifier = column_a->buffer;
cell1->qualifier_len = column_a->length;
cell1->value = cell1_data->value->buffer;
cell1->value_len = cell1_data->value->length;
cell1->ts = 1391111111111L;
hb_put_add_cell(put, cell1);
// second cell
cell_data_t *cell2_data = new_cell_data();
cell1_data->next_cell = cell2_data;
cell2_data->value = bytebuffer_printf("cell2_value_v1");
hb_cell_t *cell2 = (hb_cell_t*) calloc(1, sizeof(hb_cell_t));
cell2_data->hb_cell = cell2;
cell2->row = row_data->key->buffer;
cell2->row_len = row_data->key->length;
cell2->family = FAMILIES[1];
cell2->family_len = 1;
cell2->qualifier = column_b->buffer;
cell2->qualifier_len = column_b->length;
cell2->value = cell2_data->value->buffer;
cell2->value_len = cell2_data->value->length;
cell2->ts = 1391111111111L;
hb_put_add_cell(put, cell2);
HBASE_LOG_INFO("Sending row with row key : '%.*s'.",
cell1->row_len, cell1->row);
hb_mutation_send(client, put, put_callback, row_data);
wait_for_puts();
}
// now, let's put second version in one column
outstanding_puts_count++;
{
row_data_t *row_data = (row_data_t *) calloc(1, sizeof(row_data_t));
row_data->key = bytebuffer_printf("row_with_two_cells");
hb_put_create(row_data->key->buffer, row_data->key->length, &put);
hb_mutation_set_table(put, table_name, table_name_len);
hb_mutation_set_durability(put, DURABILITY_SYNC_WAL);
// first cell
cell_data_t *cell1_data = new_cell_data();
row_data->first_cell = cell1_data;
cell1_data->value = bytebuffer_printf("cell1_value_v2");
hb_cell_t *cell1 = (hb_cell_t*) calloc(1, sizeof(hb_cell_t));
cell1_data->hb_cell = cell1;
cell1->row = row_data->key->buffer;
cell1->row_len = row_data->key->length;
cell1->family = FAMILIES[0];
cell1->family_len = 1;
cell1->qualifier = column_a->buffer;
cell1->qualifier_len = column_a->length;
cell1->value = cell1_data->value->buffer;
cell1->value_len = cell1_data->value->length;
cell1->ts = 1392222222222L;
hb_put_add_cell(put, cell1);
HBASE_LOG_INFO("Sending row with row key : '%.*s'.",
cell1->row_len, cell1->row);
hb_mutation_send(client, put, put_callback, row_data);
wait_for_puts();
}
outstanding_puts_count++;
{
row_data_t *row_data = (row_data_t *) calloc(1, sizeof(row_data_t));
row_data->key = bytebuffer_printf("row_with_quotation_in_value");
hb_put_create(row_data->key->buffer, row_data->key->length, &put);
hb_mutation_set_table(put, table_name, table_name_len);
hb_mutation_set_durability(put, DURABILITY_SYNC_WAL);
// first cell
cell_data_t *cell_data = new_cell_data();
row_data->first_cell = cell_data;
cell_data->value = bytebuffer_printf("Cell with '");
hb_cell_t *cell = (hb_cell_t*) calloc(1, sizeof(hb_cell_t));
cell_data->hb_cell = cell;
cell->row = row_data->key->buffer;
cell->row_len = row_data->key->length;
cell->family = FAMILIES[0];
cell->family_len = 1;
cell->qualifier = column_a->buffer;
cell->qualifier_len = column_a->length;
cell->value = cell_data->value->buffer;
cell->value_len = cell_data->value->length;
hb_put_add_cell(put, cell);
HBASE_LOG_INFO("Sending row with row key : '%.*s'.",
cell->row_len, cell->row);
hb_mutation_send(client, put, put_callback, row_data);
wait_for_puts();
}
// now, scan the entire table
for (uint32_t i = 0; i < num_filters; ++i) {
HBASE_LOG_INFO("Scanning with Filter %s", filters[i]);
hb_scanner_t scanner = NULL;
hb_scanner_create(client, &scanner);
hb_scanner_set_table(scanner, table_name, table_name_len);
hb_scanner_set_num_max_rows(scanner, 3); // maximum 3 rows at a time
hb_scanner_set_num_versions(scanner, 10); // up to 10 versions of the cell
hb_scanner_set_filter(scanner, (byte_t *)filters[i], strlen(filters[i]));
hb_scanner_next(scanner, scan_callback, NULL); // dispatch the call
wait_for_scan();
}
{
hb_scanner_t scanner = NULL;
hb_scanner_create(client, &scanner);
hb_scanner_set_table(scanner, table_name, table_name_len);
hb_scanner_set_filter(scanner, NULL, 0);
hb_scanner_next(scanner, scan_callback, NULL); // dispatch the call
wait_for_scan();
}
// fetch a row with row-key="row_with_two_cells"
{
bytebuffer rowKey = bytebuffer_strcpy("row_with_two_cells");
hb_get_t get = NULL;
hb_get_create(rowKey->buffer, rowKey->length, &get);
hb_get_add_column(get, FAMILIES[0], 1, NULL, 0);
hb_get_add_column(get, FAMILIES[1], 1, NULL, 0);
hb_get_set_table(get, table_name, table_name_len);
hb_get_set_num_versions(get, 10); // up to ten versions of each column
get_done = false;
hb_get_send(client, get, get_callback, rowKey);
wait_for_get();
}
// delete a specific version of a column
{
bytebuffer rowKey = bytebuffer_strcpy("row_with_two_cells");
hb_delete_t del = NULL;
hb_delete_create(rowKey->buffer, rowKey->length, &del);
hb_delete_add_column(del, FAMILIES[0], 1,
column_a->buffer, column_a->length, 1391111111112L);
hb_mutation_set_table(del, table_name, table_name_len);
delete_done = false;
hb_mutation_send(client, del, delete_callback, rowKey);
wait_for_delete();
}
// fetch a row with row-key="row_with_two_cells"
{
bytebuffer rowKey = bytebuffer_strcpy("row_with_two_cells");
hb_get_t get = NULL;
hb_get_create(rowKey->buffer, rowKey->length, &get);
hb_get_add_column(get, FAMILIES[0], 1, NULL, 0);
hb_get_add_column(get, FAMILIES[1], 1, NULL, 0);
hb_get_set_table(get, table_name, table_name_len);
hb_get_set_num_versions(get, 10); // up to ten versions of each column
get_done = false;
hb_get_send(client, get, get_callback, rowKey);
wait_for_get();
}
cleanup:
if (client) {
HBASE_LOG_INFO("Disconnecting client.");
hb_client_destroy(client, client_disconnection_callback, NULL);
wait_client_disconnection();
}
if (connection) {
hb_connection_destroy(connection);
}
if (column_a) {
bytebuffer_free(column_a); // do not need 'column' anymore
}
if (column_b) {
bytebuffer_free(column_b);
}
if (logFile) {
fclose(logFile);
}
pthread_cond_destroy(&puts_cv);
pthread_mutex_destroy(&puts_mutex);
pthread_cond_destroy(&get_cv);
pthread_mutex_destroy(&get_mutex);
pthread_cond_destroy(&del_cv);
pthread_mutex_destroy(&del_mutex);
pthread_cond_destroy(&client_destroyed_cv);
pthread_mutex_destroy(&client_destroyed_mutex);
return retCode;
}
#ifdef __cplusplus
}
#endif