码迷,mamicode.com
首页 > 编程语言 > 详细

HBase Java API 例子

时间:2017-08-16 17:32:13      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:use   nbsp   else   log   base   mil   stat   stop   script   

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

/**
 * Created by Administrator on 2017/8/16.
 */
public class TestHBase {

    Configuration configuration = null;

    @Before
    public void setUp() throws IOException {
        configuration = HBaseConfiguration.create();
    }

    @Test
    public void testCreateTable() throws IOException {
        String tableName = "jasontest";
        HBaseAdmin admin = new HBaseAdmin(configuration);
        HTableDescriptor desc = new HTableDescriptor(tableName);
        desc.addFamily(new HColumnDescriptor("basic"));
        if(admin.tableExists(tableName)){
            System.out.println("table exist");
        }else{
            admin.createTable(desc);
            System.out.println("create table successfully.");
        }
        admin.close();
    }

    @Test
    public void testGetByRow() throws IOException {
        String tableName = "user";
        HTable table = new HTable(configuration,tableName);
        byte[] row1 = Bytes.toBytes("1000");
        Get get = new Get(row1);
        Result result = table.get(get);
        Cell[] cells = result.rawCells();
        for(Cell cell:cells){
            System.out.println(//
                    Bytes.toString(CellUtil.cloneFamily(cell))+":" //
                            + Bytes.toString(CellUtil.cloneQualifier(cell))+"->" //
                            + Bytes.toString(CellUtil.cloneValue(cell)) //
            );
        }
        table.close();
    }

    @Test
    public void testGetColumn() throws IOException {
        String tableName = "user";
        HTable table = new HTable(configuration,tableName);
        Get get = new Get(Bytes.toBytes("1000"));
        get.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"));
        get.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"));
        Result result = table.get(get);
        Cell[] cells = result.rawCells();
        for(Cell cell:cells) {
            System.out.println(
                    "Column Family is " + Bytes.toString(CellUtil.cloneFamily(cell)) + " | " //
                            + "Column is " + Bytes.toString(CellUtil.cloneQualifier(cell)) + " | " //
                            + "Value is " + Bytes.toString(CellUtil.cloneValue(cell))
            );
        }
        table.close();
    }

    @Test
    public void testScanTable() throws IOException {
        String tableName = "user";
        HTable table = new HTable(configuration,tableName);
        Scan scan = new Scan();
//        scan.setStartRow(Bytes.toBytes("1001"));
//        scan.setStopRow(Bytes.toBytes("1001"));
//        scan.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"));
        Filter filer = new PrefixFilter(Bytes.toBytes("1000"));
        scan.setFilter(filer);
        ResultScanner resultScanner = table.getScanner(scan);
        for(Result result:resultScanner){
            Cell[] cells = result.rawCells();
            for(Cell cell:cells){
                System.out.println( //
                                Bytes.toString(CellUtil.cloneRow(cell))+":"//
                                + Bytes.toString(CellUtil.cloneFamily(cell))+":" //
                                + Bytes.toString(CellUtil.cloneQualifier(cell))+"->" //
                                + Bytes.toString(CellUtil.cloneValue(cell)) //
                );
            }
        }
        table.close();
    }

    @Test
    public void testPut() throws IOException {
        String tableName = "user";
        HTable table = new HTable(configuration,tableName);
        Put put = new Put(Bytes.toBytes("1004"));
        put.add(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("Jason Zheng"));
        put.add(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes(28));
        table.put(put);
        table.close();
    }

    @Test
    public void testDelete() throws IOException {
        String tableName = "user";
        HTable table = new HTable(configuration,tableName);
        Delete delete = new Delete(Bytes.toBytes("1004"));
        delete.deleteColumns(Bytes.toBytes("info"),Bytes.toBytes("name"));
        delete.deleteColumns(Bytes.toBytes("info"),Bytes.toBytes("age"));
//        delete.deleteFamily(Bytes.toBytes("info"));
        table.delete(delete);
        table.close();
    }

}

 

HBase Java API 例子

标签:use   nbsp   else   log   base   mil   stat   stop   script   

原文地址:http://www.cnblogs.com/xdlaoliu/p/7373711.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!