我想在 cassandra 中插入大约 5000 万行(每行约 30 列),目前只有 1 个节点。
我正在从另一个数据源查询我的数据并存储在一个表对象中。我遍历单独解析每一行,然后将其添加到 mutator。目前我一次插入 100 行,100 万行需要 40 分钟!如何加快这个过程?(我也尝试过 client.batch_mutate() 但它似乎在插入几千块块大小 2 后重置了连接错误)。
通过四处搜索,我发现多线程可以提供帮助。但是我找不到任何例子,有人可以链接我吗?谢谢你 !!
我当前的代码:
List<String> colNames = new ArrayList<String>();
List<String> colValues = new ArrayList<String>();
SomeTable result = Query(...); // this contains my result set of 1M rows initially
for (Iterator itr = result.getRecordIterator(); itr.hasNext();) {
String colName =.....
String colValue = .....
int colCount = colNames.size(); // 100 * 30
for (int i = 0; i < colCount; i++) {
//add row keys and columns to mutator
mutator.addInsertion(String.valueOf(rowCounter), "data", HFactory.createStringColumn(colNames.get(i), colValues.get(i)));
}
rowCounter++;
//insert rows of block size 100
if (rowCounter % 100==0) {
mutator.execute();
//clear data
colNames = new ArrayList<String>();
colValues = new ArrayList<String>();
mutator = HFactory.createMutator(keyspace, stringSerializer);
}
}