Spark1.6.3学习01——Quick Start
来源:互联网 发布:js slice方法 mdn 编辑:程序博客网 时间:2024/06/13 07:26
Quick Start 快速入门
- Interactive Analysis with the Spark Shell Spark Shell 的交互式分析
- Basics基础
- More on RDD Operations初识RDD操作
- Caching缓冲
- Self-Contained Applications独立的应用程序
- Where to Go from Here下一步
This tutorial provides a quick introduction to using Spark. We will first introduce the API through Spark’s interactive shell (in Python or Scala), then show how to write applications in Java, Scala, and Python. See the programming guide for a more complete reference.
本教程简要介绍了使用Spark。我们将首先通过Spark的交互式shell(在Python或Scala中)介绍API,然后展示如何在Java,Scala和Python中编写应用程序。有关更完整的参考,请参阅编程指南。
To follow along with this guide, first download a packaged release of Spark from the Spark website. Since we won’t be using HDFS, you can download a package for any version of Hadoop.
要遵循本指南,首先从Spark网站下载Spark的包装版本 。由于我们不会使用HDFS,您可以下载任何版本的Hadoop的软件包。
Interactive Analysis with the Spark Shell
与Spark Shell的交互式分析
Basics 基础
Spark’s shell provides a simple way to learn the API, as well as a powerful tool to analyze data interactively. It is available in either Scala (which runs on the Java VM and is thus a good way to use existing Java libraries) or Python. Start it by running the following in the Spark directory:
SparkShell 提供了一种简单的方式去学习API,同时也是一种强大的交互式数据分析工具。它可用于Scala(它运行在Java VM上,因此是使用现有Java库的好方法)或Python。在Spark目录中运行以下命令启动它:
./bin/spark-shell
Spark’s primary abstraction is a distributed collection of items called a Resilient Distributed Dataset (RDD). RDDs can be created from Hadoop InputFormats (such as HDFS files) or by transforming other RDDs. Let’s make a new RDD from the text of the README file in the Spark source directory:
Spark的主要抽象是称为弹性分布式数据集(RDD)的项目的分布式集合。RDD可以从Hadoop InputFormats(如HDFS文件)创建,也可以通过转换其他RDD来创建。我们从Spark源目录中的README文件的文本中创建一个新的RDD:
scala> val textFile = sc.textFile("README.md")textFile: spark.RDD[String] = spark.MappedRDD@2ee9b6e3
RDDs have actions, which return values, and transformations, which return pointers to new RDDs. Let’s start with a few actions:
RDDS有动作,其返回值,以及转换,这回指向新RDDS。我们从几个动作开始:
scala> textFile.count() // Number of items in this RDDres0: Long = 126scala> textFile.first() // First item in this RDDres1: String = # Apache Spark
Now let’s use a transformation. We will use the filter
transformation to return a new RDD with a subset of the items in the file.
现在我们来使用一个转换。我们将使用filter
转换返回一个新的RDD与文件中的项目的子集。
scala> val linesWithSpark = textFile.filter(line => line.contains("Spark"))linesWithSpark: spark.RDD[String] = spark.FilteredRDD@7dd4af09
We can chain together transformations and actions:
我们可以将转化和行动联系起来:
scala> textFile.filter(line => line.contains("Spark")).count() // How many lines contain "Spark"?res3: Long = 15
More on RDD Operations
更多关于RDD操作
RDD actions and transformations can be used for more complex computations. Let’s say we want to find the line with the most words:
RDD动作和转换可用于更复杂的计算。假设我们想找到最多的单词:
scala> textFile.map(line => line.split(" ").size).reduce((a, b) => if (a > b) a else b)res4: Long = 15
This first maps a line to an integer value, creating a new RDD. reduce
is called on that RDD to find the largest line count. The arguments to map
and reduce
are Scala function literals (closures), and can use any language feature or Scala/Java library. For example, we can easily call functions declared elsewhere. We’ll use Math.max()
function to make this code easier to understand:
这首先将一行映射到一个整数值,创建一个新的RDD。reduce
在RDD上调用最大的行数。该参数map
和reduce
是Scala的函数文本(关闭),并且可以使用任何语言功能或斯卡拉/ Java库。例如,我们可以轻松地调用其他地方声明的函数。我们将使用Math.max()
函数使此代码更容易理解:
scala> import java.lang.Mathimport java.lang.Mathscala> textFile.map(line => line.split(" ").size).reduce((a, b) => Math.max(a, b))res5: Int = 15
One common data flow pattern is MapReduce, as popularized by Hadoop. Spark can implement MapReduce flows easily:
一个常见的数据流模式是由Hadoop推广的MapReduce。Spark可以轻松实现MapReduce流程:
scala> val wordCounts = textFile.flatMap(line => line.split(" ")).map(word => (word, 1)).reduceByKey((a, b) => a + b)wordCounts: spark.RDD[(String, Int)] = spark.ShuffledAggregatedRDD@71f027b8
Here, we combined the flatMap
, map
, and reduceByKey
transformations to compute the per-word counts in the file as an RDD of (String, Int) pairs. To collect the word counts in our shell, we can use the collect
action:
在这里,我们结合了flatMap
,map
和reduceByKey
转换来计算文件中的每个字数作为(String,Int)对的RDD。要在shell中收集字数,我们可以使用该collect
操作:
scala> wordCounts.collect()res6: Array[(String, Int)] = Array((means,1), (under,2), (this,3), (Because,1), (Python,2), (agree,1), (cluster.,1), ...)
Caching
高速缓存
Spark also supports pulling data sets into a cluster-wide in-memory cache. This is very useful when data is accessed repeatedly, such as when querying a small “hot” dataset or when running an iterative algorithm like PageRank. As a simple example, let’s mark our linesWithSpark
dataset to be cached:
Spark还支持将数据集拉入集群范围的内存中缓存。当数据被重复访问时,例如当查询小的“热”数据集或运行迭代算法(如PageRank)时,这是非常有用的。作为一个简单的例子,我们将linesWithSpark
数据集标记为缓存:
scala> linesWithSpark.cache()res7: spark.RDD[String] = spark.FilteredRDD@17e51082scala> linesWithSpark.count()res8: Long = 19scala> linesWithSpark.count()res9: Long = 19
It may seem silly to use Spark to explore and cache a 100-line text file. The interesting part is that these same functions can be used on very large data sets, even when they are striped across tens or hundreds of nodes. You can also do this interactively by connecting bin/spark-shell
to a cluster, as described in the programming guide.
使用Spark浏览和缓存100行文本文件似乎很愚蠢。有趣的是,这些相同的功能可以在非常大的数据集中使用,即使它们在十几个或几百个节点上进行条带化。您还可以通过连接bin/spark-shell
到群集来进行交互操作,如编程指南中所述。
Self-Contained Applications 独立应用
Suppose we wish to write a self-contained application using the Spark API. We will walk through a simple application in Scala (with sbt), Java (with Maven), and Python.
假设我们希望使用Spark API编写一个独立的应用程序。我们将在Scala(使用sbt),Java(与Maven)和Python中通过一个简单的应用程序。
We’ll create a very simple Spark application in Scala–so simple, in fact, that it’s named SimpleApp.scala
:
/* SimpleApp.scala */import org.apache.spark.SparkContextimport org.apache.spark.SparkContext._import org.apache.spark.SparkConfobject SimpleApp { def main(args: Array[String]) { val logFile = "YOUR_SPARK_HOME/README.md" // Should be some file on your system val conf = new SparkConf().setAppName("Simple Application") val sc = new SparkContext(conf) val logData = sc.textFile(logFile, 2).cache() val numAs = logData.filter(line => line.contains("a")).count() val numBs = logData.filter(line => line.contains("b")).count() println("Lines with a: %s, Lines with b: %s".format(numAs, numBs)) }}
Note that applications should define a main()
method instead of extending scala.App
. Subclasses of scala.App
may not work correctly.
请注意,应用程序应该定义一种main()
方法而不是扩展scala.App
。子类scala.App
可能无法正常工作。
This program just counts the number of lines containing ‘a’ and the number containing ‘b’ in the Spark README. Note that you’ll need to replace YOUR_SPARK_HOME with the location where Spark is installed. Unlike the earlier examples with the Spark shell, which initializes its own SparkContext, we initialize a SparkContext as part of the program.
该程序仅计算包含“a”的行数,“Spark”README中包含“b”的数字。请注意,您需要将YOUR_SPARK_HOME替换为安装Spark的位置。与早期的使用Spark Shell进行初始化SparkContext的示例不同,我们将作为程序的一部分初始化一个SparkContext。
We pass the SparkContext constructor a SparkConf object which contains information about our application.
我们传递SparkContext构造函数 SparkConf 对象,其中包含有关我们的应用程序的信息。
Our application depends on the Spark API, so we’ll also include an sbt configuration file, simple.sbt
, which explains that Spark is a dependency. This file also adds a repository that Spark depends on:
我们的应用程序取决于Spark API,所以我们还将包括一个sbt配置文件 simple.sbt
,这说明了Spark是一个依赖关系。该文件还添加了Spark依赖的存储库:
name := "Simple Project"version := "1.0"scalaVersion := "2.10.5"libraryDependencies += "org.apache.spark" %% "spark-core" % "1.6.3"
For sbt to work correctly, we’ll need to layout SimpleApp.scala
and simple.sbt
according to the typical directory structure. Once that is in place, we can create a JAR package containing the application’s code, then use the spark-submit
script to run our program.
# Your directory layout should look like this$ find .../simple.sbt./src./src/main./src/main/scala./src/main/scala/SimpleApp.scala# Package a jar containing your application$ sbt package...[info] Packaging {..}/{..}/target/scala-2.10/simple-project_2.10-1.0.jar# Use spark-submit to run your application$ YOUR_SPARK_HOME/bin/spark-submit \ --class "SimpleApp" \ --master local[4] \ target/scala-2.10/simple-project_2.10-1.0.jar...Lines with a: 46, Lines with b: 23
Where to Go from Here
Congratulations on running your first Spark application!
祝贺您运行您的第一个Spark应用程序!
- For an in-depth overview of the API, start with the Spark programming guide, or see “Programming Guides” menu for other components.
- 有关API的深入概述,请从Spark编程指南开始,或参见“编程指南”菜单中的其他组件。
- For running applications on a cluster, head to the deployment overview.
- 要在群集上运行应用程序,请转到部署概述。
- Finally, Spark includes several samples in the
examples
directory (Scala, Java, Python, R). You can run them as follows: - 最后,Spark包括
examples
目录中的几个示例(Scala, Java, Python, R)。您可以运行它们如下:
# For Scala and Java, use run-example:./bin/run-example SparkPi# For Python examples, use spark-submit directly:./bin/spark-submit examples/src/main/python/pi.py# For R examples, use spark-submit directly:./bin/spark-submit examples/src/main/r/dataframe.R
- Spark1.6.3学习01——Quick Start
- Spark1.1.0 Quick Start (link)
- DOJO Quick Start——学习dojo 第一课
- [batman-adv]学习(2) —— Quick Start
- HBase Shell 命令——Quick Start
- Quick Start 1 —— Desktop Basics
- nunit使用指南之—NUnit Quick Start
- Spark1.6.3学习02——Spark Programming Guide
- Unreal教学(8)——蓝图学习快速开始(Blueprints Quick Start Guide)
- Android自定义控件(1)——Quick Start
- jetty学习1 quick start jetty
- vue 学习笔记一之Quick Start
- spark学习笔记之quick-start
- Quick Start
- Quick Start
- IOS 开发学习二十一 instruments使用 quick start
- Solr-1-Quick Start-按官方学习教程
- spark1.6.1学习笔记01-spark调优
- js一些常用操作
- PTC.Creo.4.0.M020.
- each遍历所有input值判断是否为空
- Lua_介绍_001
- jdbc的prooerties的配置;
- Spark1.6.3学习01——Quick Start
- Python教程百度云全集网盘分享
- 【学习摘记】马士兵Servlet&JSP_课时25-28_usebean
- 数据库解锁的sql
- C++中srand函数与rand函数产生一定范围内的随机数
- Android在自己的应用中启动第三方应用
- 数据库表空间不足
- C/C++ — 实现日期计算器
- Android应用如何适配不同的屏幕