其他分享
首页 > 其他分享> > Phoenix JDBC开发

Phoenix JDBC开发

作者:互联网

在这里插入图片描述

Apache Phoenix

Phoenix的建表语法

在Phoenix中,要执行SQL,必须要建立表的结构,然后才能查询。默认Phoenix不会之前在Hbase使用create创建的表加载进来。

create table if not exists ORDER_DTL(
    "id" varchar primary key,
    "C1"."status" varchar,
    "C1"."money" double,
    "C1"."pay_way" integer,
    "C1"."user_id" varchar,
    "C1"."operation_time" varchar,
    "C1"."category" varchar
);

注意事项:

  • 每个表必须要有rowkey,通过指定某一个列后面的primary key,就表示该列就是rowkey
  • 每个除了rowkey的列必须要带列蔟名,Phoenix会自动帮助我们创建列蔟
  • 大小写的问题,在Phoenix如果要使用小写,必须得带双引号。否则会自动转换为大小,如果使用小写将来编写的任何SQL语句都得带双引号

Phoenix创建视图

create view "名称空间"."表名"(
   rowkey对应列名 varchar primary key,
   "列蔟名"."列名" varchar,
);

创建跟之前一样的的视图

create view if not exists "MOMO_CHAT"."MSG"(
    id varchar primary key,
    "C1"."msg_time" varchar,
    "C1"."sender_nickyname" varchar,
    "C1"."sender_account" varchar,
    "C1"."sender_sex" varchar,
    "C1"."sender_ip" varchar,
    "C1"."sender_os" varchar,
    "C1"."sender_phone_type" varchar,
    "C1"."sender_network" varchar,
    "C1"."sender_gps" varchar,
    "C1"."receiver_nickyname" varchar,
    "C1"."receiver_ip" varchar,
    "C1"."receiver_account" varchar,
    "C1"."receiver_os" varchar,
    "C1"."receiver_phone_type" varchar,
    "C1"."receiver_network" varchar,
    "C1"."receiver_gps" varchar,
    "C1"."receiver_sex" varchar,
    "C1"."msg_type" varchar,
    "C1"."distance" varchar,
    "C1"."message" varchar
);

Table is read only异常解决

这个时候遇到一个异常:Table is read only
在这里插入图片描述

  1. hbase-site.xml添加如下配置
    <property>
      <name>phoenix.schema.isNamespaceMappingEnabled</name>
      <value>true</value>
    </property>
    <property>
      <name>phoenix.schema.mapSystemTablesToNamespace</name>
      <value>true</value>
    </property>
  1. 复制到phoneix目录下一份
cp hbase-site.xml /apache-phoenix-4.13.1-HBase-1.3-bin/bin/
  1. 再重新执行下建表命令,可以了

在这里插入图片描述

使用jdbc开发

  1. pom新增phonex 依赖配置
         <dependency>
            <groupId>org.apache.phoenix</groupId>
            <artifactId>phoenix-core</artifactId>
            <version>4.13.1-HBase-1.3</version>
        </dependency>
        <!-- phoenix 客户端 -->
        <dependency>
            <groupId>org.apache.phoenix</groupId>
            <artifactId>phoenix-queryserver-client</artifactId>
            <version>4.13.1-HBase-1.3</version>
        </dependency>
  1. 核心查询逻辑
public class PhoenixChatMessageService implements ChatMessageService {

    private Connection connection;

    public PhoenixChatMessageService() throws Exception {
        // 1. 加载驱动
        Class.forName(PhoenixDriver.class.getName());
        // 2. 获取JDBC连接
        Properties props = new Properties();
        props.setProperty("phoenix.schema.isNamespaceMappingEnabled", "true");
        connection = DriverManager.getConnection("jdbc:phoenix:master:2181",props);
    }

    @Override
    public List<Msg> getMessage(String date, String sender, String receiver) throws Exception {
        // 1. SQL语句
        String sql = "select * from \"MOMO_CHAT\".\"MSG\" where substr(\"msg_time\", 0, 10) = ? and \"sender_account\" = ? and \"receiver_account\" = ?";

        // 2. 构建一个prepareStatement
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        // 3. 设置Prestatement对应的参数
        preparedStatement.setString(1, date);
        preparedStatement.setString(2, sender);
        preparedStatement.setString(3, receiver);

        // 4. 执行SQL语句,获取到一个ResultSet
        ResultSet resultSet = preparedStatement.executeQuery();

        ArrayList<Msg> msgList = new ArrayList<>();

        // 5. 迭代ResultSet将数据封装在Msg里面
        while(resultSet.next()) {
            Msg msg = new Msg();
            msg.setMsg_time(resultSet.getString("msg_time"));
            msg.setSender_nickyname(resultSet.getString("sender_nickyname"));
            msg.setSender_account(resultSet.getString("sender_account"));
            msg.setSender_sex(resultSet.getString("sender_sex"));
            msg.setSender_ip(resultSet.getString("sender_ip"));
            msg.setSender_os(resultSet.getString("sender_os"));
            msg.setSender_phone_type(resultSet.getString("sender_phone_type"));
            msg.setSender_network(resultSet.getString("sender_network"));
            msg.setSender_gps(resultSet.getString("sender_gps"));
            msg.setReceiver_nickyname(resultSet.getString("receiver_nickyname"));
            msg.setReceiver_ip(resultSet.getString("receiver_ip"));
            msg.setReceiver_account(resultSet.getString("receiver_account"));
            msg.setReceiver_os(resultSet.getString("receiver_os"));
            msg.setReceiver_phone_type(resultSet.getString("receiver_phone_type"));
            msg.setReceiver_network(resultSet.getString("receiver_network"));
            msg.setReceiver_gps(resultSet.getString("receiver_gps"));
            msg.setReceiver_sex(resultSet.getString("receiver_sex"));
            msg.setMsg_type(resultSet.getString("msg_type"));
            msg.setDistance(resultSet.getString("distance"));
            msg.setMessage(resultSet.getString("message"));

            msgList.add(msg);
        }

        // 关闭资源
        resultSet.close();
        preparedStatement.close();

        return msgList;
    }

    @Override
    public void close() throws Exception {
        connection.close();
    }
}

运行测试方法:

@Test
    public void getMesage() throws Exception {
        List<Msg> message = chatMessageService.getMessage("2022-01-29", "13514684105", "13647128512");
        for (Msg msg : message) {
            System.out.println(msg);
        }
    }

在这里插入图片描述
4. 测试下sql

select
 *
from
"MOMO_CHAT"."MSG"
where
substr("msg_time", 0, 10) = '2022-01-29'
and "sender_account" = '13514684105'
and "receiver_account" = '13647128512';

在这里插入图片描述

标签:JDBC,varchar,sender,Phoenix,resultSet,开发,receiver,msg,C1
来源: https://blog.csdn.net/qq_37362891/article/details/122753293