Java,Python,Scala比较(三)wordcount

简介:   众所周知,wordcount在大数据中的地位相当于helloworld在各种编程语言中的地位。本文并不分析wordcount的计算方法,而是直接给出代码,目的是为了比较Spark中Java,Python,Scala的区别。

  众所周知,wordcount在大数据中的地位相当于helloworld在各种编程语言中的地位。本文并不分析wordcount的计算方法,而是直接给出代码,目的是为了比较Spark中Java,Python,Scala的区别。
  显然,Java写法较为复杂,Python简单易懂,Scala是Spark的原生代码,故即为简洁。
Java完整代码:

import java.util.Arrays;
import java.util.Iterator;
import org.apache.spark.SparkConf; 
import org.apache.spark.api.java.JavaPairRDD; 
import org.apache.spark.api.java.JavaRDD; 
import org.apache.spark.api.java.JavaSparkContext; 
import org.apache.spark.api.java.function.FlatMapFunction; 
import org.apache.spark.api.java.function.Function2; 
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.api.java.function.VoidFunction;
import scala.Tuple2;
public class wordcount {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf().setMaster("local").setAppName("wc");
        JavaSparkContext sc = new JavaSparkContext(conf);
        //read a txtfile
        JavaRDD<String> text = sc.textFile("/home/vagrant/speech.txt");
        //split(" ")
        JavaRDD<String> words = text.flatMap(new FlatMapFunction<String, String>() {
            private static final long serialVersionUID = 1L;
            @Override
            public Iterator<String> call(String line) throws Exception {
                return Arrays.asList(line.split(" ")).iterator();
            }
        });
        //word => (word,1)
        JavaPairRDD<String,Integer> counts=words.mapToPair(
                new PairFunction<String, String, Integer>() {
                    public Tuple2<String, Integer> call(String s) throws Exception {
                        return new Tuple2(s, 1);
                    }
                }
        );
        //reduceByKey
        JavaPairRDD <String,Integer> results=counts.reduceByKey(
                new Function2<Integer, Integer, Integer>() {
                    public Integer call(Integer v1, Integer v2) throws Exception {
                        return v1 + v2;
                    }
                }
        ) ;
        //print
        results.foreach(new VoidFunction<Tuple2<String, Integer>>(){
              @Override
              public void call(Tuple2<String, Integer> t) throws Exception { 
                              System.out.println("("+t._1()+":"+t._2()+")");
              }
              });           
    }
}

Pyspark完整代码:

# Imports the PySpark libraries
from pyspark import SparkConf, SparkContext  
# Configure the Spark context to give a name to the application
sparkConf = SparkConf().setAppName("MyWordCounts")
sc = SparkContext(conf = sparkConf)
# The text file containing the words to count (this is the Spark README file)
textFile = sc.textFile('/home/vagrant/speech.txt')
# The code for counting the words (note that the execution mode is lazy)
# Uses the same paradigm Map and Reduce of Hadoop, but fully in memory
wordCounts = textFile.flatMap(lambda line: line.split()).map(lambda word: (word, 1)).reduceByKey(lambda a, b: a+b)
# Executes the DAG (Directed Acyclic Graph) for counting and collecting the result
for wc in wordCounts.collect():
    print(wc)

Scala完整代码:

import org.apache.spark.{SparkContext,SparkConf}

object test{
  def main(args:Array[String]){
    val sparkConf = new SparkConf().setMaster("local").setAppName("MyWordCounts")
    val sc = new SparkContext(sparkConf)
    sc.textFile("/home/vagrant/speech.txt").flatMap(_.split(' ')).map((_,1)).reduceByKey(_+_).foreach(println)
  }
}



本次分享到此结束,欢迎大家批评与交流~~

目录
相关文章
|
8天前
|
前端开发 Java Go
开发语言详解(python、java、Go(Golong)。。。。)
开发语言详解(python、java、Go(Golong)。。。。)
|
1月前
|
SQL Java 关系型数据库
在Python中编写Java数据库驱动是不可能的
在Python中编写Java数据库驱动是不可能的
|
1月前
|
人工智能 Java 开发者
Python与Java:两大编程语言的联系与区别
Python与Java:两大编程语言的联系与区别
36 2
|
1月前
|
Java API PHP
多多关键字API php java Python
多多关键字API接口广泛应用于商家进行市场分析、竞品分析、关键词优化等场景。商家可以通过分析关键词数据,了解用户需求,制定针对性的营销策略,提高产品的曝光率和转化率。
|
2月前
|
机器学习/深度学习 人工智能 Java
python与java的应用场景区别
python与java的应用场景区别
42 6
|
2月前
|
机器学习/深度学习 人工智能 Java
Java和Python区别
Java和Python区别
22 1
Java和Python区别
|
2月前
|
Scala 容器
Scala学习--day04--集合、常用方法、案例实操 - WordCount TopN、不同省份的商品点击排行
Scala学习--day04--集合、常用方法、案例实操 - WordCount TopN、不同省份的商品点击排行
70 2
|
3月前
|
Java Python
分别使用java和python打印99乘法表
【1月更文挑战第17天】分别使用java和python打印99乘法表
26 0
|
3月前
|
分布式计算 Java Scala
Spark编程语言选择:Scala、Java和Python
Spark编程语言选择:Scala、Java和Python
Spark编程语言选择:Scala、Java和Python
|
Java Python 数据挖掘
Java之调用Python代码
  对于一个数据分析方面的工作者来说,最熟悉的语言无疑就是Python了。对于软件开发者而言,Java又是极其重要的工具。那么,一个很自然的问题就是,我们能够在Java中调用Python代码吗?   想要在Java中调用Python代码,有以下两个办法: 直接通过Runtime进行调用 调用Jython 在Java中如果需要调用第三方程序,可以直接通过Runtime实现,这也是最直接最粗暴的做法,粒度更加粗糙,效率较高,需要安装Python软件。
2732 0