无法找到或加载主类Spark Docker

时间:2018-06-14 08:25:25

标签: scala apache-spark docker executable-jar

我已经构建了两个具有不同主类的独立jar文件--KafkaCheckinsProducer和SparkConsumer,它们都是主要方法的对象。在bash脚本中,我使用参数启动一个jar文件。我有一个Dockerfile启动这个bash脚本。我用这个命令启动我的Dockerfile:

docker run -v myvolume:/workdir built-image-name 

我收到这样的错误消息:

Error: Could not find or load main class consumer.SparkConsumer

可能导致此错误的原因以及如何修复Dockerfile或build.sbt?

这是我的Dockerfile:

FROM java:8
ARG ARG_CLASS
ENV MAIN_CLASS $ARG_CLASS
ENV SCALA_VERSION 2.11.8
ENV SBT_VERSION 1.1.1
ENV SPARK_VERSION 2.2.0
ENV SPARK_DIST spark-$SPARK_VERSION-bin-hadoop2.6
ENV SPARK_ARCH $SPARK_DIST.tgz

WORKDIR /opt

# Install Scala
RUN \
  cd /root && \
  curl -o scala-$SCALA_VERSION.tgz http://downloads.typesafe.com/scala/$SCALA_VERSION/scala-$SCALA_VERSION.tgz && \
  tar -xf scala-$SCALA_VERSION.tgz && \
  rm scala-$SCALA_VERSION.tgz && \
  echo >> /root/.bashrc && \
  echo 'export PATH=~/scala-$SCALA_VERSION/bin:$PATH' >> /root/.bashrc

# Install SBT
RUN \
  curl -L -o sbt-$SBT_VERSION.deb https://dl.bintray.com/sbt/debian/sbt-$SBT_VERSION.deb && \
  dpkg -i sbt-$SBT_VERSION.deb && \
  rm sbt-$SBT_VERSION.deb


# Install Spark
RUN \
    cd /opt && \
    curl -o $SPARK_ARCH http://d3kbcqa49mib13.cloudfront.net/$SPARK_ARCH && \
    tar xvfz $SPARK_ARCH && \
    rm $SPARK_ARCH && \
    echo 'export PATH=$SPARK_DIST/bin:$PATH' >> /root/.bashrc


EXPOSE 9851 9852 4040 9092 9200 9300 5601 7474 7687 7473

VOLUME /workdir

CMD /workdir/runDemo.sh "$MAIN_CLASS" 

Bash脚本如下所示:

#!/usr/bin/env bash
if [ "$1" = "consumer" ]
then
    java -cp "target/scala-2.11/demo_consumer.jar" consumer.SparkConsumer $2 $3 $4
elif [ "$1" = "producer" ]
then
    java -cp "target/scala-2.11/full_demo_producer.jar" producer.KafkaCheckinsProducer $5 $3 $6 $7
else
    echo "Wrong parameter. It should be consumer or producer, but it is $1"
fi

这是一个build.sbt,我通过更改主类名和jar名来构建两个jar:

name := "DemoBuildTest"
version := "0.1"
scalaVersion := "2.11.8"

assemblyJarName in assembly := "demo_producer.jar"
mainClass in assembly := Some("producer.KafkaCheckinsProducer")

val sparkVersion = "2.2.0"
resolvers += "Spark Packages Repo" at "http://dl.bintray.com/spark-packages/maven"


dependencyOverrides += "com.fasterxml.jackson.core" % "jackson-core" % "2.9.5"
dependencyOverrides += "com.fasterxml.jackson.core" % "jackson-databind" % "2.9.5"
dependencyOverrides += "com.fasterxml.jackson.module" % "jackson-module-scala_2.11" % "2.9.5"

libraryDependencies ++= Seq(
  "org.apache.kafka" %% "kafka" % "1.1.0",
  "org.apache.spark" %% "spark-core" % sparkVersion % "provided",
  "org.apache.spark" %% "spark-sql" % sparkVersion % "provided",
  "org.apache.spark" %% "spark-streaming" % sparkVersion % "provided",
  "org.apache.spark" %% "spark-streaming-kafka-0-10" % sparkVersion,
  "com.typesafe" % "config" % "1.3.0",
  "org.neo4j.driver" % "neo4j-java-driver" % "1.5.1",
  "com.opencsv" % "opencsv" % "4.1",
  "com.databricks" %% "spark-csv" % "1.5.0",
  "com.github.tototoshi" %% "scala-csv" % "1.3.5",
  "org.elasticsearch" %% "elasticsearch-spark-20" % "6.2.4"
)

assemblyMergeStrategy in assembly := {
  case PathList("org","aopalliance", xs @ _*) => MergeStrategy.last
  case PathList("javax", "inject", xs @ _*) => MergeStrategy.last
  case PathList("javax", "servlet", xs @ _*) => MergeStrategy.last
  case PathList("javax", "activation", xs @ _*) => MergeStrategy.last
  case PathList("org", "apache", xs @ _*) => MergeStrategy.last
  case PathList("org", "slf4j", xs @ _*) => MergeStrategy.last
  case PathList("org", "neo4j", xs @ _*) => MergeStrategy.last
  case PathList("com", "google", xs @ _*) => MergeStrategy.last
  case PathList("com", "esotericsoftware", xs @ _*) => MergeStrategy.last
  case PathList("com", "codahale", xs @ _*) => MergeStrategy.last
  case PathList("com", "yammer", xs @ _*) => MergeStrategy.last
  case PathList("net", "jpountz", xs @ _*) => MergeStrategy.last
  case PathList("META-INF", xs @ _*) => MergeStrategy.discard
  case "about.html" => MergeStrategy.rename
  case "META-INF/ECLIPSEF.RSA" => MergeStrategy.last
  case "META-INF/mailcap" => MergeStrategy.last
  case "META-INF/mimetypes.default" => MergeStrategy.last
  case "plugin.properties" => MergeStrategy.last
  case "log4j.properties" => MergeStrategy.last
  case x =>
    val oldStrategy = (assemblyMergeStrategy in assembly).value
    oldStrategy(x)
}

1 个答案:

答案 0 :(得分:1)

  1. 检查jar的主要类
  2. 在Dockerfile中,您在构建时声明if OBJECT_ID('Test') is not null drop table [dbo].[Test] CREATE TABLE [dbo].[Test](CustomerNumber varchar(10), FirstName varchar(10), MiddleName varchar(10), LastName varchar(10)) --populate test table insert into [dbo].[Test] values (123456, 'Test','Test1','Test2') , (234567, 'Test_2','Test_21','Test_22') , (345678, 'Test_3','Test_31','Test_32') --this variable holds all the customer numbers that will become column names declare @columns nvarchar(max)='' --this variable contains the dinamically generated TSQL code declare @sql nvarchar(max)='' select @columns = @columns + ', [' + [CustomerNumber] + ']' from [dbo].[Test] set @columns = RIGHT(@columns, len(@columns)-2) set @sql = @sql + 'select piv.COL as CustomerNumber, ' + @columns set @sql = @sql + ' from ' set @sql = @sql + ' ( ' set @sql = @sql + ' select [CustomerNumber], col, val, ord ' set @sql = @sql + ' from [dbo].[Test] ' set @sql = @sql + ' CROSS APPLY (' set @sql = @sql + ' VALUES (''FirstName'' ,FirstName , 1), ' set @sql = @sql + ' (''MiddleName'',MiddleName, 2), ' set @sql = @sql + ' (''LastName'' ,LastName, 3) ' set @sql = @sql + ' )CS (COL,VAL,ORD) ' set @sql = @sql + ' ) src ' set @sql = @sql + ' pivot ( max(val) for [CustomerNumber] in ('+@columns+') ) piv' set @sql = @sql + ' order by ord' exec(@sql) ,我想您希望在运行时将此env“动态”,因此将其从Dockerfile中删除,或使用MAIN_CLASS=consumer构建2个Docker镜像:消费者和制作者。