1、Hbase配置文件
hbase.properties:
quorum=******************
clientPort=************
recommendInterfaceTable=**************
rowkeyPre=******************
family=************
column=************
2、applicationContext.xml:
<bean id="hbaseAccess" class="com.com.HbaseAccess"/>
<bean id="hbaseInit" class="com.com.HbaseInit"/>
<import resource="classpath:/spring-hbase.xml" />
3、spring-hbase.xml:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<!-- 重试等待时间 -->
<property>
<name>hbase.client.pause</name>
<value>50</value>
</property>
<!-- 失败时重试次数 -->
<property>
<name>hbase.client.retries.number</name>
<value>3</value>
</property>
<!-- RPC请求的超时时间。如果某次RPC时间超过该值,客户端就会主动关闭socket -->
<property>
<name>hbase.rpc.timeout</name>
<value>2000</value>
</property>
<!-- HBase客户端发起一次数据操作直至得到响应之间总的超时时间 -->
<property>
<name>hbase.client.operation.timeout</name>
<value>3000</value>
</property>
<!-- 该参数是表示HBase客户端发起一次scan操作的rpc调用至得到响应之间总的超时时间 -->
<property>
<name>hbase.client.scanner.timeout.period</name>
<value>10000</value>
</property>
</configuration>
4、HbaseAccess.java:
public class HbaseAccess {
@Autowired
private HbaseTemplate htemplate;
@Autowired
HbaseInit hbaseInit;
public Map<String, String> getStringFromHbase(String tableName, String rowKey, String family_column){
Map<String, String> hbaseRes = hbaseInit.getMapFromHbase(htemplate, tableName, rowKey);
return hbaseRes;
}
}
5、HbaseInit.java:
public class HbaseInit {
private Logger logger = LoggerFactory.getLogger(HbaseInit.class);
//初始化Hbase环境变量
static{
System.setProperty("*****", "*****");
}
/**
* 获取tableName下rowKey下family下qualifier对应的值
* @param htemplate
* @param tableName
* @param rowKey
* @param family
* @param qualifier
* @return
*/
public String getValueByQualifier(HbaseTemplate htemplate, String tableName, String rowKey, String family, String qualifier)
{
String hbaseRes = "";
try{
hbaseRes = htemplate.get(tableName, rowKey, family, qualifier, new 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;
}
});
}catch(Exception e){
logger.error(e.getMessage(), e);
}
return hbaseRes;
}
/**
* 根据RowKey返回所有数据的MAP
* @param htemplate
* @param tableName
* @param rowKey
* @return
*/
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, new 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;
}
});
}catch(Exception e){
logger.error(e.getMessage(), e);
}
return hbaseRes;
}
}
6、使用代码:
@Autowired
HbaseAccess hbaseAccess;
Map<String, String> result = hbaseAccess.getStringFromHbase(tableName, rowKey, family_column);