- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的应用程序当前正在使用 CSV 解析器来解析 csv 文件并 持久化到数据库。它将整个 csv 加载到内存中并占用大量资源 时间的坚持,有时甚至会超时。我在网站上看到过
看到使用 Univocity 解析器的混合建议。请咨询 处理大量数据且花费更少时间的最佳方法。
谢谢。
代码:
int numRecords = csvParser.parse( fileBytes );
public int parse(InputStream ins) throws ParserException {
long parseTime= System.currentTimeMillis();
fireParsingBegin();
ParserEngine engine = null;
try {
engine = (ParserEngine) getEngineClass().newInstance();
} catch (Exception e) {
throw new ParserException(e.getMessage());
}
engine.setInputStream(ins);
engine.start();
int count = parse(engine);
fireParsingDone();
long seconds = (System.currentTimeMillis() - parseTime) / 1000;
System.out.println("Time taken is "+seconds);
return count;
}
protected int parse(ParserEngine engine) throws ParserException {
int count = 0;
while (engine.next()) //valuesString Arr in Engine populated with cell data
{
if (stopParsing) {
break;
}
Object o = parseObject(engine); //create individual Tos
if (o != null) {
count++; //count is increased after every To is formed
fireObjectParsed(o, engine); //put in into Bo/COl and so valn preparations
}
else {
return count;
}
}
return count;
最佳答案
univocity-parsers 是加载 CSV 文件的最佳选择,您可能无法更快地手动编写任何代码。您遇到的问题可能来自两件事:
1 - 加载内存中的所有内容。这通常是一个糟糕的设计决策,但如果您这样做,请确保为您的应用程序分配了足够的内存。给它更多内存例如使用标志 -Xms8G
和 Xmx8G
。
2 - 您可能没有对插入语句进行批处理。
我的建议是尝试这个(使用 univocity-parsers):
//configure input format using
CsvParserSettings settings = new CsvParserSettings();
//get an interator
CsvParser parser = new CsvParser(settings);
Iterator<String[]> it = parser.iterate(new File("/path/to/your.csv"), "UTF-8").iterator();
//connect to the database and create an insert statement
Connection connection = getYourDatabaseConnectionSomehow();
final int COLUMN_COUNT = 2;
PreparedStatement statement = connection.prepareStatement("INSERT INTO some_table(column1, column2) VALUES (?,?)");
//run batch inserts of 1000 rows per batch
int batchSize = 0;
while (it.hasNext()) {
//get next row from parser and set values in your statement
String[] row = it.next();
for(int i = 0; i < COLUMN_COUNT; i++){
if(i < row.length){
statement.setObject(i + 1, row[i]);
} else { //row in input is shorter than COLUMN_COUNT
statement.setObject(i + 1, null);
}
}
//add the values to the batch
statement.addBatch();
batchSize++;
//once 1000 rows made into the batch, execute it
if (batchSize == 1000) {
statement.executeBatch();
batchSize = 0;
}
}
// the last batch probably won't have 1000 rows.
if (batchSize > 0) {
statement.executeBatch();
}
这应该执行得很快,而且你甚至不需要 100mb 的内存来运行。
为了清楚起见,我没有使用任何 try/catch/finally block 来关闭此处的任何资源。您的实际代码必须能够处理该问题。
希望有帮助。
关于java - 如何在java中有效解析巨大的csv文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53048441/
我通过在共享首选项中使用 GSON 将其转换为 json 来存储我的复杂对象。但是在检索它时,无法获得预期的字符串。 代码 这里 holderListCustomizationMap 是对象的复杂映射
因此,我正在尝试对大于可用RAM的gz压缩文件执行某种面向行的操作,因此排除了将其首先读取为字符串的情况。问题是,如何在rust(缺少gunzip file.gz|./my-rust-program)
我试图更好地理解为什么具有潜在大精度的大数字处理不一致,特别是在 JavaScript 及其本地化工具(例如 ECMA-402/Intl)中。我假设这与 float 的使用有关,但我想了解限制在哪里和
我们有一个 5GB 的 csv 文件,这是我们业务的主列表。 有多个类别,每个类别包含数千条记录。我们的目标是将每个类别导出为其自己的 csv 文件。 我们如何运行查询并导出数据? 运行 OSX。有没
基于上一个问题 ( see here ),我试图通过 xmlEventParse 读取许多大型 xml 文件,同时保存节点变化数据。使用此示例 xml:https://www.nlm.nih.gov/
我正在开发一个系统,它加载一个巨大的 CSV 文件(超过 100 万行)并保存到数据库中。每行也有超过一千个字段。 CSV 文件被视为一个批处理,每一行都被视为其子对象。在添加对象的过程中,每个对象都
借助node-google模块 我编写了一个简单的 Node 模块来为我的网络应用程序启用“文本网络搜索”功能,并在我的一个 View 中显示结果。 由于在来自同一 IP 的少量查询后 Google
我有相当大的 4D 阵列 [20x20x40x15000],我使用 h5py 将其作为 HDF5 文件保存到磁盘.现在的问题是我想计算整个数组的平均值,即使用: numpy.average(HDF5_
我在遗留代码库中连接巨大的 CString 时遇到问题。 CStrings 可以包含 base64 编码的文件,因此可能很大。在某些时候,这些 CString 会像这样连接起来: result +=
我正在尝试让我的服务器提供来自另一台服务器的巨大文件。但是,为了保护我的凭据免受该远程服务器的攻击,我不能简单地将请求者重定向到文件 url;另一方面,虽然使用 StreamingHttpRespon
感谢对此的任何见解,我有 2 个问题: 1) 弄清楚为什么我的本地数据库 oplog 庞大且不断增长 2) 安全删除(或重置)我的 local.oplog 以释放 18 GB 的浪费空间 场景:我一直
我的预期任务:获取大量数据(1 GB 及更多大小)json 字符串,操作(进行一些格式化、解析 json、重组 json 数据)并写入新格式化的 json 字符串作为响应。处理这种情况的更好方法是什么
我做了一个小的 Angular 4 应用程序,但我不知道如何应用 tree shaking 和 aot 编译。我运行的命令如下: ng build --prod --aot 但我得到的结果仍然很大,供
我是一名优秀的程序员,十分优秀!