1、 环境准备:
Maven
Eclipse
Java
Spring 版本 3..2.9
2、 Maven pom.xml配置
org.apache.hbase
hbase-client
0.96.1.1-hadoop2
org.springframework.data
spring-data-jpa
1.6.0.RELEASE
org.springframework.data
spring-data-hadoop
2.0.2.RELEASE
3、 Spring和hadoop、hbase相关配置文件
其中标红的是spring hadoop xml命名空间配置。
Hadoop hbase相关配置文件如下:
fs.default.name=hdfs://192.98.8.224:8010
对应的properties如下:
hbase.zookeeper.property.clientPort=2181
hbase.zookeeper.quorum=192.98.8.224
hbase.master=192.98.8.224:600000
fs.default.name=hdfs://192.98.8.224:8010
delete-connection=true
#hive jdbc url
hive.url=jdbc:hive://192.98.8.224:10000/default
spring hbasetemplate配置如下:
Hbasetemplate使用代码示例:
Tile t = hbaseTemplate.get("GW_TILES", "0_1_1", new RowMapper() {
@Override
public Tile mapRow(Result result, int rowNum) throws Exception {
// TODO Auto-generated method stub
Tile t = new Tile();
t.setData(result.getValue("T".getBytes(), "key".getBytes()));
return t;
}
});
Hbasetemplate 常用方法简介:
hbaseTemplate.get("GW_TILES", "0_1_1", new RowMapper 常用于查询,使用示例如下所示:
Tile t = hbaseTemplate.get("GW_TILES", "0_1_1", new RowMapper() {
@Override
public Tile mapRow(Result result, int rowNum) throws Exception {
// TODO Auto-generated method stub
Tile t = new Tile();
t.setData(result.getValue("T".getBytes(), "key".getBytes()));
return t;
}
});
hbaseTemplate.execute(dataIdentifier, new TableCallback 常用于更新操作,使用示例如下所示:
return hbaseTemplate.execute(dataIdentifier, new TableCallback() {
@Override
public Boolean doInTable(HTableInterface table) throws Throwable {
// TODO Auto-generated method stub
boolean flag = false;
try{
Delete delete = new Delete(key.getBytes());
table.delete(delete);
flag = true;
}catch(Exception e){
e.printStackTrace();
}
return flag;
}
});
备注:spring hbasetemplate针对hbase接口做了强大的封装,普通功能可以使用它强大的接口,同时复杂的功能,还可以使用hbase原生的接口,如:HTableInterface、Result等。其类方法如下图:
同时hbasetemplate封装了hbase连接池等,它的创建和释放通过配置来自动管理。
文章转载请注明出处:http://www.iyunv.com/likehua/p/4016257.html
|