gpt4 book ai didi

java - Cassandra hector loader 应用内存不足

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:00:45 26 4
gpt4 key购买 nike

这个简单的应用程序采用带标题的逗号分隔文件并将其放入 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;
}

}

最佳答案

如果输入文件中的其中一个“列”大于分配的堆,我可能会认为这是一个问题。您可以通过设置突变大小的上限来解决此问题,s.t.您的 CassandraSave 函数在一次操作中仅执行 100 次左右的更改。

关于java - Cassandra hector loader 应用内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7205285/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com