1.准备
一个java的集成好spring的maven项目。
2.maven pom文件配置
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-hadoop</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
这里配置好这两个依赖项,maven会把相关的依赖包下载下来,不需要我们一个一个去找相关包。
如果不是用的maven来管理项目,那么就可能需要自己把相关的依赖包找出来。
3.hbase的配置
我们新建一个hbase-config.xml文件,配置内容为
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:hdp="http://www.springframework.org/schema/hadoop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/hadoop
http://www.springframework.org/schema/hadoop/spring-hadoop.xsd">
<hdp:configuration />
<!-- default HBase configuration -->
<hdp:hbase-configuration zk-quorum="10.20.15.197,10.20.15.198,10.20.15.200"
zk-port="2181">
zookeeper.znode.parent=/hbase1
</hdp:hbase-configuration>
<!-- wire hbase configuration (using default name 'hbaseConfiguration')
into the template -->
<bean id="hbaseTemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate">
<property name="configuration" ref="hbaseConfiguration" />
</bean>
</beans>
这里我基本使用的都是默认配置,xmlns是标签解析的时候需要用的一些hadoop相关的配置,
<hdp:configuration />是hadoop的默认配置,对应的默认生成的bean为hadoopCconfiguration,
<hdp:hbase-configuration/>是hbase的默认配置,对应默认生成的bean为hbaseConfiguration,
注意:这里我修改了zk-quorum, zk-port,zookeeper.znode.parent这三个属性
zk-quorum对应的是我们hbase服务器集群的zookeeper的地址, zk-port对应的是端口,
zookeeper.znode.parent对应的是hbase的zookeeper分配的znode的名字,这些我们要修改成
服务器对应的值,否则,我们是不能连接到对应的hbase集群的。
<bean id="hbaseTemplate" />标签是创建操作hbase模板bean,这个是spring为我们封装的一些常用的
hbase的增删改查的操作。我们可以使用这些封装好的方法,也可以自己直接操作hbase连接,封装对应的
需要的方法。
然后,我们需要把hbase-config.xml配置文件引入到总的配置文件中,这里我的总的配置文件是
application-context.xml,引入代码如下:
<import resource="classpath:config/hbase-config.xml" />
这里对应hbase-config.xml文件的存放地址,我的是放在config文件夹下。
4.java测试代码
public class HbaseTest {
/** 日志 */
private static Logger logger = Logger.getLogger(HbaseTest.class);
@Autowired
private HbaseTemplate htemplate;
//测试get方法
public void get() {
htemplate.get("bolg2", "rowkey1", new RowMapper<String>() {
@Override
public String mapRow(Result result, int rowNum) throws Exception {
String conten = Bytes.toString(result.getValue(Bytes.toBytes("article"), Bytes.toBytes("conten")));
logger.info(">>>>blog2查询数据为:" + conten);
return conten;
}
});
}
//main方法直接连接测试
public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
Configuration cf=HBaseConfiguration.create();
cf.set(HConstants.ZOOKEEPER_QUORUM,"10.20.15.197,10.20.15.198,10.20.15.200");
cf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/hbase1");
Connection connection = ConnectionFactory.createConnection(cf);
TableName name = TableName.valueOf("blog2");
Table table = connection.getTable(name);
Get get = new Get(Bytes.toBytes("rowkey1"));
Result result = table.get(get);
String conten =Bytes.toString(result.getValue(Bytes.toBytes("article"), Bytes.toBytes("conten")));
table.close();
connection.close();
logger.info(">>>>blog2查询数据为:" + conten);
}
}
以上提供了两种方法来测试我们的hbase集群的连通性,大家可以参考修改为自己可用的代码。
最后,如果我们能够从打印的日志里找到我们想要的值,那就成功了。
集成的过程中碰到很多问题,以上是问题解决后的整个流程,记录下来,供自己和他人参考。