Limit and LimitWeighted operator docs #25468

This commit is contained in:
Johan Andrén 2020-01-08 16:26:51 +01:00 committed by GitHub
parent ed1f107ab1
commit 589c6511bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 142 additions and 17 deletions

View file

@ -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
}
}

View file

@ -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
}
}

View file

@ -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
}
}

View file

@ -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
}
}