繁体   English   中英

无法使用spark-cassandra-connector

[英]Fail to use spark-cassandra-connector

我想执行一个spark项目,通过使用spark-cassandra-connector来连接到cassandra,如下所示:

版:

  • 卡桑德拉:2.1.2
  • CassandraDriver:2.1.3
  • 火花:1.1.0

build.sbt

name := "sparktest"

version := "1.0"

scalaVersion := "2.10.4"

libraryDependencies += "com.datastax.spark" %% "spark-cassandra-connector" % "1.1.0" withSources() withJavadoc()

sparkCassandra.scala

import com.datastax.spark.connector._
import org.apache.spark._

object SparkTest {
  def main(args: Array[String]){
    val conf = new SparkConf(true).set("spark.cassandra.connection.host", "127.0.0.1")
    val sc = new SparkContext("spark://master:7077", "test", conf)

    val rdd = sc.cassandraTable("mykeyspace", "users")
    println(rdd.count) 
  }
}

但是当我执行这个项目时

spark-submit --class "SparkTest"  target/scala-2.10/sparktest_2.10-1.0.jar

发生错误

Exception in thread "main" java.io.IOException: Failed to open thrift connection to Cassandra at 127.0.0.1:9160
        at com.datastax.spark.connector.cql.CassandraConnector.createThriftClient(CassandraConnector.scala:132)
        at com.datastax.spark.connector.cql.CassandraConnector.createThriftClient(CassandraConnector.scala:138)
        at com.datastax.spark.connector.cql.CassandraConnector.withCassandraClientDo(CassandraConnector.scala:144)
        at com.datastax.spark.connector.rdd.partitioner.CassandraRDDPartitioner.partitions(CassandraRDDPartitioner.scala:158)
        at com.datastax.spark.connector.rdd.CassandraRDD.getPartitions(CassandraRDD.scala:294)
        at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:204)
        at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:202)
        at scala.Option.getOrElse(Option.scala:120)
        at org.apache.spark.rdd.RDD.partitions(RDD.scala:202)
        at org.apache.spark.SparkContext.runJob(SparkContext.scala:1135)
        at org.apache.spark.rdd.RDD.count(RDD.scala:904)
        at SparkTest$.main(sparkcassandra.scala:14)
        at SparkTest.main(sparkcassandra.scala)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.apache.spark.deploy.SparkSubmit$.launch(SparkSubmit.scala:328)
        at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:75)
        at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
Caused by: java.lang.NoSuchMethodError: org.apache.cassandra.thrift.TFramedTransportFactory.openTransport(Ljava/lang/String;I)Lorg/apache/thrift/transport/TTransport;
        at com.datastax.spark.connector.cql.DefaultConnectionFactory$.createThriftClient(CassandraConnectionFactory.scala:47)
        at com.datastax.spark.connector.cql.CassandraConnector.createThriftClient(CassandraConnector.scala:127)
        ... 19 more

我发现如果键入错误的表名,则会显示“找不到表”,似乎连接成功。

但是,当我使用cassandra表rdd时,它将导致上述错误。 如果有人可以帮助我,我无法弄清楚此错误的原因是什么? 非常感谢!

我认为您的cassandra不在端口9160上运行。您可以在cassandra.yaml文件中对其进行验证,也可以使用telnet 127.0.0.1 9160进行验证,然后查看它是否可以telnet到该端口。

我之前遇到过保存问题。 此错误是由Cassandra驱动程序和主机之间的版本不匹配引起的。 因此,请检查是否在spark-application-jar中包含了较低版本的Cassandra驱动程序。 在这种情况下,您应该使用Cassandra驱动程序2.1.x。

通过两个更正解决了此问题:

  1. 使用本地主机而不是IP。
  2. val sc = new SparkContext(conf)

在后续步骤中没有其他更改。 希望它也对您有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM