前提条件 安装好了HBase, 并能够使用Phoenix连接到HBase.
我这边使用的是Hbase2.3.5
和phoenix-hbase-2.3-5.1.3-bin.tar.gz
.
如果没有的话请先参考我之前的文章: Phoenix连接单机版Hbase,亲测可用
1.导入maven依赖
pom.xml
我这里使用了druid连接池,ORM框架使用的是Mybatis-Plus
, 可以帮我们省去一些写sql的时间.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.apache.phoenix</groupId>
<artifactId>phoenix-client-hbase-2.3</artifactId>
<version>5.1.3</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.0.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
</dependency>
2.适配数据源
application.yml
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:phoenix:hadoop01:2181;phoenix.schema.isNamespaceMappingEnabled=true
driver-class-name: org.apache.phoenix.jdbc.PhoenixDriver
mybatis-plus:
mapper-locations: classpath:/mapper/*Mapper.xml
type-aliases-package: com.example.phoenix.dao.entity
3.创建表
直接在Phoenix的目录中执行sql脚本创建表
创建init.sql
cd /opt/phoenix/phoenix-hbase-2.3-5.1.3-bin/examples vim init.sql 粘贴下面的sql: -- 创建订单 CREATE TABLE IF NOT EXISTS tbl_order ( id BIGINT not null primary key, order_code char(20), total_amount decimal(10,2), create_time date, user_id bigint ); -- 插入数据 upsert into tbl_order values(1, 'A001', 10.5, '2019-3-19 23:35:00', 1); upsert into tbl_order values(2, 'A002', 60.0, '2019-3-19 23:36:00', 2); upsert into tbl_order values(3, 'B001', 66.6, '2019-3-20 01:01:00', 3); upsert into tbl_order values(4, 'C001', 66.4, '2019-3-20 02:01:00', 3);
- 使用Phoenix的py脚本运行sql文件
hadoop01:2181 是hbase连接的zk
cd /opt/phoenix/phoenix-hbase-2.3-5.1.3-bin/bin
./psql.py hadoop01:2181 ../examples/init.sql
- 使用连接Phoenix连接上HBase查看表
./sqlline.py hadoop01:2181
4. springboot中创建对应的类
entiry
@Data @ToString @RequiredArgsConstructor @TableName("tbl_order") public class Order extends Model<Order> { private Long id; private String orderCode; private BigDecimal totalAmount; private Date createTime; private Long userId; }
mapper类
public interface OrderMapper extends BaseMapper<Order> { }
service类
public interface IOrderService extends IService<Order> { } @Service public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService { }
测试类
@SpringBootTest class PhoenixDemoApplicationTests { @Autowired private IOrderService orderService; @Test void testGetList() { List<Order> orders = orderService.list(); for (Order order : orders) { System.out.println(order); } } }
运行结果
5.报错解决
- 如果在HBase的hbase-site.xml中开启了, springboot项目启动的时候会报如下错误:
Error: ERROR 726 (43M10): Inconsistent namespace mapping properties. Cannot initiate connection as SYSTEM:CATALOG is found but client does not have phoenix.schema.isNamespaceMappingEnabled enabled (state=43M10,code=726)
解决方法(可能不是最好的
):
- 修改hbase-site.xml,注释下面2个配置
重启hbase,进入hbase shell, 执行下面4步
- disable 'SYSTEM:CATALOG'
- snapshot 'SYSTEM:CATALOG','cata_tableSnapshot’
- clone_snapshot 'cata_tableSnapshot', 'SYSTEM.CATALOG'
drop 'SYSTEM:CATALOG'
- 测试springboot项目启动是否ok, 如果还不行则删除zk中的hbase目录和hbase的缓存(
学习中使用
)
本文由mdnice多平台发布
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。