package com.zoujc.Utils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* Hbase操作工具类
* java工具类建议采用单例模式封装
*/
public class HbaseUtils {
HBaseAdmin admin = null;
Configuration conf = null;
//私有构造方法,加载重要配置
private HbaseUtils(){
conf = new Configuration();
//zookeeper集群的URL配置,多个host中间用逗号(,)分割
conf.set("hbase.zookeeper.quorum","node1:2181,node2:2181,node3:2181");
//HBase集群中所有RegionServer共享目录,用来持久化HBase的数据,一般设置的是hdfs的文件目录,如hdfs://namenode.example.org:9000/hbase
conf.set("hbase.rootdir","hdfs://hadoopcluster/hbase");
try {
admin = new HBaseAdmin(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
//getInstance这个方法在单例模式用的甚多,为了避免对内存造成浪费,直到需要实例化该类的时候才将其实例化,所以用getInstance来获取该对象,
//至于其他时候,也就是为了简便而已,为了不让程序在实例化对象的时候,不用每次都用new关键字,索性提供一个instance方法,不必一执行这个类就
//初始化,这样做到不浪费系统资源!单例模式 可以防止 数据的冲突,节省内存空间
private static HbaseUtils instance = null;
//单例模式实例化对象
public static synchronized HbaseUtils getInstance(){
if(null == instance){
instance = new HbaseUtils();
}
return instance;
}
/**
* 根据表名获取到HTable实例
*/
public HTable getTable(String tableName){
HTable table = null;
try {
table = new HTable(conf,tableName);
} catch (IOException e) {
e.printStackTrace();
}
return table;
}
/**
* 添加一条记录到Hbase表
* @param tableName Hbase表名
* @param rowKey Hbase表的rowkey
* @param cf Hbase表的列族columnfamily
* @param cloumn Hbase表的列
* @param value 写入Hbase表的值
*/
public void put(String tableName,String rowKey,String cf,String cloumn,String value){
HTable table = getTable(tableName);
Put put = new Put(Bytes.toBytes(rowKey));
put.add(Bytes.toBytes(cf),Bytes.toBytes(cloumn),Bytes.toBytes(value));
try {
table.put(put);
} catch (IOException e) {
e.printStackTrace();
}
}
//根据输入表名和条件获取表中记录数
public Map<String,Long> query(String tableName,String condition) throws Exception{
Map<String,Long> map = new HashMap<String, Long>();
HTable table = getTable(tableName);
String cf = "info";
String qualifier = "click_count";
Scan scan = new Scan();
//根据rowKey的前缀来查询
Filter filter = new PrefixFilter(Bytes.toBytes(condition));
scan.setFilter(filter);
ResultScanner rs = table.getScanner(scan);
for(Result result: rs){
String row = Bytes.toString(result.getRow());
Long clickCount = Bytes.toLong(result.getValue(cf.getBytes(),qualifier.getBytes()));
map.put(row,clickCount);
}
return map;
}
public static void main(String[] args) throws Exception {
// test连接
// HTable table = HbaseUtils.getInstance().getTable("HbaseTableName");
// System.out.println(table.getName().getNameAsString());
String tablename = "HbaseTableName";
String rowKey = "HbaseTableRowKey";
String cf = "HbaseTableCF";
String cloumn = "HbaseTableCloumn";
String value = "HbaseTableValue";
HbaseUtils.getInstance().put(tablename,rowKey,cf,cloumn,value);
Map<String,Long> map = HbaseUtils.getInstance().query("HbaseTableName","2018-10-17");
for(Map.Entry<String,Long> entry: map.entrySet()){
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}
}
一:Java API操作HBase
最新推荐文章于 2024-06-12 16:04:45 发布