Limit and LimitWeighted operator docs #25468
This commit is contained in:
parent
ed1f107ab1
commit
589c6511bd
7 changed files with 142 additions and 17 deletions
|
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2020 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.stream.operators.sourceorflow;
|
||||
|
||||
import akka.NotUsed;
|
||||
import akka.actor.typed.ActorSystem;
|
||||
import akka.stream.javadsl.Sink;
|
||||
import akka.stream.javadsl.Source;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
|
||||
public class Limit {
|
||||
public void simple() {
|
||||
ActorSystem<?> system = null;
|
||||
// #simple
|
||||
Source<String, NotUsed> untrustedSource = Source.repeat("element");
|
||||
|
||||
CompletionStage<List<String>> elements =
|
||||
untrustedSource.limit(10000).runWith(Sink.seq(), system);
|
||||
// #simple
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2020 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.stream.operators.sourceorflow;
|
||||
|
||||
import akka.NotUsed;
|
||||
import akka.actor.typed.ActorSystem;
|
||||
import akka.stream.javadsl.Source;
|
||||
import akka.util.ByteString;
|
||||
|
||||
import java.util.concurrent.CompletionStage;
|
||||
|
||||
public class LimitWeighted {
|
||||
public void simple() {
|
||||
ActorSystem<?> system = null;
|
||||
// #simple
|
||||
Source<ByteString, NotUsed> untrustedSource = Source.repeat(ByteString.fromString("element"));
|
||||
|
||||
CompletionStage<ByteString> allBytes =
|
||||
untrustedSource
|
||||
.limitWeighted(
|
||||
10000, // max bytes
|
||||
bytes -> (long) bytes.length() // bytes of each chunk
|
||||
)
|
||||
.runReduce(ByteString::concat, system);
|
||||
// #simple
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2020 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package docs.stream.operators.sourceorflow
|
||||
|
||||
import akka.NotUsed
|
||||
import akka.actor.typed.ActorSystem
|
||||
import akka.stream.scaladsl.Sink
|
||||
import akka.stream.scaladsl.Source
|
||||
|
||||
import scala.concurrent.Future
|
||||
|
||||
object Limit {
|
||||
|
||||
implicit val system: ActorSystem[_] = ???
|
||||
|
||||
def simple(): Unit = {
|
||||
// #simple
|
||||
val untrustedSource: Source[String, NotUsed] = Source.repeat("element")
|
||||
|
||||
val elements: Future[Seq[String]] =
|
||||
untrustedSource.limit(10000).runWith(Sink.seq)
|
||||
// #simple
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2020 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package docs.stream.operators.sourceorflow
|
||||
|
||||
import akka.NotUsed
|
||||
import akka.actor.typed.ActorSystem
|
||||
import akka.stream.scaladsl.Source
|
||||
import akka.util.ByteString
|
||||
|
||||
import scala.concurrent.Future
|
||||
|
||||
object LimitWeighted {
|
||||
|
||||
implicit val system: ActorSystem[_] = ???
|
||||
|
||||
def simple(): Unit = {
|
||||
// #simple
|
||||
val untrustedSource: Source[ByteString, NotUsed] = Source.repeat(ByteString("element"))
|
||||
|
||||
val allBytes: Future[ByteString] =
|
||||
untrustedSource.limitWeighted(max = 10000)(_.length).runReduce(_ ++ _)
|
||||
// #simple
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue