【问题标题】:high frequency insert in cassandra with java loses some data用java在cassandra中高频插入会丢失一些数据
【发布时间】:2018-09-16 12:44:15
【问题描述】:

我在文件中有 5,000,000 个插入查询。我想从文件中读取它们并使用 java 驱动程序和 executeAsync 方法写入 cassandra,在循环语句中,如下代码:

public static void main(String[] args) {
        FileReader fr = null;
        try {
            fr = new FileReader("the-file-name.txt");
            BufferedReader br = new BufferedReader(fr);
            String sCurrentLine;
            long time1 = System.currentTimeMillis();
            while ((sCurrentLine = br.readLine()) != null) {
                session.executeAsync(sCurrentLine);
            }

            System.out.println(System.currentTimeMillis() - time1);
            fr.close();
            br.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } 

我的表定义是:

CREATE TABLE test.climate (
    city text,
    date text,
    time text,
    temprature int,
    PRIMARY KEY ((city, date), time)
) WITH CLUSTERING ORDER BY (time ASC)
    AND bloom_filter_fp_chance = 0.01
    AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
    AND comment = ''
    AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
    AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
    AND crc_check_chance = 1.0
    AND dclocal_read_repair_chance = 0.1
    AND default_time_to_live = 0
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND memtable_flush_period_in_ms = 0
    AND min_index_interval = 128
    AND read_repair_chance = 0.0
    AND speculative_retry = '99PERCENTILE';

但运行程序后,表中的行数为 2,569,725

cqlsh:test> select count(*) from climate ;

 count
---------
 2569725

我测试了 10 多次,每次 select count(*) 的结果都在 2,400,00 和 2,600,000 之间

【问题讨论】:

  • 忘记关闭会话和集群了吗?
  • 向我们展示一个示例插入语句?很多时候人们认为他们插入了 5M 行,但分区键在多行上保持不变,只有集群列发生变化。例如,在这里,对于不同的时间值,您可能具有相同的 (city,date),这些时间值有效但计为单行。同样在 cqlsh 默认一致性是 ONE,将其更改为本地仲裁
  • 为您的代码添加日志以了解发生了什么:Futures.addCallback( session.executeAsync( sCurrentLine ), new FutureCallback<ResultSet>() { @Override public void onSuccess( ResultSet result ) { //ignore } @Override public void onFailure( Throwable t ) { t.printStackTrace(); } } ); 我想您会看到有关超时或节点不可用的异常
  • 异常是:所有主机尝试查询失败(尝试:/127.0.0.1:9042 (com.datastax.driver.core.exceptions.BusyPoolException: [/127.0.0.1] 池是忙(没有可用的连接,队列已达到其最大大小 256)))@MikhailBaksheev

标签: java cassandra


【解决方案1】:

您发出异步插入的速度比它们执行的速度快,因此它们最终会超过队列大小并失败。你可以增加你的队列大小这会起作用,但是你只是向内存而不是你的生产者施加背压,并且仍然可能碰壁。尝试限制飞行中的查询,例如:

public static void main2(String[] args) {
    FileReader fr = null;
    int permits = 256;
    Semaphore l = new Semaphore(permits);
    try {
        fr = new FileReader("the-file-name.txt");
        BufferedReader br = new BufferedReader(fr);
        String sCurrentLine;
        long time1 = System.currentTimeMillis();
        while ((sCurrentLine = br.readLine()) != null) {
            l.acquire();
            session.executeAsync(sCurrentLine)
                .addListener(()->l.release(), MoreExecutors.directExecutor());
        }
        l.acquire(permits);

        System.out.println(System.currentTimeMillis() - time1);
        fr.close();
        br.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

它可能会运行得一样快,只需要找到正确大小的信号量。还要注意阻塞,直到所有的许可都被返回(最后获取最大值),否则你可以在所有可能在队列中的请求被发送之前关闭 jvm。

免责声明:我没有测试上面的代码

【讨论】:

    猜你喜欢
    • 2016-07-29
    • 1970-01-01
    • 2011-10-06
    • 2021-05-29
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多