hbase的api使用:scan,get,put,delete

  • 一般化的curd
  • 批次查询,插入
    在idea中导入maven依赖,创建Junit Test类
	
            org.apache.hbase
            hbase-client
            1.2.6
        

编写@before方法初始化属性,创建database, table

	Configuration conf;
	Connection conn;
	Admin admin;

	@Before
	public void before()throws IOException{
		 conf = HBaseConfiguration.create();
		 conn = ConnectionFactory.createConnection(conf) ;
		 admin = conn.getAdmin();
	}
	/**
	 * 创建database
	 */
	@Test
	public void testCreateNamespace() throws IOException {
		//名字空间描述符
		NamespaceDescriptor nd = NamespaceDescriptor.create("ns2").build();
		admin.createNamespace(nd);
		admin.close();
	}
	/**
	 * 创建表
	 */
	@Test
	public void testCreatTable() throws IOException {
		//创建表明对象
		TableName tname = TableName.valueOf("ns2:t2") ;
		HTableDescriptor td = new HTableDescriptor(tname) ;
		//创建列族
		HColumnDescriptor f1 = new HColumnDescriptor("f1") ;
		HColumnDescriptor f2 = new HColumnDescriptor("f2") ;

		//给表添加列族
		td.addFamily(f1) ;
		td.addFamily(f2) ;

		admin.createTable(td);
		admin.close();
	}

1,测试hbase:增,删,查询

	/**
	 * 插入数据
	 */
	@Test
	public void testPut() throws IOException {
		Table t = conn.getTable(TableName.valueOf("ns2:t2")) ;
		Put put = new Put(Bytes.toBytes("row2")) ;
		put.addColumn(Bytes.toBytes("f1") , Bytes.toBytes("id") , Bytes.toBytes(2)) ;
		put.addColumn(Bytes.toBytes("f1") , Bytes.toBytes("name") , Bytes.toBytes("tom2")) ;
		put.addColumn(Bytes.toBytes("f1") , Bytes.toBytes("age") , Bytes.toBytes(11)) ;
		t.put(put);
		t.close();
		conn.close();
	}
	/**
	 *删除数据
	 */
	@Test
	public void testDelete() throws IOException {
		Table t = conn.getTable(TableName.valueOf("ns2:t2")) ;
		Delete d = new Delete(Bytes.toBytes("row1")) ;
		d.addColumn(Bytes.toBytes("f1") , Bytes.toBytes("id")) ;
		t.delete(d);
		t.close();
		conn.close();
	}
	/**
	 * 查询: 获取某一行 某列
	 */
	@Test
	public void testGet() throws IOException {
		Table t = conn.getTable(TableName.valueOf("ns2:t2")) ;
		Get get = new Get(Bytes.toBytes("row1")) ;
//		get.addColumn(Bytes.toBytes("f1") , Bytes.toBytes("name")) ;
		Result r = t.get(get) ;
        List cells = r.listCells();
        System.out.println("=================");
        for (Cell cell : cells) {
            String row = Bytes.toString(CellUtil.cloneRow(cell));
            String f = Bytes.toString(CellUtil.cloneFamily(cell));
            String col = Bytes.toString(CellUtil.cloneQualifier(cell));
            long ver = cell.getTimestamp();
            String val = Bytes.toString(CellUtil.cloneValue(cell));
            System.out.printf("%s/%s:%s/%d=%s\r\n" , row,f,col,ver,val);
        }
		t.close();
		conn.close();
	}

	/**
	 * 查询: 扫描多行
	 */
	@Test
	public void testScan() throws IOException {
		Table t = conn.getTable(TableName.valueOf("ns2:t2")) ;
		Scan scan = new Scan() ;
		ResultScanner scanner = t.getScanner(scan) ;
		Iterator it = scanner.iterator();
		while(it.hasNext()){
            Result r = it.next();
            List cells = r.listCells();
            System.out.println("=================");
            for (Cell cell : cells) {
                String row = Bytes.toString(CellUtil.cloneRow(cell));
                String f = Bytes.toString(CellUtil.cloneFamily(cell));
                String col = Bytes.toString(CellUtil.cloneQualifier(cell));
                long ver = cell.getTimestamp();
                String val = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.printf("%s/%s:%s/%d=%s\r\n" , row,f,col,ver,val);
            }
		}
		t.close();
		conn.close();
	}

2, 优化hbase查询: 批次查询,缓存

	/**
	 * 带缓存查询:scan.setCaching(10000) ;
	 */
	@Test
	public void cacheScan() throws IOException {
		Table t = conn.getTable(TableName.valueOf("ns2:t2"));
		Scan scan = new Scan();
		//切忌全表扫描,设置起始结束行
		scan.setStartRow(Bytes.toBytes("row000001")) ;
		scan.setStopRow(Bytes.toBytes("row010001")) ;
		System.out.println(scan.getCaching());
		scan.setCaching(10000) ;

		ResultScanner scanner = t.getScanner(scan);
		Iterator it = scanner.iterator();
		long start = System.currentTimeMillis() ;
		int index = 0 ;
		while (it.hasNext()) {
			it.next().getRow();
			//System.out.println(index ++);
		}
		System.out.println(System.currentTimeMillis() - start);
		t.close();
		conn.close();
	}

	/**
	 * 批次查询: scan.setBatch(10);
	 */
	@Test
	public void batchScan() throws IOException {
		Table t = conn.getTable(TableName.valueOf("ns2:t3"));
		Scan scan = new Scan();
		scan.setBatch(10);
		Iterator it = t.getScanner(scan).iterator() ;
		while(it.hasNext()){
            Result r = it.next();
            List cells = r.listCells();
            System.out.println("=================");
            for (Cell cell : cells) {
                String row = Bytes.toString(CellUtil.cloneRow(cell));
                String f = Bytes.toString(CellUtil.cloneFamily(cell));
                String col = Bytes.toString(CellUtil.cloneQualifier(cell));
                long ver = cell.getTimestamp();
                String val = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.printf("%s/%s:%s/%d=%s\r\n" , row,f,col,ver,val);
            }
		}
		t.close();
		conn.close();
	}
}

3,hbase导入数据优化: 批量导入

	/**
	 * 批量插入的: 批次--时间消耗
	 */
	@Test
	public void testBatchInsert() throws Exception {
		Configuration conf = HBaseConfiguration.create();
		Connection conn = ConnectionFactory.createConnection(conf);
		HTable t = (HTable) conn.getTable(TableName.valueOf("ns2:t2"));
		//关闭自动清理缓冲区
		t.setAutoFlush(false);

		DecimalFormat df = new DecimalFormat("000000") ;
		long start = System.currentTimeMillis() ;
		for(int i =  1 ; i <= 2000 ; i ++){
			Put put = new Put(Bytes.toBytes("row" + df.format(i)));
			put.setDurability(Durability.SKIP_WAL) ;
			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));
			t.put(put);
		}
		System.out.println(System.currentTimeMillis() -start);
		t.close();
		conn.close();
	}

    /**
     * 批量插入的: 多线程+ 批次--时间消耗
     * @throws Exception
     */
    private static Thread insertInThread(final int start,final int end){
        Thread d = new Thread(){
            public void run(){
                try {
                    Configuration conf = HBaseConfiguration.create();
                    Connection conn = ConnectionFactory.createConnection(conf);
                    HTable t = (HTable) conn.getTable(TableName.valueOf("ns2:t2"));
                    //关闭自动清理缓冲区
                    t.setAutoFlush(false);
                    DecimalFormat df = new DecimalFormat("000000");
                    for (int i = start; i <= end; i++) {
                        Put put = new Put(Bytes.toBytes("row" + df.format(i)));
                        put.setDurability(Durability.SKIP_WAL);
                        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));
                        t.put(put);
                    }
                    t.close();
                    conn.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        d.start();
        return d ;
  }

你可能感兴趣的:(大数据hadoop-hbase)