我想在cassandra中插入约5000万行(〜30列),目前只有1个节点。向cassandra插入大量数据
我从另一个数据源查询我的数据并存储在一个表对象中。我迭代通过分别解析每一行然后将其添加到增变器。目前,我一次插入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);
}
}
感谢您的回答!所以我需要多个节点,如果我想让我的客户端多线程?我不知道多线程,我不知道你是否知道在线的任何好的多线程cassandra例子? 是的,我现在正在测试,稍后会扩展到更多节点。 @Richard – 2013-04-04 13:09:55
不,您可以为每个节点建立多个连接,这就是让您的客户端成为多线程所需的全部内容。我不知道Cassandra的例子,但ThreadPoolExecutor的javadoc是很好的http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html,并且有关于Java的教程在此处进行线程化http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html – Richard 2013-04-04 13:15:19
再次感谢@Richard。对不起另一个新手问题 - 看起来我会将我的代码封装在Runnable中,并创建许多线程并发送不同的“表”对象。我的问题是,我是否应该为每个线程或同一个线程创建一个新的Cluster/Mutator/Keyspace对象? – 2013-04-04 15:28:55