将hdfs数据流到storm(又名hdfs-spout)

wfsdck30  于 2021-06-02  发布在  Hadoop
关注(0)|答案(1)|浏览(544)

我想知道是否有任何喷口实现从hdfs到storm的数据流(类似于从hdfs的spark流)。我知道有很多实现可以将数据写入hdfs(https://github.com/ptgoetz/storm-hdfs 以及http://docs.hortonworks.com/hdpdocuments/hdp2/hdp-2.1.3/bk_user-guide/content/ch_storm-using-hdfs-connector.html)但另一方面我找不到。我很感激你的建议和暗示。

r7xajy2e

r7xajy2e1#

一种选择是使用hadoop hdfs javaapi。假设您使用的是maven,那么在pom.xml中应该包含hadoop common:

<dependency>
   <groupId>org.apache.hadoop</groupId>
   <artifactId>hadoop-common</artifactId>
   <version>2.6.0.2.2.0.0-2041</version>
</dependency>

然后,在spout实现中,您将使用hdfs filesystem对象。例如,以下是一些伪代码,用于将文件中的每一行作为字符串发送:

@Override
public void nextTuple() {
   try {
      Path pt=new Path("hdfs://servername:8020/user/hdfs/file.txt");
      FileSystem fs = FileSystem.get(new Configuration());
      BufferedReader br = new BufferedReader(new InputStreamReader(fs.open(pt)));
      String line = br.readLine();
      while (line != null){
         System.out.println(line);
         line=br.readLine();
         // emit the line which was read from the HDFS file
         // _collector is a private member variable of type SpoutOutputCollector set in the open method;
         _collector.emit(new Values(line));
      }
   } catch (Exception e) {
      _collector.reportError(e);
      LOG.error("HDFS spout error {}", e);
   }
}

相关问题