黑猴子的家:HBase Java API 基本操作

注意喉童,這部分的學(xué)習(xí)內(nèi)容撇寞,我們先學(xué)習(xí)使用老版本的API,接著再寫出新版本的API調(diào)用方式堂氯。因?yàn)樵谄髽I(yè)中蔑担,有些時(shí)候我們需要一些過時(shí)的API來提供更好的兼容性。

1咽白、HBase API Code -> GitHub

https://github.com/liufengji/HBase_API_Demo.git

2啤握、安裝Maven并配置環(huán)境變量

http://www.reibang.com/p/79544e383b6e

3、新建Maven Project

新建項(xiàng)目后在pom.xml中添加依賴

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-server</artifactId>
    <version>1.3.1</version>
</dependency>

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client</artifactId>
    <version>1.3.1</version>
</dependency>

<dependency>
    <groupId>jdk.tools</groupId>
    <artifactId>jdk.tools</artifactId>
    <version>1.8</version>
    <scope>system</scope>
    <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>

4晶框、編寫HBaseAPI

注意排抬,這部分的學(xué)習(xí)內(nèi)容,我們先學(xué)習(xí)使用老版本的API授段,接著再寫出新版本的API調(diào)用方式蹲蒲。因?yàn)樵谄髽I(yè)中,有些時(shí)候我們需要一些過時(shí)的API來提供更好的兼容性侵贵。

1)首先需要獲取Configuration對象

public static Configuration conf;
static{
    //使用HBaseConfiguration的單例方法實(shí)例化
    conf = HBaseConfiguration.create();
    conf.set("hbase.zookeeper.quorum", "192.168.216.20");
    conf.set("hbase.zookeeper.property.clientPort", "2181");
}

2)判斷表是否存在

public static boolean isTableExist(String tableName) 
throws MasterNotRunningException,ZooKeeperConnectionException, IOException{
    //在HBase中管理届搁、訪問表需要先創(chuàng)建HBaseAdmin對象
    //Connection connection = ConnectionFactory.createConnection(conf);
    //HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
    HBaseAdmin admin = new HBaseAdmin(conf);
    return admin.tableExists(tableName);
}

3)創(chuàng)建表

public static void createTable(String tableName, String... columnFamily) 
  throws MasterNotRunningException, ZooKeeperConnectionException, IOException{

    HBaseAdmin admin = new HBaseAdmin(conf);

    //判斷表是否存在
    if(isTableExist(tableName)){
        System.out.println("表" + tableName + "已存在");
        //System.exit(0);
    }else{
        //創(chuàng)建表屬性對象,表名需要轉(zhuǎn)字節(jié)
        HTableDescriptor descriptor = 
                          new HTableDescriptor(TableName.valueOf(tableName));
        //創(chuàng)建多個(gè)列族
        for(String cf : columnFamily){
            descriptor.addFamily(new HColumnDescriptor(cf));
        }
        //根據(jù)對表的配置,創(chuàng)建表
        admin.createTable(descriptor);
        System.out.println("表" + tableName + "創(chuàng)建成功窍育!");
    }
}

4)刪除表

public static void dropTable(String tableName) 
     throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
    HBaseAdmin admin = new HBaseAdmin(conf);
    if(isExisTables (tableName)){
        admin.disableTable(tableName);
        admin.deleteTable(tableName);
        System.out.println("表" + tableName + "刪除成功卡睦!");
    }else{
        System.out.println("表" + tableName + "不存在!");
    }
}

5)向表中插入數(shù)據(jù)

public static void addRowData(String tableName, String rowKey, 
      String columnFamily, String column, String value) throws IOException{

    //創(chuàng)建HTable對象
    HTable hTable = new HTable(conf, tableName);
    //向表中插入數(shù)據(jù)
    Put put = new Put(Bytes.toBytes(rowKey));
    //向Put對象中組裝數(shù)據(jù)
    put.add(Bytes.toBytes(columnFamily), 
            Bytes.toBytes(column), 
            Bytes.toBytes(value));
    hTable.put(put);
    hTable.close();
    System.out.println("插入數(shù)據(jù)成功");
}

6)刪除一行數(shù)據(jù)

//刪除一行
public static void deleteRow(String tableName, String rowKey) throws Exception {
    HTable hTable = new HTable(conf, tableName);
    Delete delete = new Delete(Bytes.toBytes(rowKey));
    hTable.delete(delete);
    hTable.close();
    System.out.println("刪除數(shù)據(jù)成功");
}

7)刪除多行數(shù)據(jù)

public static void deleteMultiRow(String tableName, String... rows) 
throws IOException{
    HTable hTable = new HTable(conf, tableName);
    List<Delete> deleteList = new ArrayList<Delete>();
    for(String row : rows){
        Delete delete = new Delete(Bytes.toBytes(row));
        deleteList.add(delete);
    }
    hTable.delete(deleteList);
    hTable.close();
}

8)得到所有數(shù)據(jù)

public static void getAllRows(String tableName) throws IOException{
    HTable hTable = new HTable(conf, tableName);
    //得到用于掃描region的對象
    Scan scan = new Scan();
    //使用HTable得到resultcanner實(shí)現(xiàn)類的對象
    ResultScanner resultScanner = hTable.getScanner(scan);
    for(Result result : resultScanner){
        Cell[] cells = result.rawCells();
        for(Cell cell : cells){
          //得到rowkey
          System.out.println("行鍵:" + Bytes.toString(CellUtil.cloneRow(cell)));
          //得到列族
          System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
          System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
          System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
        }
    }
}

9)得到某一行所有數(shù)據(jù)

public static void getRow(String tableName, String rowKey) throws IOException{
    HTable table = new HTable(conf, tableName);
    Get get = new Get(Bytes.toBytes(rowKey));
    //get.setMaxVersions();顯示所有版本
    //get.setTimeStamp();顯示指定時(shí)間戳的版本
    Result result = table.get(get);
    for(Cell cell : result.rawCells()){
        System.out.println("行鍵:" + Bytes.toString(result.getRow()));
        System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
        System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
        System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
        System.out.println("時(shí)間戳:" + cell.getTimestamp());
    }
}

10)獲取某一行指定“列族:列”的數(shù)據(jù)

public static void getRowQualifier(String tableName, String rowKey, String family, 
                 String qualifier) throws IOException{

    HTable table = new HTable(conf, tableName);
    Get get = new Get(Bytes.toBytes(rowKey));
    get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
    Result result = table.get(get);
    for(Cell cell : result.rawCells()){
       System.out.println("行鍵:" + Bytes.toString(result.getRow()));
       System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
       System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
       System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
    }

}

11)主方法

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.CellUtil;
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.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;

public static void main(String[] args) throws Exception {
        //判斷HBase表是否存在
        //System.out.println(isTableExist("Student"));
        
        //創(chuàng)建表
        //createTable("staff", "info","fa");
        
        //刪除表
        //dropTable("staff");
        
        //添加一行數(shù)據(jù)
        //addRow("staff", "1001", "info", "name", "Nick");
        //addRow("staff", "1002", "info", "name", "Nick");
        //addRow("staff", "1003", "info", "sex", "女");
        
        //刪除一行
        //deleteRow("staff", "1001");
        
        //刪除多行
        //deleteMultiRow("staff","1001","1002","1003");
        
        //獲取所有數(shù)據(jù)
        //getAllRows("staff");
        
        //獲取一行
        //getRow("staff","1001");
        
        //只獲取一列
        //getRowQualifier("staff","1003","info","sex");
}

尖叫提示:core-site.xml hbase-site.xml hdfs-site.xml log4j.properties 放入src/main/resources目錄下

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末漱抓,一起剝皮案震驚了整個(gè)濱河市么翰,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌辽旋,老刑警劉巖浩嫌,帶你破解...
    沈念sama閱讀 221,888評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件檐迟,死亡現(xiàn)場離奇詭異,居然都是意外死亡码耐,警方通過查閱死者的電腦和手機(jī)追迟,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,677評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來骚腥,“玉大人敦间,你說我怎么就攤上這事∈” “怎么了廓块?”我有些...
    開封第一講書人閱讀 168,386評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長契沫。 經(jīng)常有香客問我带猴,道長,這世上最難降的妖魔是什么懈万? 我笑而不...
    開封第一講書人閱讀 59,726評論 1 297
  • 正文 為了忘掉前任拴清,我火速辦了婚禮,結(jié)果婚禮上会通,老公的妹妹穿的比我還像新娘口予。我一直安慰自己,他們只是感情好涕侈,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,729評論 6 397
  • 文/花漫 我一把揭開白布沪停。 她就那樣靜靜地躺著,像睡著了一般裳涛。 火紅的嫁衣襯著肌膚如雪牙甫。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,337評論 1 310
  • 那天调违,我揣著相機(jī)與錄音窟哺,去河邊找鬼。 笑死技肩,一個(gè)胖子當(dāng)著我的面吹牛且轨,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播虚婿,決...
    沈念sama閱讀 40,902評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼旋奢,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了然痊?” 一聲冷哼從身側(cè)響起至朗,我...
    開封第一講書人閱讀 39,807評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎剧浸,沒想到半個(gè)月后锹引,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體矗钟,經(jīng)...
    沈念sama閱讀 46,349評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,439評論 3 340
  • 正文 我和宋清朗相戀三年嫌变,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了吨艇。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,567評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡腾啥,死狀恐怖东涡,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情倘待,我是刑警寧澤疮跑,帶...
    沈念sama閱讀 36,242評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站凸舵,受9級特大地震影響祖娘,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜贞间,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,933評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望雹仿。 院中可真熱鬧增热,春花似錦、人聲如沸胧辽。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,420評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽邑商。三九已至雇卷,卻和暖如春淑掌,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,531評論 1 272
  • 我被黑心中介騙來泰國打工踊赠, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人款票。 一個(gè)月前我還...
    沈念sama閱讀 48,995評論 3 377
  • 正文 我出身青樓跃捣,卻偏偏與公主長得像,于是被迫代替她去往敵國和親暇仲。 傳聞我的和親對象是個(gè)殘疾皇子步做,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,585評論 2 359

推薦閱讀更多精彩內(nèi)容