Asynchronously put two cells in a single row

APIs used

These APIs are defined in the header file mutations.h:

  • hb_put_create(): Creates a structure for the put operation and returns its handle.
  • hb_mutation_set_table(): Sets the table name for the mutation.
  • hb_put_add_cell(): Adds a cell to the put structure. The row key of the cell must be the same as the row key of the put structure.
  • hb_mutation_send(): Queues the put operation for sending to the server. Mutations are not performed atomically and can be batched in a non-deterministic way on either the client side or the server side. Any buffer attached to a mutation object (put or delete) must not be altered until the callback has been received.

Sequence of steps

  1. Create a row object named row_data.
  2. Create a put object.
  3. Specify the name of the table.
  4. Create cell data for the first cell.
  5. Create the first cell.
  6. Add the data to the first cell.
  7. Add the first cell to the row.
  8. Create cell data for the second cell.
  9. Create the second cell.
  10. Add the data to the second cell.
  11. Add the second cell to the row.
  12. Queue the put.
  13. Wait 3 seconds, flush the queue, and wait for the RPC calls to complete.

wait_for_puts() without hb_client_flush(): Waits for all outstanding put requests to be flushed. If hb_client_flush() is not called before wait_for_puts(), it can take up to three seconds for outstanding put requests to be flushed.

Code

  // 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();
  }