Search in sources :

Example 6 with TRowResult

use of org.apache.hadoop.hbase.thrift.generated.TRowResult in project hbase by apache.

the class TestThriftServer method doTestTableMutations.

public static void doTestTableMutations(Hbase.Iface handler) throws Exception {
    // Setup
    handler.createTable(tableAname, getColumnDescriptors());
    // Apply a few Mutations to rowA
    //     mutations.add(new Mutation(false, columnAname, valueAname));
    //     mutations.add(new Mutation(false, columnBname, valueBname));
    handler.mutateRow(tableAname, rowAname, getMutations(), null);
    // Assert that the changes were made
    assertEquals(valueAname, handler.get(tableAname, rowAname, columnAname, null).get(0).value);
    TRowResult rowResult1 = handler.getRow(tableAname, rowAname, null).get(0);
    assertEquals(rowAname, rowResult1.row);
    assertEquals(valueBname, rowResult1.columns.get(columnBname).value);
    // Apply a few BatchMutations for rowA and rowB
    // rowAmutations.add(new Mutation(true, columnAname, null));
    // rowAmutations.add(new Mutation(false, columnBname, valueCname));
    // batchMutations.add(new BatchMutation(rowAname, rowAmutations));
    // Mutations to rowB
    // rowBmutations.add(new Mutation(false, columnAname, valueCname));
    // rowBmutations.add(new Mutation(false, columnBname, valueDname));
    // batchMutations.add(new BatchMutation(rowBname, rowBmutations));
    handler.mutateRows(tableAname, getBatchMutations(), null);
    // Assert that changes were made to rowA
    List<TCell> cells = handler.get(tableAname, rowAname, columnAname, null);
    assertFalse(cells.size() > 0);
    assertEquals(valueCname, handler.get(tableAname, rowAname, columnBname, null).get(0).value);
    List<TCell> versions = handler.getVer(tableAname, rowAname, columnBname, MAXVERSIONS, null);
    assertEquals(valueCname, versions.get(0).value);
    assertEquals(valueBname, versions.get(1).value);
    // Assert that changes were made to rowB
    TRowResult rowResult2 = handler.getRow(tableAname, rowBname, null).get(0);
    assertEquals(rowBname, rowResult2.row);
    assertEquals(valueCname, rowResult2.columns.get(columnAname).value);
    assertEquals(valueDname, rowResult2.columns.get(columnBname).value);
    // Apply some deletes
    handler.deleteAll(tableAname, rowAname, columnBname, null);
    handler.deleteAllRow(tableAname, rowBname, null);
    // Assert that the deletes were applied
    int size = handler.get(tableAname, rowAname, columnBname, null).size();
    assertEquals(0, size);
    size = handler.getRow(tableAname, rowBname, null).size();
    assertEquals(0, size);
    // Try null mutation
    List<Mutation> mutations = new ArrayList<>(1);
    mutations.add(new Mutation(false, columnAname, null, true));
    handler.mutateRow(tableAname, rowAname, mutations, null);
    TRowResult rowResult3 = handler.getRow(tableAname, rowAname, null).get(0);
    assertEquals(rowAname, rowResult3.row);
    assertEquals(0, rowResult3.columns.get(columnAname).value.remaining());
    // Teardown
    handler.disableTable(tableAname);
    handler.deleteTable(tableAname);
}
Also used : ArrayList(java.util.ArrayList) TCell(org.apache.hadoop.hbase.thrift.generated.TCell) BatchMutation(org.apache.hadoop.hbase.thrift.generated.BatchMutation) Mutation(org.apache.hadoop.hbase.thrift.generated.Mutation) TRowResult(org.apache.hadoop.hbase.thrift.generated.TRowResult)

Example 7 with TRowResult

use of org.apache.hadoop.hbase.thrift.generated.TRowResult in project hbase by apache.

the class TestThriftServer method doTestTableTimestampsAndColumns.

/**
   * Similar to testTableMutations(), except Mutations are applied with
   * specific timestamps and data retrieval uses these timestamps to
   * extract specific versions of data.
   *
   * @throws Exception
   */
public void doTestTableTimestampsAndColumns() throws Exception {
    // Setup
    ThriftServerRunner.HBaseHandler handler = new ThriftServerRunner.HBaseHandler(UTIL.getConfiguration(), UserProvider.instantiate(UTIL.getConfiguration()));
    handler.createTable(tableAname, getColumnDescriptors());
    // Apply timestamped Mutations to rowA
    long time1 = System.currentTimeMillis();
    handler.mutateRowTs(tableAname, rowAname, getMutations(), time1, null);
    Thread.sleep(1000);
    // Apply timestamped BatchMutations for rowA and rowB
    long time2 = System.currentTimeMillis();
    handler.mutateRowsTs(tableAname, getBatchMutations(), time2, null);
    // Apply an overlapping timestamped mutation to rowB
    handler.mutateRowTs(tableAname, rowBname, getMutations(), time2, null);
    // the getVerTs is [inf, ts) so you need to increment one.
    time1 += 1;
    time2 += 2;
    // Assert that the timestamp-related methods retrieve the correct data
    assertEquals(2, handler.getVerTs(tableAname, rowAname, columnBname, time2, MAXVERSIONS, null).size());
    assertEquals(1, handler.getVerTs(tableAname, rowAname, columnBname, time1, MAXVERSIONS, null).size());
    TRowResult rowResult1 = handler.getRowTs(tableAname, rowAname, time1, null).get(0);
    TRowResult rowResult2 = handler.getRowTs(tableAname, rowAname, time2, null).get(0);
    // columnA was completely deleted
    //assertTrue(Bytes.equals(rowResult1.columns.get(columnAname).value, valueAname));
    assertEquals(rowResult1.columns.get(columnBname).value, valueBname);
    assertEquals(rowResult2.columns.get(columnBname).value, valueCname);
    // ColumnAname has been deleted, and will never be visible even with a getRowTs()
    assertFalse(rowResult2.columns.containsKey(columnAname));
    List<ByteBuffer> columns = new ArrayList<>(1);
    columns.add(columnBname);
    rowResult1 = handler.getRowWithColumns(tableAname, rowAname, columns, null).get(0);
    assertEquals(rowResult1.columns.get(columnBname).value, valueCname);
    assertFalse(rowResult1.columns.containsKey(columnAname));
    rowResult1 = handler.getRowWithColumnsTs(tableAname, rowAname, columns, time1, null).get(0);
    assertEquals(rowResult1.columns.get(columnBname).value, valueBname);
    assertFalse(rowResult1.columns.containsKey(columnAname));
    // Apply some timestamped deletes
    // this actually deletes _everything_.
    // nukes everything in columnB: forever.
    handler.deleteAllTs(tableAname, rowAname, columnBname, time1, null);
    handler.deleteAllRowTs(tableAname, rowBname, time2, null);
    // Assert that the timestamp-related methods retrieve the correct data
    int size = handler.getVerTs(tableAname, rowAname, columnBname, time1, MAXVERSIONS, null).size();
    assertEquals(0, size);
    size = handler.getVerTs(tableAname, rowAname, columnBname, time2, MAXVERSIONS, null).size();
    assertEquals(1, size);
    // should be available....
    assertEquals(handler.get(tableAname, rowAname, columnBname, null).get(0).value, valueCname);
    assertEquals(0, handler.getRow(tableAname, rowBname, null).size());
    // Teardown
    handler.disableTable(tableAname);
    handler.deleteTable(tableAname);
}
Also used : HBaseHandler(org.apache.hadoop.hbase.thrift.ThriftServerRunner.HBaseHandler) ArrayList(java.util.ArrayList) HBaseHandler(org.apache.hadoop.hbase.thrift.ThriftServerRunner.HBaseHandler) TRowResult(org.apache.hadoop.hbase.thrift.generated.TRowResult) ByteBuffer(java.nio.ByteBuffer)

Example 8 with TRowResult

use of org.apache.hadoop.hbase.thrift.generated.TRowResult in project hbase by apache.

the class DemoClient method run.

private void run() throws Exception {
    TTransport transport = new TSocket(host, port);
    if (secure) {
        Map<String, String> saslProperties = new HashMap<>();
        saslProperties.put(Sasl.QOP, "auth-conf,auth-int,auth");
        /**
           * The Thrift server the DemoClient is trying to connect to
           * must have a matching principal, and support authentication.
           *
           * The HBase cluster must be secure, allow proxy user.
           */
        transport = new TSaslClientTransport("GSSAPI", null, // Thrift server user name, should be an authorized proxy user.
        "hbase", // Thrift server domain
        host, saslProperties, null, transport);
    }
    transport.open();
    TProtocol protocol = new TBinaryProtocol(transport, true, true);
    Hbase.Client client = new Hbase.Client(protocol);
    byte[] t = bytes("demo_table");
    //
    // Scan all tables, look for the demo table and delete it.
    //
    System.out.println("scanning tables...");
    for (ByteBuffer name : client.getTableNames()) {
        System.out.println("  found: " + utf8(name.array()));
        if (utf8(name.array()).equals(utf8(t))) {
            if (client.isTableEnabled(name)) {
                System.out.println("    disabling table: " + utf8(name.array()));
                client.disableTable(name);
            }
            System.out.println("    deleting table: " + utf8(name.array()));
            client.deleteTable(name);
        }
    }
    //
    // Create the demo table with two column families, entry: and unused:
    //
    ArrayList<ColumnDescriptor> columns = new ArrayList<>(2);
    ColumnDescriptor col;
    col = new ColumnDescriptor();
    col.name = ByteBuffer.wrap(bytes("entry:"));
    col.timeToLive = Integer.MAX_VALUE;
    col.maxVersions = 10;
    columns.add(col);
    col = new ColumnDescriptor();
    col.name = ByteBuffer.wrap(bytes("unused:"));
    col.timeToLive = Integer.MAX_VALUE;
    columns.add(col);
    System.out.println("creating table: " + utf8(t));
    try {
        client.createTable(ByteBuffer.wrap(t), columns);
    } catch (AlreadyExists ae) {
        System.out.println("WARN: " + ae.message);
    }
    System.out.println("column families in " + utf8(t) + ": ");
    Map<ByteBuffer, ColumnDescriptor> columnMap = client.getColumnDescriptors(ByteBuffer.wrap(t));
    for (ColumnDescriptor col2 : columnMap.values()) {
        System.out.println("  column: " + utf8(col2.name.array()) + ", maxVer: " + Integer.toString(col2.maxVersions));
    }
    Map<ByteBuffer, ByteBuffer> dummyAttributes = null;
    boolean writeToWal = false;
    //
    // Test UTF-8 handling
    //
    byte[] invalid = { (byte) 'f', (byte) 'o', (byte) 'o', (byte) '-', (byte) 0xfc, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1 };
    byte[] valid = { (byte) 'f', (byte) 'o', (byte) 'o', (byte) '-', (byte) 0xE7, (byte) 0x94, (byte) 0x9F, (byte) 0xE3, (byte) 0x83, (byte) 0x93, (byte) 0xE3, (byte) 0x83, (byte) 0xBC, (byte) 0xE3, (byte) 0x83, (byte) 0xAB };
    ArrayList<Mutation> mutations;
    // non-utf8 is fine for data
    mutations = new ArrayList<>(1);
    mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(invalid), writeToWal));
    client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("foo")), mutations, dummyAttributes);
    // this row name is valid utf8
    mutations = new ArrayList<>(1);
    mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(valid), writeToWal));
    client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(valid), mutations, dummyAttributes);
    // non-utf8 is now allowed in row names because HBase stores values as binary
    mutations = new ArrayList<>(1);
    mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(invalid), writeToWal));
    client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(invalid), mutations, dummyAttributes);
    // Run a scanner on the rows we just created
    ArrayList<ByteBuffer> columnNames = new ArrayList<>();
    columnNames.add(ByteBuffer.wrap(bytes("entry:")));
    System.out.println("Starting scanner...");
    int scanner = client.scannerOpen(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("")), columnNames, dummyAttributes);
    while (true) {
        List<TRowResult> entry = client.scannerGet(scanner);
        if (entry.isEmpty()) {
            break;
        }
        printRow(entry);
    }
    //
    for (int i = 100; i >= 0; --i) {
        // format row keys as "00000" to "00100"
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMinimumIntegerDigits(5);
        nf.setGroupingUsed(false);
        byte[] row = bytes(nf.format(i));
        mutations = new ArrayList<>(1);
        mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("unused:")), ByteBuffer.wrap(bytes("DELETE_ME")), writeToWal));
        client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
        printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
        client.deleteAllRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes);
        // sleep to force later timestamp
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
        // no-op
        }
        mutations = new ArrayList<>(2);
        mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")), ByteBuffer.wrap(bytes("0")), writeToWal));
        mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(bytes("FOO")), writeToWal));
        client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
        printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
        Mutation m;
        mutations = new ArrayList<>(2);
        m = new Mutation();
        m.column = ByteBuffer.wrap(bytes("entry:foo"));
        m.isDelete = true;
        mutations.add(m);
        m = new Mutation();
        m.column = ByteBuffer.wrap(bytes("entry:num"));
        m.value = ByteBuffer.wrap(bytes("-1"));
        mutations.add(m);
        client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
        printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
        mutations = new ArrayList<>();
        mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")), ByteBuffer.wrap(bytes(Integer.toString(i))), writeToWal));
        mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:sqr")), ByteBuffer.wrap(bytes(Integer.toString(i * i))), writeToWal));
        client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
        printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
        // sleep to force later timestamp
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
        // no-op
        }
        mutations.clear();
        m = new Mutation();
        m.column = ByteBuffer.wrap(bytes("entry:num"));
        m.value = ByteBuffer.wrap(bytes("-999"));
        mutations.add(m);
        m = new Mutation();
        m.column = ByteBuffer.wrap(bytes("entry:sqr"));
        m.isDelete = true;
        // shouldn't override latest
        client.mutateRowTs(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, 1, dummyAttributes);
        printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
        List<TCell> versions = client.getVer(ByteBuffer.wrap(t), ByteBuffer.wrap(row), ByteBuffer.wrap(bytes("entry:num")), 10, dummyAttributes);
        printVersions(ByteBuffer.wrap(row), versions);
        if (versions.isEmpty()) {
            System.out.println("FATAL: wrong # of versions");
            System.exit(-1);
        }
        List<TCell> result = client.get(ByteBuffer.wrap(t), ByteBuffer.wrap(row), ByteBuffer.wrap(bytes("entry:foo")), dummyAttributes);
        if (!result.isEmpty()) {
            System.out.println("FATAL: shouldn't get here");
            System.exit(-1);
        }
        System.out.println("");
    }
    // scan all rows/columnNames
    columnNames.clear();
    for (ColumnDescriptor col2 : client.getColumnDescriptors(ByteBuffer.wrap(t)).values()) {
        System.out.println("column with name: " + new String(col2.name.array()));
        System.out.println(col2.toString());
        columnNames.add(col2.name);
    }
    System.out.println("Starting scanner...");
    scanner = client.scannerOpenWithStop(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("00020")), ByteBuffer.wrap(bytes("00040")), columnNames, dummyAttributes);
    while (true) {
        List<TRowResult> entry = client.scannerGet(scanner);
        if (entry.isEmpty()) {
            System.out.println("Scanner finished");
            break;
        }
        printRow(entry);
    }
    transport.close();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TSaslClientTransport(org.apache.thrift.transport.TSaslClientTransport) TProtocol(org.apache.thrift.protocol.TProtocol) TSocket(org.apache.thrift.transport.TSocket) ColumnDescriptor(org.apache.hadoop.hbase.thrift.generated.ColumnDescriptor) TCell(org.apache.hadoop.hbase.thrift.generated.TCell) ByteBuffer(java.nio.ByteBuffer) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TTransport(org.apache.thrift.transport.TTransport) Mutation(org.apache.hadoop.hbase.thrift.generated.Mutation) AlreadyExists(org.apache.hadoop.hbase.thrift.generated.AlreadyExists) TRowResult(org.apache.hadoop.hbase.thrift.generated.TRowResult) Hbase(org.apache.hadoop.hbase.thrift.generated.Hbase) NumberFormat(java.text.NumberFormat)

Aggregations

TRowResult (org.apache.hadoop.hbase.thrift.generated.TRowResult)8 ArrayList (java.util.ArrayList)7 ByteBuffer (java.nio.ByteBuffer)5 HBaseHandler (org.apache.hadoop.hbase.thrift.ThriftServerRunner.HBaseHandler)5 Mutation (org.apache.hadoop.hbase.thrift.generated.Mutation)5 BatchMutation (org.apache.hadoop.hbase.thrift.generated.BatchMutation)4 TCell (org.apache.hadoop.hbase.thrift.generated.TCell)4 Hbase (org.apache.hadoop.hbase.thrift.generated.Hbase)2 NumberFormat (java.text.NumberFormat)1 HashMap (java.util.HashMap)1 Configuration (org.apache.hadoop.conf.Configuration)1 Cell (org.apache.hadoop.hbase.Cell)1 HColumnDescriptor (org.apache.hadoop.hbase.HColumnDescriptor)1 HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)1 TableName (org.apache.hadoop.hbase.TableName)1 Put (org.apache.hadoop.hbase.client.Put)1 Result (org.apache.hadoop.hbase.client.Result)1 Table (org.apache.hadoop.hbase.client.Table)1 AlreadyExists (org.apache.hadoop.hbase.thrift.generated.AlreadyExists)1 ColumnDescriptor (org.apache.hadoop.hbase.thrift.generated.ColumnDescriptor)1