hbase(四)--使用java通过thrift操作hbase

一、简介
对hbase的数据操作,一种方式是直接通过hbase对应的java api进行操作,另一种是通过thrift连接
hbase进行操作。这节将对使用thrift连接对hbase数据操作进行介绍。
二、安装thrift
1、下载thrift
 wget http://mirror.bit.edu.cn/apache/thrift/0.11.0/thrift-0.11.0.tar.gz
2、解压thrift
tar -zxvf thrift-0.11.0.tar.gz
3、安装thrift依赖的库
apt-get install automake bison flex g++ git libboost1.55 libevent-dev libssl-dev libtool make pkg-config
4、编译安装thrift
cd thrift-0.11.0
./configure
make
sudo make install
5、验证安装
thrift -version
三、打开hbase的thrift服务
对于hbase的安装在此略过,详情可查看前文。
1、查看帮助
thrift命令格式为:bin/hbase thrift2 option
通过命令bin/hbase thrift2 -h 可以查看启动thrift的常用命令常用(option),下面介绍几个部分:
1)  -b:指定绑定到thrift server的地址;
2)  -p:指定server服务的端口;
3)  -h:查看帮助。
2、启动服务
这里使用守护进行启动thrift服务(非守护进行使用bin/hbase thrift2 start)。
bin/hbase-daemon.sh start thrift2
启动的默认连接端口是9090,另外可能过默认web端口9095在浏览器中查看服务。
四、代码操作
1、添加maven依赖


    org.apache.hbase
    hbase-client
    2.0.0-beta-1



    org.apache.hbase
    hbase-common
    2.0.0-beta-1



    org.apache.hbase
    hbase-thrift
    1.0.0

2、数据操作
仍然直接上代码,解释请查看注释。

import org.apache.hadoop.hbase.thrift2.generated.*;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;

public class HbaseThriftMain {
    public static void main(String[] args) throws Exception {
        //创建连接
        TTransport transport = new TSocket("192.168.0.1", 9090, 5000);
        TProtocol protocol = new TBinaryProtocol(transport);
        THBaseService.Iface client = new THBaseService.Client(protocol);
        transport.open();

        //指定表名
        ByteBuffer table = ByteBuffer.wrap("stu".getBytes());
        //指定行键
        ByteBuffer row = ByteBuffer.wrap("row1".getBytes());
        //指定列簇
        ByteBuffer family = ByteBuffer.wrap("info".getBytes());
        //指定列名
        ByteBuffer qualifier = ByteBuffer.wrap("name".getBytes());

        //查询
        TGet get = new TGet();
        get.setRow(row);
        TColumn col = new TColumn()
                .setFamily(family) //不指定列簇,即是获取所有列簇
                .setQualifier(qualifier);//不指定列名,即是获取所有列
        get.setColumns(Arrays.asList(col));
        TResult result = client.get(table, get);
        System.out.println(new String(result.getRow()));
        result.getColumnValues().forEach(c -> {
            System.out.println(new String(result.getRow()) + "  " + new String(c.getFamily()) + "_" + new String(c.getQualifier()) + ":" + new String(c.getValue()));
        });

        //批量查询
        TScan scan = new TScan();
        List resultList = client.getScannerResults(table, scan, 2);
        resultList.forEach(r -> r.getColumnValues().forEach(c -> {
            System.out.println(new String(r.getRow()) + "  " + new String(c.getFamily()) + "_" + new String(c.getQualifier()) + ":" + new String(c.getValue()));
        }));

        //保存或更新
        TPut put = new TPut();
        put.setRow("row1".getBytes());
        TColumnValue colVal = new TColumnValue();  //定义属性值
        colVal.setFamily(family);
        colVal.setQualifier("name".getBytes());
        colVal.setValue("banana".getBytes());
        put.setColumnValues(Arrays.asList(colVal));
        client.put(table, put);

        //删除
        TDelete delete = new TDelete();
        delete.setRow("row1".getBytes());
//        delete.setColumns(Arrays.asList(new TColumn().setFamily(family).setQualifier("name".getBytes()))); //只删除单个属性
        client.deleteSingle(table, delete);

        transport.close();
    }
}





 




 

你可能感兴趣的:(大数据)