如何使用Java在Storm bolt中编写prepare()方法?

时间:2015-09-01 15:59:51

标签: java apache-storm

我想从Bolt.prepare()读取文件,并将读取的文件对象传递给execute方法。任何人都可以发布示例代码如何在使用Java的Storm bolt中做到这一点?我正在使用:

public void prepare(Map stormConf, TopologyContext context) {
    try {
        this.context = context;
        this.fileReader = new FileInputStream("/home/anji/all-users.csv");
    } catch (FileNotFoundException e) {
        /*throw new RuntimeException("Error reading file "
                + conf.get("inputFile"));*/
    }
    this.collector = collector;

}

我在FileReader方法中调用execute()User p = new User(fileReader);

错误讯息:

java.io.IOException: Stream Closed
  at java.io.FileInputStream.readBytes(Native Method)
  at java.io.FileInputStream.read(FileInputStream.java:272)
  at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
  at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
  at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
  at java.io.InputStreamReader.read(InputStreamReader.java:184)
  at com.csvreader.CsvReader.checkDataLength(Unknown Source)
  at com.csvreader.CsvReader.readRecord(Unknown Source)
  at com.csvreader.CsvReader.readHeaders(Unknown Source)

1 个答案:

答案 0 :(得分:2)

只需在Bolt中使用包含所有User的成员变量即可。在prepare()中只读一次文件:

public class MyClass implements IRichBolt {
    private final List<User> users = new ArrayList<User>();

    public void prepare(...) {
        File f = new File(...);
        // open file and create objects (or similar code)
       String line;
       while((line = reader.readLine()) != null) {
           users.add(new Users(line));
       }
    }

    // other methods including .execute(...) can use users 
}