• phoenix连接hbase


    一、安装phoennix添加配置  

     1、将phoenix-server-hbase-2.4-5.1.2.jar拷贝至hbase的的lib下

    cp  phoenix-server-hbase-2.4-5.1.2.jar ../hbase/lib/
    

      2、配置phoenix可以访问hbase的系统表

        (1)将以下配置添加至hbase-site.xml中      

    1. <property>
    2. <name>phoenix.schema.isNamespaceMappingEnabled</name>
    3. <value>true</value>
    4. </property>
    5. <property>
    6. <name>phoenix.schema.mapSystemTablesToNamespace</name>
    7. <value>true</value>
    8. </property>

       (2)将hbase-stie.xml拷贝到phoenix/bin目录下

    cp ../hbase/conf/hbase-site.xml   ../phoenix/bin/
    

       二、启动phoenix服务

         1、启动hbase       

    ../hbase/bin/start-hbase.sh
    

         2、启动phoenix 

    python3  ../phoenix/bin/sqlline.py   server200:2181

         server200:2181为zookeeper地址

    三、 phoenix常用语法

     官网文档   https://phoenix.apache.org/language/index.html
    

    (1)创建表

    create  table  test1(id varchar primary key,a varchar,b varchar);
    

       id主键可视为hbase的rowkey

     (2)插入数据

     upsert into TEST1 values('202211160089','liuping','chenyingying');
    

    (3) 查询数据

    select *  from TEST1;
    

     (4)视图/表映射

         由于phoenix 无法直接访问hbase创建的非系统表,可以通过视图/表映射对非系统表进行查询,但视图不可修改,表映射可读可写

        在hbase上创建表名为eftb列族为fm1  、fm2的表

    create  'reftb','fm1','fm2'

     

       向表中添加数据

    1. put 'reftb','010101','fm1:name','zhangsan'
    2. put 'reftb','010101','fm2:age','九千岁'

       <1>视图映射

    create view "reftb"(id varchar primary key,"fm1"."name" varchar,"fm2"."age" varchar);

      <2>表映射

    create table "reftb"(id varchar primary key,"fm1"."name" varchar,"fm2"."age" varchar);

      <3>查看数据

    (5)添加修改数据(增改语法相同)

    1. upsert into "reftb" values('010102','诸葛村夫','五十');
    2. upsert into "reftb" values('010101','常山赵子龙','七十');

     (6)删除数据

     delete  from "reftb"  where ID='010101';
    

    (7)创建schema(数据库名,对用hbase是的namespace)

    CREATE SCHEMA IF NOT EXISTS "my_schema";

    四、java代码集成phoenix

    1、添加依赖

    implementation 'org.apache.phoenix:phoenix-client-hbase-2.4:5.1.2'

    2、编写代码

    1. public class PhoenixJdbcUtils {
    2. private final static Logger LOGGER = LoggerFactory.getLogger(PhoenixJdbcUtils.class);
    3. private static Connection connection;
    4. static {
    5. Properties properties =new Properties();
    6. PhoenixDriver instance = PhoenixDriver.INSTANCE;
    7. try {
    8. connection = instance.connect("jdbc:phoenix:server200:2181", properties);
    9. ///connection = DriverManager.getConnection("jdbc:phoenix:server200:2181", properties);
    10. } catch (SQLException e) {
    11. e.printStackTrace();
    12. }
    13. }
    14. /**
    15. * 插入数据
    16. * @throws SQLException
    17. */
    18. public static void testUpsertData() throws SQLException {
    19. PreparedStatement psUpsert = connection.prepareStatement( " upsert into \"reftb\" values('168936','刘备','63')");
    20. boolean addData = psUpsert.execute();
    21. LOGGER.info("addData---------"+addData);
    22. connection.commit();
    23. }
    24. /**
    25. * 查询数据
    26. * @throws SQLException
    27. */
    28. public static void testQueryData() throws SQLException {
    29. PreparedStatement psQuery = connection.prepareStatement(" select * from \"reftb\" ");
    30. ResultSet resultSet = psQuery.executeQuery();
    31. while (resultSet.next()) {
    32. LOGGER.info("id--{}",resultSet.getString(1));
    33. LOGGER.info("name--{}",resultSet.getString(2));
    34. LOGGER.info("age--{}",resultSet.getString(3));
    35. }
    36. }
    37. public static void main(String[] args) {
    38. try {
    39. testQueryData();
    40. testUpsertData();
    41. } catch (Exception e) {
    42. e.printStackTrace();
    43. }
    44. }
    45. }

  • 相关阅读:
    微博撰写-流程图-序列图-甘特图-mermaid流程图-效果不错
    特殊类的设计
    锁的种类和特点
    蓝桥杯 第 1 场算法双周赛 第2题 数树数【算法赛】c++ 位运算巧解
    整理ArrayList和LinkedList中的方法
    R语言使用order函数按照指定数据列的值倒排data.table数据(从大到小降序排序)
    在 Android 中创建静态应用程序快捷方式
    计算机网络实验五 子网划分与路由器配置
    曲线任意里程中边桩坐标正反算4800P计算序
    webpack5 - 之 生产环境的优化(dist压缩包、copy静态资源、缓存、代码切片、多线程打包、抽离重复代码 与 最小化 entry chunk)
  • 原文地址:https://blog.csdn.net/fengchengwu2012/article/details/127895690