我已在Azure虚拟机中安装Cassandra数据库,并希望通过Azure Databricks执行读/写操作。我正在通过Databricks官方documentation,这对我的配置没有帮助。我在下面分享我的代码和配置详细信息:
%sh
ping -c 2 vmname.westeurope.cloudapp.azure.com
已收到回复:
PING vmname.westeurope.cloudapp.azure.com (13.69.10.10): 56 data bytes
--- vmname.westeurope.cloudapp.azure.com ping statistics ---
2 packets transmitted, 0 packets received, 100% packet loss
// define the cluster name and cassandra host name
val sparkClusterName = "adbazewdobucluster"
val cassandraHostIP = "vmname.westeurope.cloudapp.azure.com"
dbutils.fs.put(s"/databricks/init/$sparkClusterName/cassandra.sh",
s"""
#!/usr/bin/bash
echo '[driver]."spark.cassandra.connection.host" = "$cassandraHostIP"' >> /home/ubuntu/databricks/common/conf/cassandra.conf
""".trim, true)
// setting IP of the Cassandra server
spark.conf.set("spark.cassandra.connection.host", "127.0.0.1")
//verify sparkconf is set properly
spark.conf.get("spark.cassandra.connection.host")
并且在应用了spark中的所有配置之后,我试图从表中检索记录,该记录位于Cassandra DB中,这引发了异常。
val df = sqlContext
.read
.format("org.apache.spark.sql.cassandra")
.options(Map( "table" -> "words_new", "keyspace" -> "test"))
.load
df.explain
Exception:
com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: /127.0.0.1:9042 (com.datastax.driver.core.exceptions.TransportException: [/127.0.0.1:9042] Cannot connect))
我已经检查了我的Cassandra数据库是否正在运行,并且读/写操作直接可以正常工作。所以我的[[问题是:我是否以正确的方式应用配置?如果不是这样,那么我该如何从Databricks笔记本中访问Cassandra。我将Scala用于Spark框架,并且我的集群和驱动程序版本如下:
Databricks Runtime Version
6.2 (includes Apache Spark 2.4.4, Scala 2.11)
spark-cassandra-connector
com.datastax.spark:spark-cassandra-connector_2.11:2.4.1
cassandra version: 3.11.4
NoHostAvailableException
,因为您正在连接到无法访问的localhost
。这是因为您在此处将C *主机设置为localhost
:spark.conf.set("spark.cassandra.connection.host", "127.0.0.1")
您需要将其设置为该值:
val cassandraHostIP = "vmobu4pldev.westeurope.cloudapp.azure.com"
干杯!