一、HBase java
HBase 是在 Hadoop 分佈式文件系統(簡稱:HDFS)之上的分佈式面向列的數據庫。並且是 2007 最初原型,歷史悠久。 那追根究底,Hadoop 是什麼?Hadoop是一個分佈式環境存儲並處理大數據。Hadoop 使用 MapReduce 算法統計分析大數據。node
場景:git
HBase 在 Hadoop 之上提供了相似 BigTable 的能力,它不一樣於通常的關係數據庫,是一個適合非結構化數據存儲的數據庫。它也不一樣於行式數據庫,是基於列的模式。github
HBase 一個面向列的數據庫,排序由行決定。web
與行式數據庫最大的區別就是,能夠面向列設計巨大表,適用於在線分析處理 OLAP。
與關係型數據庫 RDBMS 也有些區別以下:算法
二、使用配置spring
2.1 數據庫
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.yarn.webapp.hamlet.Hamlet.KBD;
import org.junit.Before;
public class HBaseOpreation {
public Configuration conf = null;
/** * init */
@Before
public void init(){
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hadoop1:2181"); //指定zk地址
}
/** * 刪除 * @throws Exception */
public void drop() throws Exception{
HBaseAdmin admin = new HBaseAdmin(conf);
admin.disableTable("");
admin.deleteTable("");
admin.close();
}
/** * 添加數據 * @throws Exception */
public void put() throws Exception{
HTable table = new HTable(conf, "");
List<Put> puts = new ArrayList<Put>();
Put name = new Put("".getBytes()); // 建立行健
name.add("".getBytes(), "".getBytes(), "".getBytes());
Put address = new Put("".getBytes()); // 建立行健
address.add("".getBytes(), "".getBytes(), "".getBytes());
puts.add(name);
puts.add(address);
table.put(puts);
table.close();
}
/** * @throws Exception * */
public void get() throws Exception{
HTable table = new HTable(conf, "");
Get get = new Get("".getBytes());
get.setMaxVersions(3);
Result result = table.get(get);
for (KeyValue kv : result.list()) {
String family = new String( kv.getFamily());
String qualifier = new String(kv.getQualifier());
String value = new String(kv.getValue());
System.out.println(family + "\t" + qualifier + "\t" + value);
}
table.close();
}
/** * @throws Exception */
public void del() throws Exception{
HTable table = new HTable(conf, "");
Delete del = new Delete("".getBytes());
del.deleteColumn("".getBytes(), "".getBytes());
table.delete(del);
table.close();
}
public static void main(String[] args) throws Exception{
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "");
HBaseAdmin hAdmin = new HBaseAdmin(conf);
HTableDescriptor tDescriptor = new HTableDescriptor("".getBytes());
HColumnDescriptor cDescriptor = new HColumnDescriptor("".getBytes());
cDescriptor.setMaxVersions(10);
tDescriptor.addFamily(cDescriptor);
hAdmin.createTable(tDescriptor);
hAdmin.close();
}
}
複製代碼
2.2apache
//pom.xml
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.0.0-cdh5.4.4</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Spring Boot HBase 依賴 -->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>spring-boot-starter-hbase</artifactId>
<version>${hbase-spring-boot}</version>
</dependency>複製代碼
## HBase 配置
spring.data.hbase.quorum=xxx //指定 HBase 的zk地址
spring.data.hbase.rootDir=xxx //指定 HBase 在HDFS上存儲的路徑
spring.data.hbase.nodeParent=xxx //指定 ZK 中HBase的根 ZNode
複製代碼
@Data
public class City {
/** * 城市編號 */
private Long id;
/** * 省份年齡 */
private Integer age;
/** * 城市名稱 */
private String cityName;
}複製代碼
//實現 RowMapper 進行列簇轉換
public class CityRowMapper implements RowMapper<City> {
private static byte[] COLUMN_FAMILY = "f".getBytes();
private static byte[] NAME = "name".getBytes();
private static byte[] AGE = "age".getBytes();
@Override
public City mapRow(Result result, int rowNum) throws Exception {
String name = Bytes.toString(result.getValue(COLUMN_FAMILY, NAME));
int age = Bytes.toInt(result.getValue(COLUMN_FAMILY, AGE));
City dto = new City();
dto.setCityName(name);
dto.setAge(age);
return dto;
}
}複製代碼
// HbaseTemplate 使用相似 RedisTemplate 進行api調用
@Service
public class CityServiceImpl implements CityService {
@Autowired private HbaseTemplate hbaseTemplate;
//根據開始、結束行
public List<City> query(String startRow, String stopRow) {
Scan scan = new Scan(Bytes.toBytes(startRow), Bytes.toBytes(stopRow));
scan.setCaching(5000);
List<City> dtos = this.hbaseTemplate.find("people_table", scan, new CityRowMapper());
return dtos;
}
//查詢
public City query(String row) {
City dto = this.hbaseTemplate.get("people_table", row, new CityRowMapper());
return dto;
}
//新增保存
public void saveOrUpdate() {
List<Mutation> saveOrUpdates = new ArrayList<Mutation>();
Put put =new Put(Bytes.toBytes("135xxxxxx"));
put.addColumn(Bytes.toBytes("people"), Bytes.toBytes("name"), Bytes.toBytes("test"));
saveOrUpdates.add(put);
this.hbaseTemplate.saveOrUpdates("people_table", saveOrUpdates);
}
}複製代碼
項目可參考: github.com/SpringForAl…api