phoenix java API

package cn.bsoft.phoenix;

 

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.UUID;

 

/**

*

*

* @author ZLH

* @time:2017年3月10日

*

*/

public class PhoenixDAOImpl {

 

Connection conn = null;

Statement stmt = null;

 

/**

* 获取连接

* @return

*/

public Connection getConnection() {

 

String driver = "org.apache.phoenix.jdbc.PhoenixDriver";

String url = "jdbc:phoenix:hadoop01:2181";

 

try {

Class.forName(driver);

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

 

if (conn == null) {

try {

conn = DriverManager.getConnection(url);

} catch (SQLException e) {

e.printStackTrace();

}

}

return conn;

}

 

/**

* 插入数据

*/

public void upsertTable() {

conn = getConnection();

try {

stmt = conn.createStatement();

for (int i = 0; i < 20000; i++) {

String rowkey = UUID.randomUUID().toString().replaceAll("-", "");

String sql = "upsert into ZLHTEST_EHRMAIN values('"

+ rowkey

+ "',"+2222222+i+",'dddd"+i+"dddd','sssss','aqqqqqa','ccccccccc','sssssssss')";

stmt.executeUpdate(sql);

conn.commit();

System.out.println("第" + i + "条插入成功");

}

// String sql = "upsert into ZLHTEST_EHRMAIN values('5654a722d8c071633aaae9437ab15fe218d383562c',2222222,'dddddddd','sssss','aqqqqqa','ccccccccc','sssssssss')";

// stmt.executeUpdate(sql);

// conn.commit();

// System.out.println("插入成功");

} catch (SQLException e) {

e.printStackTrace();

} finally {

try {

stmt.close();

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

 

/**

* 批量插入

*/

public void upsertBatch() {

PreparedStatement pstmt = null;

conn = getConnection();

long start = System.currentTimeMillis();

try {

conn.setAutoCommit(false);

String sql = "upsert into ZLHTEST_EHRMAIN values(?,?,?,?,?,?,?)";

pstmt = conn.prepareStatement(sql);

for (int i = 0; i < 20000; i++) {

String rowkey = UUID.randomUUID().toString().replaceAll("-", "");

pstmt.setString(1, rowkey+i);

pstmt.setLong(2, 33+i);

pstmt.setString(3, "asfsdffds");

pstmt.setString(4, "sdfggg");

pstmt.setString(5, "sdfff");

pstmt.setString(6, "safgfg");

pstmt.setString(7, "sfdghjjjj");

if(i%1000==0){

pstmt.executeBatch();

}

}

pstmt.executeBatch();

conn.commit();

long end = System.currentTimeMillis();

long tm = end - start;

System.out.println("总共使用时间"+tm);

} catch (SQLException e) {

e.printStackTrace();

} finally {

try {

pstmt.close();

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

public void queryAll() {

PreparedStatement pstmt = null;

conn = getConnection();

long start = System.currentTimeMillis();

try {

conn.setAutoCommit(false);

String sql = "select * from ZLHTEST_EHRMAIN limit 5";

pstmt = conn.prepareStatement(sql);

ResultSet rset = pstmt.executeQuery(sql);

while(rset.next()){

System.out.println(rset.getString(1)+" "+rset.getString(2)+" "+rset.getString(3)+" "+rset.getString(4)+" "+rset.getString(5)+" "+rset.getString(6)+" "+rset.getString(7));

}

long end = System.currentTimeMillis();

long tm = end - start;

System.out.println("总共使用时间"+tm);

} catch (SQLException e) {

e.printStackTrace();

} finally {

try {

pstmt.close();

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

public static void main(String[] args) {

// new PhoenixDAOImpl().upsertTable();

// new PhoenixDAOImpl().upsertBatch();

new PhoenixDAOImpl().queryAll();

}

}

 

转载于:https://my.oschina.net/zlhblogs/blog/879816

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值