Http://blog.selfup.cn/619.html
Private static final pattern space = pattern.compile (" ");p ublic Static void main (String[] args) { Streamingexamples.setstreamingloglevels (); javastreamingcontext jssc = new javastreamingcontext ("local[2") ", " Javanetworkwordcount ", new duration (10000)); jssc.checkpoint (". "); /Use the Updatestatebykey () function to set checkpoint //to open the local port 9999 Javareceiverinputdstream<string> lines = jssc.sockettextstream ("localhost", 9999); //input by line, separated by spaces JavaDStream<String> words = Lines.flatmap (Line -> arrays.aslist (line)) //each word to form pair, such as (word,1) javapairdstream<string, integer>&nbsP;pairs = words.maptopair (word -> new tuple2<> (word, 1)); //statistics and update the history occurrences of each word javapairdstream<string, integer> counts = pairs.updatestatebykey (values, state) -> { integer newsum = state.or (0); for ( Integer i :values) { newsum += i; } return optional.of (newsum); }); counts.print (); jssc.start (); jssc.awaittermination ();}
Updatestatebykey--word Count