Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The realization process of Spout

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "the implementation process of Spout". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "the implementation process of Spout"!

The implementation process of Spout:

Monitor file changes separately and monitor whether new log files are added in the directory.

After the data is described in the field, it is converted to tuple.

Declare the packet between Spout and Bolt and determine the path that tuple sends to Bolt.

The specific code of Spout is shown in Listing Three.

The logic of the open, nextTuple, and delcareOutputFields methods in Listing Three:Spout.

1. Public void open (Map conf, TopologyContext context,SpoutOutputCollector collector)

2. {

3. _ collector = collector

4. Try

5. {

6. FileReader = new BufferedReader (new FileReader (new File (file))

7.}

8. Catch (FileNotFoundException e)

9. {

10. System.exit (1)

11.}

12.}

13.

14. Public void nextTuple ()

15. {

16. Protected void ListenFile (File file)

17. {

18. Utils.sleep (2000)

19. RandomAccessFile access = null

20. String line = null

21. Try

twenty-two。 {

23. While (line = access.readLine ())! = null)

24. {

25. If (line! = null)

twenty-six。 {

twenty-seven。 String [] fields=null

twenty-eight。 If (tupleInfo.getDelimiter () .equals ("|") fields = line.split ("\" + tupleInfo.getDelimiter ())

twenty-nine。 Else

thirty。 Fields = line.split (tupleInfo.getDelimiter ())

thirty-one。 If (tupleInfo.getFieldList (). Size () = = fields.length) _ collector.emit (new Values (fields))

thirty-two。 }

thirty-three。 }

thirty-four。 }

thirty-five。 Catch (IOException ex) {}

thirty-six。 }

thirty-seven。 }

thirty-eight。

thirty-nine。 Public void declareOutputFields (OutputFieldsDeclarer declarer)

forty。 {

forty-one。 String [] fieldsArr = new String [tupleInfo.getFieldList () .size ()]

forty-two。 For (int item0; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report