HBase应用Java API的HBaseAdmin来创建和编辑模式

远程 链接 hbase之前,保证hadoop集群上做HDFS 确认是运行着的,Hbase完全分布式HA环境搭架参考:http://blog.csdn.net/liulihui1988/article/details/75085428

HBase 的 Schema 设计

  • 模式(Schema) 创建
Configuration config = HBaseConfiguration.create();  
HBaseAdmin admin = new HBaseAdmin(conf);    
String table = "myTable";

admin.disableTable(table);           

HColumnDescriptor cf1 = ...;
admin.addColumn(table, cf1);      // adding new ColumnFamily
HColumnDescriptor cf2 = ...;
admin.modifyColumn(table, cf2);    // modifying existing ColumnFamily

admin.enableTable(table);
  • Java API的HBaseAdmin 表创建
package com.jxl.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.junit.Test;

public class PhoneTest {
    @Test
    public void test1() throws Exception {

        Configuration config = HBaseConfiguration.create();
        // 链接zookeeper
        config.set("hbase.zookeeper.quorum","centos-node6,centos-node7,centos-node8");
        HBaseAdmin admin = new HBaseAdmin(config);

        String table = "t_cdr";

        if (admin.isTableAvailable(table)) {//判断表是否存在
            admin.disableTable(table);//关闭表
            admin.deleteTable(table);//删除表
        } else {
            final HTableDescriptor t = new HTableDescriptor(table.getBytes());
            final HColumnDescriptor cf1 = new HColumnDescriptor(
                    "cf1".getBytes());
            t.addFamily(cf1);
            admin.createTable(t);
        }
        admin.close();
    }
}

这里写图片描述

  • Java API的HBaseAdmin 表数据插入
package com.jxl.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.junit.Test;

public class PutPhoneTest {
    @Test
    public void test() throws Exception{
        Configuration config = HBaseConfiguration.create();
        // 链接zookeeper
        config.set("hbase.zookeeper.quorum","centos-node6,centos-node7,centos-node8");
        String table = "t_cdr";
        final HTable h = new HTable(config, table);

        String rowKye = "18698587213"+System.currentTimeMillis();
        final Put put = new Put(rowKye.getBytes());

        put.add("cf1".getBytes(), "dest".getBytes(), "18678587214".getBytes());
        put.add("cf1".getBytes(), "type".getBytes(), "1".getBytes());
        put.add("cf1".getBytes(), "time".getBytes(), "2017-07-21 16:51:30".getBytes());

        h.put(put);
        h.close();
    }

}

这里写图片描述

  • Java API的HBaseAdmin 表对象数据查询
package com.jxl.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.junit.Test;

public class GetPhoneTest{

    @Test
    public void test()throws Exception{
        Configuration config = HBaseConfiguration.create();
        // 链接zookeeper
        config.set("hbase.zookeeper.quorum","centos-node6,centos-node7,centos-node8");
        String table = "t_cdr";
        final HTable h = new HTable(config, table);

        String rowKey = "186985872131500627144537";
        final Get get = new Get(rowKey.getBytes());

        final Result result = h.get(get);
        final Cell c1 = result.getColumnLatestCell("cf1".getBytes(), "type".getBytes());
        System.out.println(new String(c1.getValue()));

        final Cell c2 = result.getColumnLatestCell("cf1".getBytes(), "dest".getBytes());
        System.out.println(new String(c2.getValue()));

        final Cell c3 = result.getColumnLatestCell("cf1".getBytes(), "time".getBytes());
        System.out.println(new String(c3.getValue()));

        h.close();
    }
}

这里写图片描述

  • Java API的HBaseAdmin 表集合数据查询
package com.jxl.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.junit.Test;

public class ListPhoneTest {

    @Test
    public void test() throws Exception{

        Configuration config = HBaseConfiguration.create();
        // 链接zookeeper
        config.set("hbase.zookeeper.quorum","centos-node6,centos-node7,centos-node8");
        String table = "t_cdr";
        final HTable h = new HTable(config, table);

        //rowKey是按照字典排序
        final Scan scan = new Scan();
        scan.setStartRow("186985872131500630770000".getBytes());
        scan.setStopRow("186985872131500630779999".getBytes());
        final ResultScanner rs = h.getScanner(scan);
        for(Result res:rs){
            final Cell c1 = res.getColumnLatestCell("cf1".getBytes(), "type".getBytes());
            System.out.println(new String(c1.getValue()));
            final Cell c2 = res.getColumnLatestCell("cf1".getBytes(), "dest".getBytes());
            System.out.println(new String(c2.getValue()));
            final Cell c3 = res.getColumnLatestCell("cf1".getBytes(), "time".getBytes());
            System.out.println(new String(c3.getValue()));
        }
        h.close();
    } 
}

Java API的HBaseAdmin 对数据表操作测试完成!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值