【发布时间】: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