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

转载于:https://my.oschina.net/u/1159254/blog/886645

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值