hbase学习笔记-----REST客户端

1. 启动REST服务 

  a.启动一个非守护进程模式的REST服务器(ctrl+c 终止)

     bin/hbase rest start 

  b.启动守护进程模式的REST服务器

     bin/hbase-daemon.sh start rest

  默认启动的是8080端口(可以使用参数在启动时指定端口),可以被访问。eg.  curl  http://<servername>:8080/

2.访问服务端的数据

3.使用Java API访问  

 1 package rest;
 2 
 3 import org.apache.hadoop.conf.Configuration;
 4 import org.apache.hadoop.hbase.HBaseConfiguration;
 5 import org.apache.hadoop.hbase.client.Get;
 6 import org.apache.hadoop.hbase.client.Result;
 7 import org.apache.hadoop.hbase.client.ResultScanner;
 8 import org.apache.hadoop.hbase.client.Scan;
 9 import org.apache.hadoop.hbase.rest.client.Client;
10 import org.apache.hadoop.hbase.rest.client.Cluster;
11 import org.apache.hadoop.hbase.rest.client.RemoteHTable;
12 import org.apache.hadoop.hbase.util.Bytes;
13 import util.HBaseHelper;
14 
15 import java.io.IOException;
16 
17 /**
18  * Created by root on 15-1-9.
19  */
20 public class RestExample {
21     public static void main(String[] args) throws IOException {
22         Configuration conf = HBaseConfiguration.create();
23 
24         HBaseHelper helper = HBaseHelper.getHelper(conf);
25         helper.dropTable("testtable");
26         helper.createTable("testtable", "colfam1");
27         System.out.println("Adding rows to table...");
28         helper.fillTable("testtable", 1, 10, 5, "colfam1");
29 
30         Cluster cluster=new Cluster();
31         cluster.add("hadoop",8080);
32 
33         Client client=new Client(cluster);
34 
35         RemoteHTable table=new RemoteHTable(client,"testtable");
36 
37         Get get = new Get(Bytes.toBytes("row-30")); 
38         get.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("col-3"));
39         Result result1 = table.get(get);
40 
41         System.out.println("Get result1: " + result1);
42 
43         Scan scan = new Scan();
44         scan.setStartRow(Bytes.toBytes("row-10"));
45         scan.setStopRow(Bytes.toBytes("row-15"));
46         scan.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("col-5"));
47         ResultScanner scanner = table.getScanner(scan); 
48         for (Result result2 : scanner) {
49             System.out.println("Scan row[" + Bytes.toString(result2.getRow()) +
50                     "]: " + result2);
51         }
52 
53     }
54 
55 }
View Code

 

posted @ 2015-01-09 15:06  senki  阅读(1342)  评论(0编辑  收藏  举报