问题描述
我已在数据块 "com.redislabs:spark-redis_2.12:2.5.0"
中安装了以下 jar。并尝试使用相应的身份验证创建 spark 会话
以下是我使用凭据创建 spark 会话的代码
redis= SparkSession.builder.appName("redis_connection").config("spark.redis.host","hostname").config("spark.redis.port","port").config("spark.redis.auth","pass").getorCreate()
但是当我尝试使用以下代码保存它时
df.write.format("org.apache.spark.sql.redis").option("table","veLocity").option("key.column","name").option("ttl",30).save()
这会引发以下错误。
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to host localhost:6379
这显然意味着连接到 localhost 而不是我提供的 主机名。如何在数据块中传递带有主机名和密码的 jar 配置以验证连接。
解决方法
最有可能的 databricks 选择了没有设置配置参数的错误 Spark 会话。 您可以尝试两种选择:
- 在 Databricks 集群配置中设置
spark.redis.host
、spark.redis.port
和spark.redis.auth
。转到集群 -> 编辑 -> 高级选项 -> Spark -> Spark 配置 - 在隐式创建的
spark
会话中使用spark.conf.set("spark.redis.host","host")
设置选项,其他参数也一样。
我在使用类似配置通过 spark 将数据摄取到 redis 时遇到了同样的错误,我使用主机、端口和身份验证作为配置而不是 spark.redis.*,这对我有用
import scala.collection.mutable.HashMap
def getRedisClusterProperties(): HashMap[String,String] = {
val properties = new HashMap[String,String]
properties.put("host","<host>")
properties.put("port","6379")
properties.put("auth","<auth>")
properties
}
df.write.mode(SaveMode.Overwrite).format("org.apache.spark.sql.redis").options(getRedisClusterProperties()).option("table","<table_name>").option("key.column","<column_name>").save