1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.thrift2;
20
21 import java.nio.ByteBuffer;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.apache.hadoop.hbase.thrift2.generated.TColumnValue;
26 import org.apache.hadoop.hbase.thrift2.generated.TGet;
27 import org.apache.hadoop.hbase.thrift2.generated.THBaseService;
28 import org.apache.hadoop.hbase.thrift2.generated.TIOError;
29 import org.apache.hadoop.hbase.thrift2.generated.TPut;
30 import org.apache.hadoop.hbase.thrift2.generated.TResult;
31 import org.apache.thrift.TException;
32 import org.apache.thrift.protocol.TBinaryProtocol;
33 import org.apache.thrift.protocol.TProtocol;
34 import org.apache.thrift.transport.TFramedTransport;
35 import org.apache.thrift.transport.TSocket;
36 import org.apache.thrift.transport.TTransport;
37
38 public class DemoClient {
39 public static void main(String[] args) throws TIOError, TException {
40 System.out.println("Thrift2 Demo");
41 System.out.println("Usage: DemoClient [host=localhost] [port=9090]");
42 System.out.println("This demo assumes you have a table called \"example\" with a column family called \"family1\"");
43
44 String host = "localhost";
45 int port = 9090;
46
47
48 if (args.length >= 1) {
49 host = args[0];
50 }
51 if (args.length >= 2) {
52 port = Integer.parseInt(args[1]);
53 }
54
55 int timeout = 10000;
56 boolean framed = false;
57
58 TTransport transport = new TSocket(host, port, timeout);
59 if (framed) {
60 transport = new TFramedTransport(transport);
61 }
62 TProtocol protocol = new TBinaryProtocol(transport);
63
64 THBaseService.Iface client = new THBaseService.Client(protocol);
65
66
67 transport.open();
68
69 ByteBuffer table = ByteBuffer.wrap("example".getBytes());
70
71 TPut put = new TPut();
72 put.setRow("row1".getBytes());
73
74 TColumnValue columnValue = new TColumnValue();
75 columnValue.setFamily("family1".getBytes());
76 columnValue.setQualifier("qualifier1".getBytes());
77 columnValue.setValue("value1".getBytes());
78 List<TColumnValue> columnValues = new ArrayList<TColumnValue>();
79 columnValues.add(columnValue);
80 put.setColumnValues(columnValues);
81
82 client.put(table, put);
83
84 TGet get = new TGet();
85 get.setRow("row1".getBytes());
86
87 TResult result = client.get(table, get);
88
89 System.out.print("row = " + new String(result.getRow()));
90 for (TColumnValue resultColumnValue : result.getColumnValues()) {
91 System.out.print("family = " + new String(resultColumnValue.getFamily()));
92 System.out.print("qualifier = " + new String(resultColumnValue.getFamily()));
93 System.out.print("value = " + new String(resultColumnValue.getValue()));
94 System.out.print("timestamp = " + resultColumnValue.getTimestamp());
95 }
96
97 transport.close();
98 }
99 }