HBase Java API 01:基础操作


HBase版本:1.2.6

1. HBaseUtil.java

import java.io.IOException;
import java.util.Date;
import java.util.List;

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.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
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.util.Bytes;

import com.aura.hbase.utils.HBasePrintUtil;

/**
* HBase基础的CRUD操作
*/
public class HbaseUtil {
    
    private static final String ZOOKEEPER_LIST = "node01:2181,node02:2181,node03:2181";
    private static Configuration conf = HBaseConfiguration.create();
    private static Connection conn;
    private static HBaseAdmin admin;
    private static HTable table;
    
    static {
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", ZOOKEEPER_LIST);
        
        try {
            conn = ConnectionFactory.createConnection(conf);
            admin = (HBaseAdmin) conn.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static HTable getTable(String tableName) throws Exception {
        return (HTable) conn.getTable(TableName.valueOf(tableName));
    }
    
    public static boolean isTableExist(String tableName) throws Exception {
        return admin.tableExists(TableName.valueOf(tableName));
    }

    public static void disableTable(String tableName) throws Exception {
        admin.disableTable(TableName.valueOf(tableName));
    }
    
    public static void enableTable(String tableName) throws Exception {
        admin.enableTable(TableName.valueOf(tableName));
    }
    
    public static boolean isTableDisabled(String tableName) throws Exception {
        return admin.isTableDisabled(TableName.valueOf(tableName));
    }
    
    public static boolean isTableEnabled(String tableName) throws Exception {
        return admin.isTableEnabled(TableName.valueOf(tableName));
    }
    
    public static void getAllTables() throws Exception {
        
        // 方法一
        /*
        TableName[] tables = admin.listTableNames();
        for(TableName  table : tables) {
            System.out.println(table.getNameAsString());
        }
        */
        
        // 方法二
        HTableDescriptor[] descs = admin.listTables();
        for(HTableDescriptor  desc : descs) {
            System.out.println(desc.getNameAsString());
        }
    }

    
    public static void createTable(String tableName, String[] family) throws Exception {
        if(isTableExist(tableName)) {
            dropTable(tableName);
        }
        HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
        if(family != null && family.length > 0) {
            for(String cf :family) {
                HColumnDescriptor hcd = new HColumnDescriptor(cf);
                desc.addFamily(hcd);
            }
        }
        admin.createTable(desc);
    }

    public static void descTable(String tableName) throws Exception {
        table = getTable(tableName);
        HTableDescriptor desc = table.getTableDescriptor();
        HColumnDescriptor[] columnDescs = desc.getColumnFamilies();
        HBasePrintUtil.printHColumnDescriptors(columnDescs);
    }

    public static void dropTable(String tableName) throws Exception {
        if(isTableDisabled(tableName)) {
            admin.deleteTable(TableName.valueOf(tableName));
        }else {
            disableTable(tableName);
            admin.deleteTable(TableName.valueOf(tableName));
        }
    }

    /*
     * 从原表中获取的列不能修改,只能再创建一个新表
     * 方法还有一些问题...
     */
    public void modifyTable(String tableName, String[] addColumn, String[] removeColumn) throws Exception {
        /*
        table = getTable(tableName);
        
        HTableDescriptor newDesc = new HTableDescriptor(TableName.valueOf(tableName));
        HTableDescriptor oldDesc = table.getTableDescriptor();
        HColumnDescriptor[] columnFamilies = oldDesc.getColumnFamilies();
        
        for(String column : addColumn) {
            newDesc.addFamily(new HColumnDescriptor(column));
        }
        
        for(String column : removeColumn) {
            newDesc.removeFamily(Bytes.toBytes(column));
        }
        admin.modifyTable(tableName, newDesc);
        */
    }

    public static void putData(String tableName, String rowKey, String familyName, String columnName, String value)
            throws Exception {
        putData(tableName, rowKey, familyName, columnName, value, new Date().getTime());
    }

    public static void putData(String tableName, String rowKey, String familyName, String columnName, String value,
            long timestamp) throws Exception {
        table = getTable(tableName);
        Put put = new Put(Bytes.toBytes(rowKey));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName), timestamp, Bytes.toBytes(value));
        table.put(put);
    }
    
    public static void putData(String tableName, Put put) throws Exception {
        table = getTable(tableName);
        table.put(put);
    }

    public static void putData(String tableName, List<Put> putList) throws Exception {
        table = getTable(tableName);
        table.put(putList);
    }

    public static Result getResult(String tableName, String rowKey) throws Exception {
        table = getTable(tableName);
        Get get = new Get(Bytes.toBytes(rowKey));
        return table.get(get);
    }

    public static Result getResult(String tableName, String rowKey, String familyName) throws Exception {
        table = getTable(tableName);
        Get get = new Get(Bytes.toBytes(rowKey));
        get.addFamily(Bytes.toBytes(familyName));
        return table.get(get);
    }

    
    public static Result getResult(String tableName, String rowKey, String familyName, String columnName) throws Exception {
        table = getTable(tableName);
        Get get = new Get(Bytes.toBytes(rowKey));
        get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
        return table.get(get);
    }

    
    public static Result getResultByVersion(String tableName, String rowKey, String familyName, String columnName,
            int versions) throws Exception {
        table = getTable(tableName);
        Get get = new Get(Bytes.toBytes(rowKey));
        get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
        get.setMaxVersions(versions);
        return table.get(get);
    }

    public static ResultScanner getResultScann(String tableName) throws Exception {
        table = getTable(tableName);
        Scan scan = new Scan();
        return table.getScanner(scan);
    }

    public static void deleteColumn(String tableName, String rowKey) throws Exception {
        table = getTable(tableName);
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        table.delete(delete);
    }

    public static void deleteColumn(String tableName, String rowKey, String falilyName) throws Exception {
        table = getTable(tableName);
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        delete.addFamily(Bytes.toBytes(falilyName));
        table.delete(delete);
    }

    public static void deleteColumn(String tableName, String rowKey, String falilyName, String columnName) throws Exception {
        table = getTable(tableName);
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        delete.addColumn(Bytes.toBytes(falilyName), Bytes.toBytes(columnName));
        table.delete(delete);
    }

}

2. HBasePrintUtil.java

package com.aura.hbase.utils;

import java.util.List;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.KeyValueUtil;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.util.Bytes;

public class HBasePrintUtil {

    public static void printResultScanner(ResultScanner resultScann) {
        for (Result result : resultScann) {
            printResult(result);
        }
    }

    public static void printResult(Result result) {
        List<Cell> cells = result.listCells();
        for(Cell cell : cells) {
            printCell(cell);
        }
    }

    public static void printCell(Cell cell) {
        System.out.println(
                Bytes.toString(CellUtil.cloneRow(cell)) + "\t" + 
                Bytes.toString(CellUtil.cloneFamily(cell)) + "\t" + 
                Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t" + 
                cell.getTimestamp() + "\t" + 
                Bytes.toString(CellUtil.cloneValue(cell)));
    }

    public static void printHTableDescriptor(HTableDescriptor desc) {
        System.out.println(
                "Name:" + desc.getNameAsString() + "\n" +
                "FlushPolicyClassName:" + desc.getFlushPolicyClassName() + "\n" + 
                "MaxFileSize:" + desc.getMaxFileSize() + "\n" +
                "MemStoreFlushSize:" + desc.getMemStoreFlushSize() + "\n" +
                "RegionReplication:" + desc.getRegionReplication() + "\n" + 
                "RegionSplitPolicyClassName:" + desc.getRegionSplitPolicyClassName() + "\n" +
                "---------------------------------------------");
    }
    
    public static void printHColumnDescriptors(HColumnDescriptor[] columnDescs) {
        for(HColumnDescriptor desc : columnDescs) {
            printHColumnDescriptor(desc);
        }
        
    }
    
    public static void printHColumnDescriptor(HColumnDescriptor desc) {
        System.out.println(
                "Name : " + desc.getNameAsString() + "\n" + 
                "BloomFilterType : " + desc.getBloomFilterType() + "\n" + 
                "MinVersions : " + desc.getMinVersions() + "\n" + 
                "MaxVersions : " + desc.getMaxVersions() + "\n" + 
                "InMemory : " + desc.isInMemory() + "\n" + 
                "DataBlockEncoding : " + desc.getDataBlockEncoding() + "\n" + 
                "TimeToLive : " + desc.getTimeToLive() + "\n" + 
                "Compression : " + desc.getCompression() + "\n" + 
                "BlockCacheEnabled : " + desc.isBlockCacheEnabled() + "\n" + 
                "Blocksize : " + desc.getBlocksize() + "\n" +
                "---------------------------------------------");
    }
    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,992评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,212评论 3 388
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,535评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,197评论 1 287
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,310评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,383评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,409评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,191评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,621评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,910评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,084评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,763评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,403评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,083评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,318评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,946评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,967评论 2 351

推荐阅读更多精彩内容