Search in sources :

Example 26 with TGet

use of org.apache.hadoop.hbase.thrift2.generated.TGet in project hbase by apache.

the class DemoClient method run.

public void run() throws Exception {
    int timeout = 10000;
    boolean framed = false;
    TTransport transport = new TSocket(host, port, timeout);
    if (framed) {
        transport = new TFramedTransport(transport);
    } else if (secure) {
        /**
       * 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.
       */
        Map<String, String> saslProperties = new HashMap<>();
        saslProperties.put(Sasl.QOP, "auth-conf,auth-int,auth");
        transport = new TSaslClientTransport("GSSAPI", null, // Thrift server user name, should be an authorized proxy user
        user != null ? user : "hbase", // Thrift server domain
        host, saslProperties, null, transport);
    }
    TProtocol protocol = new TBinaryProtocol(transport);
    // This is our thrift client.
    THBaseService.Iface client = new THBaseService.Client(protocol);
    // open the transport
    transport.open();
    ByteBuffer table = ByteBuffer.wrap("example".getBytes());
    TPut put = new TPut();
    put.setRow("row1".getBytes());
    TColumnValue columnValue = new TColumnValue();
    columnValue.setFamily("family1".getBytes());
    columnValue.setQualifier("qualifier1".getBytes());
    columnValue.setValue("value1".getBytes());
    List<TColumnValue> columnValues = new ArrayList<>(1);
    columnValues.add(columnValue);
    put.setColumnValues(columnValues);
    client.put(table, put);
    TGet get = new TGet();
    get.setRow("row1".getBytes());
    TResult result = client.get(table, get);
    System.out.print("row = " + new String(result.getRow()));
    for (TColumnValue resultColumnValue : result.getColumnValues()) {
        System.out.print("family = " + new String(resultColumnValue.getFamily()));
        System.out.print("qualifier = " + new String(resultColumnValue.getFamily()));
        System.out.print("value = " + new String(resultColumnValue.getValue()));
        System.out.print("timestamp = " + resultColumnValue.getTimestamp());
    }
    transport.close();
}
Also used : TGet(org.apache.hadoop.hbase.thrift2.generated.TGet) ArrayList(java.util.ArrayList) TSaslClientTransport(org.apache.thrift.transport.TSaslClientTransport) TColumnValue(org.apache.hadoop.hbase.thrift2.generated.TColumnValue) ByteBuffer(java.nio.ByteBuffer) TResult(org.apache.hadoop.hbase.thrift2.generated.TResult) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) TTransport(org.apache.thrift.transport.TTransport) TPut(org.apache.hadoop.hbase.thrift2.generated.TPut) HashMap(java.util.HashMap) Map(java.util.Map) TSocket(org.apache.thrift.transport.TSocket) THBaseService(org.apache.hadoop.hbase.thrift2.generated.THBaseService)

Aggregations

TGet (org.apache.hadoop.hbase.thrift2.generated.TGet)26 ByteBuffer (java.nio.ByteBuffer)25 TColumnValue (org.apache.hadoop.hbase.thrift2.generated.TColumnValue)24 TPut (org.apache.hadoop.hbase.thrift2.generated.TPut)24 Test (org.junit.Test)23 ArrayList (java.util.ArrayList)22 TResult (org.apache.hadoop.hbase.thrift2.generated.TResult)20 TDelete (org.apache.hadoop.hbase.thrift2.generated.TDelete)7 TColumn (org.apache.hadoop.hbase.thrift2.generated.TColumn)5 HashMap (java.util.HashMap)4 TAuthorization (org.apache.hadoop.hbase.thrift2.generated.TAuthorization)4 TCellVisibility (org.apache.hadoop.hbase.thrift2.generated.TCellVisibility)4 TColumnIncrement (org.apache.hadoop.hbase.thrift2.generated.TColumnIncrement)4 THBaseService (org.apache.hadoop.hbase.thrift2.generated.THBaseService)4 TIncrement (org.apache.hadoop.hbase.thrift2.generated.TIncrement)4 Put (org.apache.hadoop.hbase.client.Put)3 ThriftMetrics (org.apache.hadoop.hbase.thrift.ThriftMetrics)3 HColumnDescriptor (org.apache.hadoop.hbase.HColumnDescriptor)2 HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)2 TableName (org.apache.hadoop.hbase.TableName)2