HBase(印象笔记)及DML操作

373 阅读2分钟

DML操作(Data Manipulation Language 数据操作语言)

package main.java.com.etc;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
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.Before;
import org.junit.Test;

/**
 * @Auther: fengze
 * @Date: 2018/8/6 15:55
 * @Description:
 */
public class HbaseClientDML {

    Connection conn = null;

    @Before
    public void getConn()throws Exception{
        //构建一个连接对象
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","weekend06:2181,weekend07:2181,weekend08:2181");
        conn = ConnectionFactory.createConnection(conf);
    }


    /**
     * 创建表
     */
    @Test
    public  void createTable() {
        System.out.println("start create table ......");
        try {
            Admin admin = conn.getAdmin();
            TableName tableName = TableName.valueOf("user_fengze");
        if (admin.tableExists(tableName)) {// 如果存在要创建的表,那么先删除,再创建
            admin.disableTable(tableName);
            admin.deleteTable(tableName);
            System.out.println(tableName + " is exist,detele....");
            }
            HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
            tableDescriptor.addFamily(new HColumnDescriptor("column1"));
            tableDescriptor.addFamily(new HColumnDescriptor("column2"));
            tableDescriptor.addFamily(new HColumnDescriptor("column3"));
            admin.createTable(tableDescriptor);
        } catch (MasterNotRunningException e) {
            e.printStackTrace();
        } catch (ZooKeeperConnectionException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("end create table ......");
    }

    /**
     * 增
     * 改:put来覆盖
     * @throw Exception
     */
    @Test
    public void testput()throws Exception{

        // 获取一个操作指定表的table对象,进行DML操作
        Table table = conn.getTable(TableName.valueOf("user_fengze"));

        // 构造要插入的数据为一个Put类型(一个put对象只能对应一个rowkey)的对象
        Put put = new Put(Bytes.toBytes("001"));
        put.addColumn(Bytes.toBytes("column1"), Bytes.toBytes("username"), Bytes.toBytes("张三"));
        put.addColumn(Bytes.toBytes("column1"), Bytes.toBytes("age"), Bytes.toBytes("18"));
        put.addColumn(Bytes.toBytes("column2"), Bytes.toBytes("addr"), Bytes.toBytes("北京"));


        Put put2 = new Put(Bytes.toBytes("002"));
        put2.addColumn(Bytes.toBytes("column1"), Bytes.toBytes("username"), Bytes.toBytes("李四"));
        put2.addColumn(Bytes.toBytes("column1"), Bytes.toBytes("age"), Bytes.toBytes("28"));
        put2.addColumn(Bytes.toBytes("column2"), Bytes.toBytes("name"), Bytes.toBytes("上海"));


        ArrayList<Put> puts = new ArrayList<>();
        puts.add(put);
        puts.add(put2);


        // 插进去
        table.put(puts);

        table.close();
        conn.close();
    }

    /**
     * 循环插入大量数据
     * @ throws Exception
     */
    @Test
    public void testManyPuts()throws Exception{
        Table table = conn.getTable(TableName.valueOf("user_fengze"));
        ArrayList<Put> puts = new ArrayList<>();
        for (int i = 0; i <100; i++) {
        Put put = new Put(Bytes.toBytes(""+i));
        put.addColumn(Bytes.toBytes("column3"),Bytes.toBytes("name"),Bytes.toBytes("zhangsan"+i));
        put.addColumn(Bytes.toBytes("column3"),Bytes.toBytes("age"),Bytes.toBytes("18"+i));
        puts.add(put);
        }
        table.put(puts);
        table.close();
        conn.close();

    }

    /**
     * 删
     * @ throws Exception
     */
    @Test
    public void testDelete() throws Exception{
        Table table = conn.getTable(TableName.valueOf("user_fengze"));

        // 构造一个对象封装要删除的数据信息
        Delete delete1 = new Delete(Bytes.toBytes("001"));

        Delete delete2 = new Delete(Bytes.toBytes("002"));

        delete2.addColumn(Bytes.toBytes("column2"),Bytes.toBytes("name"));

        ArrayList<Delete> arrayList = new ArrayList();
        arrayList.add(delete1);
        arrayList.add(delete2);

        table.delete(arrayList);

        table.close();
        conn.close();

    }

    /**
     * 查
     * @ throws Exception
     */
    @Test
    public void tesGet() throws Exception{

        Table table = conn.getTable(TableName.valueOf("user_fengze"));

        Get get = new Get("002".getBytes());

        Result result = table.get(get);


        // 从结果中取用户指定的某个key的value
        byte[] value = result.getValue("column1".getBytes(), "username".getBytes());

        System.out.println(new String(value));
        System.out.println("-------------------------");

        // 遍历整行结果中的所有kv单元格
        CellScanner cellScanner = result.cellScanner();
        while (cellScanner.advance()){
       Cell cell = cellScanner.current();

       byte[] rowArray = cell.getRowArray(); //本kv所属的行键的字节数组
       byte[] familyArray = cell.getFamilyArray(); //列族名的字节数组
       byte[] qualifierArray = cell.getQualifierArray();//列名的字节数据
       byte[] valueArray = cell.getValueArray();// value的字节数组

       System.out.println("行键:"+new String(rowArray,cell.getRowOffset(),cell.getRowLength()));
       System.out.println("列族:"+new String(familyArray,cell.getFamilyOffset(),cell.getFamilyLength()));
       System.out.println("列名:"+new String(qualifierArray,cell.getQualifierOffset(),cell.getQualifierLength()));
       System.out.println("value:"+new String(valueArray,cell.getValueOffset(),cell.getValueLength()));

       table.close();
       conn.close();

        }

    }


    /**
     * 按行键范围查询数据
     * @ throws Exception
     */
    @Test
    public void testScan() throws Exception{
        Table table = conn.getTable(TableName.valueOf("user_fengze"));

        // 包含起始行键,不包含结束行键,但是如果真的想查询出末尾的那个行键,
        // 那么,可以在末尾行键上拼接一个不可见的字节(\000)
        Scan scan = new Scan("10".getBytes(),"10000\001".getBytes());

        ResultScanner scanner = table.getScanner(scan);

        Iterator<Result>iterator = scanner.iterator();

        while (iterator.hasNext()) {
        Result result = iterator.next();

        // 遍历整行结果中的所有kv单元格

        CellScanner cellScanner = result.cellScanner();

        while (cellScanner.advance()) {
        Cell cell = result.current();
        byte[] rowArray = cell.getRowArray();  //本kv所属的行键的字节数组
        byte[] familyArray = cell.getFamilyArray();  //列族名的字节数组
        byte[] qualifierArray = cell.getQualifierArray();  //列名的字节数据
        byte[] valueArray = cell.getValueArray(); // value的字节数组

        System.out.println("行键:" + new String(rowArray, cell.getRowOffset(), cell.getRowLength()));
        System.out.println("列族:" + new String(familyArray, cell.getFamilyOffset(), cell.getFamilyLength()));
        System.out.println("列名:" + new String(qualifierArray, cell.getQualifierOffset(), cell.getQualifierLength()));
        System.out.println("value:" + new String(valueArray, cell.getValueOffset(), cell.getValueLength()));

        }
        }

        table.close();
        conn.close();

    }


}

	@Test
        public void test(){
	   String a = "000";
	   String b = "000\0";
	   
	   System.out.println(a);
	   System.out.println(b);
	   
	   
	   byte[] bytes = a.getBytes();
	   byte[] bytes2 = b.getBytes();
	   
	   System.out.println("");
	   
	}
}