hbase中插入数据,用phoenix 创建的二级索引查询不到新加的数据
经查找相关资料,才知道,这种操作无法更新二级索引
所以最后只能从phoenix中插入数据
首先引入jar包
<!-- https://mvnrepository.com/artifact/org.apache.phoenix/phoenix-core -->
<dependency>
<groupId>org.apache.phoenix</groupId>
<artifactId>phoenix-core</artifactId>
<version>4.7.0-HBase-1.1</version>
</dependency>
代码
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Random;
import org.apache.phoenix.jdbc.PhoenixConnection;
public class TestPhoenix {
public static void main(String[] args) {
PhoenixConnection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.salesforce.phoenix.jdbc.PhoenixDriver");
conn = (PhoenixConnection) DriverManager.getConnection("jdbc:phoenix:node01,node02,node03");
conn.setAutoCommit(false);
int upsertBatchSize = conn.getMutateBatchSize();
String upsertStatement = "upsert into table values(?,?,?,?)";
stmt = conn.prepareStatement(upsertStatement);
int rowCount = 0;
for (int i = 0; i < 100000000; i++) {
Random r = new Random();
int d = r.nextInt(1000);
String id = "id" + i;
String name = "name" + d;
int click = r.nextInt(100);
float time = r.nextFloat() * 100;
stmt.setString(1, id);
stmt.setString(2, name);
stmt.setInt(3, click);
stmt.setFloat(4, time);
stmt.execute();
// 批量提交
if (++rowCount % upsertBatchSize == 0) {
conn.commit();
System.out.println("保存的数据条数: " + rowCount);
}
}
conn.commit();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}