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

时间:2022-04-06 20:01:38

1. 启动REST服务

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

bin/hbase rest start

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

bin/hbase-daemon.sh start rest

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

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

2.访问服务端的数据

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

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

3.使用Java API访问

 package rest;

 import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.rest.client.Client;
import org.apache.hadoop.hbase.rest.client.Cluster;
import org.apache.hadoop.hbase.rest.client.RemoteHTable;
import org.apache.hadoop.hbase.util.Bytes;
import util.HBaseHelper; import java.io.IOException; /**
* Created by root on 15-1-9.
*/
public class RestExample {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create(); HBaseHelper helper = HBaseHelper.getHelper(conf);
helper.dropTable("testtable");
helper.createTable("testtable", "colfam1");
System.out.println("Adding rows to table...");
helper.fillTable("testtable", 1, 10, 5, "colfam1"); Cluster cluster=new Cluster();
cluster.add("hadoop",8080); Client client=new Client(cluster); RemoteHTable table=new RemoteHTable(client,"testtable"); Get get = new Get(Bytes.toBytes("row-30"));
get.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("col-3"));
Result result1 = table.get(get); System.out.println("Get result1: " + result1); Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes("row-10"));
scan.setStopRow(Bytes.toBytes("row-15"));
scan.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("col-5"));
ResultScanner scanner = table.getScanner(scan);
for (Result result2 : scanner) {
System.out.println("Scan row[" + Bytes.toString(result2.getRow()) +
"]: " + result2);
} } }