Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
变量声明错误-java.lang.NoSuchMethodError:scala.runtime.IntRef.create(I)Lscala/runtime/IntRef;-scala版本不匹配_Scala_Maven_Apache Spark_Intellij Idea - Fatal编程技术网

变量声明错误-java.lang.NoSuchMethodError:scala.runtime.IntRef.create(I)Lscala/runtime/IntRef;-scala版本不匹配

变量声明错误-java.lang.NoSuchMethodError:scala.runtime.IntRef.create(I)Lscala/runtime/IntRef;-scala版本不匹配,scala,maven,apache-spark,intellij-idea,Scala,Maven,Apache Spark,Intellij Idea,我正在使用Maven在intelliJ上使用Spark/Scala应用程序。我正在尝试写入一个文件,写入完成后,我将尝试逐行读取它,并根据条件增加名为count的变量。这是我的密码: val writer: BufferedWriter = new BufferedWriter(new FileWriter("test.csv")) /* Logic for writing the file */ writer.close() var count = 0 val buffe

我正在使用Maven在intelliJ上使用Spark/Scala应用程序。我正在尝试写入一个文件,写入完成后,我将尝试逐行读取它,并根据条件增加名为
count
的变量。这是我的密码:

val writer: BufferedWriter = new BufferedWriter(new FileWriter("test.csv"))
/* Logic
    for
   writing 
  the file
*/
writer.close()

var count = 0
val bufferedSource = Source.fromFile("test.csv")

for(line <- bufferedSource.getLines()){
    count = count+1
    line.split(",").foreach(x => {if(x.toString == "1") count = count+1})
}

bufferedSource.close()
它在变量声明语句中抛出错误:
var count=0

我在网上搜索了这个错误,发现原因是scala版本不匹配,正如这里指出的:

最初,我将project SDK设置为
2.10.6
。这是我用于项目的默认版本。我在pom.xml中没有提到任何scala语言依赖性。当我运行用于构建jar的
mvn-U package
命令时,我得到以下消息:

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building project
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ IPscan ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\zkh9xcl\workspace\IPscan\src\main\resources
[INFO]
[INFO] --- scala-maven-plugin:3.2.2:compile (scala-compile-first) @ IPscan ---
[WARNING]  Expected all dependencies to require Scala version: 2.11.7
[WARNING]  com.databricks:spark-csv_2.11:1.4.0 requires scala version: 2.11.7
[WARNING]  com.twitter:chill_2.10:0.5.0 requires scala version: 2.10.4
[WARNING] Multiple versions of scala libraries detected!
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ IPscan ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ IPscan ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\zkh9xcl\workspace\IPscan\src\test\resources
[INFO]
[INFO] --- scala-maven-plugin:3.2.2:testCompile (scala-test-compile) @ IPscan ---
[WARNING]  Expected all dependencies to require Scala version: 2.11.7
[WARNING]  com.databricks:spark-csv_2.11:1.4.0 requires scala version: 2.11.7
[WARNING]  com.twitter:chill_2.10:0.5.0 requires scala version: 2.10.4
[WARNING] Multiple versions of scala libraries detected!
[INFO] No sources to compile
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ IPscan ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ IPscan ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ IPscan ---
[INFO]
[INFO] --- maven-shade-plugin:2.4.1:shade (default) @ IPscan ---
[INFO] Including com.databricks:spark-csv_2.11:jar:1.4.0 in the shaded jar.
[INFO] Including org.scala-lang:scala-library:jar:2.11.7 in the shaded jar.
[INFO] Including org.apache.commons:commons-csv:jar:1.1 in the shaded jar.
[INFO] Including com.univocity:univocity-parsers:jar:1.5.1 in the shaded jar.
[INFO] Skipping pom dependency org.jodd:jodd:pom:3.4.0 in the shaded jar.
[INFO] Including org.threeten:threetenbp:jar:1.3.3 in the shaded jar.
[INFO] Attaching shaded artifact.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.054 s
[INFO] Finished at: 2017-10-26T11:18:39-04:00
[INFO] Final Memory: 38M/663M
[INFO] ------------------------------------------------------------------------
它抛出一条警告:“检测到多个版本的scala库

以下是我的pom.xml:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>project</groupId>
  <artifactId></artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>IPscan</name>
  <repositories>
    <repository>
      <id>scala-tools.org</id>
      <name>Scala-tools Maven2 Repository</name>
      <url>http://scala-tools.org/repo-releases</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>scala-tools.org</id>
      <name>Scala-tools Maven2 Repository</name>
      <url>http://scala-tools.org/repo-releases</url>
    </pluginRepository>
  </pluginRepositories>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>

      <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>3.2.2</version>
        <executions>
          <execution>
            <id>scala-compile-first</id>
            <phase>process-resources</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>

          <execution>
            <id>scala-test-compile</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.1</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <relocations>
                <relocation>
                  <pattern>org.apache.http</pattern>
                  <shadedPattern>org.shaded.apache.http</shadedPattern>
                </relocation>
              </relocations>
              <filters>
                <filter>
                  <artifact>*:*</artifact>

                </filter>
              </filters>
              <shadedArtifactAttached>true</shadedArtifactAttached>
              <shadedClassifierName>shaded</shadedClassifierName>
            </configuration>

          </execution>
        </executions>
      </plugin>
    </plugins>   
  </build>    
  <dependencies>
    <dependency>
      <groupId>com.databricks</groupId>
      <artifactId>spark-csv_2.11</artifactId>
      <version>1.4.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.jodd/jodd -->
    <dependency>
      <groupId>org.jodd</groupId>
      <artifactId>jodd</artifactId>
      <version>3.4.0</version>
      <type>pom</type>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core_2.10 -->
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-sql_2.10</artifactId>
      <version>1.6.0</version>
      <scope>provided</scope>
      <exclusions>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>jcl-over-slf4j</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.threeten/threetenbp -->
    <dependency>
      <groupId>org.threeten</groupId>
      <artifactId>threetenbp</artifactId>
      <version>1.3.3</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-hive_2.10 -->
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-hive_2.10</artifactId>
      <version>1.6.0</version>
      <scope>provided</scope>
      <exclusions>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>
</project>

4.0.0
项目
1.0-快照
伊普斯坎
scala-tools.org
Scala工具Maven2存储库
http://scala-tools.org/repo-releases
scala-tools.org
Scala工具Maven2存储库
http://scala-tools.org/repo-releases
src
net.alchim31.maven
scala maven插件
3.2.2
scala先编译
过程资源
编译
scala测试编译
过程测试资源
测试编译
org.apache.maven.plugins
maven阴影插件
2.4.1
包裹
阴凉处
org.apache.http
org.shade.apache.http
*:*
真的
遮住的
com.databricks
spark-csv_2.11
1.4.0
org.jodd
乔德
3.4.0
聚甲醛
org.apache.spark
spark-sql_2.10
1.6.0
假如
org.slf4j
slf4j api
org.slf4j
jcl-over-slf4j
三十
三个十个基点
1.3.3
org.apache.spark
spark-hive_2.10
1.6.0
假如
org.slf4j
slf4j api
我是maven的新手,如果我的
pom.xml
有问题,请原谅

我不知道为什么jar中包含多个版本的scala库。实际上,我在intelliJ项目的全局库中将java版本改为
2.11.7
,然后改为
2.10.4
,但如果说
库scala-sdk-2.10.4未使用[Fix]
,就会出现问题。我很确定我在intelliJ的依赖项列表中添加了
2.10.4
。无论我做什么,当我构建jar时,仍然会收到
多个scala版本
警告,并且
noSuchMethodError
仍然存在。看来我错过了什么

是什么导致了错误?那么,克服这一问题并在未来避免它的最佳方法是什么?任何帮助都将不胜感激。谢谢大家!


p.S:我正在使用Spark 1.6,无法升级到新版本。

这是一个简单的依赖项不匹配的情况。我猜这是因为
com.databricks
sparkcsv
包。能否将版本更改为
2.10
,然后重试


让我知道这是否有用。干杯。

这是一个依赖不匹配的简单例子。我猜这是因为
com.databricks
sparkcsv
包。能否将版本更改为
2.10
,然后重试


让我知道这是否有用。干杯。

谢谢你的回答!我将
spark-csv_2.11
更改为
spark-csv_2.10
,现在我收到了这些警告:
[WARNING]预期所有依赖项都需要Scala版本:2.10.5[WARNING]com。数据瑞克:spark-csv_2.10:1.4.0需要Scala版本:2.10.5[WARNING]com。twitter:chill_2.10:0.5.0需要Scala版本:2.10.4[WARNING]检测到多个版本的scala库
错误仍然存在。您能确认intellij中的scala sdk版本是2.10吗?并注释掉pomYes的repositories和pluginRepositories部分,我已经在全局库中将sdk设置为
2.10.4
。正如我在上面的评论中所看到的,
spark-csv_2.10
现在要求scala版本
2.10.5
。我还尝试将IDE中的scala版本更改为
2.10.5
。我还看到
2.10.5
包含在依赖项中。我不确定scala的多个版本来自何处。注释掉了存储库。仍然是相同的警告和错误。我是从maven控制台而不是IDE本身构建jar的,如果这有助于解决错误的话..谢谢你的回答!我将
spark-csv_2.11
更改为
spark-csv_2.10
,现在我收到了这些警告:
[WARNING]预期所有依赖项都需要Scala版本:2.10.5[WARNING]com。数据瑞克:spark-csv_2.10:1.4.0需要Scala版本:2.10.5[WARNING]com。twitter:chill_2.10:0.5.0需要Scala版本:2.10.4[WARNING]检测到多个版本的scala库
错误仍然存在。您能确认intellij中的scala sdk版本是2.10吗?并注释掉pomYes的存储库和插件存储库部分,
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>project</groupId>
  <artifactId></artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>IPscan</name>
  <repositories>
    <repository>
      <id>scala-tools.org</id>
      <name>Scala-tools Maven2 Repository</name>
      <url>http://scala-tools.org/repo-releases</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>scala-tools.org</id>
      <name>Scala-tools Maven2 Repository</name>
      <url>http://scala-tools.org/repo-releases</url>
    </pluginRepository>
  </pluginRepositories>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>

      <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>3.2.2</version>
        <executions>
          <execution>
            <id>scala-compile-first</id>
            <phase>process-resources</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>

          <execution>
            <id>scala-test-compile</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.1</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <relocations>
                <relocation>
                  <pattern>org.apache.http</pattern>
                  <shadedPattern>org.shaded.apache.http</shadedPattern>
                </relocation>
              </relocations>
              <filters>
                <filter>
                  <artifact>*:*</artifact>

                </filter>
              </filters>
              <shadedArtifactAttached>true</shadedArtifactAttached>
              <shadedClassifierName>shaded</shadedClassifierName>
            </configuration>

          </execution>
        </executions>
      </plugin>
    </plugins>   
  </build>    
  <dependencies>
    <dependency>
      <groupId>com.databricks</groupId>
      <artifactId>spark-csv_2.11</artifactId>
      <version>1.4.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.jodd/jodd -->
    <dependency>
      <groupId>org.jodd</groupId>
      <artifactId>jodd</artifactId>
      <version>3.4.0</version>
      <type>pom</type>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core_2.10 -->
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-sql_2.10</artifactId>
      <version>1.6.0</version>
      <scope>provided</scope>
      <exclusions>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>jcl-over-slf4j</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.threeten/threetenbp -->
    <dependency>
      <groupId>org.threeten</groupId>
      <artifactId>threetenbp</artifactId>
      <version>1.3.3</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-hive_2.10 -->
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-hive_2.10</artifactId>
      <version>1.6.0</version>
      <scope>provided</scope>
      <exclusions>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>
</project>