Wednesday, April 10, 2019

Apache Storm main concepts

Apache Storm main concepts

Topology
Stream
Spout
Bolt
Stream groupings
Reliability
Tasks
Workers

Topology :
A topology is a graph of spouts and bolts that are connected with stream groupings.  A Storm topology is analogous to a MapReduce job. One key difference is that a MapReduce job eventually finishes, whereas a topology runs forever (or until you kill it, of course).

Stream :
A stream is an unbounded sequence of tuples that is processed and created in parallel in a distributed fashion. The stream is the core abstraction in Storm. Streams are defined with a schema that names the fields in the stream's tuples.

Spout :
A spout is a source of streams in a topology. Generally spouts will read tuples from an external source (e.g. Kafka) and emit them into the topology.
The main method on spouts is nextTuple. nextTuple either emits a new tuple into the topology or simply returns if there are no new tuples to emit.
IRichSpout: this is the interface that spouts must implement.

Bolt :
All processing in topologies is done in bolt. Bolt can do anything from filtering, functions, aggregations, joins, talking to databases, and more. Bolt can do simple stream transformations. Doing complex stream transformations often requires multiple steps and thus multiple bolts.
The main method in bolt is the execute method which takes in as input a new tuple. Bolt emit new tuples using the OutputCollector object. Its perfectly fine to launch new threads in bolts that do processing asynchronously. OutputCollector is thread-safe and can be called at any time.

Ref : http://storm.apache.org/releases/2.0.0-SNAPSHOT/Concepts.html

Longest Palindromic Substring

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example 2:
Input: "cbbd"
Output: "bb"
Sol:
class Solution {
    public String longestPalindrome(String s) {
        
        if(s==null || s.length()<1) return "";
        int start, end;
        start=end=0;
        for(int i=0; i<s.length(); i++) {
            
            int len1=expandAroundCenter(s, i, i);
            int len2=expandAroundCenter(s, i, i+1);
            int len = Math.max(len1, len2);
            if(len>(end-start)){
                start=i-(len-1)/2;
                end=i+len/2;
            }
        }
        
        return s.substring(start, end+1);
    }
    
private int expandAroundCenter(String s, int i, int j) {
       while(i>=0 && j<s.length() && s.charAt(i)==s.charAt(j)) {
            i--;
            j++;
        }
        return j-i-1;
    }
}