
正文
Redis on Spark:Task not serializable
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
|
We use Redis on Spark to cache our key-value pairs.This is the code:
But compiler gave me feedback like this:
Could somebody tell me how to serialize the data get from Redis.Thanks a lot.
scala redis apache-spark
|
||
|
add a comment
|
2 Answers
activeoldestvotes
activeoldestvotes
|
up vote10down voteaccepted
|
In Spark, the functions on
The Redis connection here is not serializable as it opens TCP connections to the target DB that are bound to the machine where it's created. The solution is to create those connections on the executors, in the local execution context. There're few ways to do that. Two that pop to mind are:
A singleton connection manager can be modeled with an object that holds a lazy reference to a connection (note: a mutable ref will also work).
This object can then be used to instantiate 1 connection per worker JVM and is used as a
The advantage of using the singleton object is less overhead as connections are created only once by JVM (as opposed to 1 per RDD partition) There're also some disadvantages:
(*) code provided for illustration purposes. Not compiled or tested.
|
||||||||||||
add a comment
|
|
up vote2down vote
|
You're trying to serialize the
client
. You have one
|
||||||||||||
|







