hbase插入数据,phoenix 二级索引数据不更新

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();
            }
        }

    }

}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

liu137612432

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

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

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

打赏作者

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

抵扣说明:

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

余额充值