package com.test.dao;
import java.io.IOException;
import java.util.ArrayList;
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.KeyValue;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.test.Conf;
import com.test.entity.DNResourceRecord;
/**
* 操作Hbase的DAO
* @author syl
*
*/
@SuppressWarnings("deprecation")
public class HBaseDAO {
private static final Logger logger = LoggerFactory
.getLogger(HBaseDAO.class);
// 声明静态配置
private static Configuration conf = null;
private static HBaseAdmin admin = null;
private volatile static HTable table = null;
// 初始化静态配置
static {
conf = HBaseConfiguration.create();
//使用eclipse时必须添加这个,否则无法定位
conf.set("hbase.zookeeper.quorum", Conf.get("zkConnect"));
// 新建一个数据库管理员
try {
admin = new HBaseAdmin(conf);
} catch (Exception e) {
e.printStackTrace();
}
}
public HBaseDAO(String tableName) {
try {
table = new HTable(conf, tableName);
//这里默认值其实是64MB,所以应该改大一点比较好!
table.setWriteBufferSize(1024 * 1024 * 10);
// 并禁用hbase的自动提交功能
table.setAutoFlushTo(false);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 根据表名,判断表是否存在
* @param tableName 表名
* @return true 存在 ,false 不存在
* @throws IOException
*/
public static boolean tableExists(String tableName) throws IOException {
// 判断表是否存在
boolean bool = admin.tableExists("emp");
logger.info(tableName + " exists? " + bool);
return bool;
}
/**
* 创建数据库表
* @param tableName 表名
* @param columnFamilys 列族名
* @return true 成功 ,false 失败
* @throws Exception
*/
public boolean createTable(String tableName, String[] columnFamilys)
throws Exception {
boolean bool;
// 判断数据库表是否存在
if (admin.tableExists(tableName)) {
System.out.println("table " + tableName + " existed!!");
System.exit(0);
bool = false;
} else {
// 新建一个表的描述
HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tableName));
// 在描述里添加列族
for (String columnFamily : columnFamilys) {
HColumnDescriptor descriptor = new HColumnDescriptor(columnFamily);
descriptor.setMaxVersions(10);
tableDesc.addFamily(new HColumnDescriptor(descriptor));
}
// 根据配置好的描述建表
admin.createTable(tableDesc);
System.out.println("create table " + tableName + " success!");
bool = true;
}
return bool;
}
/**
* 列出数据库中所有表
* @return 返回数据库中所有表的描述的数组
* @throws IOException
*/
public HTableDescriptor[] showTables() throws IOException {
// 获取数据库中表的集合
HTableDescriptor[] tableDescriptor = admin.listTables();
// 遍历打印所有表名
for (int i = 0; i < tableDescriptor.length; i++ ){
System.out.println(tableDescriptor[i].getNameAsString());
}
return tableDescriptor;
}
/**
* 根据表名,获取数据库表的列族信息
* @param tableName 表名
* @throws IOException
*/
public void tableDetail(String tableName) throws IOException {
HTableDescriptor tableDescriptor = admin.getTableDescriptor(Bytes.toBytes(tableName));
// 获取数据库表的名称
byte[] name = tableDescriptor.getName();
System.out.println("result:");
System.out.println("table name: " + new String(name));
// 获取数据库表的列族名称
HColumnDescriptor[] columnFamilies = tableDescriptor.getColumnFamilies();
for(HColumnDescriptor d : columnFamilies){
System.out.println("column Families: " + d.getNameAsString());
}
}
/**
* 添加一列族到现有的表
* @param tableName 表名
* @param coloumn 列族名
* @throws IOException
*/
public static void addColoumn(String tableName, String coloumn) throws IOException {
// 初始化columnDescriptor对象
HColumnDescriptor columnDescriptor = new HColumnDescriptor(coloumn);
// 添加一个列族
admin.addColumn(tableName, columnDescriptor);
System.out.println("added " + coloumn + " to " + tableName);
}
/**
* 删除一列族从现有的表
* @param tableName 表名
* @param coloumn 列族名
* @throws IOException
*/
public static void deleteColoumn(String tableName, String coloumn) throws IOException {
// 删除一个列族
admin.deleteColumn(tableName, coloumn);
System.out.println("delete " + coloumn + " from " + tableName);
}
/**
* 根据表名,禁用数据库表
* @param tableName 表名
* @throws IOException
*/
public static void disableTable(String tableName) throws IOException {
// 判断数据库表是否被禁用
Boolean bool = admin.isTableDisabled(tableName);
System.out.println(bool);
// 禁用这张表
if(!bool){
admin.disableTable(tableName);
System.out.println("Table:" + tableName + "disabled");
}
}
/**
* 根据表名,禁用并删除数据库表
* @param tableName 表名
* @throws Exception
*/
public void deleteTable(String tableName) throws Exception {
if (admin.tableExists(tableName)) {
// 禁用一个表
admin.disableTable(tableName);
// 删除表
admin.deleteTable(tableName);
System.out.println("delete table " + tableName + " success!");
} else {
System.out.println("delete tabled failed! " + tableName + " not exists!");
System.exit(0);
}
}
/**
* 添加一条数据
* @param tableName 表名
* @param row 行名
* @param columnFamily 列族名
* @param column 列名
* @param value 值
* @throws Exception
*/
public void addRow(String tableName, String row,
String columnFamily, String column, String value) throws Exception {
// 指定行
Put put = new Put(Bytes.toBytes(row));
// 参数分别:列族、列、值
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(put);
// logger.info("add " + columnFamily + ":" + column + ":" + value + " success!");
}
/**
* 批量插入
* @param row 行名 需要变化
* @param columnFamily 列族
* @param set 值 一直变化
* @throws Exception
*/
public void addRows(List<String> row, String columnFamily, List<String> set) throws Exception {
Put put = null;
String columns = null;
for (int i = 0; i < set.size(); i++) {
switch (i % 7) {
case 0 : columns = "ttl"; break;
case 1 : columns = "clazz"; break;
case 2 : columns = "type"; break;
case 3 : columns = "rdata"; break;
case 4 : columns = "ispId"; break;
case 5 : columns = "version"; break;
case 6 : columns = "owner"; break;
}
put = new Put(Bytes.toBytes( row.get((int)(i / 7)) ) );// 设置rowkey
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(columns), Bytes.toBytes(set.get(i)));
table.put(put);
}
table.close();
}
/**
* 根据表名、列族名、列名,更新表中的某一列
* @param tableName 表名
* @param rowKey
* @param familyName 列族名
* @param columnName 列名
* @param value 更新后的值
* @throws IOException
*/
public static void updateTable(String tableName, String rowKey,
String familyName, String columnName, String value)
throws IOException {
Put put = new Put(Bytes.toBytes(rowKey));
put.add(Bytes.toBytes(familyName), Bytes.toBytes(columnName),
Bytes.toBytes(value));
table.put(put);
System.out.println("update table Success!");
}
/**
* 为表添加数据(适合知道有多少列族的固定表)
* @param rowKey
* @param tableName 表名
* @param column1 第一个列族列表
* @param value1 第一个列的值的列表
* @param column2 第二个列族列表
* @param value2 第二个列的值的列表
* @throws IOException
*/
public static void addData(String rowKey, String tableName,
String[] column1, String[] value1, String[] column2, String[] value2)
throws IOException {
// 设置rowkey
Put put = new Put(Bytes.toBytes(rowKey));
// 获取所有的列族
HColumnDescriptor[] columnFamilies = table.getTableDescriptor().getColumnFamilies();
for (int i = 0; i < columnFamilies.length; i++) {
// 获取列族名
String familyName = columnFamilies[i].getNameAsString();
// article列族put数据
if (familyName.equals("article")) {
for (int j = 0; j < column1.length; j++) {
put.add(Bytes.toBytes(familyName),
Bytes.toBytes(column1[j]), Bytes.toBytes(value1[j]));
}
}
// author列族put数据
if (familyName.equals("author")) {
for (int j = 0; j < column2.length; j++) {
put.add(Bytes.toBytes(familyName),
Bytes.toBytes(column2[j]), Bytes.toBytes(value2[j]));
}
}
}
table.put(put);
System.out.println("add data Success!");
}
/**
* 根据表名、行名,删除一条(行)数据
* @param tableName 表名
* @param row 行名
* @throws Exception
*/
public void deleteRow(String tableName, String row) throws Exception {
Delete del = new Delete(Bytes.toBytes(row));
table.delete(del);
}
/**
* 根据表名、行名、列族名、列名删除指定的列
* @param tableName 表名
* @param rowKey 行名
* @param falilyName 列族名
* @param columnName 列名
* @throws IOException
*/
public static void deleteColumn(String tableName, String rowKey,
String falilyName, String columnName) throws IOException {
Delete deleteColumn = new Delete(Bytes.toBytes(rowKey));
deleteColumn.deleteColumns(Bytes.toBytes(falilyName), Bytes.toBytes(columnName));
table.delete(deleteColumn);
System.out.println(falilyName + ":" + columnName + "is deleted!");
}
/**
* 根据表名、行名的数组。删除多条数据
* @param tableName 表名
* @param rows 行名的数组
* @throws Exception
*/
public void delMultiRows(String tableName, String[] rows)
throws Exception {
List<Delete> delList = new ArrayList<Delete>();
for (String row : rows) {
Delete del = new Delete(Bytes.toBytes(row));
delList.add(del);
}
table.delete(delList);
}
/**
* 根据表名、行名,获取一条数据
* @param tableName 表名
* @param row 行名
* @throws Exception
*/
public void getRow(String tableName, String row) throws Exception {
Get get = new Get(Bytes.toBytes(row));
Result result = table.get(get);
// 输出结果,raw方法返回所有keyvalue数组
for (KeyValue rowKV : result.raw()) {
System.out.print("表名:" + tableName + " ");
System.out.print("列族名:" + new String(rowKV.getFamily()) + " ");
System.out.print("行名:" + new String(rowKV.getRow()) + " ");
System.out.print("时间戳:" + rowKV.getTimestamp() + " ");
System.out.print("列名:" + new String(rowKV.getQualifier()) + " ");
System.out.println("值:" + new String(rowKV.getValue()));
}
}
/**
* 根据表名、行名、列族名、列名,查询某列数据的多个版本
* @param tableName 表名
* @param rowKey 行名
* @param familyName 列族名
* @param columnName 列名
* @throws IOException
*/
public void getResultByVersion(String tableName, String rowKey,
String familyName, String columnName) throws IOException {
Get get = new Get(Bytes.toBytes(rowKey));
// 设置一次性获取所有版本
get.setMaxVersions();
get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
Result result = table.get(get);
for (KeyValue kv : result.list()) {
System.out.print("表名:" + tableName + " ");
System.out.print("行名:" + rowKey + " ");
System.out.print("列族名:" + new String(kv.getFamily()) + " ");
System.out.print("列名:" + new String(kv.getQualifier()) + " ");
System.out.print("值:" + new String(kv.getValue()) + " ");
System.out.println("时间戳:" + kv.getTimestamp() + " ");
}
}
/**
* 根据表名、列名、列族名、列名查询表中的某一列
* @param tableName 表名
* @param rowKey 行名
* @param familyName 列族名
* @param columnName 列名
* @throws IOException
*/
public static void getRowsByColumn(String tableName, String rowKey,
String familyName, String columnName) throws IOException {
Get get = new Get(Bytes.toBytes(rowKey));
// 获取指定列族和列修饰符对应的列
get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
Result result = table.get(get);
for (KeyValue kv : result.list()) {
System.out.print("表名:" + tableName + " ");
System.out.print("列族名:" + new String(kv.getFamily()) + " ");
System.out.print("行名:" + new String(kv.getRow()) + " ");
System.out.print("时间戳:" + kv.getTimestamp() + " ");
System.out.print("列名:" + new String(kv.getQualifier()) + " ");
System.out.println("值:" + new String(kv.getValue()));
}
}
/**
* 遍历查询hbase表start_rowkey到stop_rowkey之间的数据
* @param tableName 表名
* @param start_rowkey 开始的rowkey
* @param stop_rowkey 结束的rowkey
* @throws IOException
*/
public static ResultScanner getRangeRows(String tableName, String startrow,
String endrow) throws Exception {
Scan s = new Scan(Bytes.toBytes(startrow), Bytes.toBytes(endrow));
// 一次next()返回10列Result实例
// s.setBatch(10);
ResultScanner rs = table.getScanner(s);
return rs;
}
/**
* 根据表名,获取所有的数据
* @param tableName 表名
* @throws Exception
*/
public void getAllRows(String tableName) throws Exception {
Scan scan = new Scan();
// 设置缓存大小
scan.setCaching(100);
// 一次next()返回Result实例的列数,防止超过客户端进程的内存容量
scan.setBatch(6);
ResultScanner results = table.getScanner(scan);
// 输出结果
for (Result result : results) {
for (KeyValue rowKV : result.raw()) {
System.out.print("表名:" + tableName + " ");
System.out.print("列族名:" + new String(rowKV.getFamily()) + " ");
System.out.print("行名:" + new String(rowKV.getRow()) + " ");
System.out.print("时间戳:" + rowKV.getTimestamp() + " ");
System.out.print("列名:" + new String(rowKV.getQualifier()) + " ");
System.out.println("值:" + new String(rowKV.getValue()));
}
}
}
/**
* 释放HTable的所有资源
* @throws IOException
*/
public static void closeTbale() throws IOException {
if (table != null) {
table.close();
}
System.out.println("close table!");
}
/**
* 将DNResourceRecord对象保存到HBase中
* @param dnResourceRecords 对象集合
* @param tableName 表名
* @param columnFamily 列族名
*/
public static void intoHbase(List<DNResourceRecord> dnResourceRecords,
String tableName, String columnFamily) {
HBaseDAO hBaseDAO = new HBaseDAO(tableName);
ArrayList<String> rows = new ArrayList<String>(); // rowKey
ArrayList<String> set = new ArrayList<String>(); // value
String rowKey;
try {
// 将集合对象存入hbase中
for (DNResourceRecord dnResourceRecord : dnResourceRecords) {
rowKey = dnResourceRecord.getRowKey();
rows.add(rowKey);
set.add(dnResourceRecord.getTtl().toString());
set.add(dnResourceRecord.getClazz());
set.add(dnResourceRecord.getType());
set.add(dnResourceRecord.getRdata());
set.add(dnResourceRecord.getIspId());
set.add(dnResourceRecord.getVersion().toString());
set.add(dnResourceRecord.getOwner());
}
hBaseDAO.addRows(rows, columnFamily, set);
logger.info(dnResourceRecords.size() + " row dNResourceRecord has inesrt into hbase!");
} catch(Exception e) {
logger.error(" inesrt into hbase error! " + e.toString());
e.printStackTrace();
}
}
// 主函数
public static void main(String[] args) {
HBaseDAO hBaseDAO = new HBaseDAO("records");
try {
// String tableName = "records";
// // 第一步:创建数据库表:“student”
// String[] columnFamilys = { "info"};
// hBaseDAO.createTable(tableName, columnFamilys);
// // 第二步:向数据表的添加数据
// // 添加第一行数据
// if (tableExists(tableName)) {
// hBaseDAO.addRow(tableName, "zpc", "info", "age", "20");
// hBaseDAO.addRow(tableName, "zpc", "info", "sex", "boy");
// hBaseDAO.addRow(tableName, "zpc", "course", "china", "97");
// hBaseDAO.addRow(tableName, "zpc", "course", "math", "128");
// hBaseDAO.addRow(tableName, "zpc", "course", "english", "85");
// // 添加第二行数据
// hBaseDAO.addRow(tableName, "henjun", "info", "age", "19");
// hBaseDAO.addRow(tableName, "henjun", "info", "sex", "boy");
// hBaseDAO.addRow(tableName, "henjun", "course", "china","90");
// hBaseDAO.addRow(tableName, "henjun", "course", "math","120");
// hBaseDAO.addRow(tableName, "henjun", "course", "english","90");
// // 添加第三行数据
// hBaseDAO.addRow(tableName, "niaopeng", "info", "age", "18");
// hBaseDAO.addRow(tableName, "niaopeng", "info", "sex","girl");
// hBaseDAO.addRow(tableName, "niaopeng", "course", "china","100");
// hBaseDAO.addRow(tableName, "niaopeng", "course", "math","100");
// hBaseDAO.addRow(tableName, "niaopeng", "course", "english","99");
// // 第三步:获取一条数据
// System.out.println("**************获取一条(zpc)数据*************");
// hBaseDAO.getRow(tableName, "zpc");
// // 第四步:获取所有数据
// System.out.println("**************获取所有数据***************");
// hBaseDAO.getAllRows(tableName);
//
// // 第五步:删除一条数据
// System.out.println("************删除一条(zpc)数据************");
// hBaseDAO.deleteRow(tableName, "zpc");
// hBaseDAO.getAllRows(tableName);
// // 第六步:删除多条数据
// System.out.println("**************删除多条数据***************");
// String rows[] = new String[] { "qingqing","xiaoxue" };
// hBaseDAO.delMultiRows(tableName, rows);
// hBaseDAO.getAllRows(tableName);
// // 第七步:删除数据库
// System.out.println("***************删除数据库表**************");
// hBaseDAO.deleteTable("records");
// System.out.println("表"+tableName+"存在吗?"+tableExists(tableName));
// } else {
// System.out.println(tableName + "此数据库表不存在!");
// }
// hBaseDAO.showTables();
// hBaseDAO.deleteTable("records");
// String[] fam = new String[]{"info"};
// hBaseDAO.createTable("records", fam);
// System.out.println("*******************获取表的信息*****************");
// hBaseDAO.tableDetail("records");
// System.out.println("*******************获取表中某一条数据*****************");
// hBaseDAO.getRow("records", "sex2.tld.");
// System.out.println("=====================数据的多个版本===================");
// hBaseDAO.getResultByVersion("records", "sina.com.cn.", "info", "rdata");
//
// // 第五步:删除一条数据
// System.out.println("************删除一条(zpc)数据************");
// hBaseDAO.deleteRow("records", "乾杯.tld.");
//
// System.out.println("=====================数据的多个版本===================");
// hBaseDAO.getResultByVersion("records", "乾杯.tld.www2", "info", "rdata");
System.out.println("*******************获取数据库中所有数据*****************");
hBaseDAO.getAllRows("records");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
HBaseDAO.closeTbale();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Java操作hbase的工具类
最新推荐文章于 2022-07-17 17:30:54 发布