使用java程序操作hbase数据库

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;

import java.io.IOException;
import java.util.Map;
import java.util.NavigableMap;

public class HbaseTest {

    public static Configuration conf;

    static {
        //使用HBaseConfiguration的单例方法实例化
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.93.101");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
    }

    @Test
    public void isTableExist() throws IOException {
        //在HBase中管理、访问表需要先创建HBaseAdmin对象
        Connection connection = ConnectionFactory.createConnection(conf);
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
//        HBaseAdmin admin = new HBaseAdmin(conf);
        boolean flag = admin.tableExists(TableName.valueOf("student"));
        System.out.println("表已经存在:"+flag);
    }

    @org.junit.Test
    public void createTable() throws Exception {
        //在HBase中管理、访问表需要先创建HBaseAdmin对象
        Connection connection = ConnectionFactory.createConnection(conf);
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
        if (!admin.tableExists(TableName.valueOf("test:teacher"))){
            TableDescriptorBuilder bulilder=
                    TableDescriptorBuilder.newBuilder(TableName.valueOf("test:teacher"));
            ColumnFamilyDescriptorBuilder cfdb=
                    ColumnFamilyDescriptorBuilder.newBuilder("info".getBytes());
            cfdb.setMaxVersions(20);
            ColumnFamilyDescriptor cfd=cfdb.build();
            bulilder.setColumnFamily(cfd);
            TableDescriptor td=bulilder.build();
            //4。指向相关DDL操作
            admin.createTable(td);
            System.out.println("表创建成功");
        }else {
            System.out.println("表已经存在!");
        }
    }

    @org.junit.Test
    public void insert() throws IOException {
        //在HBase中管理、访问表需要先创建HBaseAdmin对象
        Connection conn = ConnectionFactory.createConnection(conf);
        HTable hTable = (HTable) conn.getTable(TableName.valueOf("test:teacher"));
        Put put = new Put("1001".getBytes());
        put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("lisi"));
        put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes("30"));
        put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("gender"),Bytes.toBytes("male"));
        put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("phone"),Bytes.toBytes("321312"));
        hTable.put(put);

    }

    /*
        得到某一行的数据
     */
    @org.junit.Test
    public void getInfoRow() throws Exception {
        Connection conn = ConnectionFactory.createConnection(conf);
        Table table = conn.getTable(TableName.valueOf("student"));
        Get get = new Get("1001".getBytes());
        get.setTimestamp(1573197358407L);//显示指定时间戳的版本
        Result result = table.get(get);
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            System.out.print("行键: " + Bytes.toString(result.getRow()));
            System.out.print("列族: " + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.print("列: " + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.print("值: " + Bytes.toString(CellUtil.cloneValue(cell)));
            System.out.print("时间戳: " + cell.getTimestamp());
            System.out.println();
        }


    }

    @org.junit.Test
    public void getALLInfoScan() throws Exception {
        Connection conn = ConnectionFactory.createConnection(conf);
        Table table = conn.getTable(TableName.valueOf("student"));
        Scan scan = new Scan();
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                //得到rowkey
                System.out.print("行键: " + Bytes.toString(CellUtil.cloneRow(cell)));
                //得到列族
                System.out.print("列族 " + Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.print("列: " + Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.out.print("值: " + Bytes.toString(CellUtil.cloneValue(cell)));
                System.out.println();
            }
        }

    }

    @org.junit.Test
    public void getAllInfoMap() throws IOException {
        Connection conn = ConnectionFactory.createConnection(conf);

        Table table = conn.getTable(TableName.valueOf("student"));
        Get get = new Get(Bytes.toBytes("1001"));
        //通过行健得到列族的信息表
        Result result = table.get(get);
        //通过行健得到列族的映射信息
        NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = result.getMap();
        for (Map.Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> m : map.entrySet()) {
            //拿到列族的信息
            String cf_key = new String(m.getKey());
            NavigableMap<byte[], NavigableMap<Long, byte[]>> qv = m.getValue();
            for (Map.Entry<byte[], NavigableMap<Long, byte[]>> m1 : qv.entrySet()) {
                //拿到时间戳的信息
                String qf_key = new String(m1.getKey());
                NavigableMap<Long, byte[]> vv = m1.getValue();
                for (Map.Entry<Long, byte[]> vvs : vv.entrySet()) {
                    long version = vvs.getKey();
                    //拿到值的信息
                    String cell_value = new String(vvs.getValue());
                    System.out.println("列族:" + cf_key + " 列名:" + qf_key + " " +
                            " 版本:" + version + " cell值:" + cell_value);
                }
            }
        }
    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值