Channels ▼
RSS

Web Development

Easy, Real-Time Big Data Analysis Using Storm


Listing Two shows the specific XML file for a tuple, which specifies the fields and the delimiter separating the fields in a log file. Both the XML file and the data are kept in a directory whose path is specified in the spout.

Listing Two: An XML file created for describing the log file.

<TUPLEINFO>
              <FIELDLIST>
                  <FIELD>
                            <COLUMNNAME>vehicle_number</COLUMNNAME> 
                            <COLUMNTYPE>string</COLUMNTYPE> 
                  </FIELD>
                            
                  <FIELD>
                            <COLUMNNAME>speed</COLUMNNAME> 
                            <COLUMNTYPE>int</COLUMNTYPE> 
                  </FIELD>
                                                         
                  <FIELD>
                             <COLUMNNAME>location</COLUMNNAME> 
                             <COLUMNTYPE>string</COLUMNTYPE> 
                  </FIELD>
              </FIELDLIST>  
           <DELIMITER>,</DELIMITER> 
</TUPLEINFO>

An instance of Spout is initialized with constructor parameters of Directory, Path, and TupleInfo object. The TupleInfo object stores necessary information related to log file such as fields, delimiter, and type of field. This object is created by serializing the XML file using XStream.

Spout implementation steps are:

  • Listen to changes on individual log files. Monitor the directory for the addition of new log files.
  • Convert rows read by the spout to tuples after declaring fields for them.
  • Declare the grouping between spout and bolt, deciding the way in which tuples are given to bolt.

The code for Spout is shown in Listing Three.

Listing Three: Logic in Open, nextTuple, and declareOutputFields methods of Spout.

public void open( Map conf, TopologyContext context,SpoutOutputCollector collector ) 
{
    _collector = collector;
    try 
    {
	fileReader  =  new BufferedReader(new FileReader(new File(file)));
    } 
    catch (FileNotFoundException e) 
    {
	System.exit(1);
    }
}
                                                                                                 
public void nextTuple() 
{
    protected void ListenFile(File file) 
    {
	Utils.sleep(2000);
	RandomAccessFile access = null; 
	String line = null;                  
       try 
       { 
           while ((line = access.readLine()) != null)
           { 
               if (line !=null)
               {
                    String[] fields=null;
                    if (tupleInfo.getDelimiter().equals("|"))
                       fields = line.split("\\"+tupleInfo.getDelimiter());
                    else                         		                                                                			 fields = line.split(tupleInfo.getDelimiter());                                                
                    if (tupleInfo.getFieldList().size() == fields.length)
                       _collector.emit(new Values(fields)); 
               }          
           } 
      } 
      catch (IOException ex) { } 	          
      }
}

public void declareOutputFields(OutputFieldsDeclarer declarer) 
{
     String[] fieldsArr = new String [tupleInfo.getFieldList().size()];
     for(int i=0; i<tupleInfo.getFieldList().size(); i++)
     {
         fieldsArr[i] = tupleInfo.getFieldList().get(i).getColumnName();
     }           
     declarer.declare(new Fields(fieldsArr));
}	

declareOutputFields() decides the format in which the tuple is emitted, so that the bolt can decode the tuple in a similar fashion. Spout keeps on listening to the data added to the log file and as soon as data is added, it reads and emits the data to the bolt for processing.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 
Dr. Dobb's TV