• 案例 WordCount


    // 创建 Spark 运行配置对象
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("WordCount")
    // 创建 Spark 上下文环境对象(连接对象)
    val sc : SparkContext = new SparkContext(sparkConf)
    // 读取文件数据
    val fileRDD: RDD[String] = sc.textFile("input/word.txt")
    // 将文件中的数据进行分词
    val wordRDD: RDD[String] = fileRDD.flatMap( _.split(" ") )
    // 转换数据结构 word => (word, 1)
    val word2OneRDD: RDD[(String, Int)] = wordRDD.map((_,1))
    // 将转换结构后的数据按照相同的单词进行分组聚合
    val word2CountRDD: RDD[(String, Int)] = word2OneRDD.reduceByKey(_+_)
    // 将数据聚合结果采集到内存中
    val word2Count: Array[(String, Int)] = word2CountRDD.collect()
    // 打印结果
    word2Count.foreach(println)
    //关闭 Spark 连接
    sc.stop()
    

      

    执行过程中,会产生大量的执行日志,如果为了能够更好的查看程序的执行结果,可以在项
    目的 resources 目录中创建 log4j.properties 文件,并添加日志配置信息:
    log4j.rootCategory=ERROR, console
     尚硅谷大数据技术之 Spark 
    —————————————————————————————
    更多 Java –大数据 –前端 –python 人工智能资料下载,可百度访问:尚硅谷官网
    8
    log4j.appender.console=org.apache.log4j.ConsoleAppender
    log4j.appender.console.target=System.err
    log4j.appender.console.layout=org.apache.log4j.PatternLayout
    log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd 
    HH:mm:ss} %p %c{1}: %m%n
    # Set the default spark-shell log level to ERROR. When running the spark-shell, 
    the
    # log level for this class is used to overwrite the root logger's log level, so 
    that
    # the user can have different defaults for the shell and regular Spark apps.
    log4j.logger.org.apache.spark.repl.Main=ERROR
    # Settings to quiet third party logs that are too verbose
    log4j.logger.org.spark_project.jetty=ERROR
    log4j.logger.org.spark_project.jetty.util.component.AbstractLifeCycle=ERROR
    log4j.logger.org.apache.spark.repl.SparkIMain$exprTyper=ERROR
    log4j.logger.org.apache.spark.repl.SparkILoop$SparkILoopInterpreter=ERROR
    log4j.logger.org.apache.parquet=ERROR
    log4j.logger.parquet=ERROR
    # SPARK-9183: Settings to avoid annoying messages when looking up nonexistent 
    UDFs in SparkSQL with Hive support
    log4j.logger.org.apache.hadoop.hive.metastore.RetryingHMSHandler=FATAL
    log4j.logger.org.apache.hadoop.hive.ql.exec.FunctionRegistry=ERROR
    

      

  • 相关阅读:
    ubuntu安装QQ
    Ubuntu 14.04/14.10下安装VMware Workstation
    在Macbook上安装ubuntu
    MacBook Air密码忘了,苹果电脑密码忘了怎么办
    Win7下U盘安装Ubuntu14.04双系统
    linux紧急救援模式
    Standard Library Modules Using Notes
    【LeetCode】136 & 137 & 260
    Python获取脚本所在目录的正确方法【转】
    Autorun a python script after reboot using rc.local
  • 原文地址:https://www.cnblogs.com/huaobin/p/15675622.html
Copyright © 2020-2023  润新知