2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2019-01-02 18:55:26 +08:00
|
|
|
* Copyright (C) 2018-2019 Lightbend Inc. <https://www.lightbend.com>
|
2018-06-30 05:10:10 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package jdocs.stream.operators;
|
|
|
|
|
|
2018-10-02 18:26:26 +03:30
|
|
|
import akka.stream.Materializer;
|
2018-06-30 05:10:10 +02:00
|
|
|
import akka.stream.javadsl.Flow;
|
|
|
|
|
|
2018-10-25 17:07:48 +03:30
|
|
|
import akka.NotUsed;
|
|
|
|
|
import akka.japi.function.Function2;
|
|
|
|
|
|
|
|
|
|
//#zip
|
|
|
|
|
//#zip-with
|
2018-10-02 18:26:26 +03:30
|
|
|
//#zip-with-index
|
2018-10-25 17:07:48 +03:30
|
|
|
//#or-else
|
|
|
|
|
//#prepend
|
|
|
|
|
//#concat
|
|
|
|
|
//#interleave
|
|
|
|
|
//#merge
|
|
|
|
|
//#merge-sorted
|
2018-10-02 18:26:26 +03:30
|
|
|
import akka.stream.javadsl.Source;
|
2018-10-25 17:07:48 +03:30
|
|
|
import akka.stream.javadsl.Sink;
|
2018-10-02 18:26:26 +03:30
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
2018-10-25 17:07:48 +03:30
|
|
|
//#merge-sorted
|
|
|
|
|
//#merge
|
|
|
|
|
//#interleave
|
|
|
|
|
//#concat
|
|
|
|
|
//#prepend
|
|
|
|
|
//#or-else
|
2018-10-02 18:26:26 +03:30
|
|
|
//#zip-with-index
|
2018-10-25 17:07:48 +03:30
|
|
|
//#zip-with
|
|
|
|
|
//#zip
|
|
|
|
|
|
2018-06-30 05:10:10 +02:00
|
|
|
//#log
|
|
|
|
|
import akka.stream.Attributes;
|
2018-09-20 16:22:11 +09:00
|
|
|
import akka.stream.javadsl.Source;
|
2018-06-30 05:10:10 +02:00
|
|
|
//#log
|
|
|
|
|
|
2018-09-20 16:22:11 +09:00
|
|
|
import java.time.Duration;
|
|
|
|
|
import java.util.Arrays;
|
2018-10-25 17:07:48 +03:30
|
|
|
import java.util.Comparator;
|
2018-09-20 16:22:11 +09:00
|
|
|
|
|
|
|
|
|
2018-06-30 05:10:10 +02:00
|
|
|
class SourceOrFlow {
|
2018-10-25 17:07:48 +03:30
|
|
|
private static Materializer materializer = null;
|
2018-06-30 05:10:10 +02:00
|
|
|
|
|
|
|
|
void logExample() {
|
|
|
|
|
Flow.of(String.class)
|
2018-09-20 16:22:11 +09:00
|
|
|
//#log
|
|
|
|
|
.log("myStream")
|
|
|
|
|
.addAttributes(Attributes.createLogLevels(
|
|
|
|
|
Attributes.logLevelOff(), // onElement
|
|
|
|
|
Attributes.logLevelError(), // onFailure
|
|
|
|
|
Attributes.logLevelInfo())) // onFinish
|
2018-06-30 05:10:10 +02:00
|
|
|
//#log
|
|
|
|
|
;
|
|
|
|
|
}
|
2018-09-20 16:22:11 +09:00
|
|
|
|
2018-10-02 18:26:26 +03:30
|
|
|
void zipWithIndexExample() {
|
|
|
|
|
Materializer materializer = null;
|
|
|
|
|
//#zip-with-index
|
|
|
|
|
Source.from(Arrays.asList("apple", "orange", "banana"))
|
|
|
|
|
.zipWithIndex()
|
|
|
|
|
.runWith(Sink.foreach(System.out::print), materializer);
|
|
|
|
|
// this will print ('apple', 0), ('orange', 1), ('banana', 2)
|
|
|
|
|
//#zip-with-index
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 17:07:48 +03:30
|
|
|
void zipExample() {
|
|
|
|
|
//#zip
|
|
|
|
|
Source<String, NotUsed> sourceFruits = Source.from(Arrays.asList("apple", "orange", "banana"));
|
|
|
|
|
Source<String, NotUsed> sourceFirstLetters = Source.from(Arrays.asList("A", "O", "B"));
|
|
|
|
|
sourceFruits.zip(sourceFirstLetters).runWith(Sink.foreach(System.out::print), materializer);
|
|
|
|
|
// this will print ('apple', 'A'), ('orange', 'O'), ('banana', 'B')
|
|
|
|
|
|
|
|
|
|
//#zip
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void zipWithExample() {
|
|
|
|
|
//#zip-with
|
|
|
|
|
Source<String, NotUsed> sourceCount = Source.from(Arrays.asList("one", "two", "three"));
|
|
|
|
|
Source<String, NotUsed> sourceFruits = Source.from(Arrays.asList("apple", "orange", "banana"));
|
|
|
|
|
sourceCount.zipWith(
|
|
|
|
|
sourceFruits,
|
|
|
|
|
(Function2<String, String, String>) (countStr, fruitName) -> countStr + " " + fruitName
|
|
|
|
|
).runWith(Sink.foreach(System.out::print), materializer);
|
|
|
|
|
// this will print 'one apple', 'two orange', 'three banana'
|
|
|
|
|
|
|
|
|
|
//#zip-with
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void prependExample() {
|
|
|
|
|
//#prepend
|
|
|
|
|
Source<String, NotUsed> ladies = Source.from(Arrays.asList("Emma", "Emily"));
|
|
|
|
|
Source<String, NotUsed> gentlemen = Source.from(Arrays.asList("Liam", "William"));
|
|
|
|
|
gentlemen.prepend(ladies).runWith(Sink.foreach(System.out::print), materializer);
|
|
|
|
|
// this will print "Emma", "Emily", "Liam", "William"
|
|
|
|
|
|
|
|
|
|
//#prepend
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void concatExample() {
|
|
|
|
|
//#concat
|
|
|
|
|
Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 2, 3, 4));
|
|
|
|
|
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(10, 20, 30, 40));
|
|
|
|
|
sourceA.concat(sourceB).runWith(Sink.foreach(System.out::print), materializer);
|
|
|
|
|
//prints 1, 2, 3, 4, 10, 20, 30, 40
|
|
|
|
|
|
|
|
|
|
//#concat
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void interleaveExample() {
|
|
|
|
|
//#interleave
|
|
|
|
|
Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 2, 3, 4));
|
|
|
|
|
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(10, 20, 30, 40));
|
|
|
|
|
sourceA.interleave(sourceB, 2).runWith(Sink.foreach(System.out::print), materializer);
|
|
|
|
|
//prints 1, 2, 10, 20, 3, 4, 30, 40
|
|
|
|
|
|
|
|
|
|
//#interleave
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void mergeExample() {
|
|
|
|
|
//#merge
|
|
|
|
|
Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 2, 3, 4));
|
|
|
|
|
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(10, 20, 30, 40));
|
|
|
|
|
sourceA.merge(sourceB).runWith(Sink.foreach(System.out::print), materializer);
|
|
|
|
|
// merging is not deterministic, can for example print 1, 2, 3, 4, 10, 20, 30, 40
|
|
|
|
|
|
|
|
|
|
//#merge
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void mergeSortedExample() {
|
|
|
|
|
//#merge-sorted
|
|
|
|
|
Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 3, 5, 7));
|
|
|
|
|
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(2, 4, 6, 8));
|
|
|
|
|
sourceA.mergeSorted(sourceB, Comparator.<Integer>naturalOrder()).runWith(Sink.foreach(System.out::print), materializer);
|
|
|
|
|
//prints 1, 2, 3, 4, 5, 6, 7, 8
|
|
|
|
|
|
|
|
|
|
Source<Integer, NotUsed> sourceC = Source.from(Arrays.asList(20, 1, 1, 1));
|
|
|
|
|
sourceA.mergeSorted(sourceC, Comparator.<Integer>naturalOrder()).runWith(Sink.foreach(System.out::print), materializer);
|
|
|
|
|
//prints 1, 3, 5, 7, 20, 1, 1, 1
|
|
|
|
|
//#merge-sorted
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void orElseExample() {
|
|
|
|
|
//#or-else
|
|
|
|
|
Source<String, NotUsed> source1 = Source.from(Arrays.asList("First source"));
|
|
|
|
|
Source<String, NotUsed> source2 = Source.from(Arrays.asList("Second source"));
|
|
|
|
|
Source<String, NotUsed> emptySource = Source.empty();
|
|
|
|
|
|
|
|
|
|
source1.orElse(source2).runWith(Sink.foreach(System.out::print), materializer);
|
|
|
|
|
// this will print "First source"
|
|
|
|
|
|
|
|
|
|
emptySource.orElse(source2).runWith(Sink.foreach(System.out::print), materializer);
|
|
|
|
|
// this will print "Second source"
|
|
|
|
|
|
|
|
|
|
//#or-else
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-20 16:22:11 +09:00
|
|
|
void conflateExample() {
|
|
|
|
|
//#conflate
|
|
|
|
|
Source.cycle(() -> Arrays.asList(1, 10, 100).iterator())
|
|
|
|
|
.throttle(10, Duration.ofSeconds(1)) // fast upstream
|
|
|
|
|
.conflate((Integer acc, Integer el) -> acc + el)
|
|
|
|
|
.throttle(1, Duration.ofSeconds(1)); // slow downstream
|
|
|
|
|
//#conflate
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static //#conflateWithSeed-type
|
|
|
|
|
class Summed {
|
|
|
|
|
|
|
|
|
|
private final Integer el;
|
|
|
|
|
|
|
|
|
|
public Summed(Integer el) {
|
|
|
|
|
this.el = el;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Summed sum(Summed other) {
|
|
|
|
|
return new Summed(this.el + other.el);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#conflateWithSeed-type
|
|
|
|
|
|
|
|
|
|
void conflateWithSeedExample() {
|
|
|
|
|
//#conflateWithSeed
|
|
|
|
|
|
|
|
|
|
Source.cycle(() -> Arrays.asList(1, 10, 100).iterator())
|
|
|
|
|
.throttle(10, Duration.ofSeconds(1)) // fast upstream
|
|
|
|
|
.conflateWithSeed(Summed::new, (Summed acc, Integer el) -> acc.sum(new Summed(el)))
|
|
|
|
|
.throttle(1, Duration.ofSeconds(1)); // slow downstream
|
|
|
|
|
//#conflateWithSeed
|
|
|
|
|
}
|
2018-06-30 05:10:10 +02:00
|
|
|
}
|