2015-10-17 67 views
0

我正在使用Cassandra作为我的项目,并且在写入期间我正面临超时问题,这个人在这篇文章中收到的同样是Cassandra cluster with bad insert performance and insert stability(目前我只用一个节点进行测试,Java驱动程序,Cassandra的最新版本)。该应用程序必须为每个用户每天(夜间)插入大量数据。我有一个休息控制器接受文件,然后处理它们并行到达Cassandra插入值。我必须为每个用户插入100万条记录,其中一个条目最多有8个值(时间不是那么重要,也可能需要10分钟)。根据Cassandra cluster with bad insert performance and insert stability提供的答案,我决定将executeAsync(),Semaphore和PreparedStatement添加到我的应用程序中,而以前我没有使用它们。Cassandra PreparedStatement vs普通插入

现在的问题是,使用变量键空间(每个用户一个)并且有必要更新数据库中的列表,我无法在初始化阶段初始化我的PreparedStatements,但我必须至少每次执行一次文件处理(一个文件包含10 + k条目),用户必须每天上传100个文件。出于这个原因,我得到这样的警告:

Re-preparing already prepared query INSERT INTO c2bdd9f7073dce28ed973238ac85b6e5d6162fce.sensorMonitoringLog (timestamp, sensorId, isLogging) VALUES (?, ?, ?). Please note that preparing the same query more than once is generally an anti-pattern and will likely affect performance. Consider preparing the statement only once. 

我的问题是:这是一个很好的做法,使用PreparedStatement的这样或最好是使用正常的插有executeAsync()?

谢谢

回答

0

如果您在写期间面临超时问题,它是在使用PreparedStatement一个好主意,但不要使用异步插入。超时是防止Cassandra超负荷工作的一种方法。借助异步性,您可以在同一时间为其提供更多的工作,而OOM的风险将会增加。

要使用PreparedStatement正确执行操作,您必须通过keyspace创建一个且唯一的一个Session对象。然后每个会议必须准备一次自己的陈述。

此外,请注意它们是PreparedStatement和异步的线程安全风险。准备一份声明必须是同步的。但是,我再次建议你不要在这种情况下使用ExecuteAsynch。