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();
    }
}

HBase应用Java API的HBaseAdmin来创建和编辑模式_第1张图片

  • 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();
    }

}

HBase应用Java API的HBaseAdmin来创建和编辑模式_第2张图片

  • 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();
    }
}

HBase应用Java API的HBaseAdmin来创建和编辑模式_第3张图片

  • 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 对数据表操作测试完成!

你可能感兴趣的:(hadoop)