1. 依赖包: 见Hbase实践
2. HbaseService接口
package com.rz.hservice;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.apache.hadoop.hbase.client.Result;
import com.rz.model.hbase.HbaseQuaCondition;
public interface HbaseService{
/**
* 查询全表的数据
* @param tablename
* @return
*/
public List<Result> scaner(String tablename);
/**
* 根据rowKey查询单条记录
* @param tableName
* @param rowKey
* @return
*/
public Result getRow(String tableName, String rowKey);
/**
* 根据regxKey正则匹配数据
* @param tableName
* @param regxKey
* @return
*/
public List<Result> getRegexRow(String tableName,String regxKey);
/**
* 根据regxKey正则匹配数据,取出num条
* @param tableName
* @param regxKey
* @param num
* @return
*/
public List<Result> getRegexRow(String tableName,String regxKey,int num);
/**
* 根据startKey和endKey的范围匹配数据
* @param tableName
* @param startKey
* @param stopKey
* @return
*/
public List<Result> getStartRowAndEndRow(String tableName, String startKey, String stopKey);
/**
* 确定startKey和endKey的范围,根据regKey匹配数据
* @param tableName
* @param startKey
* @param stopKey
* @param regxKey
* @return
*/
public List<Result> getRegexRow(String tableName, String startKey, String stopKey, String regxKey);
/**
* 确定startKey和endKey的范围,根据regKey匹配数据,取出num条
* @param tableName
* @param startKey
* @param stopKey
* @param regxKey
* @param num
* @return
*/
public List<Result> getRegexRow(String tableName, String startKey, String stopKey, String regxKey,int num);
/**
* 添加数据
* @param rowKey
* @param tableName
* @param column
* @param value
*/
public void addData(String rowKey, String tableName, String[] column, String[] value);
/**
* 添加数据
* @param rowKey
* @param tableName
* @param Map<Qualifier,value>
*/
public int insertData(String rowKey, String tableName, Map<String, String> map);
/**
* 删除记录
* @param tableName
* @param rowKeys
*/
public int delRecord(String tableName, String... rowKeys);
/**
* 修改一条数据
* @param tableName
* @param rowKey
* @param familyName
* @param column
* @param value
* @throws IOException
*/
public void updateRecord(String tableName, String rowKey,String familyName, String column[], String value[]) throws IOException;
/**
* 修改一条数据
* @param tableName
* @param rowKey
* @param familyName
* @param column
* @param value
* @throws IOException
*/
public int updateRecord(String tableName, String rowKey,String familyName, Map<String, String> map) ;
/**
* 查找最新的一条数据,或者说倒序查询
* @param tableName
* @return
*/
public Result getNewRow(String tableName);
/**
* 正则查出所有匹配的key
* @param tableName
* @param regxKey
* @return
*/
public List<String> queryKeys(String tableName,String regxKey);
/**
* 增加表中对应字段的值
* @param tableName
* @param cf
* @param rowKey
* @param column
* @param num
* @return
*/
long incrQualifier(String tableName, String familyName, String rowKey, String column, long num);
Result getNewRow(String tableName, String regxKey);
/**
* 最后数据过滤产出
* @param <T>
* @param list Hbase结果集
* @return
*/
public <T> List<T> dataOutput(Class<T> t,List<Result> list);
/**
* 最后数据过滤产出
* @param <T>
* @param list Hbase结果集
* @return
*/
public List<Map<String, Object>> dataOutput(String tableName, List<HbaseQuaCondition> hcList);
/**
* 最后数据过滤产出
* @param t 类型
* @param tableName 表名
* @param hcList 条件
* @return
*/
public List<Map<String, Object>> dataOutput(String tableName,HbaseQuaCondition... hcList);
/**
* 最后数据过滤产出
* @param t 类型
* @param tableName 表名
* @param hcList 条件
* @return
*/
public <T> List<T> dataOutput(Class<T> t,String tableName,List<HbaseQuaCondition> hcList);
/**
* 最后数据过滤产出
* @param t 类型
* @param tableName 表名
* @param hcList 条件
* @return
*/
public <T> List<T> dataOutput(Class<T> t,String tableName,HbaseQuaCondition... hcList);
}
3. HbaseServiceImpl 实现
package com.rz.hservice.impl;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTableInterface;
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.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.PageFilter;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.hadoop.hbase.HbaseTemplate;
import org.springframework.data.hadoop.hbase.TableCallback;
import org.springframework.stereotype.Service;
import com.rz.commons.utils.HbaseClientUtils;
import com.rz.commons.utils.ReflectUtil;
import com.rz.hservice.HbaseService;
import com.rz.model.hbase.HbaseQuaCondition;
@SuppressWarnings("deprecation")
@Service
public class HbaseServiceImpl implements HbaseService {
@Autowired
private HbaseTemplate hbaseTemplate;
private final String encoding = "utf-8";
protected final String increment_subfix = "_increment";
protected final String increment_name = "id";
@Override
public List<Result> scaner(final String tableName) {
return hbaseTemplate.execute(tableName, new TableCallback<List<Result>>() {
List<Result> list = new ArrayList<>();
@Override
public List<Result> doInTable(HTableInterface table) throws Throwable {
Scan scan = new Scan();
ResultScanner rs = table.getScanner(scan);
for(Result result:rs){
list.add(result);
}
return list;
}
});
}
@Override
public Result getRow(final String tableName, final String rowKey) {
return hbaseTemplate.execute(tableName, new TableCallback<Result>() {
@Override
public Result doInTable(HTableInterface table) throws Throwable {
Get get = new Get(rowKey.getBytes(encoding));
return table.get(get);
}
});
}
@Override
public List<Result> getRegexRow(final String tableName, final String regxKey) {
return hbaseTemplate.execute(tableName, new TableCallback<List<Result>>() {
List<Result> list = new ArrayList<>();
@Override
public List<Result> doInTable(HTableInterface table) throws Throwable {
RegexStringComparator rc = new RegexStringComparator(regxKey);
RowFilter rowFilter = new RowFilter(CompareOp.EQUAL, rc);
Scan scan = new Scan();
scan.setFilter(rowFilter);
ResultScanner rs = table.getScanner(scan);
for(Result result:rs){
list.add(result);
}
return list;
}
});
}
@Override
public List<Result> getRegexRow(final String tableName, final String regxKey, final int num) {
return hbaseTemplate.execute(tableName, new TableCallback<List<Result>>() {
List<Result> list = new ArrayList<>();
@Override
public List<Result> doInTable(HTableInterface table) throws Throwable {
FilterList fl = new FilterList(FilterList.Operator.MUST_PASS_ALL);
RegexStringComparator rc = new RegexStringComparator(regxKey);
RowFilter rf = new RowFilter(CompareOp.EQUAL, rc);
if(num > 0){// 过滤获取的条数
Filter filterNum = new PageFilter(num);// 每页展示条数
fl.addFilter(filterNum);
}
// 过滤器的添加
fl.addFilter(rf);
Scan scan = new Scan();
scan.setFilter(fl);// 为查询设置过滤器的list
ResultScanner rscanner = table.getScanner(scan);
for(Result result : rscanner){
list.add(result);
}
return list;
}
});
}
@Override
public List<Result> getStartRowAndEndRow(final String tableName, final String startKey, final String stopKey) {
return hbaseTemplate.execute(tableName, new TableCallback<List<Result>>() {
List<Result> list = new ArrayList<>();
@Override
public List<Result> doInTable(HTableInterface table) throws Throwable {
// 过滤器的添加
Scan scan = new Scan();
scan.setStartRow(startKey.getBytes(encoding));// 开始的key
scan.setStopRow(stopKey.getBytes(encoding));// 结束的key
ResultScanner rscanner = table.getScanner(scan);
for(Result result : rscanner){
list.add(result);
}
return list;
}
});
}
@Override
public List<Result> getRegexRow(final String tableName, final String startKey, final String stopKey, final String regxKey) {
return hbaseTemplate.execute(tableName, new TableCallback<List<Result>>() {
List<Result> list = new ArrayList<>();
@Override
public List<Result> doInTable(HTableInterface table) throws Throwable {
// 设置正则过滤器
RegexStringComparator rc = new RegexStringComparator(regxKey);
RowFilter rf = new RowFilter(CompareOp.EQUAL, rc);
// 过滤器的添加
Scan scan = new Scan();
scan.setStartRow(startKey.getBytes(encoding));// 开始的key
scan.setStopRow(stopKey.getBytes(encoding));// 结束的key
scan.setFilter(rf);// 为查询设置过滤器的list
ResultScanner rscanner = table.getScanner(scan);
for(Result result : rscanner){
list.add(result);
}
return list;
}
});
}
@Override
public List<Result> getRegexRow(final String tableName, final String startKey, final String stopKey, final String regxKey,final int num) {
return hbaseTemplate.execute(tableName, new TableCallback<List<Result>>() {
List<Result> list = new ArrayList<>();
@Override
public List<Result> doInTable(HTableInterface table) throws Throwable {
FilterList fl = new FilterList(FilterList.Operator.MUST_PASS_ALL);
// 设置正则过滤器
RegexStringComparator rc = new RegexStringComparator(regxKey);
RowFilter rf = new RowFilter(CompareOp.EQUAL, rc);
if(num > 0){// 过滤获取的条数
Filter filterNum = new PageFilter(num);// 每页展示条数
fl.addFilter(filterNum);
}
// 过滤器的添加
fl.addFilter(rf);
// 过滤器的添加
Scan scan = new Scan();
scan.setStartRow(startKey.getBytes(encoding));// 开始的key
scan.setStopRow(stopKey.getBytes(encoding));// 结束的key
scan.setFilter(fl);// 为查询设置过滤器的list
ResultScanner rscanner = table.getScanner(scan);
for(Result result : rscanner){
list.add(result);
}
return list;
}
});
}
@Override
public void addData(final String rowKey, final String tableName, final String[] column, final String[] value) {
hbaseTemplate.execute(tableName, new TableCallback<String>() {
@Override
public String doInTable(HTableInterface table) throws Throwable {
Put put = new Put(Bytes.toBytes(rowKey));// 设置rowkey
for (int j = 0; j < column.length; j++) {
put.add(Bytes.toBytes(HbaseClientUtils.family), Bytes.toBytes(column[j]), Bytes.toBytes(value[j]));
}
table.put(put);
return "ok";
}
});
}
@Override
public int delRecord(final String tableName, final String... rowKeys) {
return hbaseTemplate.execute(tableName, new TableCallback<Integer>() {
@Override
public Integer doInTable(HTableInterface table) throws Throwable {
List<Delete> list = new ArrayList<>();
for(String rowKey : rowKeys){
Delete del = new Delete(Bytes.toBytes(rowKey));
list.add(del);
}
table.delete(list);
return 1;
}
});
}
@Override
public void updateRecord(final String tableName, final String rowKey, final String familyName, final String[] column, final String[] value)
throws IOException {
hbaseTemplate.execute(tableName, new TableCallback<String>() {
@Override
public String doInTable(HTableInterface table) throws Throwable {
Put put = new Put(Bytes.toBytes(rowKey));
for (int j = 0; j < column.length; j++) {
put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(column[j]),Bytes.toBytes(value[j]));
}
table.put(put);
return "ok";
}
});
}
@Override
public Result getNewRow(final String tableName) {
return hbaseTemplate.execute(tableName, new TableCallback<Result>() {
@Override
public Result doInTable(HTableInterface table) throws Throwable {
Filter filterNum = new PageFilter(1);// 每页展示条数
Scan scan = new Scan();
scan.setFilter(filterNum);
scan.setReversed(true);
ResultScanner scanner = table.getScanner(scan);
return scanner.next();
}
});
}
@Override
public Result getNewRow(final String tableName,final String regxKey) {
return hbaseTemplate.execute(tableName, new TableCallback<Result>() {
@Override
public Result doInTable(HTableInterface table) throws Throwable {
FilterList fl = new FilterList(FilterList.Operator.MUST_PASS_ALL);
RegexStringComparator rc = new RegexStringComparator(regxKey);
RowFilter rf = new RowFilter(CompareOp.EQUAL, rc);
Filter filterNum = new PageFilter(1);// 每页展示条数
fl.addFilter(rf);
fl.addFilter(filterNum);
Scan scan = new Scan();
scan.setFilter(fl);
scan.setReversed(true);
ResultScanner scanner = table.getScanner(scan);
return scanner.next();
}
});
}
@Override
public List<String> queryKeys(final String tableName, final String regxKey) {
// TODO Auto-generated method stub
return hbaseTemplate.execute(tableName, new TableCallback<List<String>>() {
List<String> list = new ArrayList<>();
@Override
public List<String> doInTable(HTableInterface table) throws Throwable {
PrefixFilter filter = new PrefixFilter(regxKey.getBytes(encoding));
Scan scan = new Scan();
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result rs : scanner) {
list.add(new String(rs.getRow()));
}
return list;
}
});
}
@Override
public long incrQualifier(final String tableName, final String familyName,final String rowKey,final String column,final long num) {
// TODO Auto-generated method stub
return hbaseTemplate.execute(tableName, new TableCallback<Long>() {
@Override
public Long doInTable(HTableInterface table) throws Throwable {
long qualifie = table.incrementColumnValue(rowKey.getBytes(encoding), familyName.getBytes(encoding), column.getBytes(encoding), num);
return qualifie;
}
});
}
@Override
public int insertData(String rowKey, String tableName, Map<String, String> map) {
return hbaseTemplate.execute(tableName, new TableCallback<Integer>() {
@SuppressWarnings("deprecation")
@Override
public Integer doInTable(HTableInterface table) throws Throwable {
Put put = new Put(Bytes.toBytes(rowKey));// 设置rowkey
for (Map.Entry<String, String> qMap:map.entrySet()) {
put.addColumn(Bytes.toBytes(HbaseClientUtils.family), Bytes.toBytes(qMap.getKey()), Bytes.toBytes(qMap.getValue()));
}
table.put(put);
return 1;
}
});
}
@Override
public int updateRecord(String tableName, String rowKey, String familyName, Map<String, String> map){
return hbaseTemplate.execute(tableName, new TableCallback<Integer>() {
@Override
public Integer doInTable(HTableInterface table) throws Throwable {
Put put = new Put(Bytes.toBytes(rowKey));
for (Entry<String, String> qMap:map.entrySet()) {
put.addColumn(Bytes.toBytes(HbaseClientUtils.family), Bytes.toBytes(qMap.getKey()),Bytes.toBytes(qMap.getValue()));
}
table.put(put);
return 1;
}
});
}
@Override
public <T> List<T> dataOutput(Class<T> t, List<Result> list){
//匹配产出
List<T> rList = new ArrayList<>();
for (Result result : list) {
T acp = null;
try {
acp = t.newInstance();
} catch (InstantiationException | IllegalAccessException e1) {
e1.printStackTrace();
}
if (acp!=null) {
for (Cell cell : result.rawCells()) {
try {
String qualifier = new String(CellUtil.cloneQualifier(cell), HbaseClientUtils.encoding);
String value = new String(CellUtil.cloneValue(cell), HbaseClientUtils.encoding);
ReflectUtil.setValueByFieldName(acp, qualifier, value);
rList.add(acp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
return rList;
}
@Override
public <T> List<T> dataOutput(Class<T> t, String tableName, List<HbaseQuaCondition> hcList) {
//查询表值
List<Result> list = scaner(tableName);
//过滤
list = HbaseClientUtils.filterResultList(list, hcList);
//匹配产出
List<T> result = dataOutput(t,list);
return result;
}
@Override
public List<Map<String, Object>> dataOutput(String tableName, List<HbaseQuaCondition> hcList) {
//查询表值
List<Result> list = scaner(tableName);
//过滤
list = HbaseClientUtils.filterResultList(list, hcList);
//匹配产出
List<Map<String,Object>> rList = new ArrayList<>();
for (Result result : list) {
Map<String, Object> acp = new HashMap<>();
if (acp!=null) {
for (Cell cell : result.rawCells()) {
try {
String qualifier = new String(CellUtil.cloneQualifier(cell), HbaseClientUtils.encoding);
String value = new String(CellUtil.cloneValue(cell), HbaseClientUtils.encoding);
acp.put(qualifier, value);
rList.add(acp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
return rList;
}
@Override
public <T> List<T> dataOutput(Class<T> t, String tableName, HbaseQuaCondition... hcList) {
return dataOutput(t, tableName, Arrays.asList(hcList));
}
@Override
public List<Map<String, Object>> dataOutput(String tableName, HbaseQuaCondition... hcList) {
return dataOutput(tableName, Arrays.asList(hcList));
}
}
4. 辅助类:HbaseClientUtils、ReflectUtil、HbaseQuaCondition、HbaseBuilder、FromBuilder
HbaseClientUtils:
package com.rz.commons.utils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
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.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
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.client.Table;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.rz.model.hbase.HbaseQuaCondition;
import com.util.DateUtil;
public class HbaseClientUtils {
public static Configuration configuration = null;
public static Connection connection = null;
public static final Logger LOGGER = LoggerFactory.getLogger(HbaseClientUtils.class);
public static final String encoding = "utf-8";
public static final String family = "chronost";
public static final String like = "like";
public static final String less = "<";
public static final String less_or_equal = "<=";
public static final String equal = "=";
public static final String not_equal = "!=";
public static final String greater = ">";
public static final String greater_or_equal = ">=";
static{
if (configuration == null || connection == null || connection.isClosed()) {
System.setProperty("HADOOP_USER_NAME", "hdfs");
configuration = HBaseConfiguration.create();
configuration.addResource("xml/hbase-site.xml");
configuration.addResource("xml/core-site.xml");
configuration.setInt("hbase.rpc.timeout", 120000);
configuration.setInt("hbase.client.operation.timeout", 120000);
configuration.setInt("hbase.client.scanner.timeout.period", 200000);
try {
connection = ConnectionFactory.createConnection(configuration);
} catch (Exception e1) {
LOGGER.error("Failed to open the connection ", e1);
}
}
}
public static void close() {
if (connection != null) {
try {
connection.close();
} catch (Exception e1) {
LOGGER.error("Failed to close the connection ", e1);
}
}
}
/**
* 判断表是否存在
*
* @param tableName
* 表名
* @return 是否存在
* @throws IOException
* 异常
*/
public static boolean isTableExists(String tableName) throws IOException {
Admin admin = ConnectionFactory.createConnection(configuration).getAdmin();
boolean isExists = admin.tableExists(TableName.valueOf(tableName));
return isExists;
}
/**
* 取得table接口对象
*
* @param tableName
* 表名
* @return Table interface
* @throws IOException
* 异常
*/
public static Table getTable(String tableName) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
return table;
}
/**
* 创建表
*
* @param tableName
* @param hColumn
* @throws IOException
*/
public static void createTable(String tableName, String... hColumn) throws IOException {
LOGGER.info("start create table ......");
Admin admin = ConnectionFactory.createConnection(configuration).getAdmin();
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
for (String hc : hColumn) {
HColumnDescriptor hcdInfo = new HColumnDescriptor(hc);
hcdInfo.setMaxVersions(3);
htd.addFamily(hcdInfo);
}
admin.createTable(htd);
// 用完关闭
admin.close();
LOGGER.info("end create table ......");
}
/**
* 创建hbase表
*
* @param tableName
* 创建表名称
* @param version
* 版本数量,如果指定版本,则创建是设置的版本,如果version《=0,则默认设置版本为3
* @param hColumn
* 列族
* @throws IOException
*/
public static void createTableByVersion(String tableName, int version, String... hColumn) throws IOException {
LOGGER.info("创建hbase表:【" + tableName + "】 版本数量为:" + version);
Admin admin = ConnectionFactory.createConnection(configuration).getAdmin();
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
for (String hc : hColumn) {
HColumnDescriptor hcdInfo = new HColumnDescriptor(hc);
if (version > 0) {
hcdInfo.setMaxVersions(version);
} else {
hcdInfo.setMaxVersions(3);
}
htd.addFamily(hcdInfo);
}
admin.createTable(htd);
// 用完关闭
admin.close();
LOGGER.info("创建hbase表【" + tableName + "】完成!");
}
/**
* 删除表
*
* @param tableName
* @throws IOException
*/
public static void deleteTable(String tableName) throws IOException {
Admin admin = ConnectionFactory.createConnection(configuration).getAdmin();
if (admin.tableExists(TableName.valueOf(tableName))) {
admin.disableTable(TableName.valueOf(tableName));
LOGGER.info("禁用表" + tableName + "!");
admin.deleteTable(TableName.valueOf(tableName));
LOGGER.info("删除表成功!");
} else {
LOGGER.info(tableName + "表不存在 !");
}
}
/**
* 删除Qualifier
*
* @param tableName
* @throws IOException
*/
public static void deleteQualifier(String tableName,String rowkey,String qualifier) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowkey));
delete.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
table.delete(delete);
}
/**
* 清空表
*
* @param tableName
* @throws IOException
*/
public static void truncateTable(String tableName) throws IOException {
Admin admin = ConnectionFactory.createConnection(configuration).getAdmin();
if (admin.tableExists(TableName.valueOf(tableName))) {
admin.disableTable(TableName.valueOf(tableName));
LOGGER.info("禁用表" + tableName + "!");
admin.truncateTable(TableName.valueOf(tableName), true);
LOGGER.info("清空表成功!");
} else {
LOGGER.info(tableName + "表不存在 !");
}
}
/**
* 插入一单元
*
* @param tableName
* @param rowkey
* @param family
* @param qualifier
* @param value
* @throws IOException
*/
public static void insertCell(String tableName, String rowkey, String family, String qualifier, String value) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(rowkey.getBytes());
put.addColumn(family.getBytes(), qualifier.getBytes(), value.getBytes());
table.put(put);
LOGGER.info(tableName + "插入key:" + rowkey + "行成功!");
}
/**
* 插入一行
*
* @param tableName
* @param rowkey
* @param family
* @param qualifier
* @param value
* @throws IOException
*/
public static void insertRecord(String tableName, String rowkey, String family,Map<String, String> map) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(rowkey.getBytes());
for (Map.Entry<String,String> dMap:map.entrySet()) {
put.addColumn(family.getBytes(),dMap.getKey().getBytes(),dMap.getValue().getBytes());
}
table.put(put);
LOGGER.info(tableName + "插入key:" + rowkey + "行成功!");
}
/**
* 删除行
*
* @param tableName
* @param rowkey
* @throws IOException
*/
public static void deleteRecord(String tableName, String rowkey) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Delete del = new Delete(rowkey.getBytes());
table.delete(del);
System.out.println(tableName + "删除行" + rowkey + "成功!");
}
/**
* 获取一行
*
* @param tableName
* @param rowkey
* @return
* @throws IOException
*/
public static Result getOneRecord(String tableName, String rowkey) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(rowkey.getBytes());
Result rs = table.get(get);
return rs;
}
/**
* 取得全部 单个版本
*
* @param tableName
* @return
*
* for (Result r : scanner) {
* list.add(r);
* }
* scanner.close()
* @throws IOException
*/
public static ResultScanner getAllRecord(String tableName) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
return scanner;
}
/**
* 查询全部,并根绝版本取值
*
* @param tableName
* hbase表名
* @param version
* hbase版本
* @return 返回值 List<Result>
* @throws IOException
* 异常
*/
public static List<Result> getAllRecordByVersion(String tableName, Integer version) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
if (version == null || version == 0) {
scan.setMaxVersions();
} else {
scan.setMaxVersions(version);
}
ResultScanner scanner = table.getScanner(scan);
List<Result> list = new ArrayList<Result>();
for (Result r : scanner) {
list.add(r);
}
scanner.close();
return list;
}
/**
* @param tableName
* @return
* @throws IOException
*/
public static ResultScanner getAllRecordByTableName(String tableName) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
// 设置客户端缓存
scan.setCaching(1000);
ResultScanner scanner = table.getScanner(scan);
return scanner;
}
/**
* 更新表的一列
*
* @param name
* @param rowKey
* @param familyName
* @param columnName
* @param value
* @throws IOException
*/
public static void updateTable(String name, String rowKey, String familyName, String columnName, String value) throws IOException {
TableName tableName = TableName.valueOf(name);
Table table = connection.getTable(tableName);
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName), Bytes.toBytes(value));
table.put(put);
table.close();
}
/**
* 批量删除数据
*
* @param list
* @throws IOException
*/
public static void deleteDataList(String tableNameStr, String family, String rowKEY, String colemn) throws IOException {
Table table = null;
List<Delete> deletes = new ArrayList<Delete>();
TableName tableName = TableName.valueOf(tableNameStr);
table = connection.getTable(tableName);
Delete delete = new Delete(Bytes.toBytes(rowKEY));
delete.addColumn(family.getBytes(), colemn.getBytes());
deletes.add(delete);
table.delete(deletes);
table.close();
}
/**
* 根据条件筛选
*
* @param tableName
* @param filters
* @return
* @throws IOException
*/
public static ResultScanner queryByFilterList(String tableName, List<Filter> filters) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
FilterList filterList1 = new FilterList(filters);
Scan scan = new Scan();
scan.setFilter(filterList1);
ResultScanner rs = table.getScanner(scan);
return rs;
}
/**
* 根绝rowkey和clumn查询
*
* @param tableName
* @param rowKey
* @param family
* @param column
* @return
* @throws IOException
*/
public static Result getRowKeyAndClumn(String tableName, String rowKey, String family, String column) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(family), Bytes.toBytes(column));
Result r = table.get(get);
// System.out.println("lala");
for (Cell cell : r.rawCells()) {
System.out.println("column: " + new String(CellUtil.cloneQualifier(cell),encoding));
System.out.println("value: " + new String(CellUtil.cloneValue(cell),encoding));
}
return r;
}
/**
* 表字段id
* @param tableName
* @param rowKey
* @param family
* @param qualifier
* @param step 步长
* @return
* @throws IOException
*/
public static long getTableIncrement(String tableName, String rowKey, String family, String qualifier,long step) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
long qualifie = table.incrementColumnValue(rowKey.getBytes(encoding),family.getBytes(encoding), qualifier.getBytes(encoding), (Long)step);
return qualifie;
}
/**
* 根据条件查询
*
* @param tableName
* @param columnFilterMap
* Map<familyname, List<columnValue>>
* @param filters
* List<Filter>
* @return
* @throws IOException
*/
public static ResultScanner queryByCondition(String tableName, Map<String, List<String>> columnFilterMap, List<Filter> filters) throws IOException {
ResultScanner rs = null;
Table table = connection.getTable(TableName.valueOf(tableName));
if (CollectionUtils.isNotEmpty(filters)) {
FilterList filterList = new FilterList(filters);
Scan scan = new Scan();
for (String familyName : columnFilterMap.keySet()) {
List<String> columnList = columnFilterMap.get(familyName);
if (columnList == null || columnList.isEmpty()) {
scan.addFamily(Bytes.toBytes(familyName));
} else {
for (String cl : columnList) {
scan.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(cl));
}
}
}
scan.setFilter(filterList);
rs = table.getScanner(scan);
} else {
// 如果没有条件查询全量
Scan scan = new Scan();
scan.setFilter(new FirstKeyOnlyFilter());
rs = table.getScanner(scan);
}
return rs;
}
/***
* 组合条件查询*
*
* @param tableName
* @throws IOException
*/
public static ResultScanner queryByCondition(String tableName, String family, String qualifier, List<Filter> filters) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
scan.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
if(CollectionUtils.isNotEmpty(filters)){
FilterList filterList = new FilterList(filters);
// List<Filter> filters = new ArrayList<Filter>();
// Filter filter1 = new SingleColumnValueFilter(Bytes.toBytes("family_1"), null, CompareOp.EQUAL, Bytes.toBytes("v001"));
// filters.add(filter1);
scan.setFilter(filterList);
}
System.out.println("haha");
ResultScanner rs = table.getScanner(scan);
for (Result r : rs) {
System.out.println("获得到rowkey:" + new String(r.getRow()));
for (Cell cell : r.rawCells()) {
System.out.print(new String(CellUtil.cloneRow(cell)) + " ");
System.out.print(new String(CellUtil.cloneFamily(cell)) + ":");
System.out.print(new String(CellUtil.cloneQualifier(cell)) + " ");
System.out.print(cell.getTimestamp() + " ");
System.out.println(new String(CellUtil.cloneValue(cell)));
}
}
// rs.close();
return rs;
}
public static long rowCount(String tableName) throws IOException {
long rowCount = 0;
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
scan.setFilter(new FirstKeyOnlyFilter());
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
System.out.println(result.size());
rowCount += result.size();
}
return rowCount;
}
/**
* 根据qualifier值过滤result
* @param results
* @param map
* @return
*/
/*public List<Result> filterResultList(List<Result> results,Map<String,Map<String, String>> map){
List<Result> rList = new ArrayList<>();
for (int i = 0; i < results.size(); i++) {
Result result = results.get(i);
for (Cell cell : result.rawCells()) {
try {
String qualifier = new String(CellUtil.cloneQualifier(cell),encoding);
String value = new String(CellUtil.cloneValue(cell),encoding);
for (Entry<String, Map<String, String>> qMap: map.entrySet()) {
if (!StringUtils.isBlank(qMap.getKey())
&& qMap.getKey().equalsIgnoreCase(qualifier)){
for (Entry<String, String> vMap:qMap.getValue().entrySet()) {
if (vMap.getKey().equals(like)) {
if (value.contains(vMap.getValue())) {
rList.add(result);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
boolean ok = false;
}
return rList;
}*/
/**
* 根据单元格数据过滤结果集
* @param results
* @param map
* @return
*/
public static List<Result> filterResultList(List<Result> results,List<HbaseQuaCondition> hcList){
if (CollectionUtils.isEmpty(hcList)) {
return results;
}
hcList.add(new HbaseQuaCondition("is_deleted", HbaseClientUtils.equal, "0"));
for (HbaseQuaCondition hc: hcList) {
List<Result> rList = new ArrayList<>();
for (int i = 0; i < results.size(); i++) {
Result result = results.get(i);
for (Cell cell : result.rawCells()) {
try {
String qualifier = new String(CellUtil.cloneQualifier(cell),encoding);
String value = new String(CellUtil.cloneValue(cell),encoding);
if (qualifier.equals(hc.getFieldName())) {
if (like.equals(hc.getOperator())) {
if (value.contains(hc.getValue())) {
rList.add(result);
}
}else if(equal.equals(hc.getOperator())){
if (value.equals(hc.getValue())) {
rList.add(result);
}
}else if(not_equal.equals(hc.getOperator())){
if (!value.equals(hc.getValue())) {
rList.add(result);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
results = rList;
}
return results;
}
public static String getRowKey(){
return UuidUtil.get32UUID().concat("_").concat(DateUtil.getAgoBackDate(0));
}
public static void main(String[] args) throws Throwable {
//创建ct_approval_chain表
// String[] hct = new String[]{"id","step","approval_name","createUser","updateUser","createTime","updateTime","is_deleted"};
// System.out.println(HbaseClientUtils.isTableExists("ct_approval_chain"));
// HbaseClientUtils.createTable("ct_approval_chain","chronost");
// getRowKeyAndClumn("ct_approval_chain", "ct_approval_chain_increment", "chronost", "id");
// @SuppressWarnings("serial")
Map<String,String> qMap = new LinkedHashMap<String,String>(){{
put("id", "0");
// put("step", "2");
// put("approval_name", "rz");
// put("createUser", "admin");
// put("updateUser", "admin");
// put("createTime", DateUtils.getNowTimeStamp());
// put("updateTime", DateUtils.getNowTimeStamp());
// put("is_deleted", "0");
}};
// HbaseClientUtils.insertRecord("ct_approval_chain", "ct_approval_chain_increment",family,qMap);
// System.out.println(getTableIncrement("ct_approval_chain", "ct_approval_chain_increment",family,"id",1));
deleteQualifier("ct_approval_chain", "ct_approval_chain_increment", "id");
}
}
ReflectUtil:
package com.rz.commons.utils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.hf.model.ApprovalChainParam;
public class ReflectUtil {
/**
* 单个对象的所有键值
*
* @param obj
* 单个对象
* @return Map<String, Object> map 所有 String键 Object值
*/
public static Map<String, Object> getKeyAndValue(Object obj) {
Map<String, Object> map = new HashMap<String, Object>();
// 得到类对象
Class<? extends Object> userCla = obj.getClass();
/* 得到类中的所有属性集合 */
Field[] fs = userCla.getDeclaredFields();
for (int i = 0; i < fs.length; i++) {
Field f = fs[i];
System.out.println(f.getName());
f.setAccessible(true); // 设置些属性是可以访问的
Object val = new Object();
try {
val = f.get(obj);
// 得到此属性的值
map.put(f.getName(), val);// 设置键值
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return map;
}
/**
* 单个对象的某个键的值
*
* @param obj
* 对象
*
* @param key
* 键
*
* @return Object 键在对象中所对应得值 没有查到时返回空字符串
*/
public static Object getValueByKey(Object obj, String key) {
// 得到类对象
Class<? extends Object> userCla = obj.getClass();
/* 得到类中的所有属性集合 */
Field[] fs = userCla.getDeclaredFields();
for (int i = 0; i < fs.length; i++) {
Field f = fs[i];
f.setAccessible(true); // 设置些属性是可以访问的
try {
if (f.getName().endsWith(key)) {
return f.get(obj);
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
// 没有查到时返回空字符串
return "";
}
/**
* 多个(列表)对象的所有键值
*
* @param object
* @return List<Map<String,Object>> 列表中所有对象的所有键值
*/
public static List<Map<String, Object>> getKeysAndValues(List<Object> object) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (Object obj : object) {
Class<? extends Object> userCla;
// 得到类对象
userCla = obj.getClass();
/* 得到类中的所有属性集合 */
Field[] fs = userCla.getDeclaredFields();
Map<String, Object> listChild = new HashMap<String, Object>();
for (int i = 0; i < fs.length; i++) {
Field f = fs[i];
f.setAccessible(true); // 设置些属性是可以访问的
Object val = new Object();
try {
val = f.get(obj);
// 得到此属性的值
listChild.put(f.getName(), val);// 设置键值
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
list.add(listChild);// 将map加入到list集合中
}
return list;
}
/**
* 多个(列表)对象的某个键的值
*
* @param object
* @param key
* @return List<Object> 键在列表中对应的所有值
*/
public static List<Object> getValuesByKey(List<Object> object, String key) {
List<Object> list = new ArrayList<Object>();
for (Object obj : object) {
// 得到类对象
Class<? extends Object> userCla = obj.getClass();
/* 得到类中的所有属性集合 */
Field[] fs = userCla.getDeclaredFields();
for (int i = 0; i < fs.length; i++) {
Field f = fs[i];
f.setAccessible(true); // 设置些属性是可以访问的
try {
if (f.getName().endsWith(key)) {
list.add(f.get(obj));
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
return list;
}
/**
* 根据字段名获取指定对象的Field对象
*
* @param object
* @param fieldName
* @return
*/
public static Field obtainFieldByFieldName(Object object, String fieldName) {
for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass
.getSuperclass()) {
for (Field field : superClass.getDeclaredFields()) {
if (fieldName.equals(field.getName())) {
return field;
}
}
}
return null;
}
/**
* 根据字段名获取字段值
*
* @param object
* @param fieldName
* @return
*/
public static Object obtainValueByFieldName(Object object, String fieldName) {
Field field = obtainFieldByFieldName(object, fieldName);
Object value = null;
try {
if (null != field) {
if (field.isAccessible()) {
value = field.get(object);
} else {
field.setAccessible(true);
value = field.get(object);
field.setAccessible(false);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
/**
* 设置指定对象的某字段值
*
* @param object
* @param fieldName
* @param value
*/
public static void setValueByFieldName(Object object, String fieldName, Object value) {
Field field = obtainFieldByFieldName(object, fieldName);
if (null == field)
return;
try {
if (field.isAccessible()) {
setValue(object, field, value);
} else {
field.setAccessible(true);
setValue(object, field, value);
field.setAccessible(false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 给指定Field对象设置合适类型的值
*
* @param field
* @param value
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
private static void setValue(Object obj, Field field, Object value)
throws IllegalArgumentException, IllegalAccessException {
String fieldClassName = field.getType().getSimpleName().toLowerCase();
System.out.println(fieldClassName);
switch (fieldClassName) {
case "string":
field.set(obj, value.toString());
break;
case "boolean":
field.set(obj, (boolean) value);
break;
case "int":
case "integer":
// Can not set java.lang.Integer field com.hf.model.xxx.xxx to (int)
// 如果字段定义的基本类型为包装类型,则不能用基本类型set方式;其它类型也由setXXX更改为set
// field.setInt(obj, Integer.parseInt(value.toString()));
field.set(obj, Integer.parseInt(value.toString()));
break;
case "double":
field.set(obj, Double.parseDouble(value.toString()));
break;
case "long":
field.set(obj, Long.parseLong(value.toString()));
break;
case "bigdecimal":
field.set(obj, new BigDecimal(value.toString()));
break;
case "float":
field.set(obj, Float.parseFloat(value.toString()));
break;
default:
System.out.println("不支持的数据类型:" + field.getName() + " : " + fieldClassName);
break;
}
}
/**
* 指定字段是否存在
*
* @param object
* @param fieldName
* @return
*/
public static boolean isExistField(Object object, String fieldName) {
try {
for (Field field : object.getClass().getFields()) {
if (fieldName.equals(field.getName())) {
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 指定方法是否存在
*
* @param object
* @param methodName
* @return
*/
public static boolean isExistMethod(Object object, String methodName) {
try {
for (Method method : object.getClass().getMethods()) {
if (methodName.equals(method.getName())) {
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static void main(String[] args)
throws IllegalArgumentException, IllegalAccessException, SecurityException, ClassNotFoundException {
System.out.println(ReflectUtil.getKeyAndValue(new ApprovalChainParam()));
ApprovalChainParam test = new ApprovalChainParam();
ReflectUtil.setValueByFieldName(test, "is_deleted", "1");
ReflectUtil.setValueByFieldName(test, "iStep", 111);
System.out.println(ReflectUtil.getKeyAndValue(test));
System.out.println(test.getIs_deleted());
System.out.println(test.getiStep());
}
}
HbaseQuaCondition:
package com.rz.model.hbase;
/**
* Hbase条件配置类
*/
public class HbaseQuaCondition {
//匹配字段名称
private String fieldName;
//匹配字符
private String operator;
//匹配值
private String value;
public String getFieldName() {
return fieldName;
}
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "HbaseQuaCondition [fieldName=" + fieldName + ", operator=" + operator + ", value=" + value + "]";
}
public HbaseQuaCondition(String fieldName, String operator, String value) {
super();
this.fieldName = fieldName;
this.operator = operator;
this.value = value;
}
public HbaseQuaCondition() {
super();
// TODO Auto-generated constructor stub
}
}
HbaseBuilder:
package com.rz.model.hbase;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.rz.commons.utils.HbaseClientUtils;
/**
* 结果集构建链
*/
public class HbaseBuilder extends FromBuilder {
private List<Map<String, Object>> leftList;
private List<Map<String, Object>> result;
public HbaseBuilder fromList(List<Map<String, Object>> list) {
this.fromList = list;
this.result = list;
return this;
}
public HbaseBuilder leftList(List<Map<String, Object>> list) {
this.leftList = list;
return this;
}
public HbaseBuilder on(String primaryField, String foreignField) {
if (CollectionUtils.isNotEmpty(result)) {
for (Map<String, Object> map : result) {
if (map.containsKey(primaryField) && map.get(primaryField) != null) {
for (Map<String, Object> fMap : leftList) {
if (fMap.containsKey(foreignField) && fMap.get(foreignField) != null
&& map.get(primaryField).equals(fMap.get(foreignField))) {
map.putAll(fMap);
}
}
}
}
}
return this;
}
public HbaseBuilder where(List<HbaseQuaCondition> hcList) {
for (HbaseQuaCondition hc : hcList) {
List<Map<String, Object>> rList = new ArrayList<>();
for (Map<String, Object> rMap : result) {
try {
if (rMap.containsKey(hc.getFieldName())) {
Object value = rMap.get(hc.getFieldName());
if (value == null) continue;
if (HbaseClientUtils.like.equals(hc.getOperator())) {
if (value.toString().contains(hc.getValue())) {
rList.add(rMap);
}
} else if (HbaseClientUtils.equal.equals(hc.getOperator())) {
if (value.equals(hc.getValue())) {
rList.add(rMap);
}
} else if (HbaseClientUtils.not_equal.equals(hc.getOperator())) {
if (!value.equals(hc.getValue())) {
rList.add(rMap);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
result = rList;
}
return this;
}
public HbaseBuilder where(HbaseQuaCondition... hcList) {
where(Arrays.asList(hcList));
return this;
}
public List<Map<String, Object>> build() {
return result;
}
public <T> List<T> result(Class<T> t) {
if (CollectionUtils.isEmpty(result)) {
return Lists.newArrayList();
}
List<T> list = JSON.parseArray(JSON.toJSONString(result), t);
return list;
}
}
FromBuilder:
package com.rz.model.hbase;
import java.util.List;
import java.util.Map;
public class FromBuilder {
protected List<Map<String,Object>> fromList;
}
5. 使用案例1:
package com.rz.hservice.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Result;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rz.commons.utils.HbaseClientUtils;
import com.rz.commons.utils.HbaseTableUtil;
import com.rz.commons.utils.ReflectUtil;
import com.rz.hservice.HApprovalChainService;
import com.rz.model.ApprovalChainParam;
import com.rz.model.hbase.HbaseQuaCondition;
@Service
public class HApprovalChainServiceImpl extends HbaseServiceImpl implements HApprovalChainService {
Logger log = Logger.getLogger(HApprovalChainServiceImpl.class);
@Override
public List<ApprovalChainParam> getApprovalChainList(Map<String, Object> param) {
// TODO Auto-generated method stub
@SuppressWarnings("serial")
List<HbaseQuaCondition> hcList = new ArrayList<HbaseQuaCondition>() {
{
add(new HbaseQuaCondition("is_deleted", HbaseClientUtils.equal, param.get("is_deleted").toString()));
add(new HbaseQuaCondition("iApproval_name", HbaseClientUtils.like,
param.get("iApproval_name").toString()));
}
};
//查询表值
List<Result> list = scaner(HbaseTableUtil.CT_APPROVAL_CHAIN.name());
//过滤
list = HbaseClientUtils.filterResultList(list, hcList);
//匹配产出
List<ApprovalChainParam> rList = dataOutput(ApprovalChainParam.class,list);
// //简写法
// List<ApprovalChainParam> rList2 = dataOutput(ApprovalChainParam.class,HbaseTableUtil.CT_APPROVAL_CHAIN.name(),hcList);
return rList;
}
@Override
public int getApprovalChainNumList(Map<String, Object> param) {
// TODO Auto-generated method stub
@SuppressWarnings("serial")
List<HbaseQuaCondition> hcList = new ArrayList<HbaseQuaCondition>() {
{
add(new HbaseQuaCondition("is_deleted", HbaseClientUtils.equal, param.get("is_deleted").toString()));
add(new HbaseQuaCondition("iApproval_name", HbaseClientUtils.like,
param.get("iApproval_name").toString()));
}
};
List<Result> list = scaner(HbaseTableUtil.CT_APPROVAL_CHAIN.name());
list = HbaseClientUtils.filterResultList(list, hcList);
return list.size();
}
@Override
public int insertApprovalChain(ApprovalChainParam approvalChainParam) {
String tableName= HbaseTableUtil.CT_APPROVAL_CHAIN.name();
String tableIncrement=tableName+increment_subfix;
long id=incrQualifier(tableName,HbaseClientUtils.family, tableIncrement,increment_name,1);
approvalChainParam.setiAutoID(Long.valueOf(id).intValue());
Map map = JSONObject.parseObject(JSON.toJSONString(approvalChainParam), Map.class);
return insertData(HbaseClientUtils.getRowKey(), HbaseTableUtil.CT_APPROVAL_CHAIN.name(), map);
}
@Override
public int deleteApprovalChain(ApprovalChainParam approvalChainParam) {
// TODO Auto-generated method stub
return delRecord(HbaseTableUtil.CT_APPROVAL_CHAIN.name(),approvalChainParam.getRowkey());
}
@Override
public int updateApprovalChainByPriKey(ApprovalChainParam approvalChainParam) {
// TODO Auto-generated method stub
Map map = JSONObject.parseObject(JSON.toJSONString(approvalChainParam), Map.class);
return updateRecord(HbaseTableUtil.CT_APPROVAL_CHAIN.name(), HbaseClientUtils.getRowKey(),
HbaseClientUtils.family, map);
}
@Override
public List<ApprovalChainParam> queryCobomBoxApprovaName() {
// TODO Auto-generated method stub
List<HbaseQuaCondition> hcList = new ArrayList<HbaseQuaCondition>() {
{
add(new HbaseQuaCondition("is_deleted", HbaseClientUtils.equal, "0"));
}
};
List<Result> list = scaner(HbaseTableUtil.CT_AUTH_USER.name());
list = HbaseClientUtils.filterResultList(list, hcList);
List<ApprovalChainParam> rList = new ArrayList<>();
for (Result result : list) {
ApprovalChainParam acp = new ApprovalChainParam();
for (Cell cell : result.rawCells()) {
try {
String qualifier = new String(CellUtil.cloneQualifier(cell), HbaseClientUtils.encoding);
String value = new String(CellUtil.cloneValue(cell), HbaseClientUtils.encoding);
if(qualifier.equals("name")){
qualifier="iApproval_name";//赋别名
// assign(acp, qualifier, value);
ReflectUtil.setValueByFieldName(acp, qualifier, value);
rList.add(acp);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
return rList;
}
@Override
public List<ApprovalChainParam> getApprovalNameList() {
//条件
@SuppressWarnings("serial")
List<HbaseQuaCondition> hcList = new ArrayList<HbaseQuaCondition>() {
{
add(new HbaseQuaCondition("is_deleted", HbaseClientUtils.equal, "0"));
}
};
//查询表值
List<Result> list = scaner(HbaseTableUtil.CT_APPROVAL_CHAIN.name());
//过滤
list = HbaseClientUtils.filterResultList(list, hcList);
//匹配产出
List<ApprovalChainParam> rList = dataOutput(ApprovalChainParam.class,list);
return rList;
}
@Override
public ApprovalChainParam getApprovalChainByStepAndName(ApprovalChainParam param) {
// TODO Auto-generated method stub
List<HbaseQuaCondition> hcList = new ArrayList<HbaseQuaCondition>() {
{
add(new HbaseQuaCondition("is_deleted", HbaseClientUtils.equal, "0"));
add(new HbaseQuaCondition("iApproval_name", HbaseClientUtils.equal,
param.getiApproval_name()));
}
};
//查询表值
List<Result> list = scaner(HbaseTableUtil.CT_APPROVAL_CHAIN.name());
//过滤
list = HbaseClientUtils.filterResultList(list, hcList);
ApprovalChainParam acp = new ApprovalChainParam();
//匹配产出
List<ApprovalChainParam> rList = dataOutput(ApprovalChainParam.class,list);
if(CollectionUtils.isNotEmpty(rList)){
acp=rList.get(0);
}
return acp;
}
// 忽略
@Override
public ApprovalChainParam queryApprovalChainParamById(Integer id) {
// TODO Auto-generated method stub
return null;
}
@Override
public ApprovalChainParam queryApprovalChainParamByRowkey(String rowKey) {
// TODO Auto-generated method stub
Result result = getRow(HbaseTableUtil.CT_APPROVAL_CHAIN.name(), rowKey);
ApprovalChainParam acp = new ApprovalChainParam();
for (Cell cell : result.rawCells()) {
try {
String qualifier = new String(CellUtil.cloneQualifier(cell), HbaseClientUtils.encoding);
String value = new String(CellUtil.cloneValue(cell), HbaseClientUtils.encoding);
// assign(acp, qualifier, value);
ReflectUtil.setValueByFieldName(acp, qualifier, value);
} catch (Exception e) {
e.printStackTrace();
}
}
return acp;
}
}
使用案例2(多表连接):
@Override
public UserVo selectVoById(Long id) {
// TODO Auto-generated method stub
List<Map<String, Object>> userList = dataOutput(User_Table);
List<Map<String, Object>> userRoleList = dataOutput(User_Role_Table);
List<Map<String, Object>> roleList = dataOutput(Role_Table);
List<HbaseQuaCondition> conditions = Lists
.newArrayList(new HbaseQuaCondition("id", HbaseClientUtils.equal, String.valueOf(id)));
List<UserVo> list = new HbaseBuilder().fromList(userList).leftList(userRoleList).on("id", "id")
.leftList(roleList).on("id", "id").where(conditions).result(UserVo.class);
if (CollectionUtils.isEmpty(list))
return list.get(0);
return new UserVo();
}