spark mllib 应用程序开发及提交到spark集群运行--入门

来源:互联网 发布:网络音乐最新排行榜 编辑:程序博客网 时间:2024/06/08 00:13

一、程序开发

Dependencies

MLlib uses the linear algebra package Breeze, which depends on netlib-java for optimised numerical processing. If natives libraries1 are not available at runtime, you will see a warning message and a pure JVM implementation will be used instead.

Due to licensing issues with runtime proprietary binaries, we do not include netlib-java’s native proxies by default. To configure netlib-java / Breeze to use system optimised binaries, include com.github.fommil.netlib:all:1.1.2 (or build Spark with -Pnetlib-lgpl) as a dependency of your project and read the netlib-java documentation for your platform’s additional installation instructions.

如上是官网给出的开发spark mllib需要的依赖,由于我阅读的不认真,本以为在pom中加依赖com.github.fommil.netlib:all:1.1.2就万事大吉了。我用官方给出的FP-growth为例(代码和输入数据在最下边),输入文件也是官方给出的,于是我把输入文件放到了hdfs上,文件名是LR.avsc。我将程序粘贴到我的eclipse中,在pom中加了如下的依赖(见下图)结果输出到控制台上测试通过


eclipse运行结果图


二、提交到spark集群

于是我兴高采烈的用maven打包,将jar包上传到spark client,执行spark-submit命令


  在yarn的日志中出现如下的错误


classNotFoundException错误,我检查了自己的全类名,没有错误。只好再看官方文档,发现最后还有这么一句话呢

and read the netlib-java documentation for your platform’s additional installation instructions.

于是我看到the netlib-java documentation文档里讲述了如何安装netlib-java到centos,当然我选择依赖的方式,但是有个重要的前提就是linux系统中必须要安装libgfortran,而且每个节点都得安装,命令如下

centos use     sudo yum install gcc-gfortran

ubuntu use     sudo apt-get install libgfortran3

 

安装成功后,再运行spark-submit命令,运行成功。输出结果在yarn日志中


   解释一下后加的那个依赖的作用:mllib使用了线性代数包Breeze,而这包依赖netlib-java,所以加入netlib-java依赖顺利成章了,而netlib-java又需要 native Fortran routines,所以就要在各个节点安装 the gfortran runtime library

  程序提交成功,顿时觉得晴空万里(虽然我在屋里,而且已经是下午五点),其实我想表达的是,当你认真去做你喜欢做的事情,又在每一天有点小收获的时候,那种心情真的无法言表。就像大学里的acm生活,虽然生活单一,但是每当看到代码ac的那刻,总是会美美的激动一会,永远不会忘记acm竞赛带给我的压力和快乐。

附上代码和测试数据

public static void main(String[] args) {SparkConf conf = new SparkConf().setAppName("FP-growth Example").setMaster("local");JavaSparkContext sc = new JavaSparkContext(conf);// $example on$JavaRDD<String> data = sc.textFile(args[0]);JavaRDD<List<String>> transactions = data.map(new Function<String, List<String>>() {public List<String> call(String line) {String[] parts = line.trim().split(" ");return Arrays.asList(parts);}});//支持度设为0.2     置信度设为0.8FPGrowth 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());}// $example off$}
r z h k pz y x w v u t ss x o n rx z y m t s q ezx z y r q t p




0 0