2015-10-17 76 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。