zoukankan      html  css  js  c++  java
  • Spark操作实战

    1. local模式

    $SPARK_HOME/bin/spark-shell --master local
    import org.apache.log4j.{Level,Logger}                     // 导入java log4j的日志相关类
    Logger.getLogger("org.apache.spark").setLevel(Level.WARN)  //设置日志级别
    val data= sc.textFile("file:///home/workspace/software/spark-2.0.0/README.md")
    data.collect   // 显示所有内容
    data.first     // 显示第一条记录内容,本例中是String
    data.take(0)   // 显示第一条内容,但是是Array
    val filterdData=data.filter(line=>line.contains("Spark"))                        //对数据集进行过滤
    filterdData.collect   //如果数据量比较大的话,该方法可能会占用很大的内存,要慎用
    filterdData.count     //行数统计
    filterdData.map(line=>line.split(" ").size).reduce((a,b)=>if (a>b) a else b)     //获取单词数最多的行
    filterdData.map(line=>line.split(" ").size).reduce((a,b)=>Math.max(a,b))         //获取单词数最多的行
    val  countResult=data.flatMap(line=>line.split(" ")).map(word=>(word,1)).reduceByKey((a,b)=>a+b)  //单词个数统计  
    countResult.collect       // 显示统计结果,可能比较消耗内存哦
    data.map(line=>line.split(" ").size).top(3)     //获取单词个数排名前三的统计结果
    
    data.count    //cache前的行数统计
    data.cache    //设置data为内存缓存,注意RDD.cache()与RDD.persist()的区别,可用RDD.unpersist()取消缓存的rdd
    data.count    //cache后的行数统计
    
    data.saveAsTextFile("file:///home/workspace/countresult") //保存结果到文件系统/home/workspace/countresult文件夹下面

    2. Spark standalone模式

     先上传文件到hdfs

    hdfs dfs -put /home/workspace/software/spark-2.0.0/README.md  /

    spark操作

    $SPARK_HOME/bin/spark-shell --master spark://192.168.1.193:7077
    import org.apache.log4j.{Level,Logger}                     // 导入java log4j的日志相关类
    Logger.getLogger("org.apache.spark").setLevel(Level.WARN)  //设置日志级别
    val data= sc.textFile("/README.md")
    data.collect   // 显示所有内容
    data.first     // 显示第一条记录内容,本例中是String
    data.take(0)   // 显示第一条内容,但是是Array
    val filterdData=data.filter(line=>line.contains("Spark"))                        //对数据集进行过滤
    filterdData.collect   //如果数据量比较大的话,该方法可能会占用很大的内存,要慎用
    filterdData.count     //行数统计
    filterdData.map(line=>line.split(" ").size).reduce((a,b)=>if (a>b) a else b)     //获取单词数最多的行
    filterdData.map(line=>line.split(" ").size).reduce((a,b)=>Math.max(a,b))         //获取单词数最多的行
    val  countResult=data.flatMap(line=>line.split(" ")).map(word=>(word,1)).reduceByKey((a,b)=>a+b)  //单词个数统计  
    countResult.collect       // 显示统计结果,可能比较消耗内存哦
    data.map(line=>line.split(" ").size).top(3)     //获取单词个数排名前三的统计结果
    
    data.count    //cache前的行数统计
    data.cache    //设置data为内存缓存,注意RDD.cache()与RDD.persist()的区别,可用RDD.unpersist取消缓存的RDD
    data.count    //cache后的行数统计
    
    data.saveAsTextFile("/countresult") //保存结果到hdfs的/countresult文件夹下面

    最后保存的结果为:

    3. yarn模式

    $SPARK_HOME/bin/spark-shell --master yarn
    import org.apache.log4j.{Level,Logger}                     // 导入java log4j的日志相关类
    Logger.getLogger("org.apache.spark").setLevel(Level.WARN)  //设置日志级别
    val data= sc.textFile("/README.md")
    data.collect   // 显示所有内容
    data.first     // 显示第一条记录内容,本例中是String
    data.take(0)   // 显示第一条内容,但是是Array
    val filterdData=data.filter(line=>line.contains("Spark"))                        //对数据集进行过滤
    filterdData.collect   //如果数据量比较大的话,该方法可能会占用很大的内存,要慎用
    filterdData.count     //行数统计
    filterdData.map(line=>line.split(" ").size).reduce((a,b)=>if (a>b) a else b)     //获取单词数最多的行
    filterdData.map(line=>line.split(" ").size).reduce((a,b)=>Math.max(a,b))         //获取单词数最多的行
    val  countResult=data.flatMap(line=>line.split(" ")).map(word=>(word,1)).reduceByKey((a,b)=>a+b)  //单词个数统计  
    countResult.collect       // 显示统计结果,可能比较消耗内存哦
    data.map(line=>line.split(" ").size).top(3)     //获取单词个数排名前三的统计结果
    
    data.count    //cache前的行数统计
    data.cache    //设置data为内存缓存,注意RDD.cache()与RDD.persist()的区别
    data.count    //cache后的行数统计
    
    data.saveAsTextFile("/countresult") //保存结果到hdfs的/countresult文件夹下面

    还有一种mesos部署模式,因为环境没有做部署,没有做测试。

    spark-shell参数列表:

    [druid@palo101 workspace]$ spark-shell --help
    Usage: ./bin/spark-shell [options]
    
    Options:
      --master MASTER_URL         spark://host:port, mesos://host:port, yarn, or local.
      --deploy-mode DEPLOY_MODE   Whether to launch the driver program locally ("client") or
                                  on one of the worker machines inside the cluster ("cluster")
                                  (Default: client).
      --class CLASS_NAME          Your application's main class (for Java / Scala apps).
      --name NAME                 A name of your application.
      --jars JARS                 Comma-separated list of local jars to include on the driver
                                  and executor classpaths.
      --packages                  Comma-separated list of maven coordinates of jars to include
                                  on the driver and executor classpaths. Will search the local
                                  maven repo, then maven central and any additional remote
                                  repositories given by --repositories. The format for the
                                  coordinates should be groupId:artifactId:version.
      --exclude-packages          Comma-separated list of groupId:artifactId, to exclude while
                                  resolving the dependencies provided in --packages to avoid
                                  dependency conflicts.
      --repositories              Comma-separated list of additional remote repositories to
                                  search for the maven coordinates given with --packages.
      --py-files PY_FILES         Comma-separated list of .zip, .egg, or .py files to place
                                  on the PYTHONPATH for Python apps.
      --files FILES               Comma-separated list of files to be placed in the working
                                  directory of each executor.
    
      --conf PROP=VALUE           Arbitrary Spark configuration property.
      --properties-file FILE      Path to a file from which to load extra properties. If not
                                  specified, this will look for conf/spark-defaults.conf.
    
      --driver-memory MEM         Memory for driver (e.g. 1000M, 2G) (Default: 1024M).
      --driver-java-options       Extra Java options to pass to the driver.
      --driver-library-path       Extra library path entries to pass to the driver.
      --driver-class-path         Extra class path entries to pass to the driver. Note that
                                  jars added with --jars are automatically included in the
                                  classpath.
    
      --executor-memory MEM       Memory per executor (e.g. 1000M, 2G) (Default: 1G).
    
      --proxy-user NAME           User to impersonate when submitting the application.
                                  This argument does not work with --principal / --keytab.
    
      --help, -h                  Show this help message and exit.
      --verbose, -v               Print additional debug output.
      --version,                  Print the version of current Spark.
    
     Spark standalone with cluster deploy mode only:
      --driver-cores NUM          Cores for driver (Default: 1).
    
     Spark standalone or Mesos with cluster deploy mode only:
      --supervise                 If given, restarts the driver on failure.
      --kill SUBMISSION_ID        If given, kills the driver specified.
      --status SUBMISSION_ID      If given, requests the status of the driver specified.
    
     Spark standalone and Mesos only:
      --total-executor-cores NUM  Total cores for all executors.
    
     Spark standalone and YARN only:
      --executor-cores NUM        Number of cores per executor. (Default: 1 in YARN mode,
                                  or all available cores on the worker in standalone mode)
    
     YARN-only:
      --driver-cores NUM          Number of cores used by the driver, only in cluster mode
                                  (Default: 1).
      --queue QUEUE_NAME          The YARN queue to submit to (Default: "default").
      --num-executors NUM         Number of executors to launch (Default: 2).
                                  If dynamic allocation is enabled, the initial number of
                                  executors will be at least NUM.
      --archives ARCHIVES         Comma separated list of archives to be extracted into the
                                  working directory of each executor.
      --principal PRINCIPAL       Principal to be used to login to KDC, while running on
                                  secure HDFS.
      --keytab KEYTAB             The full path to the file that contains the keytab for the
                                  principal specified above. This keytab will be copied to
                                  the node running the Application Master via the Secure
                                  Distributed Cache, for renewing the login tickets and the
                                  delegation tokens periodically.
  • 相关阅读:
    马尔科夫过程的CKS方程的推导
    科研的一些工具和想法
    读研究生后的一些想法
    读过的一些好书以后可做参考
    latex学习笔记
    机器人工程师学习要求
    《MySQL实战45讲》(8-15)笔记
    《MySQL实战45讲》(1-7)笔记
    java11运行javaFX项目
    ClickHouse入门笔记
  • 原文地址:https://www.cnblogs.com/lenmom/p/10374697.html
Copyright © 2011-2022 走看看