2015-10-14 52 views
0

當我在maven中構建它時,它會成功編譯。maven依賴項java.util.function未正確加載函數類

package com.hf.arm; 

import org.apache.spark.SparkConf; 
import java.util.Arrays; 
import java.util.List; 
import org.apache.spark.api.java.JavaRDD; 
import org.apache.spark.api.java.JavaSparkContext; 
import org.apache.spark.mllib.fpm.AssociationRules; 
import org.apache.spark.mllib.fpm.FPGrowth; 
import org.apache.spark.mllib.fpm.FPGrowthModel; 
public class App 
{ 
    public static void main(String[] args) 
    { 
     SparkConf conf = new SparkConf().setAppName("FP-growth Example"); 
     JavaSparkContext sc = new JavaSparkContext(conf); 
     JavaRDD<String> data = sc.textFile("/Users/lincolnsmith/Table_csv/sample_fpgrowth.txt"); 
    } 
} 

我需要添加一些代碼,因此新的腳本的樣子:

package com.hf.arm; 

import org.apache.spark.SparkConf; 
import java.util.Arrays; 
import java.util.List; 
import org.apache.spark.api.java.JavaRDD; 
import org.apache.spark.api.java.JavaSparkContext; 
import org.apache.spark.mllib.fpm.AssociationRules; 
import org.apache.spark.mllib.fpm.FPGrowth; 
import org.apache.spark.mllib.fpm.FPGrowthModel; 

public class App 
{ 
    public static void main(String[] args) 
    { 
    SparkConf conf = new SparkConf().setAppName("FP-growth Example"); 
    JavaSparkContext sc = new JavaSparkContext(conf); 
    JavaRDD<String> data = sc.textFile("/Users/lincolnsmith/Table_csv/sample_fpgrowth.txt"); 
    JavaRDD<List<String>> transactions = data.map(
     new Function<String, List<String>>() { 
     public List<String> call(String line) { 
      String[] parts = line.split(" "); 
      return Arrays.asList(parts); 
     } 
     } 
    ); 
    FPGrowth fpg = new FPGrowth() 
     .setMinSupport(0.2) 
     .setNumPartitions(10); 
    FPGrowthModel<String> model = fpg.run(transactions); 
    for (FPGrowth.FreqItemset<String> itemset: model.freqItemsets().toJavaRDD().collect()) { 
     System.out.println("[" + itemset.javaItems() + "], " + itemset.freq()); 
    } 
    double minConfidence = 0.8; 
    for (AssociationRules.Rule<String> rule 
     : model.generateAssociationRules(minConfidence).toJavaRDD().collect()) { 
     System.out.println(
     rule.javaAntecedent() + " => " + rule.javaConsequent() + ", " + rule.confidence()); 
    } 

    } 
} 

但我得到這個錯誤: 「找不到符號符號:類功能」 顯然我」中號失蹤包java.utils.function,所以我導入此

import java.util.function.Function; 

我的依賴添加到我的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>com.hf.arm</groupId> 
    <artifactId>arm</artifactId> 
    <packaging>jar</packaging> 
    <version>1.0-SNAPSHOT</version> 
    <name>arm</name> 
    <url>http://maven.apache.org</url> 
    <dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>3.8.1</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> <!-- Spark dependency --> 
     <groupId>org.apache.spark</groupId> 
     <artifactId>spark-core_2.10</artifactId> 
     <version>1.5.1</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.spark</groupId> 
    <artifactId>spark-mllib_2.10</artifactId> 
    <version>1.5.1</version> 
    </dependency> 
    <dependency> 
    <groupId>net.sf.m-m-m</groupId> 
    <artifactId>mmm-util-backport-java.util.function</artifactId> 
    <version>1.0.1</version> 
    </dependency> 
    </dependencies> 
<build> 
<plugins> 
    <plugin> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <version>3.3</version> 
     <configuration> 
      <source>1.7</source> 
      <target>1.7</target> 
     </configuration> 
    </plugin> 
</plugins> 
</build> 
</project> 

但現在我不斷得到錯誤輸出:

Lincolns-MacBook-Pro:arm lincolnsmith$ mvn package 
[INFO] Scanning for projects... 
[INFO]                   
[INFO] ------------------------------------------------------------------------ 
[INFO] Building arm 1.0-SNAPSHOT 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ arm --- 
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! 
[INFO] skip non existing resourceDirectory /Users/lincolnsmith/Table_csv/arm/src/main/resources 
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ arm --- 
[INFO] Changes detected - recompiling the module! 
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! 
[INFO] Compiling 1 source file to /Users/lincolnsmith/Table_csv/arm/target/classes 
[INFO] ------------------------------------------------------------- 
[ERROR] COMPILATION ERROR : 
[INFO] ------------------------------------------------------------- 
[ERROR] /Users/lincolnsmith/Table_csv/arm/src/main/java/com/hf/arm/App.java:[6,11] '.' expected 
[ERROR] /Users/lincolnsmith/Table_csv/arm/src/main/java/com/hf/arm/App.java:[6,12] ';' expected 
[ERROR] /Users/lincolnsmith/Table_csv/arm/src/main/java/com/hf/arm/App.java:[6,16] class, interface, or enum expected 
[ERROR] /Users/lincolnsmith/Table_csv/arm/src/main/java/com/hf/arm/App.java:[6,17] class, interface, or enum expected 
[INFO] 4 errors 
[INFO] ------------------------------------------------------------- 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD FAILURE 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 1.241 s 
[INFO] Finished at: 2015-10-14T10:05:13-04:00 
[INFO] Final Memory: 19M/437M 
[INFO] ------------------------------------------------------------------------ 
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project arm: Compilation failure: Compilation failure: 
[ERROR] /Users/lincolnsmith/Table_csv/arm/src/main/java/com/hf/arm/App.java:[6,11] '.' expected 
[ERROR] /Users/lincolnsmith/Table_csv/arm/src/main/java/com/hf/arm/App.java:[6,12] ';' expected 
[ERROR] /Users/lincolnsmith/Table_csv/arm/src/main/java/com/hf/arm/App.java:[6,16] class, interface, or enum expected 
[ERROR] /Users/lincolnsmith/Table_csv/arm/src/main/java/com/hf/arm/App.java:[6,17] class, interface, or enum expected 
[ERROR] -> [Help 1] 
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. 
[ERROR] Re-run Maven using the -X switch to enable full debug logging. 
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles: 
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException 

我找不到這種依賴性不同的行家源,我已經試過清潔和排空my.m2目錄。是否有java.utils.function的不同依賴項源,我可以使用它作爲maven還是其他錯誤?

這裏是我的Java版本:

Lincolns-MacBook-Pro:arm lincolnsmith$ java -version 
java version "1.8.0_51" 
Java(TM) SE Runtime Environment (build 1.8.0_51-b16) 
Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode) 
+0

可能是個愚蠢的問題,但是您是否在編譯JDK 8?同樣在你的pom.xml中,你很難宣稱你的源代碼和/或目標是<1.8?我這樣說是因爲'函數'只能用於1.8 編輯:好的我看你正在使用某種後端口。 –

+0

另一個愚蠢的問題 - 哪裏是java.util.Function的import語句?使用[IDE](https://www.jetbrains.com/idea/download/)可能會更簡單? – ursa

+0

@ DamienO'Reilly他爲較老的JDK使用Function的backport。請發佈您的代碼和POM的簡單示例,我無法複製此內容。 – Tunaki

回答

0

其實你並不需要的backport包作爲功能屬於星火API,而不是Java的API 8。試試這個:

JavaRDD<List<String>> transactions = data.map(
    new org.apache.spark.api.java.function.Function<String, List<String>>() { 
     public List<String> call(String line) throws Exception { 
      String[] parts = line.split(" "); 
      return Arrays.asList(parts); 
     } 
    } 
); 

然後你就可以建立並使用正確的類路徑運行應用程序:

mvn clean compile exec:java -Dexec.mainClass="com.hf.App" 

而且它非常精細(與預期的結果在我的情況):

... 
org.apache.spark.SparkException: A master URL must be set in your configuration 
     at org.apache.spark.SparkContext.<init>(SparkContext.scala:394) 
     ... 
+0

見上面,我做了這個改變。同樣的錯誤。 – user1518003

+0

@ user1518003我已經更新了答案。 – ursa

+0

好吧,現在編譯它,但執行它似乎有問題與函數從org.apache.spark.api.java – user1518003