1. 程式人生 > >HBase的javaApi一個應用(建立刪除表等)

HBase的javaApi一個應用(建立刪除表等)

本文為傳智播客hadoop八天——第六天學習筆記

目的:使用HBase提供的api在eclipse中建立表,刪除表,查詢資料,使用過濾器有選擇的查詢資料

在啟動HBase之前一定要啟動Hadoop和Zookeeper!!今天調了一上午的錯,竟然是因為沒啟動Zookeeper。o(╥﹏╥)o

以下為java程式碼。

package cn.bigdata;

import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import
org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HBaseAdmin; import
org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.filter.BinaryComparator; import org.apache.hadoop.hbase.filter.BinaryPrefixComparator; import
org.apache.hadoop.hbase.filter.ByteArrayComparable; import org.apache.hadoop.hbase.filter.ColumnPrefixFilter; import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; import org.apache.hadoop.hbase.filter.FamilyFilter; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.filter.MultipleColumnPrefixFilter; import org.apache.hadoop.hbase.filter.PrefixFilter; import org.apache.hadoop.hbase.filter.QualifierFilter; import org.apache.hadoop.hbase.filter.RegexStringComparator; import org.apache.hadoop.hbase.filter.RowFilter; import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; import org.apache.hadoop.hbase.filter.SubstringComparator; import org.apache.hadoop.hbase.util.Bytes; import org.junit.Before; import org.junit.Test; public class HbaseDao { private Configuration conf; private HTable testDemo; @Before public void init() throws Exception { conf = HBaseConfiguration.create(); //設定Hbase所依賴的Zookeeper叢集,我使用的是偽分佈模式,所以只有一個節點 conf.set("hbase.zookeeper.quorum", "localhost:2181"); //表名 testDemo = new HTable(conf, "testDemo01"); } /** * 根據鍵名插入資料 * * @throws Exception */ @Test public void testInsert() throws Exception { //鍵名 Put name = new Put(Bytes.toBytes("rk0002")); name.add(Bytes.toBytes("base_info"), Bytes.toBytes("name"), Bytes.toBytes("天使")); Put age = new Put(Bytes.toBytes("rk0002")); age.add(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes("28")); ArrayList<Put> puts = new ArrayList<Put>(); puts.add(name); puts.add(age); testDemo.put(puts); } /** * 刪除表 * * @throws Exception */ @Test public void testDrop() throws Exception { //建立表和刪除表都需要HBaseAdmin HBaseAdmin admin = new HBaseAdmin(conf); admin.disableTable("testDemo01"); admin.deleteTable("testDemo01"); admin.close(); } /** * 根據鍵名取出資料 * * @throws Exception */ @Test public void testGet() throws Exception { Get get = new Get(Bytes.toBytes("rk0002")); get.setMaxVersions(5); Result result = testDemo.get(get); List<Cell> cell = result.listCells(); for (KeyValue kv : result.list()) { String family = new String(kv.getFamily()); System.out.println(family); String qualifier = new String(kv.getQualifier()); System.out.println(qualifier); System.out.println(new String(kv.getValue())); } } public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "localhost:2181"); HBaseAdmin admin = new HBaseAdmin(conf); //表名 TableName name = TableName.valueOf("testDemo01"); HTableDescriptor desc = new HTableDescriptor(name); //列族 HColumnDescriptor base_info = new HColumnDescriptor("base_info"); HColumnDescriptor extra_info = new HColumnDescriptor("base_info"); //版本數 base_info.setMaxVersions(5); desc.addFamily(base_info); desc.addFamily(extra_info); admin.createTable(desc); } }

HBase提供了很多篩選的過濾器,用於scan方法

    /**
     * 多種過濾條件的使用方法
     * @throws Exception
     */
    @Test
    public void testScan() throws Exception{
        HTable table = new HTable(conf, "person_info".getBytes());
        Scan scan = new Scan(Bytes.toBytes("person_rk_bj_zhang_000001"), Bytes.toBytes("person_rk_bj_zhang_000002"));
        // 字首過濾器----針對行鍵
        Filter filter = new PrefixFilter(Bytes.toBytes("rk"));

        // 行過濾器
        ByteArrayComparable rowComparator = new BinaryComparator(
                Bytes.toBytes("person_rk_bj_zhang_000001"));
        RowFilter rf = new RowFilter(CompareOp.LESS_OR_EQUAL, rowComparator);

        /**
         * 假設rowkey格式為:建立日期_釋出日期_ID_TITLE 目標:查詢 釋出日期 為 2014-12-21 的資料
         */
        rf = new RowFilter(CompareOp.EQUAL, new SubstringComparator(
                "_2014-12-21_"));

        // 單值過濾器 1 完整匹配位元組陣列
        new SingleColumnValueFilter("base_info".getBytes(), "name".getBytes(),
                CompareOp.EQUAL, "zhangsan".getBytes());
        // 單值過濾器2 匹配正則表示式
        ByteArrayComparable comparator = new RegexStringComparator("zhang.");
        new SingleColumnValueFilter("info".getBytes(), "NAME".getBytes(),
                CompareOp.EQUAL, comparator);

        // 單值過濾器2 匹配是否包含子串,大小寫不敏感
        comparator = new SubstringComparator("wu");
        new SingleColumnValueFilter("info".getBytes(), "NAME".getBytes(),
                CompareOp.EQUAL, comparator);

        // 鍵值對元資料過濾-----family過濾----位元組陣列完整匹配
        FamilyFilter ff = new FamilyFilter(CompareOp.EQUAL,
                new BinaryComparator(Bytes.toBytes("base_info")) // 表中不存在inf列族,過濾結果為空
        );
        // 鍵值對元資料過濾-----family過濾----位元組陣列字首匹配
        ff = new FamilyFilter(CompareOp.EQUAL, new BinaryPrefixComparator(
                Bytes.toBytes("inf")) // 表中存在以inf打頭的列族info,過濾結果為該列族所有行
        );

        // 鍵值對元資料過濾-----qualifier過濾----位元組陣列完整匹配

        filter = new QualifierFilter(CompareOp.EQUAL, new BinaryComparator(
                Bytes.toBytes("na")) // 表中不存在na列,過濾結果為空
        );
        filter = new QualifierFilter(CompareOp.EQUAL,
                new BinaryPrefixComparator(Bytes.toBytes("na")) // 表中存在以na打頭的列name,過濾結果為所有行的該列資料
        );

        // 基於列名(即Qualifier)字首過濾資料的ColumnPrefixFilter
        filter = new ColumnPrefixFilter("na".getBytes());

        // 基於列名(即Qualifier)多個字首過濾資料的MultipleColumnPrefixFilter
        byte[][] prefixes = new byte[][] { Bytes.toBytes("na"),
                Bytes.toBytes("me") };
        filter = new MultipleColumnPrefixFilter(prefixes);

        // 為查詢設定過濾條件
        scan.setFilter(filter);

        scan.addFamily(Bytes.toBytes("base_info"));
        ResultScanner scanner = testDemo.getScanner(scan);
        for (Result r : scanner) {

            // 直接從result中取到某個特定的value
            byte[] value = r.getValue(Bytes.toBytes("base_info"),
                    Bytes.toBytes("name"));
            System.out.println(new String(value));
        }
        testDemo.close();
    }