Apache Phoenix:HBase之上的SQL工具[3]:快速入门之Java API(JDBC)

Apache Phoenix:HBase之上的SQL工具[3]:快速入门之Java API(JDBC)

作者:胡佳辉(Dennis)  时间:2019年1月1日  CSDN博客:https://blog.csdn.net/gobitan 

 

Phoenix快速入门之Java API(JDBC)

第一步:创建工程

mvn archetype:generate -DgroupId=cn.dennishucd -DartifactId=PhoenixJavaClient -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

 

第二步:添加Maven依赖

注意:版本需要跟部署的phoenix的保持一致

  <dependency>

      <groupId>org.apache.phoenix</groupId>

      <artifactId>phoenix</artifactId>

      <version>4.14.1-HBase-1.2</version>

      <type>pom</type>

    </dependency>

 

第三步:编写代码

package cn.dennishucd;

 

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.PreparedStatement;

import java.sql.Statement;

 

/**

* @description Phoenix Java Client

* @author dennis

*/

public class PhoenixJavaClient {

 

    public static void main(String[] args) throws SQLException {

        Statement stmt = null;

        ResultSet rset = null;

 

        Connection con = DriverManager.

                getConnection("jdbc:phoenix:hadoop01,hadoop02,hadoop03:/hbase");

        stmt = con.createStatement();

 

        stmt.executeUpdate("create table helloworld (mykey integer not null primary key, mycolumn varchar)");

        stmt.executeUpdate("upsert into helloworld values (1,'Hello')");

        stmt.executeUpdate("upsert into helloworld values (2,'World!')");

        con.commit();

 

        PreparedStatement statement = con.prepareStatement("select * from helloworld");

        rset = statement.executeQuery();

 

        while (rset.next()) {

            System.out.println(rset.getString("mycolumn"));

        }

        statement.close();

 

        con.close();

 

        System.out.println("The End!");

    }

}

 

注:

[1] jdbc url格式可参考官网首页;

[2] 如果运行时提示找不到驱动"java.sql.SQLException: No suitable driver found for jdbc:phoenix",在idea环境可以通过File->Project Structure->Libraries,选择'+'号,从"From Maven"中添加,如下所示:

在弹出框中输入"org.apache.phoenix:phoenix-client",然后点击搜索,如下。

选择对应的版本,添加即可。

 

第四步:配置log4j.properties

在src/main目录下创建resources,并设置为Resources Root。创建log4j.properties,内容如下:

log4j.appender.console=org.apache.log4j.ConsoleAppender

log4j.appender.console.Target=System.out

log4j.appender.console.layout=org.apache.log4j.PatternLayout

log4j.appender.console.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

log4j.rootLogger=INFO, console

 

第五步:运行

没有其他问题的话,会打印出来!

 

参考资料:

[1] http://phoenix.apache.org/

[2] http://phoenix.apache.org/installation.html

[3] http://phoenix.apache.org/faq.html

[4] https://www.cnblogs.com/ballwql/p/8371234.html 浅谈Phoenix在HBase中的应用

[5] https://item.jd.com/18853108679.html 《Pro Apache Phoenix: An SQL Driver for Hbase》

[6] https://dzone.com/articles/hbase-phoenix-and-java-part-1 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gobitan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值