Hbase Java API 使用

1.配置环境

1)eclipse和jdk的下载安装配置

2)加载相关xml文件

pom.xml添加maven依赖

《Hbase Java API 使用》

添加配置文件至新创建的resources中   找到hbase  zookeeper  hadoop

configuration实例中添加   conf  将配置文件放到classpath

新建一个source folder,将下面5个文件放入

hbase-site  regionserver   log4j  hdfs-site   core-site

《Hbase Java API 使用》

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

//特殊配置

//设置是否开启缓存

scan.setCacheBlocks(true);

//缓存的条数

scan.setCaching(100);

//设置每次取多少条

scan.setBatch(10);

//这两个参数共同决定了RPC请求次数

//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);

}

}

    原文作者:柳仁儿
    原文地址: https://www.jianshu.com/p/2b079b34ae33
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞