使用H2数据库可以对从Java Reader读取的CSV文本执行SQL查询,例如StringReader? [英] With H2 Database can I perform a SQL query on CSV text read from a Java Reader e.g. StringReader?

查看:403
本文介绍了使用H2数据库可以对从Java Reader读取的CSV文本执行SQL查询,例如StringReader?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种方法可以对保存在内存中的CSV文本执行SQL查询,并从Java阅读器读入。 StringReader。

Is there a way to perform SQL queries on CSV text held in memory and read in from a Java Reader e.g. StringReader.

org.h2.tools.Csv.read(Reader reader,String [] colNames)将允许我检索包含所有行和列的结果集。

org.h2.tools.Csv.read(Reader reader, String[] colNames) would allow me to retrieve a result set containing all the rows and columns. However, I actually want to perform a query on the CSV text read from the Reader.

背景 - 我收到一个包含每个实体的多个CSV段落的文件,请参阅< a href =http://stackoverflow.com/q/8067343/316566> H2数据库可以查询包含不同记录组的多个部分的CSV文件吗?,在解析文件时,我存储每个CSV部分我需要在字符串(每个字符串)。这不应该浪费内存,因为我只保留数据在内存中很短的时间,每个CSV部分相对较小)。我需要对这些CSV部分执行查询,以自定义格式构建一个文档。

The background - I receive a file containing multiple CSV sections for each entity, see Can H2 Database query a CSV file containing multiple sections of different record groups?, and whilst parsing the file I store each of the CSV sections I need in Strings (a String for each one). This shouldn't bog down memory as I only keep the data in memory for a short time and each CSV section is relatively small). I need to perform queries on these CSV sections to build a document in a custom format.

我可以将每个CSV部分写入一个文件(作为一组文件),使用CSVREAD,但我不想这样做,因为我需要我的应用程序尽可能快,分裂和写入到磁盘的部分会使硬盘驱动器死亡。

I could write each CSV section to a file (as a set of files) and use CSVREAD, but I don't want to do that as I need my application to be as fast as possible and splitting and writing the sections to disk will thrash the hard drive to death.

推荐答案

您可以撰写用户定义的函数< a>返回一个结果集,并使用它来生成所需的行。在您的用户定义的功能中,您可以使用H2的Csv工具(实际上是任何Csv工具)。

You could write a user defined function that returns a result set, and use that to generate the required rows. Within your user defined function, you can use the Csv tool from H2 (actually any Csv tool).

这篇关于使用H2数据库可以对从Java Reader读取的CSV文本执行SQL查询,例如StringReader?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆