Hbase访问优化

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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值