Hbase CURD

package com.mao.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;

public class HbaseCURDTest {

    /**
     * hbase插入
     * @throws IOException
     */
    @Test
    public void putTest() throws IOException {
        //创建conf对象
        Configuration conf = HBaseConfiguration.create();
        //通过连接工厂创建连接对象
        Connection conn = ConnectionFactory.createConnection(conf);
        //通过连接查询TableName对象
        TableName tableName = TableName.valueOf("ns1:t1");
        //获得table
        Table table = conn.getTable(tableName);
        //通过bytes工具类创建字节数组(将字符串)
        //put 'ns1:t1','row1','f1:id',100
        byte[] rowid = Bytes.toBytes("row2");

        //创建put对象
        Put put = new Put(rowid);
        byte[] f1 = Bytes.toBytes("f1");
        byte[] id = Bytes.toBytes("age");
        byte[] value = Bytes.toBytes(1001);
        put.addColumn(f1,id,value);

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

    /**
     * hbase 批量插入
     * @throws IOException
     */
    @Test
    public void putManyTest() throws IOException {
        long startTime = System.currentTimeMillis();
        //创建conf对象
        Configuration conf = HBaseConfiguration.create();
        //通过连接工厂创建连接对象
        Connection conn = ConnectionFactory.createConnection(conf);
        //通过连接查询TableName对象
        TableName tableName = TableName.valueOf("ns1:t1");
        //获得table
        Table table = conn.getTable(tableName);
        //通过bytes工具类创建字节数组(将字符串)
        DecimalFormat format = new DecimalFormat();
        format.applyPattern("000000");
        for (int i=5;i<10000;i++){
            byte[] rowid = Bytes.toBytes("row"+i);

            //创建put对象
            Put put = new Put(rowid);
            put.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("id"),Bytes.toBytes(format.format(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);
        }
        System.out.println(System.currentTimeMillis()-startTime);

    }

    /**
     * hbase读取
     * @throws IOException
     */
    @Test
    public void getTest() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tableName = TableName.valueOf("ns1:t1");
        Table table = conn.getTable(tableName);

        Get get = new Get(Bytes.toBytes("row9998"));
        Result result =table.get(get);
        byte[] idValue= result.getValue(Bytes.toBytes("f1"),Bytes.toBytes("id"));
        System.out.println("id:"+Bytes.toInt(idValue));
        byte[] nameValue = result.getValue(Bytes.toBytes("f1"),Bytes.toBytes("name"));
        System.out.println("name: "+Bytes.toString(nameValue));
        byte[] ageValue1= result.getValue(Bytes.toBytes("f1"),Bytes.toBytes("age"));
        System.out.println("age: "+Bytes.toInt(ageValue1));

    }

    /**
     * 数据格式化
     */
    @Test
    public void formatNum(){
        DecimalFormat format = new DecimalFormat();
        format.applyPattern("000000");
        //format.format(1234567.78915333);
        System.out.println( format.format(67.78915333));
    }
    /**
     * 创建名称空间
     */
    @Test
    public void createNameSpace() throws IOException {
        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 GetNameSpaces() throws IOException {
        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 IOException {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        Admin admin = conn.getAdmin();
        //创建表名对象
        TableName tableName = TableName.valueOf("ns2:t2");
        //创建表描述符对象
        HTableDescriptor tb1 = new HTableDescriptor(tableName);
        //创建列族描述符
        HColumnDescriptor col = new HColumnDescriptor("f1");
        tb1.addFamily(col);
        admin.createTable(tb1);
        System.out.println("over");
    }
    /**
     * 禁用表、删除表
     */
    @Test
    public void disableTable() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        Admin admin = conn.getAdmin();
        //禁用表
        admin.disableTable(TableName.valueOf("ns2:t2"));
        //删除表
        admin.deleteTable(TableName.valueOf("ns2:t2"));
    }

    /**
     * 删除数据
     * 如果只有Delete delete = new Delete(Bytes.toBytes("row9998")),则删除此行数据
     * 否则,只删除添加的column
     */
    @Test
    public void deleteData() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tableName = TableName.valueOf("ns1:t1");
        Table table = conn.getTable(tableName);
       //删除整行数据
        Delete delete = new Delete(Bytes.toBytes("row9999"));

        /**
         * 删除row9998的id,name
         * 还有age存在于Hbase中
            Delete delete = new Delete(Bytes.toBytes("row9998"));
            delete.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("id"));
            delete.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("name"));
         */
        /**
         * 删除row9998的id,name,age
         Delete delete = new Delete(Bytes.toBytes("row9998"));
         delete.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("id"));
         delete.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("name"));
         delete.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("age"));
         */
        table.delete(delete);
    }

    /**
     * 遍历数据
     */
    @Test
    public void scanData() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tableName = TableName.valueOf("ns1:t1");
        Table table = conn.getTable(tableName);
        Scan scan = new Scan();
        scan.withStartRow(Bytes.toBytes("row2000"));
        scan.withStopRow(Bytes.toBytes("row3000"));
        ResultScanner rs = table.getScanner(scan);
        Iterator<Result> it = rs.iterator();
        while (it.hasNext()){
            Result r = it.next();
            byte[] id = r.getValue(Bytes.toBytes("f1"),Bytes.toBytes("id"));
            byte[] name = r.getValue(Bytes.toBytes("f1"),Bytes.toBytes("name"));
            byte[] age = r.getValue(Bytes.toBytes("f1"),Bytes.toBytes("age"));
            System.out.println("id = "+Bytes.toInt(id) + ",name = "+Bytes.toString(name) + ",age = "+Bytes.toInt(age) + ",");
        }
    }

    /**
     * 动态遍历
     */
    @Test
    public void scan2() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tableName = TableName.valueOf("ns1:t1");
        Table table = conn.getTable(tableName);
        Scan scan = new Scan();
        scan.withStartRow(Bytes.toBytes("row2000"));
        scan.withStopRow(Bytes.toBytes("row3000"));
        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());
                if (!col.equals("name")){
                    System.out.print(col+":"+Bytes.toInt(entrySet.getValue())+",");
                }else {
                    System.out.print(col+":"+Bytes.toString(entrySet.getValue())+",");
                }
            }
            System.out.println();
        }
    }

    /**
     * 动态遍历
     */
    @Test
    public void scan3() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tname = TableName.valueOf("ns1:t1");
        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<Col,Map<Timestamp,value>>
            NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = r.getMap();
            for (Map.Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> entry : map.entrySet()){
                String f = Bytes.toString(entry.getKey()); //得到列族
                Map<byte[], NavigableMap<Long, byte[]>> colDataMap = entry.getValue();
                for (Map.Entry<byte[], NavigableMap<Long, byte[]>> ets : colDataMap.entrySet()){
                    String col = Bytes.toString(ets.getKey());  //id||name||age
                    Map<Long,byte[]> tsValueMap = ets.getValue();
                    for (Map.Entry<Long,byte[]> e : tsValueMap.entrySet()){
                        Long timeStamp = e.getKey();  //时间戳
                        if (!col.equals("name")){
                            int val = Bytes.toInt(e.getValue()); //获取 id||age 对应的值
                            System.out.print(f+ ":"+ col+":"+val+", timeStamp:"+timeStamp+",   ");
                        }else {
                            String value = Bytes.toString(e.getValue()); //获取name对应的值
                            System.out.print(f+ ":"+ col+":"+value+", timeStamp:"+timeStamp+",   ");
                        }
                    }
                }
            }
            System.out.println();
        }
    }

    /**
     * 按照指定版本数查询
     *
     * 创建表时指定列族的版本数,该列族的所有列都具有相同数量版本
     * -----------------------------------------------------------
     * 	$hbase>create 'ns1:t3',{NAME=>'f1',VERSIONS=>3}			//创建表时,指定列族的版本数。
     * 	$hbase>get 'ns1:t3','row1',{COLUMN=>'f1',VERSIONS=>4}	//检索的时候,查询多少版本,查询的时候是查询最新的版本。
     */
    @Test
    public void getWithVersions() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tname = TableName.valueOf("ns1:t4");
        Table table = conn.getTable(tname);
        Get get = new Get(Bytes.toBytes("row1"));
        //检索所有版本
        get.setMaxVersions();
        Result r = table.get(get);
        List<Cell> cells = r.getColumnCells(Bytes.toBytes("f1"), Bytes.toBytes("name"));
        for (Cell c : cells) {
            long ts = c.getTimestamp();
            System.out.println(c);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/mao502010435/article/details/89351895