The merge operation of a stream is to combine two streams into a stream based on the two-stream association conditions, and then perform subsequent processing operations, which can be difficult and tedious to write if you use the programming model of spout and bolt, because the buffers are set to hold the first data, and the subsequent comparisons are made. It is convenient to use Trident to apply, and the original programming model is abstracted in some degree. code example:
Demand:
Two spout:spout1: The data inside is name, ID and Tel,
SPOUT2 is sex and ID;
First filter the SPOUT1, filter out the phone number that is not 186, and then display
Then merge the filtered stream and spout2 according to the
The code is as follows:
Filter:
public class Fileltertest extends basefilter{@Overridepublic boolean iskeep (Tridenttuple tuple) {String get = Tuple.getstring (2), if (Get.startswith ("186")) {return true;} return false;}}
Filter Display method
public class FunctionTest extends Basefunction {@Overridepublic void execute (tridenttuple tuple, Tridentcollector Collector) {String getName = tuple.getstring (0); String GetID = tuple.getstring (1); String Gettel = tuple.getstring (2); SYSTEM.OUT.PRINTLN ("Filtered data: ============" +tuple.getvalues ()), Collector.emit (New Values (Getname,getid,gettel));}}
Final Display method: public class FunctionTest3 extends Basefunction {@Overridepublic void execute (tridenttuple tuple, Tridentcollector collector) {System.out.println (Tuple.getvalues ());}}
Topomain
public class Testtrident {static fixedbatchspout spout = new Fixedbatchspout ("name", "Idname", "Tel"), 3, new Val UEs ("Jack", "1", "186107"), New values ("Tome", "2", "1514697"), New values ("Lay", "3", "186745"), New values ("Lucy", "4", " 1396478 ")), static fixedbatchspout Spout2 = new Fixedbatchspout (" Sex "," Idsex "), 3, New Values (" Boy "," 1 "), new VALUES ("Boy", "2"), new values ("Gril", "3"), New values ("Gril", "4"));p ublic static void Main (string[] args) {// Sets whether to loop Spout.setcycle (false);//Build Trident Topotridenttopology topology = new Tridenttopology ();//define Filter: Phone number is not 186 Opening filter fileltertest ft =new fileltertest ();//define the method used to display the filtered data functiontest function = new FunctionTest ();// Build the first Streamstream st = Topology.newstream ("SP1", spout) according to spout;//For the first stream data display. Stream St_1=st.each ("name", "Idname", "tel"), Function, new fields ("Out_name", "Out_idname", "Out_tel"));// Build the stream according to the second spout, in order to test the join with stream st2 = Topology.newstream ("SP2", spout2),/** * Start the join St and st2 These two streams, analogy to join in SQL is: * sT join St2 on JOINFIELDS1 = JOINFIELDS2 * Note that the ST is the data base * Topology.join (St, new Fields ("Idname"), St2, new fields ("Idse X "), New fields (" id "," name "," tel "," Sex ")) * Then the result will be based on spout, the result will be the above 4 data information all out * * * Stream St3=topology.join (St, New Fie lds ("Idname"), St2, new fields ("Idsex"), New Fields ("res_id", "Res_name", "Res_tel", "Res_sex")); Create a method in order to display the merged and filtered results FunctionTest3 T3 =new FunctionTest3 (); St3.each ("res_id", "Res_name", "Res_tel", "Res_sex"), ft). Each (The new fields ("res_id", "Res_name", "Res_tel", " Res_sex "), T3, new fields (" out1_id "," Out1_name "," Ou1t_tel "," Out1_sex ")); Config CF = new config (); cf.setnumworkers (2); cf.setnumackers (0); Cf.setdebug (false); Localcluster LC = New Localcluster () lc.submittopology ("Testtopo", CF, Topology.build ());}}
Results:
SPOUT1 data: [Jack, 1, 186107]SPOUT1 data: [Tome, 2, 1514697]SPOUT1 data: [Lay, 3, 186745]SPOUT1 data: [Lucy, 4, 1396478][1, Jack, 186107, Boy][3, Lay, 186745, Gril]
Example of Trident flow merging in Storm demo