Java 如何从CSV读取H2结果集,并为第二遍重置光标
我正在编写一个Java库,用于从CSV文件导入数据,并根据一些正则表达式测试的结果,将结果返回为INTEGER、DECIMAL、VARCHAR和其他列类型 因此,我需要阅读由Java 如何从CSV读取H2结果集,并为第二遍重置光标,java,h2,Java,H2,我正在编写一个Java库,用于从CSV文件导入数据,并根据一些正则表达式测试的结果,将结果返回为INTEGER、DECIMAL、VARCHAR和其他列类型 因此,我需要阅读由Csv.read()返回的ResultSet两次;一次用于确定列类型,另一次用于填充新的SimpleResultSet(或者最好将原始的ResultSet包装在SimpleRowSource中,这样我就不必存储表数据)。我已尝试调用ResultSet.beforeFirst(),但第二次调用时没有可用的行 // Create
Csv.read()
返回的ResultSet
两次;一次用于确定列类型,另一次用于填充新的SimpleResultSet
(或者最好将原始的ResultSet
包装在SimpleRowSource
中,这样我就不必存储表数据)。我已尝试调用ResultSet.beforeFirst()
,但第二次调用时没有可用的行
// Create an input stream with test data.
String data =
"name,value\n" +
"one,1\n" +
"two,2\n" +
"three,3\n";
InputStream stream = new ByteArrayInputStream(data.getBytes());
Reader reader = new InputStreamReader(stream);
// Read the test data into a new ResultSet.
Csv csv = new Csv();
ResultSet resultSet = csv.read(reader, null);
// Iterate over the ResultSet on first pass.
int firstPass = 0;
while (resultSet.next())
{
// Test all values for integer, decimal etc.
++firstPass;
}
System.out.println("Read " + firstPass + " row(s) on first pass");
// Move the cursor to before the first row; this doesn't work!
resultSet.beforeFirst();
// Check if the cursor is before the first row; throws exception!
/* resultSet.isBeforeFirst(); */
// Iterate over the ResultSet on second pass.
int secondPass = 0;
while (resultSet.next())
{
// Add all values to new SimpleResultSet, or wrap this code
// in an H2 SimpleRowSource, and pass to the constructor of
// a new SimpleResultSet.
++secondPass;
}
System.out.println("Read " + secondPass + " row(s) on second pass");
这将产生以下输出:
Read 3 row(s) on first pass
Read 0 row(s) on second pass
我注意到beforeFirst()
不会引发异常,但是isBeforeFirst()
会:
org.h2.jdbc.JdbcSQLException: Feature not supported: null [50100-172]
我会很感激任何关于为什么这不起作用的建议,或者其他的方法
更新
由于H2Csv.read()
函数生成了一个无法重绕的ResultSet
,因此我最终复制了第一次读取时的源InputStream
,然后在第二次读取时重放生成的字节[]
。这样,我可以确保导入相同的数据。下面是我编写的帮助器类:
public class CopyStream extends InputStream
{
private final InputStream mInputStream;
private final ByteArrayOutputStream mOutputStream;
public CopyStream(InputStream inputStream)
{
// Initialise the class.
mInputStream = inputStream;
mOutputStream = new ByteArrayOutputStream();
}
public InputStream copy()
{
// Return a new input stream based on the copied data.
byte[] buffer = mOutputStream.toByteArray();
return new ByteArrayInputStream(buffer);
}
@Override
public int read() throws IOException
{
// Read a byte from the input stream.
int b = mInputStream.read();
if (b >= 0)
{
// Copy the byte to the output stream.
mOutputStream.write(b);
}
return b;
}
@Override
public int read(byte[] buffer) throws IOException
{
// Read some bytes from the input stream.
int length = mInputStream.read(buffer);
if (length > 0)
{
// Copy the bytes to the output stream.
mOutputStream.write(buffer, 0, length);
}
return length;
}
@Override
public int read(byte[] buffer, int offset, int length) throws IOException
{
// Read some bytes from the input stream.
length = mInputStream.read(buffer, offset, length);
if (length > 0)
{
// Copy the bytes to the output stream.
mOutputStream.write(buffer, offset, length);
}
return length;
}
}
其用途如下:
Csv csv = new Csv();
CopyStream copyStream = new CopyStream(inputStream);
ResultSet resultSet1 = csv.read(new InputStreamReader(copyStream), null);
ResultSet resultSet2 = csv.read(new InputStreamReader(copyStream.copy()), null);
您不需要读取
结果集
两次。您可以使用非标准H2命令CSVREAD
从文件中填充表格
或者,您可以再次创建Csv对象。据我所知,
CSVREAD
提供了与Csv.read()
Java方法相同的功能,并且只创建VARCHAR列。我试图创建一个包含整数、小数等列的ResultSet
,不幸的是,我确实需要读取两次数据。是的,我可以调用两次Csv.read()。(:嗯。我目前正在将Csv.read()
的结果存储在列表中;但是如果我必须调用Csv.read()
两次,最好将原始文件的内容存储在一个大的字节[]
取而代之。出于好奇……我最终将InputStream
子类化,并将从源文件读取的所有字节复制到一个ByteArrayOutputStream
,这样我就可以使用重复的字节数组再次调用Csv.read()
(我担心底层文件在此期间可能发生了更改)@HuwWalters您应该将其作为答案发布并接受。这会将问题标记为已关闭,并将您的解决方案推到顶部。我有空时会这样做,并包括我编写的帮助器类;对于其他SO用户可能很方便。