spark 2.4 java8 hello world

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spark 2.4 java8 hello world相关的知识,希望对你有一定的参考价值。

download JDK 8, extract and add to .bashrc:

export JAVA_HOME=/home/bonelee/jdk1.8.0_211
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
export JRE_HOME=$JAVA_HOME/jre

download spark, unzip. and run:

 

./bin/spark-submit ~/src_test/spark_hello.py

 

spark_hello.py :

from pyspark.context import SparkContext
from pyspark.conf import SparkConf

sc = SparkContext(conf=SparkConf().setAppName("mnist_parallelize"))
text_file = sc.textFile("file:///tmp/test.txt")
counts = text_file.flatMap(lambda line: line.split(" "))              .map(lambda word: (word, 1))              .reduceByKey(lambda a, b: a + b)
print(counts.collect())

 

/tmp/test.txt

text_file = sc.textFile("hdfs://...")
counts = text_file.flatMap(lambda line: line.split(" "))              .map(lambda word: (word, 1))                           .reduceByKey(lambda a, b: a + b)
                          counts.saveAsTextFile("hdfs://...")

 

output:

[(‘100‘, 1), (‘text_file‘, 1), (‘=‘, 2), (‘counts‘, 1), (‘text_file.flatMap(lambda‘, 1), (‘line.split("‘, 1), (‘"))‘, 1), (‘‘, 65), (‘word:‘, 1), (‘(word,‘, 1), (‘1))‘, 1), (‘b:‘, 1), (‘sc.textFile("hdfs://...")‘, 1), (‘line:‘, 1), (‘\\‘, 2), (‘.map(lambda‘, 1), (‘.reduceByKey(lambda‘, 1), (‘a,‘, 1), (‘a‘, 1), (‘+‘, 1), (‘b)‘, 1), (‘counts.saveAsTextFile("hdfs://...")‘, 1)]

 

以上是关于spark 2.4 java8 hello world的主要内容,如果未能解决你的问题,请参考以下文章

Spark 2.4 standalone 部署

Spark 2.4新特性概述

spark2.x由浅入深深到底系列六之RDD 支持java8 lambda表达式

如何使用用户提供的 Hadoop 正确配置 Spark 2.4

spark jdk8 单词统计示例

Spark 2.4 上带有字典的 UDF