Skip to content

Commit

Permalink
Fix redis examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kellen committed Sep 11, 2024
1 parent 078519c commit 8655860
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import org.apache.beam.examples.common.ExampleUtils
import org.apache.beam.sdk.options.{PipelineOptions, StreamingOptions}
import scala.concurrent.{ExecutionContext, Future}

// Example: Redis Examples

// ## Redis Read Strings example
// Read strings from Redis by a key pattern

Expand Down Expand Up @@ -135,7 +137,7 @@ object RedisWriteStreamingExample {
// `sbt "runMain com.spotify.scio.examples.extra.RedisLookUpStringsExample
// --project=[PROJECT] --runner=DataflowRunner --region=[REGION NAME]
// --redisHost=[REDIS_HOST]
// --redisPort=[REDIS_PORT]
// --redisPort=[REDIS_PORT]`
object RedisLookUpStringsExample {

def main(cmdlineArgs: Array[String]): Unit = {
Expand All @@ -146,7 +148,6 @@ object RedisLookUpStringsExample {
val connectionOptions = RedisConnectionOptions(redisHost, redisPort)

sc.parallelize(Seq("key1", "key2", "unknownKey"))
// #RedisLookup_example
.parDo(
new RedisDoFn[String, (String, Option[String])](connectionOptions, 1000) {
override def request(value: String, client: Client)(implicit
Expand All @@ -157,7 +158,6 @@ object RedisLookUpStringsExample {
.map { case r: List[String @unchecked] => (value, r.headOption) }
}
)
// #RedisLookup_example
.debug()

sc.run()
Expand Down
24 changes: 23 additions & 1 deletion site/src/main/paradox/io/Redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,29 @@ val elements: SCollection[(String, String)] = sc.redis(connectionOptions, keyPat

Looking up specific keys from redis can be done with @scaladoc[RedisDoFn](com.spotify.scio.redis.RedisDoFn):

@@snip [RedisExamples.scala](/scio-examples/src/main/scala/com/spotify/scio/examples/extra/RedisExamples.scala) { #RedisLookup_example }
```scala
import com.spotify.scio.redis._
import com.spotify.scio.values.SCollection

val redisHost: String = ???
val redisPort: Int = ???
val batchSize: Int = ???
val connectionOptions = RedisConnectionOptions(redisHost, redisPort)

val keys: SCollection[String] = ???

keys
.parDo(
new RedisDoFn[String, (String, Option[String])](connectionOptions, batchSize) {
override def request(value: String, client: Client)(
implicit ec: ExecutionContext
): Future[(String, Option[String])] =
client
.request(p => p.get(value) :: Nil)
.map { case r: List[String @unchecked] => (value, r.headOption) }
}
)
```

# Write

Expand Down

0 comments on commit 8655860

Please sign in to comment.