Hbase shell 和 Java API 1

Hbase shell

在Hbase中,namespace(名字空间)相当于关系型数据库的数据库(database)。Hbase是一种非关系型数据库,以键值对(key-value)形式进行存储。所以,对于Hbase数据库的CRUD与Map的操作很相似: put(插入),get(查询)

$>hbase shell							//登录shell终端.
$hbase>help								//查看帮助
$hbase>help	'list_namespace'			//查看特定的命令帮助
$hbase>list_namespace					//列出名字空间(数据库)
$hbase>list_namespace_tables 'defalut'	//列出名字空间(数据库)
$hbase>create_namespace 'ns1'			//创建名字空间
$hbase>create 'ns1:t1','f1'				//创建指定空间下的表(形如:名字空间:表名),后面跟上列族

$hbase>put 'ns1:t1','row1','f1:id',100		//插入数据
$hbase>put 'ns1:t1','row1','f1:name','tom'	

$hbase>get 'ns1:t1','row1'					//查询指定行的值
$hbase>scan 'ns1:t1'						//扫描表(全表扫描)

$hbase>flush 'ns1:t1'		//清理内存数据到磁盘。
$hbase>count 'ns1:t1'		//统计函数(统计表中的数据)
$hbase>disable 'ns1:t1'		//删除表之前需要禁用表
$hbase>drop 'ns1:t1'		//删除表

$hbase>scan 'hbase:meta'	//查看元数据表(元数据表非常重要,存放了所建表的一切信息)
$hbase>split 'ns1:t1'		//切割表 
$hbase>split ''				//切割区域

//移动区域
move 区域标识符, 被移动区域主机标识(主机名,端口号,服务器起始码)
move '5ecb5fb4f7a070cb894420c518c1327c', 's202,16020,1534674768444'

//分割区域
split 区域名称, 行号
split 'ns1:t2,,1534675408158.5ecb5fb4f7a070cb894420c518c1327c.', 'row8888'

//合并两个区域
merge_region 第一个区域标识符(MD5), 第二个区域标识符(MD5)
merge_region 'fea835d1a68ec6c7a6a7c98abe0f748d', '210d7754edad92e3e1fb067e803f04e7'
Hbase的 Java API
//maven依赖
<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client</artifactId>
    <version>1.2.6</version>
</dependency>


  /**
     * 插入数据
     */
    @Test
    public void put() throws Exception {
        //创建conf对象
        Configuration conf = HBaseConfiguration.create();
        //通过连接工厂创建连接对象
        Connection conn = ConnectionFactory.createConnection(conf);
        //通过连接查询tableName对象
        TableName tname = TableName.valueOf("ns1:t1");
        //获得table
        Table table = conn.getTable(tname);

        //通过bytes工具类创建字节数组
        byte[] rowid = Bytes.toBytes("row3");

        //创建put对象
        Put put = new Put(rowid);

        byte[] f1 = Bytes.toBytes("f1");
        byte[] id = Bytes.toBytes("id");
        byte[] value = Bytes.toBytes(100);
        put.addColumn(f1, id, value);

        //执行插入
        table.put(put);
    }

    /**
     * 得到数据
     */
    @Test
    public void get() throws Exception {
        //创建conf对象
        Configuration conf = HBaseConfiguration.create();
        //通过连接工厂创建连接对象
        Connection conn = ConnectionFactory.createConnection(conf);
        //通过连接查询tableName对象
        TableName tname = TableName.valueOf("ns1:t1");
        //获得table
        Table table = conn.getTable(tname);

        //通过bytes工具类创建字节数组(将字符串)
        Get get = new Get(Bytes.toBytes("row3"));
        Result r = table.get(get);
        byte[] idvalue = r.getValue(Bytes.toBytes("f1"), Bytes.toBytes("id"));
        System.out.println(Bytes.toInt(idvalue));
    }

    /**
     * 导入大量数据
     */
    @Test
    public void bigPut() throws Exception {
        DecimalFormat format = new DecimalFormat();
        format.applyPattern("0000");

        long start = System.currentTimeMillis();
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tname = TableName.valueOf("ns1:t2");
        HTable table = (HTable) conn.getTable(tname);
        //不要自动清理缓冲区
        table.setAutoFlush(false);

        for (int i = 1; i < 10000; i++) {
            Put put = new Put(Bytes.toBytes("row" + format.format(i)));
            //关闭写前日志
            put.setWriteToWAL(false);
            put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("id"), Bytes.toBytes(i));
            put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("name"), Bytes.toBytes("tom" + i));
            put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("age"), Bytes.toBytes(i % 100));
            table.put(put);

            if (i % 200 == 0) {
                table.flushCommits();
            }
        }
        //剩余不足的刷新提交
        table.flushCommits();
        System.out.println(System.currentTimeMillis() - start);
    }

    /**
     * 创建名字空间
     */
    @Test
    public void createNameSpace() throws Exception {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        Admin admin = conn.getAdmin();
        //创建名字空间描述符
        NamespaceDescriptor nsd = NamespaceDescriptor.create("ns2").build();
        admin.createNamespace(nsd);
    }

    /**
     * 列出名字空间
     */
    @Test
    public void listNameSpaces() throws Exception {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        Admin admin = conn.getAdmin();

        NamespaceDescriptor[] ns = admin.listNamespaceDescriptors();
        for (NamespaceDescriptor n : ns) {
            System.out.println(n.getName());
        }
    }

    /**
     * 创建表
     */
    @Test
    public void createTable() throws Exception {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        Admin admin = conn.getAdmin();
        //创建表名对象
        TableName tableName = TableName.valueOf("ns2:t1");
        //创建表描述符对象
        HTableDescriptor tbl = new HTableDescriptor(tableName);
        //创建列族描述符对象
        HColumnDescriptor col = new HColumnDescriptor("f1");
        tbl.addFamily(col);

        admin.createTable(tbl);
        System.out.println("over");
    }

    /**
     * 删除表:需要先禁用表
     * > disable 禁用表
     * > enable 启用表
     */
    @Test
    public void dropTable() throws Exception {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        Admin admin = conn.getAdmin();
        //禁用表
        admin.disableTable(TableName.valueOf("ns2:t1"));
        //删除表
        admin.disableTable(TableName.valueOf("ns2:t1"));
    }

    /**
     * 删除数据
     */
    @Test
    public void deleteData() throws Exception {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tname = TableName.valueOf("ns1:t2");

        Table table = conn.getTable(tname);
        Delete del = new Delete(Bytes.toBytes("row0001"));
        del.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("id"));
        del.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("name"));

        table.delete(del);

        System.out.println("over");
    }

    /**
     * 测试全表扫描
     */
    @Test
    public void scan() throws Exception {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tname = TableName.valueOf("ns1:t2");
        Table table = conn.getTable(tname);

        //创建一个扫描器对象
        Scan scan = new Scan();
        //设置起始行
        scan.setStartRow(Bytes.toBytes("row5000"));
        //设置结束行
        scan.setStopRow(Bytes.toBytes("row8000"));

        ResultScanner rs = table.getScanner(scan);
        Iterator<Result> it = rs.iterator();
        while (it.hasNext()) {
            Result r = it.next();
            byte[] name = r.getValue(Bytes.toBytes("f1"), Bytes.toBytes("name"));
            System.out.println(Bytes.toString(name));
        }
    }

    /**
     * 动态遍历
     */
    @Test
    public void scan2() throws Exception {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tname = TableName.valueOf("ns1:t2");
        Table table = conn.getTable(tname);

        //创建一个扫描器对象
        Scan scan = new Scan();
        //设置起始行
        scan.setStartRow(Bytes.toBytes("row5000"));
        //设置结束行
        scan.setStopRow(Bytes.toBytes("row8000"));

        ResultScanner rs = table.getScanner(scan);
        Iterator<Result> it = rs.iterator();
        while (it.hasNext()) {
            Result r = it.next();
            Map<byte[], byte[]> map = r.getFamilyMap(Bytes.toBytes("f1"));
            for (Map.Entry<byte[], byte[]> entrySet : map.entrySet()) {
                String col = Bytes.toString(entrySet.getKey());
                String val = Bytes.toString(entrySet.getValue());
                System.out.print(col + ":" + val + ",");
            }
            System.out.println();
        }
    }

    /**
     * 动态遍历
     */
    @Test
    public void scan3() throws Exception {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tname = TableName.valueOf("ns1:t2");
        Table table = conn.getTable(tname);

        //创建一个扫描器对象
        Scan scan = new Scan();
        //设置起始行
        scan.setStartRow(Bytes.toBytes("row5000"));
        //设置结束行
        scan.setStopRow(Bytes.toBytes("row8000"));

        ResultScanner rs = table.getScanner(scan);
        Iterator<Result> it = rs.iterator();
        while (it.hasNext()) {
            Result r = it.next();
            //得到一行中的所有Map,key=f1(列族),value=Map>
            NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = r.getMap();
            for (Map.Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> entry : map.entrySet()) {
                //得到列族
                String f1 = Bytes.toString(entry.getKey());
                Map<byte[], NavigableMap<Long, byte[]>> colDataMap = entry.getValue();
                for (Map.Entry<byte[], NavigableMap<Long, byte[]>> ets : colDataMap.entrySet()) {
                    //得到列
                    String c = Bytes.toString(ets.getKey());
                    Map<Long, byte[]> tsValueMap = ets.getValue();
                    for (Map.Entry<Long, byte[]> e : tsValueMap.entrySet()) {
                        //得到时间戳
                        Long ts = e.getKey();
                        String value = Bytes.toString(e.getValue());
                        System.out.print(f1 + ":" + c + ":" + ts + "=" + value + ",");
                    }
                }
            }
            System.out.println();
        }
    }

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