本文上接前面文章:
Hbase初始化并根据RowKey获取数据
1、重写前面的文章中的类HbaseInit.java:
public class HbaseInit {
private Logger logger = LoggerFactory.getLogger(HbaseInit.class);
private Logger logger = LoggerFactory.getLogger(HbaseInit.class);
//代表转化为String
private GetRow getRow = new GetRow();
private GetRow getRow = new GetRow();
//代表转化为Map
private GetRowMap getRowMap = new GetRowMap();
//初始化Hbase环境变量
static{
System.setProperty("HADOOP_USER_NAME", "sousuo");
}
public String getValueFromHbase(HbaseTemplate htemplate, String tableName, String rowKey)
{
String hbaseRes = "";
try{
hbaseRes = htemplate.get(tableName, rowKey, getRow);
}catch(Exception e){
logger.error(e.getMessage(), e);
}
return hbaseRes;
}
/ /获取tableName下rowKey下family下qualifier对应的值
public String getValueByQualifier(HbaseTemplate htemplate, String tableName, String rowKey, String family, String qualifier)
{
String hbaseRes = "";
try{
hbaseRes = htemplate.get(tableName, rowKey, family, qualifier, getRow);
}catch(Exception e){
logger.error(e.getMessage(), e);
}
return hbaseRes;
}
//根据RowKey返回所有数据的MAP
public Map<String, String> getMapFromHbase(HbaseTemplate htemplate, String tableName, String rowKey){
Map<String, String> hbaseRes = new HashMap<String, String>();
try{
hbaseRes = htemplate.get(tableName, rowKey, getRowMap);
}catch(Exception e){
logger.error(e.getMessage(), e);
}
return hbaseRes;
}
}
private GetRowMap getRowMap = new GetRowMap();
//初始化Hbase环境变量
static{
System.setProperty("HADOOP_USER_NAME", "sousuo");
}
public String getValueFromHbase(HbaseTemplate htemplate, String tableName, String rowKey)
{
String hbaseRes = "";
try{
hbaseRes = htemplate.get(tableName, rowKey, getRow);
}catch(Exception e){
logger.error(e.getMessage(), e);
}
return hbaseRes;
}
/ /获取tableName下rowKey下family下qualifier对应的值
public String getValueByQualifier(HbaseTemplate htemplate, String tableName, String rowKey, String family, String qualifier)
{
String hbaseRes = "";
try{
hbaseRes = htemplate.get(tableName, rowKey, family, qualifier, getRow);
}catch(Exception e){
logger.error(e.getMessage(), e);
}
return hbaseRes;
}
//根据RowKey返回所有数据的MAP
public Map<String, String> getMapFromHbase(HbaseTemplate htemplate, String tableName, String rowKey){
Map<String, String> hbaseRes = new HashMap<String, String>();
try{
hbaseRes = htemplate.get(tableName, rowKey, getRowMap);
}catch(Exception e){
logger.error(e.getMessage(), e);
}
return hbaseRes;
}
}
2、
//代表转化为String
public class GetRow implements RowMapper<String>{
public String mapRow(Result result, int rowNum) throws Exception {
String hbaseRes = "";
List<Cell> ceList = result.listCells();
if(ceList!=null&&ceList.size()>0){
for(Cell cell:ceList){
hbaseRes += Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength())+
"_"+Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength())
+"="+Bytes.toString( cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())+" ";
}
}
return hbaseRes;
}
}
public String mapRow(Result result, int rowNum) throws Exception {
String hbaseRes = "";
List<Cell> ceList = result.listCells();
if(ceList!=null&&ceList.size()>0){
for(Cell cell:ceList){
hbaseRes += Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength())+
"_"+Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength())
+"="+Bytes.toString( cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())+" ";
}
}
return hbaseRes;
}
}
3、
//代表转化为Map
public class GetRowMap implements RowMapper<Map<String, String>>{
public Map<String, String> mapRow(Result result, int rowNum) throws Exception {
Map<String, String> hbaseRes = new HashMap<String, String>();
List<Cell> ceList = result.listCells();
if(ceList!=null&&ceList.size()>0){
for(Cell cell:ceList){
String key = Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength())+
"_"+Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
String value = Bytes.toString( cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
hbaseRes.put(key, value);
}
}
return hbaseRes;
}
}
public Map<String, String> mapRow(Result result, int rowNum) throws Exception {
Map<String, String> hbaseRes = new HashMap<String, String>();
List<Cell> ceList = result.listCells();
if(ceList!=null&&ceList.size()>0){
for(Cell cell:ceList){
String key = Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength())+
"_"+Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
String value = Bytes.toString( cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
hbaseRes.put(key, value);
}
}
return hbaseRes;
}
}
4、应用:
Map<String, String> hbaseRes = hbaseInit.getMapFromHbase(htemplate, tableName, rowKey);
//性别画像
String sex_value = hbaseRes.get("statistics_sex_value");
//性别画像
String sex_value = hbaseRes.get("statistics_sex_value");