跟我一起数据挖掘(22)——spark入门

Spark简介

Spark是UC Berkeley AMP lab所开源的类Hadoop MapReduce的通用的并行,Spark,拥有Hadoop MapReduce所具有的优点;但不同于MapReduce的是Job中间输出结果可以保存在内存中,从而不再需要读写HDFS,因此Spark能更好地适用于数据挖掘与机器学习等需要迭代的map reduce的算法。

Spark优点

Spark是基于内存,是云计算领域的继Hadoop之后的下一代的最热门的通用的并行计算框架开源项目,尤其出色的支持Interactive Query、流计算、图计算等。 
Spark在机器学习方面有着无与伦比的优势,特别适合需要多次迭代计算的算法。同时Spark的拥有非常出色的容错和调度机制,确保系统的稳定运行,Spark目前的发展理念是通过一个计算框架集合SQL、Machine Learning、Graph Computing、Streaming Computing等多种功能于一个项目中,具有非常好的易用性。目前SPARK已经构建了自己的整个大数据处理生态系统,如流处理、图技术、机器学习、NoSQL查询等方面都有自己的技术,并且是Apache顶级Project,可以预计的是2014年下半年在社区和商业应用上会有爆发式的增长。Spark最大的优势在于速度,在迭代处理计算方面比Hadoop快100倍以上;Spark另外一个无可取代的优势是:“One Stack to rule them all”,Spark采用一个统一的技术堆栈解决了云计算大数据的所有核心问题,这直接奠定了其一统云计算大数据领域的霸主地位;

下图是使用逻辑回归算法的使用时间:

Spark目前支持scala、python、JAVA编程。

作为Spark的原生语言,scala是开发Spark应用程序的首选,其优雅简洁的代码,令开发过mapreduce代码的码农感觉象是上了天堂。

可以架构在hadoop之上,读取hadoop、hbase数据。

spark的部署方式

1、standalone模式,即独立模式,自带完整的服务,可单独部署到一个集群中,无需依赖任何其他资源管理系统。

2、Spark On Mesos模式。这是很多公司采用的模式,官方推荐这种模式(当然,原因之一是血缘关系)。

3、Spark On YARN模式。这是一种最有前景的部署模式。

spark本机安装

流程:进入linux->安装JDK->安装scala->安装spark。

JDK的安装和配置(略)。

安装scala,进入http://www.scala-lang.org/download/下载。

下载后解压缩。

tar zxvf scala-2.11.6.tgz
//改名
mv scala-2.11.6 scala
//设置配置
export SCALA_HOME=/home/hadoop/software/scala
export PATH=$SCALA_HOME/bin;$PATH

source /etc/profile

scala -version
Scala code runner version 2.11.6 -- Copyright 2002-2013, LAMP/EPFL

scala设置成功。

http://spark.apache.org/downloads.html下载spark并安装。

下载后解压缩。

进入$SPARK_HOME/bin,运行

./run-example SparkPi

运行结果

Spark assembly has been built with Hive, including Datanucleus jars on classpath
Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
15/03/14 23:41:40 INFO SparkContext: Running Spark version 1.3.0
15/03/14 23:41:40 WARN Utils: Your hostname, localhost.localdomain resolves to a loopback address: 127.0.0.1; using 192.168.126.147 instead (on interface eth0)
15/03/14 23:41:40 WARN Utils: Set SPARK_LOCAL_IP if you need to bind to another address
15/03/14 23:41:41 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
15/03/14 23:41:41 INFO SecurityManager: Changing view acls to: hadoop
15/03/14 23:41:41 INFO SecurityManager: Changing modify acls to: hadoop
15/03/14 23:41:41 INFO SecurityManager: SecurityManager: authentication disabled; ui acls disabled; users with view permissions: Set(hadoop); users with modify permissions: Set(hadoop)
15/03/14 23:41:42 INFO Slf4jLogger: Slf4jLogger started
15/03/14 23:41:42 INFO Remoting: Starting remoting
15/03/14 23:41:42 INFO Remoting: Remoting started; listening on addresses :[akka.tcp://sparkDriver@192.168.126.147:60926]
15/03/14 23:41:42 INFO Utils: Successfully started service 'sparkDriver' on port 60926.
15/03/14 23:41:42 INFO SparkEnv: Registering MapOutputTracker
15/03/14 23:41:43 INFO SparkEnv: Registering BlockManagerMaster
15/03/14 23:41:43 INFO DiskBlockManager: Created local directory at /tmp/spark-285a6144-217c-442c-bfde-4b282378ac1e/blockmgr-f6cb0d15-d68d-4079-a0fe-9ec0bf8297a4
15/03/14 23:41:43 INFO MemoryStore: MemoryStore started with capacity 265.1 MB
15/03/14 23:41:43 INFO HttpFileServer: HTTP File server directory is /tmp/spark-96b3f754-9cad-4ef8-9da7-2a2c5029c42a/httpd-b28f3f6d-73f7-46d7-9078-7ba7ea84ca5b
15/03/14 23:41:43 INFO HttpServer: Starting HTTP Server
15/03/14 23:41:43 INFO Server: jetty-8.y.z-SNAPSHOT
15/03/14 23:41:43 INFO AbstractConnector: Started SocketConnector@0.0.0.0:42548
15/03/14 23:41:43 INFO Utils: Successfully started service 'HTTP file server' on port 42548.
15/03/14 23:41:43 INFO SparkEnv: Registering OutputCommitCoordinator
15/03/14 23:41:43 INFO Server: jetty-8.y.z-SNAPSHOT
15/03/14 23:41:43 INFO AbstractConnector: Started SelectChannelConnector@0.0.0.0:4040
15/03/14 23:41:43 INFO Utils: Successfully started service 'SparkUI' on port 4040.
15/03/14 23:41:43 INFO SparkUI: Started SparkUI at http://192.168.126.147:4040
15/03/14 23:41:44 INFO SparkContext: Added JAR file:/home/hadoop/software/spark-1.3.0-bin-hadoop2.4/lib/spark-examples-1.3.0-hadoop2.4.0.jar at http://192.168.126.147:42548/jars/spark-examples-1.3.0-hadoop2.4.0.jar with timestamp 1426347704488
15/03/14 23:41:44 INFO Executor: Starting executor ID <driver> on host localhost
15/03/14 23:41:44 INFO AkkaUtils: Connecting to HeartbeatReceiver: akka.tcp://sparkDriver@192.168.126.147:60926/user/HeartbeatReceiver
15/03/14 23:41:44 INFO NettyBlockTransferService: Server created on 39408
15/03/14 23:41:44 INFO BlockManagerMaster: Trying to register BlockManager
15/03/14 23:41:44 INFO BlockManagerMasterActor: Registering block manager localhost:39408 with 265.1 MB RAM, BlockManagerId(<driver>, localhost, 39408)
15/03/14 23:41:44 INFO BlockManagerMaster: Registered BlockManager
15/03/14 23:41:45 INFO SparkContext: Starting job: reduce at SparkPi.scala:35
15/03/14 23:41:45 INFO DAGScheduler: Got job 0 (reduce at SparkPi.scala:35) with 2 output partitions (allowLocal=false)
15/03/14 23:41:45 INFO DAGScheduler: Final stage: Stage 0(reduce at SparkPi.scala:35)
15/03/14 23:41:45 INFO DAGScheduler: Parents of final stage: List()
15/03/14 23:41:45 INFO DAGScheduler: Missing parents: List()
15/03/14 23:41:45 INFO DAGScheduler: Submitting Stage 0 (MapPartitionsRDD[1] at map at SparkPi.scala:31), which has no missing parents
15/03/14 23:41:45 INFO MemoryStore: ensureFreeSpace(1848) called with curMem=0, maxMem=278019440
15/03/14 23:41:45 INFO MemoryStore: Block broadcast_0 stored as values in memory (estimated size 1848.0 B, free 265.1 MB)
15/03/14 23:41:45 INFO MemoryStore: ensureFreeSpace(1296) called with curMem=1848, maxMem=278019440
15/03/14 23:41:45 INFO MemoryStore: Block broadcast_0_piece0 stored as bytes in memory (estimated size 1296.0 B, free 265.1 MB)
15/03/14 23:41:45 INFO BlockManagerInfo: Added broadcast_0_piece0 in memory on localhost:39408 (size: 1296.0 B, free: 265.1 MB)
15/03/14 23:41:45 INFO BlockManagerMaster: Updated info of block broadcast_0_piece0
15/03/14 23:41:45 INFO SparkContext: Created broadcast 0 from broadcast at DAGScheduler.scala:839
15/03/14 23:41:45 INFO DAGScheduler: Submitting 2 missing tasks from Stage 0 (MapPartitionsRDD[1] at map at SparkPi.scala:31)
15/03/14 23:41:45 INFO TaskSchedulerImpl: Adding task set 0.0 with 2 tasks
15/03/14 23:41:45 INFO TaskSetManager: Starting task 0.0 in stage 0.0 (TID 0, localhost, PROCESS_LOCAL, 1340 bytes)
15/03/14 23:41:45 INFO TaskSetManager: Starting task 1.0 in stage 0.0 (TID 1, localhost, PROCESS_LOCAL, 1340 bytes)
15/03/14 23:41:45 INFO Executor: Running task 1.0 in stage 0.0 (TID 1)
15/03/14 23:41:45 INFO Executor: Running task 0.0 in stage 0.0 (TID 0)
15/03/14 23:41:45 INFO Executor: Fetching http://192.168.126.147:42548/jars/spark-examples-1.3.0-hadoop2.4.0.jar with timestamp 1426347704488
15/03/14 23:41:45 INFO Utils: Fetching http://192.168.126.147:42548/jars/spark-examples-1.3.0-hadoop2.4.0.jar to /tmp/spark-db1e742b-020f-4db1-9ee3-f3e2d90e1bc2/userFiles-96c6db61-e95e-4f9e-a6c4-0db892583854/fetchFileTemp5600234414438914634.tmp
15/03/14 23:41:46 INFO Executor: Adding file:/tmp/spark-db1e742b-020f-4db1-9ee3-f3e2d90e1bc2/userFiles-96c6db61-e95e-4f9e-a6c4-0db892583854/spark-examples-1.3.0-hadoop2.4.0.jar to class loader
15/03/14 23:41:47 INFO Executor: Finished task 1.0 in stage 0.0 (TID 1). 736 bytes result sent to driver
15/03/14 23:41:47 INFO Executor: Finished task 0.0 in stage 0.0 (TID 0). 736 bytes result sent to driver
15/03/14 23:41:47 INFO TaskSetManager: Finished task 0.0 in stage 0.0 (TID 0) in 1560 ms on localhost (1/2)
15/03/14 23:41:47 INFO TaskSetManager: Finished task 1.0 in stage 0.0 (TID 1) in 1540 ms on localhost (2/2)
15/03/14 23:41:47 INFO TaskSchedulerImpl: Removed TaskSet 0.0, whose tasks have all completed, from pool
15/03/14 23:41:47 INFO DAGScheduler: Stage 0 (reduce at SparkPi.scala:35) finished in 1.578 s
15/03/14 23:41:47 INFO DAGScheduler: Job 0 finished: reduce at SparkPi.scala:35, took 2.099817 s
Pi is roughly 3.14438
15/03/14 23:41:47 INFO ContextHandler: stopped o.s.j.s.ServletContextHandler{/metrics/json,null}
15/03/14 23:41:47 INFO ContextHandler: stopped o.s.j.s.ServletContextHandler{/stages/stage/kill,null}
15/03/14 23:41:47 INFO ContextHandler: stopped o.s.j.s.ServletContextHandler{/,null}
15/03/14 23:41:47 INFO ContextHandler: stopped o.s.j.s.ServletContextHandler{/static,null}
15/03/14 23:41:47 INFO ContextHandler: stopped o.s.j.s.ServletContextHandler{/executors/threadDump/json,null}
15/03/14 23:41:47 INFO ContextHandler: stopped o.s.j.s.ServletContextHandler{/executors/threadDump,null}
15/03/14 23:41:47 INFO ContextHandler: stopped o.s.j.s.ServletContextHandler{/executors/json,null}
15/03/14 23:41:47 INFO ContextHandler: stopped o.s.j.s.ServletContextHandler{/executors,null}
15/03/14 23:41:47 INFO ContextHandler: stopped o.s.j.s.ServletContextHandler{/environment/json,null}
15/03/14 23:41:47 INFO ContextHandler: stopped o.s.j.s.ServletContextHandler{/environment,null}
15/03/14 23:41:47 INFO ContextHandler: stopped o.s.j.s.ServletContextHandler{/storage/rdd/json,null}
15/03/14 23:41:47 INFO ContextHandler: stopped o.s.j.s.ServletContextHandler{/storage/rdd,null}
15/03/14 23:41:47 INFO ContextHandler: stopped o.s.j.s.ServletContextHandler{/storage/json,null}
15/03/14 23:41:47 INFO ContextHandler: stopped o.s.j.s.ServletContextHandler{/storage,null}
15/03/14 23:41:47 INFO ContextHandler: stopped o.s.j.s.ServletContextHandler{/stages/pool/json,null}
15/03/14 23:41:47 INFO ContextHandler: stopped o.s.j.s.ServletContextHandler{/stages/pool,null}
15/03/14 23:41:47 INFO ContextHandler: stopped o.s.j.s.ServletContextHandler{/stages/stage/json,null}
15/03/14 23:41:47 INFO ContextHandler: stopped o.s.j.s.ServletContextHandler{/stages/stage,null}
15/03/14 23:41:47 INFO ContextHandler: stopped o.s.j.s.ServletContextHandler{/stages/json,null}
15/03/14 23:41:47 INFO ContextHandler: stopped o.s.j.s.ServletContextHandler{/stages,null}
15/03/14 23:41:47 INFO ContextHandler: stopped o.s.j.s.ServletContextHandler{/jobs/job/json,null}
15/03/14 23:41:47 INFO ContextHandler: stopped o.s.j.s.ServletContextHandler{/jobs/job,null}
15/03/14 23:41:47 INFO ContextHandler: stopped o.s.j.s.ServletContextHandler{/jobs/json,null}
15/03/14 23:41:47 INFO ContextHandler: stopped o.s.j.s.ServletContextHandler{/jobs,null}
15/03/14 23:41:47 INFO SparkUI: Stopped Spark web UI at http://192.168.126.147:4040
15/03/14 23:41:47 INFO DAGScheduler: Stopping DAGScheduler
15/03/14 23:41:47 INFO MapOutputTrackerMasterActor: MapOutputTrackerActor stopped!
15/03/14 23:41:47 INFO MemoryStore: MemoryStore cleared
15/03/14 23:41:47 INFO BlockManager: BlockManager stopped
15/03/14 23:41:47 INFO BlockManagerMaster: BlockManagerMaster stopped
15/03/14 23:41:47 INFO OutputCommitCoordinator$OutputCommitCoordinatorActor: OutputCommitCoordinator stopped!
15/03/14 23:41:47 INFO SparkContext: Successfully stopped SparkContext
15/03/14 23:41:47 INFO RemoteActorRefProvider$RemotingTerminator: Shutting down remote daemon.
15/03/14 23:41:47 INFO RemoteActorRefProvider$RemotingTerminator: Remote daemon shut down; proceeding with flushing remote transports.

可以看到输出结果为3.14438。

时间: 2024-11-10 01:14:22

跟我一起数据挖掘(22)——spark入门的相关文章

《Spark与Hadoop大数据分析》一一第2章 Apache Hadoop和Apache Spark入门

第2章 Apache Hadoop和Apache Spark入门 在本章,我们将学习 Hadoop 和 Spark 的基本知识,了解 Spark 与 MapReduce 有哪些不同,并开始安装集群和设置分析所需的工具.本章分为以下几个子主题:介绍 Apache Hadoop介绍 Apache Spark讨论为什么要配套使用 Hadoop 和 Spark安装 Hadoop 和 Spark 集群

《Spark与Hadoop大数据分析》——第2章 Apache Hadoop和Apache Spark入门

第2章 Apache Hadoop和Apache Spark入门 在本章,我们将学习 Hadoop 和 Spark 的基本知识,了解 Spark 与 MapReduce 有哪些不同,并开始安装集群和设置分析所需的工具. 本章分为以下几个子主题:

【Python数据挖掘课程】一.环境配置及数据挖掘与爬虫入门普及

        最近因为需要给大数据金融学院的学生讲解<Python数据挖掘及大数据分析>的课程,所以在这里,我将结合自己的上课内容,详细讲解每个步骤.作为助教,我更希望这门课程以实战为主,同时按小组划分学生,每个小组最后都提交一个基于Python的数据挖掘及大数据分析相关的成果.但是前面这节课没有在机房上,所以我在CSDN也将开设一个专栏,用于对该课程的补充.        希望该文章对你有所帮助,尤其是对大数据或数据挖掘的初学者,很开心和夏博.小民一起分享该课程,上课的感觉真的挺不错的,挺

Spark修炼之道(进阶篇)——Spark入门到精通:第四节 Spark编程模型(一)

作者:周志湖 网名:摇摆少年梦 微信号:zhouzhihubeyond 本节主要内容 Spark重要概念 弹性分布式数据集(RDD)基础 1. Spark重要概念 本节部分内容源自官方文档:http://spark.apache.org/docs/latest/cluster-overview.html (1)Spark运行模式 目前最为常用的Spark运行模式有: - local:本地线程方式运行,主要用于开发调试Spark应用程序 - Standalone:利用Spark自带的资源管理与调度

Spark修炼之道(进阶篇)——Spark入门到精通:第二节 Hadoop、Spark生成圈简介

作者:周志湖 网名:摇摆少年梦 微信号:zhouzhihubeyond 本节主要内容 Hadoop生态圈 Spark生态圈 1. Hadoop生态圈 原文地址:http://os.51cto.com/art/201508/487936_all.htm#rd?sukey=a805c0b270074a064cd1c1c9a73c1dcc953928bfe4a56cc94d6f67793fa02b3b983df6df92dc418df5a1083411b53325 下图给出了Hadoop生态圈中的重要

Spark修炼之道(进阶篇)——Spark入门到精通:第一节 Spark 1.5.0集群搭建

作者:周志湖 网名:摇摆少年梦 微信号:zhouzhihubeyond 本节主要内容 操作系统环境准备 Hadoop 2.4.1集群搭建 Spark 1.5.0 集群部署 注:在利用CentOS 6.5操作系统安装spark 1.5集群过程中,本人发现Hadoop 2.4.1集群可以顺利搭建,但在Spark 1.5.0集群启动时出现了问题(可能原因是64位操作系统原因,源码需要重新编译,但本人没经过测试),经本人测试在ubuntu 10.04 操作系统上可以顺利成功搭建.大家可以利用CentOS

Spark修炼之道(进阶篇)——Spark入门到精通:第六节 Spark编程模型(三)

作者:周志湖 网名:摇摆少年梦 微信号:zhouzhihubeyond 本节主要内容 RDD transformation(续) RDD actions 1. RDD transformation(续) (1)repartitionAndSortWithinPartitions(partitioner) repartitionAndSortWithinPartitions函数是repartition函数的变种,与repartition函数不同的是,repartitionAndSortWithin

Spark入门:实现WordCount的3种方式

WordCount作为Spark的入门任务,可以很简单,也可以做到比较复杂. 本文从实现功能的角度提出了3种实现方式,至于性能影响,会在后文继续讨论. 注意: 本文使用的Spark版本还是1.6.1.如果读者您已经切换到2.0+版本,请参考GitHub spark的官方例子进行学习. 因为2.0版本的API与1.X 并不能完全兼容,特别是2.0开始使用了SparkSession的概念,而不是SparkContext! 第一种方式:mapToPair + reduceByKey 这是官方提供的实现

Spark修炼之道(进阶篇)——Spark入门到精通:第十节 Spark SQL案例实战(一)

作者:周志湖 放假了,终于能抽出时间更新博客了--. 1. 获取数据 本文通过将github上的Spark项目git日志作为数据,对SparkSQL的内容进行详细介绍 数据获取命令如下: [root@master spark]# git log --pretty=format:'{"commit":"%H","author":"%an","author_email":"%ae",&quo