zoukankan      html  css  js  c++  java
  • spark配置(2)

    进入交互界面

    1. ./bin/spark-shell

    创建textFile,从本地文件,

    1. val textFile = sc.textFile("file:///usr/local/spark/README.md")

    从HDFS读取,

    1. scala> val textFile = sc.textFile("input/yarn-site.xml")

    1. scala> textFile.count() // RDD 中的 item 数量,对于文本文件,就是总行数

    1. scala> textFile.first() // RDD 中的第一个 item,对于文本文件,就是第一行内容

    1. scala>  val linesWithSpark = textFile.filter(line => line.contains("Spark"))
    1. scala> linesWithSpark.count()

    1. textFile.filter(line => line.contains("Spark")).count()

    RDD的更多

    1. textFile.map(line => line.split(" ").size).reduce((a, b) => if (a > b) a else b)

    将每一行内容 map 为一个整数,这将创建一个新的 RDD,并在这个 RDD 中执行 reduce 操作,找到最大的数。

    map()、reduce() 中的参数是 Scala 的函数字面量(function literals,也称为闭包 closures),并且可以使用语言特征或 Scala/Java 的库。

    例如,通过使用 Math.max() 函数(需要导入 Java 的 Math 库),可以使上述代码更容易理解:


    1. import java.lang.Math
      textFile
      .map(line => line.split(" ").size).reduce((a, b) => Math.max(a, b))

    wordcount

    1. val wordCounts = textFile.flatMap(line => line.split(" ")).map(word => (word, 1)).reduceByKey((a, b) => a + b)  
       

    2. wordCounts.collect()  




    参考/转载:http://www.powerxing.com/spark-quick-start-guide/ 





  • 相关阅读:
    Trapping Rain Water
    Construct Binary Tree from Preorder and Inorder Traversal
    Flatten Binary Tree to Linked List
    Permutations II
    Unique Paths II
    Path Sum II
    Unique Binary Search Trees II
    evdev module-----uinput.py
    evdev module-----events.py
    evdev module-----device.py
  • 原文地址:https://www.cnblogs.com/iathena/p/5615740.html
Copyright © 2011-2022 走看看