加载位于 H2 数据库类路径中的 CSV 文件
出于测试目的,我想使用 SQL 脚本和 CSV 文件创建并填充一些表。
因此,我创建了一个如下所示的 SQL 脚本:
CREATE TABLE T_FOO (
...
) as select * from CSVREAD('classpath:/foo.csv');
foo.csv
文件存在,并且位于 src/test/resources
中。
当此脚本在 Eclipse 上运行时(其中 src/test/resources 被定义为源目录,因此包含在类路径中),我收到以下错误:
Caused by: java.io.FileNotFoundException: resource /foo.csv
at org.h2.store.fs.FileSystemDisk.openFileInputStream(FileSystemDisk.java:388)
at org.h2.util.IOUtils.openFileInputStream(IOUtils.java:708)
at org.h2.tools.Csv.initRead(Csv.java:317)
at org.h2.tools.Csv.readResultSet(Csv.java:217)
at org.h2.tools.Csv.read(Csv.java:193)
... 49 more
我做错了什么?如何正确使用classpath:
协议加载CSV文件?
如果我输入文件的完整路径(例如 ... CSVREAD('C:\my-project\src\test\resources\foo.csv');
),那么它就可以工作。但这不是我想要这样做的原因:)
请注意,我使用最新版本的 H2 (1.3.153) 作为 我想使用 classpath:
协议加载我的文件。
For tests purposes, I want to create and fill some tables using SQL scripts as well as CSV files.
So I created a SQL script like this one:
CREATE TABLE T_FOO (
...
) as select * from CSVREAD('classpath:/foo.csv');
The foo.csv
file exists, and is located in src/test/resources
.
When this script is run on Eclipse (where src/test/resources
is defined as a source directory and thus is included in the classpath), I get the following error:
Caused by: java.io.FileNotFoundException: resource /foo.csv
at org.h2.store.fs.FileSystemDisk.openFileInputStream(FileSystemDisk.java:388)
at org.h2.util.IOUtils.openFileInputStream(IOUtils.java:708)
at org.h2.tools.Csv.initRead(Csv.java:317)
at org.h2.tools.Csv.readResultSet(Csv.java:217)
at org.h2.tools.Csv.read(Csv.java:193)
... 49 more
What did I do wrong? How to use correctly the classpath:
protocol to load a CSV file?
If I put the complete path for the file (like ... CSVREAD('C:\my-project\src\test\resources\foo.csv');
), then it works. But that's not why I want to do :)
Note that I use the latest version of H2 (1.3.153) as I wanted to use the classpath:
protocol to load my file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使官方文档给出了
CSVREAD('classpath:/org /acme/data/address.csv')
例如,Sean Patrick Floyd 建议删除前导斜线,即具有:,这是工作!
Even if the official docs give the
CSVREAD('classpath:/org/acme/data/address.csv')
example, Sean Patrick Floyd suggested to remove the leading slash, i.e. having:and this is working!
对上述答案的更新。
我尝试了上面提供的解决方案,但仍然收到错误,然后我尝试了下面的解决方案,它有效。
H2数据库文档需要更新。
An update to the above answer.
I tried the solution provided above however I was still getting the error, then I tried the below and it works.
The H2 database document needs to be updated.