如何通过apache storm topology的螺栓访问目录中的文件
在拓扑类中传递AnnotationTest类的对象
Config conf = new Config();
conf.put("gateObject", new AnnotationTest());
在访问该对象的bolt类中
AnnotationTest at;
@Override
public void prepare(Map stormConf, TopologyContext context) {
at=(AnnotationTest)stormConf.get("gateObject");
}
但AnnotationTest类从 sand 目录中访问大量资源 sand目录也包含在jar文件的根目录中(storm-starter-0.9.5-jar-with-dependencies.jar)
下面的maven构建脚本
<resource>
<directory>.</directory>
<includes>
<include>sand/**/*.*</include>
</includes>
</resource>
但是在AnnotationTest类中访问该sand目录中的文件时,如下所示
application = (ConditionalSerialAnalyserController) PersistenceManager
.loadObjectFromUrl(new URL("file:///sand/application.xgapp"));
该单个jar文件中包含的所有源代码,依赖项,资源目录 我通过运行拓扑类来提交拓扑,拓扑类也通过以下命令在同一个jar文件中
storm jar storm-starter-0.9.5-jar-with-dependencies.jar backtype.storm.testing.CopyTopology
以下异常提升
6892 [main-EventThread] INFO org.apache.storm.curator.framework.state.ConnectionStateManager - State change: CONNECTED
6930 [main] INFO backtype.storm.daemon.supervisor - Starting supervisor with id 60c35cb1-f020-4f6f-bc79-fcafe3fff844 at host ubuntu
java.io.FileNotFoundException: /sand/application.xgapp (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
at java.net.URL.openStream(URL.java:1045)
at gate.util.persistence.PersistenceManager.isXmlApplicationFile(PersistenceManager.java:1013)
at gate.util.persistence.PersistenceManager.loadObjectFromUrl(PersistenceManager.java:857)
at backtype.storm.testing.AnnotationTest.<init>(AnnotationTest.java:38)
at backtype.storm.testing.CopyTopology.submitTopology(CopyTopology.java:30)
at backtype.storm.testing.CopyTopology.main(CopyTopology.java:54)
那么请告诉我一种从风暴拓扑的bolt类中的目录(可在jar文件中找到)中访问文件的方法。
答案 0 :(得分:0)
您收到java.io.FileNotFoundException
个例外。这表明文件的路径是'application.xgapp&#39;您传递给java.net.URL
构造函数,不包含该文件。从您的问题描述中,您似乎已将文件保存在jar的根目录中。如果这是真的,那么就不能以这种方式在jar中形成文件的URI - &#34; file:///sand/application.xgapp"。
更改
application = (ConditionalSerialAnalyserController) PersistenceManager
.loadObjectFromUrl(new URL("file:///sand/application.xgapp"));
到
application = (ConditionalSerialAnalyserController) PersistenceManager
.loadObjectFromUrl(new URL("file:/<jar-path>/<yourjar.jar>!/sand/application.xgapp"));
或者,您可以将sand / application.xgapp移到jar外部并使用当前形式的URI。