使用java,hbase2.0以上版,批量插入数据,总共1000020条数据,当2000条时候插入一批

你可以使用Java的HBase API来批量插入数据到HBase 2.0以上版本。当达到2000条数据时,可以一次性插入一批数据。下面是一个示例代码:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.BufferedMutator;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HBaseExample {
    public static void main(String[] args) {
        try {
            // 创建HBase配置对象

            Configuration conf = HBaseConfiguration.create();
            conf.set("hbase.zookeeper.quorum", "your_zookeeper_quorum");
            conf.set("hbase.zookeeper.property.clientPort", "2181");

            // 创建HBase连接对象

            Connection connection = ConnectionFactory.createConnection(conf);

            // 获取表对象

            TableName tableName = TableName.valueOf("your_table_name");
            BufferedMutator mutator = connection.getBufferedMutator(tableName);

            // 构建要插入的数据

            List<Put> putList = new ArrayList<>();
            int batchSize = 2000;
            int totalRecords = 1000020;
            for (int i = 1; i <= totalRecords; i++) {
                Put put = new Put(Bytes.toBytes("row_key_" + i));
                put.addColumn(Bytes.toBytes("column_family"), Bytes.toBytes("column_qualifier"), Bytes.toBytes("value_" + i));
                putList.add(put);
                if (i % batchSize == 0) {
                    mutator.mutate(putList);
                    mutator.flush();
                    putList.clear();
                }
            }

            // 插入剩余的数据

            if (!putList.isEmpty()) {
                mutator.mutate(putList);
                mutator.flush();
            }

            // 关闭资源

            mutator.close();
            connection.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

请注意,上述代码中的"your_zookeeper_quorum"和"your_table_name"需要替换为你实际的ZooKeeper地址和表名。

你可能感兴趣的:(java,开发语言)