java.lang.noclassdeffounderror:org/apache/spark/streaming/twitter/twitterutils$运行twitterpopulartags时

0aydgbwb  于 2021-06-06  发布在  Kafka
关注(0)|答案(7)|浏览(318)

我是spark streaming和scala的初学者。对于一个项目需求,我试图运行github中的twitterpoulartags示例。由于sbt组装不适合我,我也不熟悉sbt,所以我尝试使用maven来构建。在经历了许多初始的停顿之后,我能够创建jar文件。但在尝试执行它时,我得到了以下错误。有人能帮我解决这个问题吗?

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/spark/streaming/twitter/TwitterUtils$
    at TwitterPopularTags$.main(TwitterPopularTags.scala:43)
    at TwitterPopularTags.main(TwitterPopularTags.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:331)
    at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:75)
    at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
Caused by: java.lang.ClassNotFoundException: org.apache.spark.streaming.twitter.TwitterUtils$
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 9 more

我添加了以下依赖项spark-streaming\u2.10:1.1.0 spark-core\u2.10:1.1.0 spark-streaming-twitter\u2.10:1.1.0
我甚至尝试了spark streaming twitter的1.2.0版本,但这也给了我同样的错误。
提前谢谢你的帮助。
你好,副总裁

r7knjye2

r7knjye21#

找到了一个简单的解决方案(当然可以与1.5.1一起使用,但也可以与早期版本一起使用):
提交 --packages 参数和maven坐标,如:

spark-submit --master local[*] \
    --class TwitterStreaming \
    --packages "org.apache.spark:spark-streaming-twitter_2.10:1.5.1" \
    ${PATH_TO_JAR_IN_TARGET}

描述于
http://spark.apache.org/docs/latest/programming-guide.html#using-贝壳

but5z9lq

but5z9lq2#

试着这样做。。。

./bin/spark-submit \
  --class TwitterPopularTags \
  --jars (external_jars like twitter4j,streaming-twitter) \
  --master local[4] \
  /path/to/TwitterPopilarTags.jar \

comsumerkey ConsumerCret accesstoken accesssecret用户密钥

olhwl3o2

olhwl3o23#

要在jar中包含依赖项,您需要指示maven构建一个“胖jar”。“fatjar”是一个jar,它不仅包含项目的.class文件,还包含所有必需的依赖项(这就是sbt assembly所做的)。默认的maven行为是将项目视为库,因此只使用.class文件构建jar。
下面是一个简单的maven pom,它可以做您想做的事情,注意我已经包括了一些其他常见的spark+maven行为,例如使用scala,但是最相关的部分在下面:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.jmess.sparkexamples</groupId>
    <artifactId>example</artifactId>
    <version>1.0.0</version>

    <properties>
        <!-- Use java 1.8 -->
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <!-- Keep compiled against scala version uniform -->
        <scala.base.version>2.11</scala.base.version>
        <!-- Use most recent version of Scala compatible with stable Spark release -->
        <scala.version>${scala.base.version}.12</scala.version>
        <!-- Facilitates keeping multiple Spark dependencies aligned  -->
        <spark.version>2.4.0</spark.version>
    </properties>

    <dependencies>
        <!-- Begin Spark Dependencies -->
        <!-- Provides the base Spark APIs. Required for base functionality -->
        <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-sql -->
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql_${scala.base.version}</artifactId>
            <version>${spark.version}</version>
            <!-- In most cases this dependency is supplied by Spark -->
            <scope>provided</scope>
        </dependency>
        <!-- Provides the expanded APIs for Streaming with Kafka. Required in addition to spark-sql library -->
        <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-sql-kafka-0-10 -->
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql-kafka-0-10_${scala.base.version}</artifactId>
            <version>${spark.version}</version>
        </dependency>
        <!-- End Spark Dependencies -->

        <!-- Popular scala configuration library -->
        <dependency>
            <groupId>com.typesafe</groupId>
            <artifactId>config</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!-- To write to Splunk HTTP endpoint -->
    </dependencies>

    <build>
        <!-- Tells scala-maven-plugin where to look -->
        <sourceDirectory>src/main/scala</sourceDirectory>
        <testSourceDirectory>src/test/scala</testSourceDirectory>

        <plugins>
            <!-- For building scala projects using maven -->
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>4.0.1</version>
                <!-- Includes the compiled Scala .class files in some maven goals -->
                <executions>
                    <execution>
                        <goals>
                            <goal>add-source</goal>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- !!!!!!! BUILD FAT JAR !!!!!!! -->
            <!-- Build a fat jar named example-1.0.0-jar-with-dependencies.jar -->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

注意**如果您是通过spark而不是yarn提交作业,那么请取消对 <scope>provided</scope> 线

z4bn682m

z4bn682m4#

谢谢你的建议。我只能通过使用sbt组装来解决这个问题。下面是关于我是如何做到这一点的细节。
spark-已经存在于cloudera vm scala中-不确定是否存在于cloudera中,如果没有,我们可以安装它sbt-这也需要安装。我在本地机器上进行了两次安装,并将jar传输到vm。为了安装这个,我使用了以下链接
https://gist.github.com/visenger/5496675
1) 一旦所有这些都被创建。我们必须为我们的项目创建父文件夹。我创建了一个名为twitter的文件夹。
2) 创建另一个具有以下结构的文件夹twitter/src/main/scala,并在此文件夹中创建名为twitterpoulartags.scala的.scala文件。这与我们从github获得的代码略有不同。我不得不更改进口报表

import org.apache.spark.streaming.Seconds
import org.apache.spark.streaming.StreamingContext
import org.apache.spark.streaming.StreamingContext._
import org.apache.spark.SparkContext._
import org.apache.spark.streaming.twitter._
import org.apache.spark.SparkConf

3) 在此之后,在父文件夹下用以下名称创建另一个文件夹
twitter/项目
并创建一个名为assembly.sbt的文件。这有程序集插件的路径。以下是文件中的完整代码。

resolvers += Resolver.url("sbt-plugin-releases-scalasbt", url("http://repo.scala-sbt.org/scalasbt/sbt-plugin-releases/"))

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

4) 创建了以上两个之后,在项目的父目录(twitter)中创建一个名为build.sbt的文件。在这里,我们需要提供需要创建的.jar文件的名称以及依赖项。请注意,即使文件中的代码之间的空行也很重要。

name := "TwitterPopularTags"

version := "1.0"

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

libraryDependencies += "org.apache.spark" %% "spark-core" % "1.1.0" % "provided"

libraryDependencies += "org.apache.spark" %% "spark-streaming" % "1.1.0" % "provided"

libraryDependencies += "org.apache.spark" %% "spark-streaming-twitter" % "1.2.0" 

libraryDependencies += "org.twitter4j" % "twitter4j-stream" % "3.0.3" 

resolvers += "Akka Repository" at "http://repo.akka.io/releases/"

5) 最后,我们必须打开终端并转到项目的父文件夹(twitter)。从这里输入以下命令:

sbt assembly

这将下载依赖项并创建我们需要的jar文件。
6) 为了运行该程序,我们需要在我们的id下创建一个twitter应用程序,并提供身份验证令牌和其他详细信息。下面的链接中提供了有关如何创建此文件的详细步骤。
http://ampcamp.berkeley.edu/3/exercises/realtime-processing-with-spark-streaming.html
7) 一旦我们完成了上述所有操作,我们就可以使用vm中的spark submit命令来运行作业。命令示例为

./bin/spark-submit \
  --class TwitterPopularTags \
  --master local[4] \
  /path/to/TwitterPopilarTags.jar \
  comsumerkey consumersecret accesstoken accesssecret

8) 这会将输出打印到控制台,以便监视输出,最好通过调整代码来降低频率。
如果需要更多的细节,请告诉我。
谢谢和问候,
vpv公司

kulphzqa

kulphzqa5#

我也面临同样的问题,通过从sbt中删除“provided”来解决它。

hfsqlsce

hfsqlsce6#


**I have the same problem and I am not able to fix**

    name := "SentimentAnalyser"

    version := "0.1"

    scalaVersion := "2.11.11"

    libraryDependencies += "org.apache.spark" % "spark-core_2.11" % "2.2.0"

    // https://mvnrepository.com/artifact/org.apache.spark/park-streaming-twitter_2.11

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

    package com

    import org.apache.spark.SparkConf
    import org.apache.spark.streaming.{Seconds, StreamingContext}
    import org.apache.spark.streaming.twitter.TwitterUtils

    object Sentiment {

      def main(args: Array[String]): Unit = {

        if(args.length<4){
          System.out.print("Enter Consumer Key (API Key) Consumer Secret (API Secret)Access Token Access Token Secret")
          System.exit(1);
        }

        val Array(customer_key,customer_secrect,access_token,access_token_secret)=args.take(4)

        System.setProperty("twiteer4j.oauth.consumerKey",customer_key)
        System.setProperty("twiteer4j.oauth.customerSecret",customer_secrect)
        System.setProperty("twiteer4j.oauth.accessToken",access_token)
        System.setProperty("twiteer4j.oauth.accessTokenSecret",access_token_secret)

        val conf=new SparkConf().setAppName("Sentiment").setMaster("local")
        val scc=new StreamingContext(conf,Seconds(30))
        //Dstream
        val stream=TwitterUtils.createStream(scc,None)

        val hashTag=stream.flatMap(status=>{status.getText.split(" ").filter(_.startsWith("#"))})

        val topHashTag60=hashTag.map((_,1)).reduceByKeyAndWindow(_+_,Seconds(60))
          .map{case (topic,count)=>(topic,count)}.transform(_.sortByKey(false))

        val topHashTag10=hashTag.map((_,1)).reduceByKeyAndWindow(_+_,Seconds(10))
          .map{case (topic,count)=>(topic,count)}.transform(_.sortByKey(false))

        topHashTag60.foreachRDD(rdd=>{
          val topList=rdd.take(10)
          println("Popular topic in last 60 sec (%s total)".format(rdd.count()))
          topList.foreach{case (count,tag)=>println("%s (%s tweets)".format(tag,count))}
        })

        topHashTag10.foreachRDD(rdd=>{
          val topList=rdd.take(10)
          println("Popular topic in last 10 sec (%s total)".format(rdd.count()))
          topList.foreach{case (count,tag)=>println("%s (%s tweets)".format(tag,count))}
        })

        scc.start()
        scc.awaitTermination()

      }

    }

I build jar using artifact in IJ ..

spark-submit --class com.Sentiment  /root/Desktop/SentimentAnalyser.jar XX XX XX XX

ERROR:
17/10/29 01:22:24 INFO BlockManager: Initialized BlockManager: BlockManagerId(driver, 192.168.46.132, 34179, None)
17/10/29 01:22:27 WARN StreamingContext: spark.master should be set as local[n], n > 1 in local mode if you have receivers to get data, otherwise Spark jobs will not get resources to process the received data.
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/spark/streaming/twitter/TwitterUtils$
        at com.Sentiment$.main(Sentiment.scala:26)
        at com.Sentiment.main(Sentiment.scala)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:755)
        at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:180)
        at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:205)
        at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:119)
        at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
Caused by: java.lang.ClassNotFoundException: org.apache.spark.streaming.twitter.TwitterUtils$
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at java.lang.ClassLoader.loadClass(ClassLoader.java
2ekbmq32

2ekbmq327#

这个错误仅仅意味着在运行时twitterutils类(或者scala语言中的twitterutils对象)没有出现,但是在编译时出现了它(因为否则您将无法使用maven构建)。您应该确保您创建的jar文件确实包含该类/对象。您可以简单地解压缩jar文件以查看实际包含的内容。maven构建文件很可能使用依赖关系来构建项目,但最终不会将其包含在最终的jar中。

相关问题