如何使用Java 8在Docker容器内导入CSV? [英] How to import a CSV inside a Docker container with Java 8?
问题描述
我希望在启动时将CSV文件中的数据加载到我的程序中。在我的本地计算机上,它工作正常,但是当我尝试对spring boot应用程序进行Dockerize时,找不到我的CSV文件。
I want load data from a CSV file into my program on startup. On my local machine it works just fine, but when I try to Dockerize the spring boot application, my CSV file cannot be found.
private final static String GIFTS_CSV = "gifts.csv";
public final static String PATH = "src/main/resources/static/";
public static Map<Integer, Gift> getGifts() throws IOException {
String line;
HashMap<Integer, Gift> gifts = new HashMap<>();
BufferedReader br = new BufferedReader(new FileReader(PATH + GIFTS_CSV));
br.readLine();
while ((line = br.readLine()) != null) {
String[] giftStr = line.split(CVS_SPLIT_BY);
Gift gift = new Gift(Integer.parseInt(giftStr[0]),
new Point(Double.parseDouble(giftStr[1]),
Double.parseDouble(giftStr[2])), Double.parseDouble(giftStr[3]));
gifts.put(gift.getId(), gift);
}
return gifts;
}
在两种环境中都可以获取此数据吗?还是Docker映像上的路径是什么?
Is there a way to get this data in both environments? Or what would be the path on the docker image?
推荐答案
Docker的全部目的是将执行过程与任何依赖项隔离开在执行环境上。为了使这样的过程对文件之类的数据起作用,您需要将其包含在容器中。
The entire point of Docker is to isolate the executing process from any dependencies on the execution environment. In order for such a process to act upon data like files you need to include these within the container.
一种方法是构建一个包含CSV文件的容器(使用Dockerfile),但使用docker卷会更自然。我建议您阅读以下文档:
One method is build a container that includes the CSV file (using the Dockerfile), but it would be more natural to use a docker volume. I suggest you read the following documentation:
https://docs.docker.com/engine/userguide/dockervolumes/
这篇关于如何使用Java 8在Docker容器内导入CSV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!