Spark-Shell操作

Spark-Shell操作

spark-shell简述

​ spark-shell是REPL(Read-Eval-Print Loop,交互式解释器),它为我们提供了交互式执行环境,表达式计算完成以后就会立即输出结果,而不必等到整个程序运行完毕,因此可以及时查看中间结果并对程序进行修改,这样可以在很大程度上提升程序开发效率。spark-shell支持ScalaPythonSpark框架使用Scala语言开发的,使用spark-shell命令会默认进入Scala的交互式执行环境。如果要进入Python的交互式执行环境,则需要执行pyspark命令。

spark运行模式简述

在Linux终端中运行spark-shell命令,就可以启动进入spark-shell交互式执行环境。spark-shell命令及其常用参数如下:

./bin/spark-shell  --master <master-url>

Spark的运行环境取决于传递给SparkContext的的值。可以是表中的任一形式。

含义
local 使用一个Worker线程本地化运行Spark(完全不并行)
local[*] 使用与逻辑CPU个数相同数量的线程来本地化运行Spark(“逻辑CPU个数”等于“物理CPU个数”乘以“每个物理CPU包含的CPU核数”)
local[K]
spark://HOST:PORT
yarn-client
yarn-cluster
mesos://HOST:PORT

在Spark中采用Local模型启动spark-shell的命令主要包含以下参数:

  • master

    ​ 这个参数表示当前的spark-shell要连接到哪个Master,如果是local[*],就是使用Local模式(单机模式)启动spark-shell,中括号内的星号表示需要几个CPU核心(Core),也就是启动几个线程模拟Spark集群;

  • jars

    这个参数用于把相关的JAR包添加到CLASSPATH中,如果有多个Jar包,可以使用逗号分隔符连接它们。

Spark-Shell命令

帮助命令

./bin/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. File paths of these files
                              in executors can be accessed via SparkFiles.get(fileName).

  --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.

例子

进入spark程序根目录

cd /use/local/spark

使用Local模式,在4个CPU核心(Core)上运行spark-shell,命令如下:

./bin/spark-shell --master local[4]

在CLASSPATH中添加code.jar

 ./bin/spark-shell --master local[4] --jars code.jar

spark-shell操作

当使用spark-shell命令没有带上任何参数时,默认使用Local[*]模式启动进入spark-shell交互式执行环境。

spark-shell本身就是一个Driver,Driver会生成一个SparkContext对象。

SparkContext

Spark context available as 'sc' (master = local[*], app id = local-1617853048872).

SparkSession

Spark session available as 'spark'.

启动spark-shell

./bin/spark-shell

启动界面

[root@hadoop spark]# ./bin/spark-shell 
Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).
21/04/08 11:37:27 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
21/04/08 11:37:34 WARN ObjectStore: Failed to get database global_temp, returning NoSuchObjectException
Spark context Web UI available at http://192.168.174.134:4040
Spark context available as 'sc' (master = local[*], app id = local-1617853048872).
Spark session available as 'spark'.
Welcome to
      ____              __
     / __/__  ___ _____/ /__
    _\ \/ _ \/ _ `/ __/  '_/
   /___/ .__/\_,_/_/ /_/\_\   version 2.2.0
      /_/
         
Using Scala version 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_152)
Type in expressions to have them evaluated.
Type :help for more information.

scala> :quit
[root@hadoop spark]# 

退出spark-shell

命令退出

:quit

快捷键退出

Ctrl+D

代码部署到服务器运行

  1. 上传Jar包

  2. 进入Spark安装目录

  3. 执行以下命令

    sh ./bin/spark-submit --class com.spark.WordCount /home/myjar/hotel.jar
    

你可能感兴趣的:(spark,大数据,spark,大数据,scala)