您的位置:首页 > 其它

Hbase访问 配置信息直接写在程序中的访问方式

2018-01-28 15:03 351 查看
public class HbaseInit{

       private String QUORUM = "*****";

      private String CLIENTPORT = "2015";

      private Configuration conf = null;

      private static HConnection conn = null;

       static{
System.setProperty("HADOOP_USER_NAME", "sousuo");
}

public Map<String, String> getMapFromHbase(String tableName, String rowKey){
Map<String, String> hbaseRes = new HashMap<String, String>();
conf =  HBaseConfiguration.create();  

        conf.set("hbase.zookeeper.quorum", QUORUM);   

        conf.set("hbase.zookeeper.property.clientPort", CLIENTPORT);  

        conf.set("zookeeper.znode.parent", "/hbase"); 

        conf.set("hbase.client.retries.number", "2"); 

        conf.set("hbase.client.pause", "100"); 

        conf.set("zookeeper.recovery.retry", "3"); 

        conf.set("zookeeper.session.timeout", "1000"); 

        conf.set("zookeeper.recovery.retry.intervalmill", "200"); 

        conf.set("ipc.socket.timeout", "2000");

        conf.set("hbase.rpc.timeout", "2000");

        conf.set("hbase.client.scanner.timeout.period", "5000");

        conf.set("hbase.client.operation.timeout", "5000");

        try {

        logger.info("取表前++++++++++++++++++++++++++++++++++++++++++");
conn = HConnectionManager.createConnection(conf);
HTable table = new HTable(conf, tableName);
logger.info("取表后++++++++++++++++++++++++++++++++++++++++++");
        Get get = new Get(Bytes.toBytes(rowKey));
        Result result = table.get(get);
        for (KeyValue rowKV : result.raw()) {
            System.out.print("行名:" + new String(rowKV.getRow()) + " ");
            System.out.print("时间戳:" + rowKV.getTimestamp() + " ");
            System.out.print("列族名:" + new String(rowKV.getFamily()) + " ");
            System.out.print("列名:" + new String(rowKV.getQualifier()) + " ");
            System.out.println("值:" + new String(rowKV.getValue()));
            hbaseRes.put("*******", new String(rowKV.getValue()));
        }

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return hbaseRes;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐