Spring Boot 2.x 集成 HBase

版权声明:“假装、是个有灵魂的程序员” —— Go Big Or Go Home https://blog.csdn.net/u011663149/article/details/88830411

前言:

1、HBase     

     HBase 是在 Hadoop 分布式文件系统(简称:HDFS)之上的分布式面向列的数据库。而且是 2007 最初原型,历史悠久。 那追根究底,Hadoop 是什么?Hadoop是一个分布式环境存储并处理大数据。Hadoop 使用 MapReduce 算法统计分析大数据。

     场景:

  • 监控数据的日志详情
  • 交易订单的详情数据(淘宝、有赞)
  • facebook 的消息详情

     HBase 在 Hadoop 之上提供了类似 BigTable 的能力,它不同于一般的关系数据库,是一个适合非结构化数据存储的数据库。它也不同于行式数据库,是基于列的模式。

     HBase 一个面向列的数据库,排序由行决定。

  • 表是行的集合。
  • 行是列族的集合。列族,就是键值对。每个列族以 key 为列命名,可以有无数的列。
  • 列族就是列的集合。列连续存储,并且每个单元会有对应的时间戳
  • 列的存储也是键值对。

image.png

与行式数据库最大的区别就是,可以面向列设计巨大表,适用于在线分析处理 OLAP。
与关系型数据库 RDBMS 也有些区别如下:

  • HBase 宽表,横向扩展。RDBMS 小表,难成规模
  • HBase 没有事务
  • HBase 无规范化数据,都是键值对 key value

2、使用配置

   2.1 org.apache.hadoop.conf.Configuration 

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.2 spring-boot-starter-hbase

    //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);
    }
}

  项目可参考: https://github.com/SpringForAll/spring-boot-starter-hbase


猜你喜欢

转载自blog.csdn.net/u011663149/article/details/88830411