美文网首页大数据BigData
HBase客户端Java-API

HBase客户端Java-API

作者: geekAppke | 来源:发表于2018-12-04 16:28 被阅读11次
HBase在存储数据时,没有数据类型!以字节数组存储
"cf".getBytes();
Bytes.toBytes("cf");

环境准备

(1)制作hbase的用户库,并导入
(2)导入hadoop用户库
(3)导入Junit测试库
(4)src目录中拷贝hdfs-site.xmlhbase-site.shregionserversbackup-masters4个文件,build source file.

HBaseDemo

public class HBaseDemo {
    HBaseAdmin admin;
    HTable htable;
    String TN = "phone"; // 表名

    @Before
    public void init() throws Exception {
        Configuration conf = new Configuration();
        // 设置zk配置信息,必须配置,否则无法定位
        conf.set("hbase.zookeeper.quorum", "node002,node003,node004");
        admin = new HBaseAdmin(conf);
        htable = new HTable(conf, TN.getBytes());
    }

    @Test
    public void creatTable() throws Exception {
        if (admin.tableExists(TN)) {
            admin.disableTable(TN);
            admin.deleteTable(TN);
            System.out.println("table exists!");
        }

        // 表描述
        HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(TN));
        // 列族
        HColumnDescriptor cf = new HColumnDescriptor("cf".getBytes());
        desc.addFamily(cf);
        // 创建表
        admin.createTable(desc);
        System.out.println("create table success!");
    }

    @Test
    public void insertDB() throws Exception {
        String rowKey = "111111";
        Put put = new Put(rowKey.getBytes());
        put.add("cf".getBytes(), "name".getBytes(), "xiaohong".getBytes());
        put.add("cf".getBytes(), "age".getBytes(), "23".getBytes());
        put.add("cf".getBytes(), "sex".getBytes(), "women".getBytes());
        htable.put(put);
    }

    @Test
    public void getDB() throws Exception {
        String rowKey = "111111";
        Get get = new Get(rowKey.getBytes());
        // 获取指定的列,不指定的列不去查,开发必须写!不写是全部列!
        // 就像 select * 
        get.addColumn("cf".getBytes(), "name".getBytes());
        get.addColumn("cf".getBytes(), "age".getBytes());
        get.addColumn("cf".getBytes(), "sex".getBytes());
        Result rs = htable.get(get);

        Cell cell = rs.getColumnLatestCell("cf".getBytes(), "name".getBytes());
        Cell cell2 = rs.getColumnLatestCell("cf".getBytes(), "age".getBytes());
        Cell cell3 = rs.getColumnLatestCell("cf".getBytes(), "sex".getBytes());
        // System.out.println(new String(cell.getValue())); 过期了,用下面的工具类
        System.out.println(new String(CellUtil.cloneValue(cell)));
        System.out.println(new String(CellUtil.cloneValue(cell2)));
        System.out.println(new String(CellUtil.cloneValue(cell3)));
    }

    @Test
    public void scan() throws IOException {
        Scan scan = new Scan();
        ResultScanner scanner = htable.getScanner(scan);
  
        for (Result rs : scanner) {
            System.out.println("扫描结果: "+rs);
        }
    }
    
    @After
    public void destory() throws Exception {
        if (admin != null) {
            admin.close();
        }
    }
}
扫描结果: keyvalues={111111/cf:age/1543959930854/Put/vlen=2/mvcc=0, 111111/cf:name/1543959930854/Put/vlen=8/mvcc=0, 111111/cf:sex/1543959930854/Put/vlen=5/mvcc=0}

相关文章

  • Docker(java-api连接Hbase 卡住问题的解决方法

    一. 安装docker单机hbase 二. 现状 连接可以通过,但是java-api无法读写hbase数据 持续连...

  • HBase客户端Java-API

    环境准备 (1)制作hbase的用户库,并导入(2)导入hadoop用户库(3)导入Junit测试库(4)src目...

  • HBase处理通话记录

    HBase客户端Java-API 生产通话记录 统计二月份到三月份的通话记录 查询某个手机号主叫为1 的所有记录 ...

  • HBase系列 - HBase Shell操作

    HBase Shell 基本操作 进入HBase客户端命令行bin/hbase shell 查看帮助命令hbase...

  • 07. HBase数据存取流程解析

    客户端数据存取流程 客户端与HBase系统的写入交互阶段 用户提交put请求后,HBase客户端会将put请求添加...

  • 3、HBase Shell操作

    基本操作 进入HBase客户端命令行[hadoop@hadoop-100 hbase]$ cd bin/hbase...

  • HBaseAPI应用与优化

    HBase API 客户端操作 HBaseAPI 客户端操作 代码: Hbase协处理器 协处理器概述 官方地址[...

  • HBase优化四——HTable优化

    HTable是HBase客户端与HBase服务端通讯的Java API对象,客户端可以通过HTable对象与服务端...

  • HBase设置外网访问集群

    HBase集群搭建在内网,现在需要从外网访问HBase,对数据进行读写。 首先,需要明白HBase客户端连接HBa...

  • HBase客户端API-表管理

    上一篇博客说了怎样搭建HBase环境,今天说说怎样使用 HBase 的客户端 API 来操作 HBase 中的数据...

网友评论

    本文标题:HBase客户端Java-API

    本文链接:https://www.haomeiwen.com/subject/nphmcqtx.html