導航:首頁 > 軟體知識 > spark如何運行python程序

spark如何運行python程序

發布時間:2023-01-28 21:21:31

㈠ 如何用Python寫spark

1.RDD是PariRDD類型
def add1(line):
return line[0] + line[1]
def add2(x1,x2):
return x1 + x2
sc = SparkContext(appName="gridAnalyse")
rdd = sc.parallelize([1,2,3])
list1 = rdd.map(lambda line: (line,1)).map(lambda (x1,x2) : x1 + x2).collect() #只有一個參數,通過匹配來直接獲取(賦值給裡面對應位置的變數)
list1 = rdd.map(lambda line: (line,1)).map(lambda x1,x2 : x1 + x2).collect() #錯誤,相當於函數有兩個參數
list2 = rdd.map(lambda line: (line,1)).map(lambda line : line[0] + line[1]).collect() #只有一個參數,參數是Tuple或List數據類型,再從集合的對應位置取出數據
list3 = rdd.map(lambda line: (line,1)).map(add1).collect() #傳遞函數,將Tuple或List類型數據傳給形參
list4 = rdd.map(lambda line: (line,1)).map(add2).collect() #錯誤,因為輸入只有一個,卻有兩個形參
當RDD是PairRDD時,map中可以寫lambda表達式和傳入一個函數。
a、寫lambda表達式:
可以通過(x1,x2,x3)來匹配獲取值;或者使用line獲取集合,然後從集合中獲取。
b、傳入函數
根據spark具體的transaction OR action 操作來確定自定義函數參數的個數,此例子中只有一個參數,從形參(集合類型)中獲取相應位置的數據。

㈡ 科普Spark,Spark是什麼,如何使用Spark

科普Spark,Spark是什麼,如何使用Spark


1.Spark基於什麼演算法的分布式計算(很簡單)

2.Spark與MapRece不同在什麼地方

3.Spark為什麼比Hadoop靈活

4.Spark局限是什麼

5.什麼情況下適合使用Spark

Spark與Hadoop的對比

Spark的中間數據放到內存中,對於迭代運算效率更高。

Spark更適合於迭代運算比較多的ML和DM運算。因為在Spark裡面,有RDD的抽象概念。

Spark比Hadoop更通用

Spark提供的數據集操作類型有很多種,不像Hadoop只提供了Map和Rece兩種操作。比如map, filter, flatMap, sample, groupByKey, receByKey, union, join, cogroup, mapValues, sort,partionBy等多種操作類型,Spark把這些操作稱為Transformations。同時還提供Count, collect, rece, lookup, save等多種actions操作。

這些多種多樣的數據集操作類型,給給開發上層應用的用戶提供了方便。各個處理節點之間的通信模型不再像Hadoop那樣就是唯一的Data Shuffle一種模式。用戶可以命名,物化,控制中間結果的存儲、分區等。可以說編程模型比Hadoop更靈活。

不過由於RDD的特性,Spark不適用那種非同步細粒度更新狀態的應用,例如web服務的存儲或者是增量的web爬蟲和索引。就是對於那種增量修改的應用模型不適合。

容錯性

在分布式數據集計算時通過checkpoint來實現容錯,而checkpoint有兩種方式,一個是checkpoint data,一個是logging the updates。用戶可以控制採用哪種方式來實現容錯。

可用性

Spark通過提供豐富的Scala, Java,Python API及互動式Shell來提高可用性。

Spark與Hadoop的結合

Spark可以直接對HDFS進行數據的讀寫,同樣支持Spark on YARN。Spark可以與MapRece運行於同集群中,共享存儲資源與計算,數據倉庫Shark實現上借用Hive,幾乎與Hive完全兼容。

Spark的適用場景

Spark是基於內存的迭代計算框架,適用於需要多次操作特定數據集的應用場合。需要反復操作的次數越多,所需讀取的數據量越大,受益越大,數據量小但是計算密集度較大的場合,受益就相對較小(大資料庫架構中這是是否考慮使用Spark的重要因素)

由於RDD的特性,Spark不適用那種非同步細粒度更新狀態的應用,例如web服務的存儲或者是增量的web爬蟲和索引。就是對於那種增量修改的應用模型不適合。總的來說Spark的適用面比較廣泛且比較通用。

運行模式

本地模式

Standalone模式

Mesoes模式

yarn模式

Spark生態系統

Shark ( Hive on Spark): Shark基本上就是在Spark的框架基礎上提供和Hive一樣的H iveQL命令介面,為了最大程度的保持和Hive的兼容性,Shark使用了Hive的API來實現query Parsing和 Logic Plan generation,最後的PhysicalPlan execution階段用Spark代替Hadoop MapRece。通過配置Shark參數,Shark可以自動在內存中緩存特定的RDD,實現數據重用,進而加快特定數據集的檢索。同時,Shark通過UDF用戶自定義函數實現特定的數據分析學習演算法,使得SQL數據查詢和運算分析能結合在一起,最大化RDD的重復使用。

Spark streaming: 構建在Spark上處理Stream數據的框架,基本的原理是將Stream數據分成小的時間片斷(幾秒),以類似batch批量處理的方式來處理這小部分數據。Spark Streaming構建在Spark上,一方面是因為Spark的低延遲執行引擎(100ms+)可以用於實時計算,另一方面相比基於Record的其它處理框架(如Storm),RDD數據集更容易做高效的容錯處理。此外小批量處理的方式使得它可以同時兼容批量和實時數據處理的邏輯和演算法。方便了一些需要歷史數據和實時數據聯合分析的特定應用場合。

Bagel: Pregel on Spark,可以用Spark進行圖計算,這是個非常有用的小項目。Bagel自帶了一個例子,實現了Google的PageRank演算法。

End.

㈢ 機器學習實踐:如何將Spark與Python結合

可以學習一下林大貴這本書,從頭到尾教你如何使用python+spark+hadoop實現常用的演算法訓練和部署。

《Python+Spark2.0+Hadoop機器學習與大數據實戰_林大貴》

鏈接:https://pan..com/s/1VGUOyr3WnOb_uf3NA_ZdLA

提取碼:ewzf

㈣ 如何在Spark2.0.2中啟動Ipython Notebook

IPython Configuration
This installation workflow loosely follows the one contributed by Fernando Perez here. This should be performed on the machine where the IPython Notebook will be executed, typically one of the Hadoop nodes.
First create an IPython profile for use with PySpark.

1

ipython profile create pyspark

This should have created the profile directory ~/.ipython/profile_pyspark/. Edit the file~/.ipython/profile_pyspark/ipython_notebook_config.py to have:

1
2
3
4
5

c = get_config()

c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False
c.NotebookApp.port = 8880 # or whatever you want; be aware of conflicts with CDH

If you want a password prompt as well, first generate a password for the notebook app:

1

python -c 'from IPython.lib import passwd; print passwd()' > ~/.ipython/profile_pyspark/nbpasswd.txt

and set the following in the same .../ipython_notebook_config.py file you just edited:

1
2

PWDFILE='~/.ipython/profile_pyspark/nbpasswd.txt'
c.NotebookApp.password = open(PWDFILE).read().strip()

Finally, create the file ~/.ipython/profile_pyspark/startup/00-pyspark-setup.py with the following contents:

1
2
3
4
5
6
7
8
9

import os
import sys

spark_home = os.environ.get('SPARK_HOME', None)
if not spark_home:
raise ValueError('SPARK_HOME environment variable is not set')
sys.path.insert(0, os.path.join(spark_home, 'python'))
sys.path.insert(0, os.path.join(spark_home, 'python/lib/py4j-0.8.1-src.zip'))
execfile(os.path.join(spark_home, 'python/pyspark/shell.py'))

Starting IPython Notebook with PySpark
IPython Notebook should be run on a machine from which PySpark would be run on, typically one of the Hadoop nodes.
First, make sure the following environment variables are set:

1
2
3
4
5

# for the CDH-installed Spark
export SPARK_HOME='/opt/cloudera/parcels/CDH/lib/spark'

# this is where you specify all the options you would normally add after bin/pyspark
export PYSPARK_SUBMIT_ARGS='--master yarn --deploy-mode client --num-executors 24 --executor-memory 10g --executor-cores 5'

Note that you must set whatever other environment variables you want to get Spark running the way you desire. For example, the settings above are consistent with running the CDH-installed Spark in YARN-client mode. If you wanted to run your own custom Spark, you could build it, put the JAR on HDFS, set theSPARK_JAR environment variable, along with any other necessary parameters. For example, see here for running a custom Spark on YARN.
Finally, decide from what directory to run the IPython Notebook. This directory will contain the .ipynb files that represent the different notebooks that can be served. See the IPython docs for more information. From this directory, execute:

1

ipython notebook --profile=pyspark

Note that if you just want to serve the notebooks without initializing Spark, you can start IPython Notebook using a profile that does not execute the shell.py script in the startup file.
Example Session
At this point, the IPython Notebook server should be running. Point your browser to , which should open up the main access point to the available notebooks. This should look something like this:

This will show the list of possible .ipynb files to serve. If it is empty (because this is the first time you』re running it) you can create a new notebook, which will also create a new .ipynb file. As an example, here is a screenshot from a session that uses PySpark to analyze the GDELT event data set:

The full .ipynb file can be obtained as a GitHub gist.

㈤ python開發spark環境該如何配置,又該如何操作

1)輸入:welcome="Hello!"回車

再輸入:printwelcome或者直接welcome回車就可以看到輸出Hello!

2)

[html]viewplain
welcome="hello"
you="world!"
printwelcome+you


輸出:helloworld!

以上使用的是字元串,變數還有幾種類型:數,字元串,列表,字典,文件。其他的和別的語言類似,下面先講下列表:

3)

[html]viewplain
my_list=[]//這個就產生了一個空的列表。然後給它賦值
my_list=[1,2]
printmy_list
my_list.append(3)
printmy_list

4)字典:

[html]viewplain
contact={}
contact["name"]="shiyuezhong"
contact["phone"]=12332111

5)結合列表和字典:

[html]viewplain
contact_list=[]
contact1={}
contact1['name']='shiyuezhong'
contact1['phone']=12332111
contact_list.append(contact1)
contact2={}
contact2['name']='buding'
contact2['phone']=88888888
contact_list.append(contact2)

㈥ Spark的四種運行模式

介紹
本地模式
Spark單機運行,一般用於開發測試。

Standalone模式
構建一個由Master+Slave構成的Spark集群,Spark運行在集群中。

Spark on Yarn模式
Spark客戶端直接連接Yarn。不需要額外構建Spark集群。

Spark on Mesos模式
Spark客戶端直接連接Mesos。不需要額外構建Spark集群。

啟動方式: spark-shell.sh(Scala)
spark-shell通過不同的參數控制採用何種模式進行。 涉及兩個參數:

對於Spark on Yarn模式和Spark on Mesos模式還可以通過 –deploy-mode參數控制Drivers程序的啟動位置。

進入本地模式:

進入Standalone模式:

備註:測試發現MASTER_URL中使用主機名替代IP地址無法正常連接(hosts中有相關解析記錄),即以下命令連接不成功:

./spark-shell --master spark://ctrl:7077 # 連接失敗
Spark on Yarn模式

備註:Yarn的連接信息在Hadoop客戶端的配置文件中指定。通過spark-env.sh中的環境變數HADOOPCONFDIR指定Hadoop配置文件路徑。

Spark on Mesos模式:

啟動方式: pyspark(Python)
參數及用法與Scala語言的spark-shell相同,比如:

㈦ 如何運行含spark的python腳本

1、Spark腳本提交/運行/部署1.1spark-shell(交互窗口模式)運行Spark-shell需要指向申請資源的standalonespark集群信息,其參數為MASTER,還可以指定executor及driver的內存大小。sudospark-shell--executor-memory5g--driver-memory1g--masterspark://192.168.180.216:7077spark-shell啟動完後,可以在交互窗口中輸入Scala命令,進行操作,其中spark-shell已經默認生成sc對象,可以用:valuser_rdd1=sc.textFile(inputpath,10)讀取數據資源等。1.2spark-shell(腳本運行模式)上面方法需要在交互窗口中一條一條的輸入scala程序;將scala程序保存在test.scala文件中,可以通過以下命令一次運行該文件中的程序代碼:sudospark-shell--executor-memory5g--driver-memory1g--masterspark//192.168.180.216:7077

㈧ 如何在ipython或python中使用Spark

如何在ipython中使用spark
說明:

spark 1.6.0
scala 2.10.5
spark安裝路徑是/usr/local/spark;已經在.bashrc中配置了SPARK_HOME環境變數。
方法一
/usr/local/spark/bin/pyspark默認打開的是python,而不是ipython。通過在pyspark文件中添加一行,來使用ipython打開。
cp pyspark ipyspark
vi ipyspark

# 在最前面添加

IPYTHON=1

# 啟動

ipyspark

1
2
3
4
5
6
7
8
9
10
方法二:
通過為spark創建一個ipython 配置的方式實現。

# 為spark創建一個ipython 配置

ipython profile create spark

# 創建啟動配置文件

cd ~/.config/ipython/profile_spark/startup
vi 00-pyspark-setup.py

1
2
3
4
5
6
7
8
9
在00-pyspark-setup.py中添加如下內容:
import os
import sys

# Configure the environment

if 'SPARK_HOME' not in os.environ:
os.environ['SPARK_HOME'] = '/srv/spark'

# Create a variable for our root path

SPARK_HOME = os.environ['SPARK_HOME']

# Add the PySpark/py4j to the Python Path

sys.path.insert(0, os.path.join(SPARK_HOME, "python", "pyspark"))
sys.path.insert(0, os.path.join(SPARK_HOME, "python", "lib", "py4j-0.9-src.zip"))
sys.path.insert(0, os.path.join(SPARK_HOME, "python"))

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
啟動ipython
ipython –profile spark

1
測試程序
在ipython中輸入一下命令,如果下面的程序執行完後輸出一個數字,說明正確。
from pyspark import SparkContext
sc = SparkContext( 'local', 'pyspark')

def isprime(n):
"""
check if integer n is a prime
"""
# make sure n is a positive integer
n = abs(int(n))
# 0 and 1 are not primes
if n < 2:
return False
# 2 is the only even prime number
if n == 2:
return True
# all other even numbers are not primes
if not n & 1:
return False
# for all odd numbers
for x in range(3, int(n**0.5)+1, 2):
if n % x == 0:
return False
return True

# Create an RDD of numbers from 0 to 1,000,000

nums = sc.parallelize(xrange(1000000))

# Compute the number of primes in the RDD

print 「Result: 」, nums.filter(isprime).count()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
方法三
將上面的程序放入test.py文件,執行命令python test.py。發現錯誤。因為沒有將pyspark路徑加入PYTHONPATH環境變數。
在~/.bashrc或/etc/profile中添加如下內容:

# python can call pyspark directly

export PYTHONPATH=$SPARK_HOME/python:$SPARK_HOME/python/pyspark:$SPARK_HOME/python/lib/py4j-0.9-src.zip:$PYTHONPATH

1
2
3
4
執行如下命令:

# 使配置生效

source ~/.bashrc

# 測試程序

python test.py

1
2
3
4
5
6
7
8
此時,已經能夠運行了。

㈨ 如何運行含spark的python腳本

2~spark$ bin/spark-submit first.py
-----------first.py-------------------------------
from pyspark import SparkConf, SparkContext
conf = SparkConf().setMaster("local").setAppName("My App")
sc = SparkContext(conf = conf)
lines = sc.textFile("first.py")
pythonLines = lines.filter(lambda line: "Python" in line)
print "hello python"
print pythonLines.first()
print pythonLines.first()
print "hello spark!"
---------------------------------------------------
hello python
pythonLines = lines.filter(lambda line: "Python" in line)
pythonLines = lines.filter(lambda line: "Python" in line)
hello spark!

到spark的安裝目錄下/bin 下面 spark-submit ***.py 即可

㈩ 求助,python + spark運行程序出現錯誤

tmprdd1 = csdnRDD.map(lambda x: (x.split("\t")[2]))
x.split("\t")會產生一個list,有些數據是異常異常,產生的list不一定會有三個元素,所以就會異常退出。
你可以使用csdnRDD.map(lambda x:x.split("\t")).filter(lambda x:len(x)<3) 看看有哪一寫異常數據,然後確定如何過濾掉這些異常數據。

閱讀全文

與spark如何運行python程序相關的資料

熱點內容
存錢里的交易類型什麼意思 瀏覽:31
創客雲商怎麼代理 瀏覽:312
代理行為無因性是什麼 瀏覽:816
sqi資料庫如何打開表 瀏覽:336
藝龍酒店怎麼做代理 瀏覽:703
工程技術軍官是什麼意思 瀏覽:356
加大對市場投入屬於什麼戰略 瀏覽:614
電腦版通達信如何登陸交易 瀏覽:534
沈陽傢具市場叫什麼 瀏覽:359
喬治的交易用了哪些選秀權 瀏覽:221
唐山馬市場在哪裡 瀏覽:853
華清五金機電市場往哪裡搬遷 瀏覽:963
華苓鋼鐵今天市場價是多少 瀏覽:813
生活中有哪些成熟信息 瀏覽:937
螞蟻農場怎麼代理商 瀏覽:21
中國移動渠道代理怎麼樣 瀏覽:873
微信玩游戲的小程序為什麼未成年 瀏覽:634
買新車一般怎麼交易 瀏覽:798
宣化職業技術學院男女比例是多少 瀏覽:717
微信程序怎麼寫的 瀏覽:747