Hbase Java API 使用

1.配置環(huán)境

1)eclipse和jdk的下載安裝配置

2)加載相關(guān)xml文件

pom.xml添加maven依賴

添加配置文件至新創(chuàng)建的resources中 ? 找到hbase? zookeeper? hadoop

configuration實例中添加 ? conf ?將配置文件放到classpath

新建一個source folder,將下面5個文件放入

hbase-site ?regionserver ? log4j ?hdfs-site ? core-site

3)client 操作 ? ? ? ? ?增刪改查

package org.apache.hadoop.hbase;

import java.io.IOException;

import java.io.InterruptedIOException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.client.Delete;

import org.apache.hadoop.hbase.client.Get;

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

import org.apache.hadoop.hbase.client.Scan;

import org.apache.hadoop.hbase.filter.Filter;

import org.apache.hadoop.hbase.filter.PrefixFilter;

import org.apache.hadoop.hbase.util.Bytes;

public class HClient {

public static HTable getTable(String name) throws Exception{

//create a hbase configuration instance

Configuration conf = HBaseConfiguration.create();

//create a htabel instance

HTable table = new HTable(conf, name);

return table;

}

/**

* get data from hbase table

* get:

* get 'student:stu_info','20161204_1001'

* get 'student:stu_info','20161204_1001','info'

* get 'student:stu_info','20161204_1001','info:name'

* @throws Exception

*/

public static void getData(HTable table) throws Exception {

// TODO Auto-generated method stub

//create a get instance

Get get = new Get(Bytes.toBytes("20161204_1003"));

//configure the get

//get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));

//get.addFamily(family);

//add to table

Result? rs = table.get(get);

//print

for(Cell cell : rs.rawCells()){

System.out.println(

Bytes.toString(CellUtil.cloneRow(cell))

+"->"+

Bytes.toString(CellUtil.cloneFamily(cell))

+"->"+

Bytes.toString(CellUtil.cloneQualifier(cell))

+"->"+

Bytes.toString(CellUtil.cloneValue(cell))

+"->"+

cell.getTimestamp()

);

System.out.println("---------------------------------------------------");

}

}

/**

* put the data to hbase table

* put:

* put 'student:stu_info','20161204_1003','info:name','laosan'

* @param args

* @throws Exception

* @throws RetriesExhaustedWithDetailsException

* @throws Exception

*/

public static void putData(HTable table) throws RetriesExhaustedWithDetailsException, Exception{

//create a put instance

Put put = new Put(Bytes.toBytes("20161204_1003"));

put.add(

Bytes.toBytes("info"),

Bytes.toBytes("name"),

Bytes.toBytes("laosan")

);

//add to table

table.put(put);

getData(table);

}

/**

* delete data from hbase table

* delete

* delete 'student:stu_info', '20161204_1003', 'info:age'

* @param args

* @throws Exception

* @throws Exception

*/

public static void deleteData(HTable table) throws Exception{

//create a delete instance

Delete delete = new Delete(Bytes.toBytes("20161204_1003"));

//conf the delete

delete.deleteColumn(Bytes.toBytes("info"), Bytes.toBytes("age"));

//delete.deleteColumns(family, qualifier);

//add the delete

table.delete(delete);

getData(table);

}

/**

* scan the data from the hbase table

* scan :

* scan 'student:stu_info'

*

* @param args

* @throws Exception

* @throws Exception

*/

public static void scanData(HTable table) throws Exception{

//create a scan instance

Scan scan = new Scan();

//add to table

ResultScanner rsscan = table.getScanner(scan);

//print

for(Result rs : rsscan){

System.out.println(Bytes.toString(rs.getRow()));

for(Cell cell: rs.rawCells()){

System.out.println(

Bytes.toString(CellUtil.cloneRow(cell))

+"->"+

Bytes.toString(CellUtil.cloneFamily(cell))

+"->"+

Bytes.toString(CellUtil.cloneQualifier(cell))

+"->"+

Bytes.toString(CellUtil.cloneValue(cell))

+"->"+

cell.getTimestamp()

);

}

System.out.println("------------------------------");

}

}

/**

* range scan

* @param table

* @throws Exception

*/

public static void rangeScan(HTable table) throws Exception {

// TODO Auto-generated method stub

//create a scan

Scan scan = new Scan();

//conf the scan

//scan.addColumn(family, qualifier);

//scan.addFamily(Bytes.toBytes("info"));

//scan.setStartRow(Bytes.toBytes("20161204_1002"));

//scan.setStopRow(Bytes.toBytes("20161204_1003"));

Filter filter = new PrefixFilter(Bytes.toBytes("2016121"));

scan.setFilter(filter);

//特殊配置

//設(shè)置是否開啟緩存

scan.setCacheBlocks(true);

//緩存的條數(shù)

scan.setCaching(100);

//設(shè)置每次取多少條

scan.setBatch(10);

//這兩個參數(shù)共同決定了RPC請求次數(shù)

//add to table

ResultScanner rsscan = table.getScanner(scan);

//print

for(Result rs : rsscan){

System.out.println(Bytes.toString(rs.getRow()));

for(Cell cell: rs.rawCells()){

System.out.println(

Bytes.toString(CellUtil.cloneRow(cell))

+"->"+

Bytes.toString(CellUtil.cloneFamily(cell))

+"->"+

Bytes.toString(CellUtil.cloneQualifier(cell))

+"->"+

Bytes.toString(CellUtil.cloneValue(cell))

+"->"+

cell.getTimestamp()

);

}

System.out.println("------------------------------");

}

}

public static void main(String[] args) throws Exception {

HTable table = getTable("student:stu_info");

//getData(table);

//putData(table);

//deleteData(table);

//scanData(table);

rangeScan(table);

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • http://blog.csdn.net/fengzheku/article/details/48447791 p...
    木子Qing閱讀 2,651評論 1 1
  • 目錄: 引言 -- 參數(shù)基礎(chǔ) 1. 結(jié)構(gòu)(Structural)過濾器--FilterList 2.列值過濾器--...
    磊寶萬歲閱讀 1,635評論 0 2
  • 最近寫HBase,發(fā)現(xiàn)很多功能都不建議使用,依據(jù)官方的文檔寫了份關(guān)于增刪改查的測試案例,供學(xué)習(xí)交流。 import...
    沈穎閱讀 1,375評論 0 1
  • hbase是一個數(shù)據(jù)庫,用于數(shù)據(jù)存儲,mapreduce是一個計算框架,用于數(shù)據(jù)處理 集成的模型 hbase作為m...
    柳仁兒閱讀 1,967評論 0 2
  • 入門指南 1. 簡介 Quickstart會讓你啟動和運行一個單節(jié)點單機HBase。 2. 快速啟動 – 單點HB...
    和心數(shù)據(jù)閱讀 4,997評論 1 41

友情鏈接更多精彩內(nèi)容