添加依赖
<dependency>
<groupId>org.springframework.datagroupId>
<artifactId>spring-data-hadoopartifactId>
<version>2.5.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.apache.hbasegroupId>
<artifactId>hbase-clientartifactId>
<version>1.3.2version>
<exclusions>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
exclusion>
<exclusion>
<groupId>org.mortbay.jettygroupId>
<artifactId>servlet-api-2.5artifactId>
exclusion>
<exclusion>
<groupId>org.mortbay.jettygroupId>
<artifactId>servlet-api-2.5-6.1.14artifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.hadoopgroupId>
<artifactId>hadoop-hdfsartifactId>
<version>2.7.3version>
<exclusions>
<exclusion>
<artifactId>servlet-apiartifactId>
<groupId>javax.servletgroupId>
exclusion>
exclusions>
dependency>
2.添加配置文件
添加配置文件:hbase.properties
hbase.zookeeper.quorum=n1,n2,n3
hbase.zookeeper.port=2181
hbase.zookeeper.basePath=/hbase-unsecure
其中basePath参数为hbase在zk下的默认存储路径,自己装的hbase默认为/hbase,使用ambari安装的hbase默认如上。
添加一个类用于加载此配置文件:
package com.hbase.config;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "hbase.zookeeper")
@Getter
@Setter
public class HbaseProperties {
// zookeeper队列地址
private String quorum;
// zookeeper端口号
private String port;
// hbase在zk中的根路径
private String basePath;
}
3.配置hbase模板
添加类:
package com.hbase.config;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.hadoop.hbase.HbaseTemplate;
import lombok.Getter;
@Configuration
@PropertySource("classpath:hbase.properties")
@EnableConfigurationProperties(HbaseProperties.class)
@Getter
public class HbaseConfig {
@Autowired
private HbaseProperties hbaseProperties;
@Bean(name = "hbaseTemplate")
public HbaseTemplate hbaseTemplate() {
org.apache.hadoop.conf.Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", this.hbaseProperties.getQuorum());
configuration.set("hbase.zookeeper.port", hbaseProperties.getPort());
configuration.set("zookeeper.znode.parent", this.hbaseProperties.getBasePath());
return new HbaseTemplate(configuration);
}
}
4.创建service类
注:此类待日后完善,目前只测试了createTable和get方法,表示测通了。因忙于测试其他技术,此类需要在使用时来完善之。
package com.hbase.service;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.io.Charsets;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.FilterList.Operator;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.data.hadoop.hbase.HbaseTemplate;
import org.springframework.data.hadoop.hbase.RowMapper;
import org.springframework.data.hadoop.hbase.TableCallback;
import org.springframework.stereotype.Service;
import com.google.common.collect.Maps;
@Service
public class HbaseService {
private FilterList filterlist;
@Resource(name = "hbaseTemplate")
private HbaseTemplate htemplate;
public void createTable(String tableName) throws Exception{
System.out.println("-----------");
//配置ZooKeeper的地址信息
Configuration conf = htemplate.getConfiguration();
//得到HBase的一个客户端
HBaseAdmin client = new HBaseAdmin(conf);
//创建表的描述符
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
//创建列族
HColumnDescriptor h1 = new HColumnDescriptor("info");
HColumnDescriptor h2 = new HColumnDescriptor("prop");
//把列族加入表的描述符中
htd.addFamily(h1);
htd.addFamily(h2);
//创建表
client.createTable(htd);
client.close();
}
// 获取一行的数据,并封装成vo
public Map
return this.htemplate.get(tableName, rowKey, new RowMapper
@Override
public Map
return result2Map(result);
}
});
}
// 获取表中指定行,列簇,列的值
public String get(String tableName, String rowName, String familyName, String qualifier) {
return htemplate.get(tableName, rowName, familyName, qualifier,
new RowMapper
public String mapRow(Result result, int rowNum)
throws Exception {
List
return cellListToString(ceList);
}
});
}
public List
Scan scan = new Scan();
if (startRow != null) {
scan.setStartRow(Bytes.toBytes(startRow));
}
if (stopRow != null) {
scan.setStopRow(Bytes.toBytes(stopRow));
}
if (filterlist != null) {
scan.setFilter(filterlist);
}
return this.htemplate.find(tableName, scan, new RowMapper
@Override
public String mapRow(Result result, int rowNum) throws Exception {
List
return cellListToString(ceList);
}
});
}
// 将值插入表中指定的行,列簇,列中
public void put(final String tableName, final String rowKey,
final String familyName, final String column, final String value) {
htemplate.put(tableName, rowKey, familyName, column, value.getBytes());
}
public void del(final String tableName, final String rowKey, final String family, final String qualifier) throws Exception {
this.htemplate.execute(tableName, new TableCallback
@Override
public Object doInTable(HTableInterface table) throws Throwable {
Delete d = new Delete(rowKey.getBytes(Charsets.UTF_8));
if ((qualifier != null) && !"".equals(qualifier)) {
d.addColumn(family.getBytes(Charsets.UTF_8), qualifier.getBytes(Charsets.UTF_8));
}
table.delete(d);
return null;
}
});
}
public void addFilter(Filter filter) {
if (filterlist == null) {
filterlist = new FilterList(Operator.MUST_PASS_ALL);
}
filterlist.addFilter(filter);
}
public void clearFilter() {
filterlist = null;
}
private Map
Map
if (result != null) {
List
if ((cellList != null) && (cellList.size() > 0)) {
String rowKey = "";
for (Cell cell : cellList) {
rowKey = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
String family = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
String qualifier = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
byte[] data = Bytes.copy(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
map.put(family + "_" + qualifier, data);
}
map.put("rowKey", rowKey);
}
}
return map;
}
private String cellListToString(List
String result = "";
if ((ceList != null) && (ceList.size() > 0)) {
Cell cell = ceList.get(0);
result = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
}
return result;
}
}
5.测试
在另一个子项目中,先在pom中引入此包。
在controller中直接使用:
@Controller
public class HbaseController {
@Resource
private HbaseService hbaseService;
@RequestMapping(value = "/hbase/get", method = RequestMethod.GET)
public String sendEmail(){
try {
hbaseService.createTable("test2");
Map
System.out.println(map.toString());
} catch (Exception e) {
e.printStackTrace();
}
return "home";
}
}