Storm inside fieldsGrouping and Field parameters and declareOutputFields

Fields, understand, similar to a table, and data fields that you get the fields corresponding to the back of the bolt by

 

 

Use this Field and fieldsGrouping usually grouped together mechanisms, particularly difficult to understand this Field, I also read a lot of articles on the Internet, the feeling is still talking about is not very clear, Specious, it did not catch the point. This problem has troubled me for a full 3-4 days, has been not understand the concept of Field,

I think the current new Fields ( "word") is equivalent to the table header , this field is defined, this field put inside something that is emit into the

Spouts--->Bolts;
Bolts---->Bolts;
Field:"Word"
            the
            sporm
            is
            ...

Example 1:
The first step: a header defines
public void declareOutputFields (OutputFieldsDeclarer declarer)
    {
        declarer.declare (new new Fields ( "Word"));
    }
Second Step: To emit into the inside space of this Field content (may be Bolt and Spolt)
public void Execute (INPUT Tuple, BasicOutputCollector Collector)
    {
        String sentence = input.getString (0);
        String [] = sentence.split words ( "");
        for (String Word: words)
        {
            Word = word.trim ();
            IF (word.isEmpty ()!)
            {
                Word word.toLowerCase = ();
                collector.emit (new new Values (Word));
            }
        }
    }
The third step: associating step
TopologyBuilder builder = new TopologyBuilder();
builder.setSpout("word-reader",new WordReader());
builder.setBolt("word-normalizer", new WordNormalizer()).shuffleGrouping("word-reader");
Integer number = 2;
builder.setBolt("word-counter", new WordCounter(), 4).fieldsGrouping("word-normalizer", new Fields("word"));

Step four:
the final results achieved:
Field,: Word
            at The
            sporm
            IS
            ...

Example 2:

第一步:
public void declareOutputFields(OutputFieldsDeclarer declarer)
{
      declarer.declare(new Fields("word", "count"));
}

第二步:
public void execute(Tuple tuple, BasicOutputCollector collector)
 {
            String word = tuple.getString(0);
            Integer count = counts.get(word);
            if (count == null)
                count = 0;
            count++;
            counts.put(word, count);
            collector.emit(new Values(word, count));
}
第三步:
Fields("word", "count")
            “is”,1
            “sporm”,3
            “the”,2
              .....
例子3:
D:\.....\Workspaces\MyEclipse 8.5\bigData\examples-ch06-real-life-app-master\src\main\java\storm\analytics\....
第一步:
TopologyBuilder builder = new TopologyBuilder();
builder.setSpout("read-feed", new UsersNavigationSpout(), 3);
builder.setBolt("get-categ", new GetCategoryBolt(), 3).shuffleGrouping("read-feed");
builder.setBolt("user-history", new UserHistoryBolt(), 5).fieldsGrouping("get-categ", new Fields("user"));

Step Two: The sender output is three structures: Fields ( "User", "Product", "Categ")
GetCategoryBolt.java
public void Execute (INPUT Tuple, BasicOutputCollector Collector)
 {
        NavigationEntry entry = (NavigationEntry) input.getValue (. 1);
        IF ( "PRODUCT" .equals (entry.getPageType ())) {
            the try {
                String Product = (String) entry.getOtherData () GET ( "Product").;

                // Call the items API to get item information
                Product itm = reader.readItem(product);
                if(itm ==null)
                    return ;

                String categ = itm.getCategory();

                collector.emit(new Values(entry.getUserId(), product, categ));

            } catch (Exception ex) {
                System.err.println("Error processing PRODUCT tuple"+ ex);
                ex.printStackTrace();
            }
        }
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields("user","product", "categ"));
    }

第三步:new Fields("user"))只取Fields("user","product", "categ"))中的User
builder.setBolt("user-history", new UserHistoryBolt(), 5).fieldsGrouping("get-categ", new Fields("user"));

 


declareOutputFields the number of declared method of the bolt / spout output fields for downstream use, the bolt in the Execute method, the number of fields to be transmitted and emit the same declarations


Reference: https: //blog.csdn.net/vessalasd1/article/details/50472123

Guess you like

Origin www.cnblogs.com/51python/p/11005880.html