HBase单机模式下Java API操作示例

1、环境

    (1)Hbase部署在CentOS下,单机模式,配置的host为:hadoophost

    (2)Hbase版本为1.2.4

2、代码

public class HBasePutExample {
    static Configuration conf = null;    
    static {
        //设置连接信息
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","10.28.16.197");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        conf.setInt("hbase.rpc.timeout",2000);
        conf.setInt("hbase.client.operation.timeout",3000);
        conf.setInt("hbase.client.scanner.timeout.period",6000);
    }
    
    public static void main(String[] args) throws Exception {
        String tableName = "test2";
        String[] colfam1 = new String[]{"colfam1"};
        creatTable(tableName,colfam1);

        HTable table = new HTable(conf, tableName);
//        queryResult(colfam1, table);
        queryByScanFilter(table,colfam1);
//        putData(colfam1, table);
        table.close();
    }

    /**
     * 通过scan方式过滤数据
     * @param table
     * @param colfam1
     */
    private static void queryByScanFilter(HTable table, String[] colfam1) {
        FilterList fList = new FilterList();
        fList.addFilter(new SingleColumnValueFilter(colfam1[0].getBytes(),Bytes.toBytes("quall"), CompareFilter.CompareOp.EQUAL,
               Bytes.toBytes("value-83")));
        Scan scan = new Scan();
        scan.setFilter(fList);
        try {
            ResultScanner scanner = table.getScanner(scan);
            for(Result r=scanner.next();r != null;r = scanner.next()){
                System.out.println(new String(r.getValue(Bytes.toBytes(colfam1[0]),Bytes.toBytes("quall"))));
                System.out.println(new String(r.getValue(Bytes.toBytes(colfam1[0]),Bytes.toBytes("qual2"))));
                for(Cell c: r.listCells()){
                    System.out.println("--row : "+ new String(c.getRowArray()));
                    System.out.println("--column : "+ new String(c.getFamilyArray()));
                    System.out.println("--value : "+ new String(c.getValueArray()));
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 向表中添加数据
     * @param colfam1
     * @param table
     * @throws IOException
     */
    private static void putData(String[] colfam1, HTable table) throws IOException {
        for(int i=3;i<100;i++){
            Put put = new Put(Bytes.toBytes("row"+i));
            put.add(Bytes.toBytes(colfam1[0]),Bytes.toBytes("quall"),Bytes.toBytes("value-"+i));
            put.add(Bytes.toBytes(colfam1[0]),Bytes.toBytes("qual2"),Bytes.toBytes("value-2-"+i));
            table.put(put);
        }
        table.flushCommits();
    }

    /**
     * 根据列族的rowKey获取单行数据
     * @param colfam1
     * @param table
     * @throws IOException
     */
    private static void queryResult(String[] colfam1, HTable table) throws IOException {
        Get get = new Get(Bytes.toBytes("row1"));
        Result result = table.get(get);
        System.out.println("result = "+ new String(result.getValue(colfam1[0].getBytes(),"quall".getBytes())));
        System.out.println(get.familySet().toString());
    }

    /*
  * 创建表
  *
  * @tableName 表名
  *
  * @family 列族列表
  */
    public static void creatTable(String tableName, String[] family) throws Exception {
        Connection conn = ConnectionFactory.createConnection(conf);
//        HBaseAdmin admin =  new HBaseAdmin(conf);
        HBaseAdmin admin = (HBaseAdmin)conn.getAdmin();
        HTableDescriptor desc = new HTableDescriptor(tableName);
        for (int i = 0; i < family.length; i++) {
            desc.addFamily(new HColumnDescriptor(family[i]));
        }
        if (admin.tableExists(tableName)) {
            System.out.println("table Exists!");
            return;
        } else {
            admin.createTable(desc);
            System.out.println("create table Success!");
        }
    }
}

3、运行

运行前,需要在本地环境配置Hbase部署服务器的hosts,否则会连接超时,报主机名找不到,如:

10.28.16.197 hadoophost

猜你喜欢

转载自my.oschina.net/u/1159254/blog/886645