Apache Spark簡單介紹、安裝及使用,apachespark

來源:互聯網
上載者:User

Apache Spark簡單介紹、安裝及使用,apachespark
Apache Spark簡介
Apache Spark是一個高速的通用型計算引擎,用來實現分布式的大規模資料的處理任務。 分布式的處理方式可以使以前單台電腦面對大規模資料時處理不了的情況成為可能。
Apache Spark安裝及配置(OS X下的Ubuntu虛擬機器)
學習新東西最好是在虛擬機器下操作,以免對現在的開發環境造成影響,我的系統是OS X,安裝的是VirtualBox虛擬機器,然後在虛擬機器裡安裝的Ubuntu系統。 VirtualBox安裝方法請查看教程: YouTube: Install Ubuntu in Mac with Virtual Box 注意在安裝過程中設定4GB的RAM和20GB的空間,否則會出現不夠用的情況。
安裝 Anaconda
Anaconda 是Python科學計算包的合集,在接下來的例子中,會用到其中的matplotlib用來產生一張柱狀圖。 :https://www.continuum.io/downloads 然後在Terminal中輸入命令:

bash Anaconda2-4.1.1-Linux-x86_64.sh

 

安裝 Java SDK
Spark運行在JVM上,所以還需要安裝Java SDK:
$ sudo apt-get install software-properties-common$ sudo add-apt-repository ppa:webupd8team/java$ sudo apt-get update$ sudo apt-get install oracle-java8-installer

設定JAVA_HOME
開啟.bashrc檔案
gedit .bashrc
在.bashrc中添加如下設定:
JAVA_HOME=/usr/lib/jvm/java-8-oracleexport JAVA_HOMEPATH=$PATH:$JAVA_HOMEexport PATH

 

安裝Spark
去官網下載壓縮包, http://spark.apache.org/downloads.html 將安裝包解壓,命令如下:
$ tar -zxvf spark-2.0.0-bin-hadoop2.7.tgz$ rm spark-2.0.0-bin-hadoop2.7.tgz

 

啟用IPython Notebook  開啟.bashrc檔案
gedit .bashrc
在.bashrc中添加如下設定:
export PYSPARK_DRIVER_PYTHON=ipythonexport PYSPARK_DRIVER_PYTHON_OPTS=notebook

 

檢查是否安裝成功 (需重啟Terminal)
cd ~/spark-2.0.0-bin-hadoop2.7./bin/pyspark

Apache Spark簡單使用  開啟Spark服務後,點擊new - Notebooks - Python建立一個Notebook檔案。 在這個小例子中,我們讀取Spark檔案夾下的NOTICE檔案裡的內容,然後統計詞頻,最後產生一張圖表。樣本很簡單,直接貼出代碼和最後的結果:  # coding: utf-8# In[1]:import refrom operator import add# In[13]:file_in = sc.textFile("/home/carl/spark/NOTICE")# In[3]:words = file_in.flatMap(lambda line: re.split(' ', line.lower().strip()))# In[4]:words = words.filter(lambda w: len(w) > 3)# In[5]:words = words.map(lambda w:(w,1))# In[6]:words = words.reduceByKey(add)# In[7]:words = words.map(lambda x: (x[1], x[0])).sortByKey(False)# In[8]:words.take(15)# In[9]:get_ipython().magic(u'matplotlib inline')import matplotlib.pyplot as pltdef histogram(words): count = map(lambda x: x[1], words) word = map(lambda x:x[0], words) plt.barh(range(len(count)), count, color="green") plt.yticks(range(len(count)), word)# In[10]:words = words.map(lambda x:(x[1], x[0]))# In[11]:words.take(15)# In[12]:histogram(words.take(15))View Code 這些內容是在學習  Spark for Python Developers 這本書過程中的隨筆,接下來還會繼續分享和Spark相關的知識,有興趣的朋友歡迎關注本部落格,也歡迎大家留言進行討論。 福利Spark for Python Developers電子版下載連結:Spark for Python Developers.pdf 我們處於大資料時代,對資料處理感興趣的朋友歡迎查看另一個系列隨筆: 利用Python進行資料分析 基礎系列隨筆匯總 
如果你對網路爬蟲感興趣,請查看另一篇隨筆: 網路爬蟲:使用Scrapy架構編寫一個抓取書籍資訊的爬蟲服務 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.