这个简单的应用程序使用带有标题的逗号delim文件并将其放入Cassandra中.
它适用于小文件,但是内存只会增加,直到内存不足异常将其杀死为止.
我想念什么?
package com.company;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.mutation.Mutator;
public class QuickLoad {
public static Keyspace keyspace = null;
public static void main(String[] args) {
File file = new File(args[0]);
String keyspaceName = args[1];
String columnFamilyName = args[2];
BufferedReader reader = null;
try {
keyspace = GetKeyspace(keyspaceName);
reader = new BufferedReader(new FileReader(file));
String fileLine = null;
String[] headers = null;
String[] fields = null;
boolean headerLine = true;
while ((fileLine = reader.readLine()) != null) {
if (headerLine){
headerLine = false;
headers = fileLine.substring(1, fileLine.length()-1).split("\",\"");
} else {
fields = fileLine.substring(1, fileLine.length()-1).split("\",\"");
CassandraSave(keyspace, columnFamilyName, headers, fields);
}
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
System.exit(0);
}
public static void CassandraSave(Keyspace keyspace, String columnFamily, String[] headers, String[] columns)
{
try
{
Mutator mutator = HFactory.createMutator(keyspace, StringSerializer.get());
for (int i = 1; i < headers.length-1; i++)
{
if ((columns[i] != null) || (!columns[i].equals("null"))) {
if (columns[i].length() > 0) {
HColumn<String, String> col = HFactory.createStringColumn(headers[i], columns[i]);
mutator.insert(columns[1], columnFamily, col);
}
}
}
mutator.execute();
} catch (Exception e){
e.printStackTrace();
}
}
public static Keyspace GetKeyspace(String keyspaceName)
{
String serverAddress = "localhost:9160";
Cluster cluster = HFactory.getOrCreateCluster("My Cluster", serverAddress);
Keyspace keyspace = HFactory.createKeyspace(keyspaceName, cluster);
return keyspace;
}
}
解决方法:
如果您输入文件中的“列”之一大于分配的堆,我可能会认为这是一个问题.您可以通过设置突变大小的上限来解决此问题.您的CassandraSave函数一次只能执行100个左右的突变.