一、shell模式
1.1 shell本地模式
pyspark #进入shell本地模式
# 输入数据
data = ["hello", "world", "hello", "world"]
# 将collection的data转为spark中的rdd并进行操作
rdd = sc.parallelize(data)
res_rdd = rdd.map(lambda word: (word, 1)).reduceByKey(lambda a, b: a + b)
# 将rdd转为collection并打印
res_rdd_coll = res_rdd.collect()
for line in res_rdd_coll:
print(line) #此处加table键
注:
shell本地模式无法通过masterip:8080监控到
只能通过shell所在机器ip:4040监控到
1.2 shell集群模式
pyspark --master spark://big07:7077 #进入shell集群模式
二、集群模式
spark-submit --master spark://big07:7077 test1.py
from pyspark import SparkContext,SparkConf
conf=SparkConf()
conf.setMaster("spark://big07:7077")
conf.setAppName("test application")
sc=SparkContext(conf=conf)
# 输入数据
data = ["hello", "world", "hello", "world"]
# 将collection的data转为spark中的rdd并进行操作
rdd = sc.parallelize(data)
res_rdd = rdd.map(lambda word: (word, 1)).reduceByKey(lambda a, b: a + b)
# 将rdd转为collection并打印
res_rdd_coll = res_rdd.collect()
for line in res_rdd_coll:
print(line) #此处加table键
#sc.close()
sc.stop()