无法在Spark Scala中导入org.apache.spark.streaming.twitter

时间:2016-07-01 12:30:58

标签: scala twitter apache-spark streaming

以下导入无法在SBT中编译

import org.apache.spark.streaming.twitter._

[error] /home/hduser/workspace/TweetStream/src/main/scala/TweetStream.scala:8: object twitter is not a member of package org.apache.spark.streaming
[error] import org.apache.spark.streaming.twitter._
[error]  

以下随后也是

val tweetStream = TwitterUtils.createStream(ssc, None, filters, StorageLevel.MEMORY_ONLY_SER_2).map(gson.toJson(_))


[error] /home/hduser/workspace/TweetStream/src/main/scala/TweetStream.scala:36: not found: value TwitterUtils
[error]     val tweetStream = TwitterUtils.createStream(ssc, None, filters, StorageLevel.MEMORY_ONLY_SER_2).map(gson.toJson(_))
[error]                       ^
                                 ^

build.sbt是以下传递所有依赖项解析

name := "TweetStream"
version := "1.0"
scalaVersion := "2.11.7"

libraryDependencies += "org.apache.spark" %% "spark-core" % "1.5.2" 
libraryDependencies += "org.apache.spark" %% "spark-streaming" % "1.5.2"
libraryDependencies += "org.apache.spark" % "spark-streaming_2.11" % "1.5.2"
libraryDependencies += "com.google.code.gson" % "gson" % "2.7"
libraryDependencies += "org.twitter4j" % "twitter4j-core" % "4.0.4"

我是否添加了错误的依赖项?

2 个答案:

答案 0 :(得分:2)

您需要添加以下依赖项:

// https://mvnrepository.com/artifact/org.apache.spark/spark-streaming-twitter_2.11
libraryDependencies += "org.apache.spark" % "spark-streaming-twitter_2.11" % "1.5.2"

PS:其他依赖项Scala版本可能会给您带来一些问题。你应该为你的其他火花依赖指定_2.11。

答案 1 :(得分:1)

这是build.sbt ...

lazy val root = (project in file(".")).
  settings(
    name := "TweetStream",
    version := "1.0",
    scalaVersion := "2.11.7",
    mainClass in Compile := Some("TweetStream")        
  )

libraryDependencies ++= Seq(
  "org.apache.spark" %% "spark-core" % "1.5.2",
  "org.apache.spark" %% "spark-streaming" % "1.5.2",
  "org.apache.spark" % "spark-streaming-twitter_2.11" % "1.5.2",
  "com.google.code.gson" % "gson" % "2.7",
  "org.twitter4j" % "twitter4j-core" % "3.0.3",
  "org.twitter4j" % "twitter4j-stream" % "3.0.3"
)

// META-INF discarding
mergeStrategy in assembly <<= (mergeStrategy in assembly) { (old) =>
   {
    case PathList("META-INF", xs @ _*) => MergeStrategy.discard
    case x => MergeStrategy.first
   }
}

项目子文件夹中的assembly.sbt

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.3")