View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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      // use passed in arguments instead of defaults
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      // This is our thrift client.
64      THBaseService.Iface client = new THBaseService.Client(protocol);
65  
66      // open the transport
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  }