package com.atguigu.day01;
import org.apache.phoenix.queryserver.client.ThinClientUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.sql.*;
import java.util.Properties;
import java.util.Random;
/**
* @ClassName: PhoenixTest
* @Description:Phoneix-JDBC操作
* @Author: kele
* @Date: 2021/2/23 11:24
**/
public class PhoenixTest {
private Connection connection = null;
private PreparedStatement statement = null;
/**
* 创建连接
* @throws SQLException
*/
@Before
public void Init() throws SQLException {
//获取url ****需要先启动queryserver.py start
String url = ThinClientUtil.getConnectionUrl("hadoop102",8765);
Properties prop = new Properties();
//******如果hbase设置namespace mapping,这里也必须设置,否则报错
prop.setProperty("phoenix.schema.isNamespaceMappingEnabled","true");
prop.setProperty("phoenix.schema.mapSystemTablesToNamespace","true");
connection = DriverManager.getConnection(url,prop);
//phoenix默认任务是手动提交事务,需要设置为自动提交,或者connection.commit()
//在mysql中是自动提交事务
connection.setAutoCommit(true);
}
/**
* 关闭连接
* @throws SQLException
*/
@After
public void closeLast() throws SQLException {
if(connection == null)
connection.close();
if(statement == null)
statement.close();
}
/**
* 创建表
* @throws SQLException
*/
@Test
public void createTable() throws SQLException {
String sql = "create table dog(" +
"id varchar primary key," +
"name varchar," +
"age varchar)COLUM_ENCODED_BYTES=0";
statement = connection.prepareStatement(sql);
statement.executeUpdate();
}
/**
* 插入一条数据
* @throws SQLException
*/
@Test
public void upsertTable() throws SQLException {
String sql = "upsert into dog values('1001','zhangsan','20')";
statement = connection.prepareStatement(sql);
statement.executeUpdate();
}
/**
* 批量插入数据
* @throws SQLException
*/
@Test
public void upsertTableBatch() throws SQLException {
Random random = new Random();
String sql = "upsert into dog values(?,?,?)";
statement = connection.prepareStatement(sql);
int index=0;
for (int i = 0; i < 123; i++) {
statement.setString(1,"1000"+i);
statement.setString(2,"zhang"+i);
statement.setString(3,random.nextInt(100)+"");
statement.addBatch();
//满30次提交一下
if(index%30==0){
//提交batch
statement.executeBatch();
//清空batch
statement.clearBatch();
}
index++;
statement.executeBatch();
}
}
/***********************
* 数据查询
* @throws SQLException
*/
@Test
public void selectData() throws SQLException {
String sql = "select *from dog where id>=?";
statement = connection.prepareStatement(sql);
statement.setString(1,"10009");
ResultSet resultSet = statement.executeQuery();
/****************与HBase不同,HBase通过scan查询的数据需要通过iterator、listcell获取数据
* **/
//通过result判断是否查询到下一条数据
while(resultSet.next()){
String name = resultSet.getString("name");
String age = resultSet.getString("age");
System.out.println("name:"+name+",age:"+age);
}
}
/**
* 删除数据
* @throws SQLException
*/
@Test
public void deleteData() throws SQLException {
String sql = "delete from dog where id >= '10001'";
statement = connection.prepareStatement(sql);
statement.executeUpdate();
}
/**
* 删除表
* @throws SQLException
*/
@Test
public void dropTable() throws SQLException {
statement = connection.prepareStatement("drop table dog");
statement.executeUpdate();
}
}
Phoenix connects jadbc's API to operate HBase
Guess you like
Origin blog.csdn.net/qq_38705144/article/details/114001439
Ranking