我有这个程序来计算sample.txt中的单词:
import os
from operator import add
from pyspark import SparkContext
os.environ["SPARK_HOME"] = "/usr/local/spark"
os.environ["PYSPARK_PYTHON"]="/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6"
print("Hello World") #This is printed
if __name__ == "__main__":
sc = SparkContext(appName="PythonWordCount")
lines = sc.textFile("sample.txt", 1)
counts = lines.flatMap(lambda x: x.split(' ')) \
.map(lambda x: (x, 1)) \
.reduceByKey(add)
output = counts.collect()
for (word, count) in output:
print("%s: %i" % (word, count))
sc.stop()
但似乎该程序没有运行主要的if句子。我是python的新编程,代码中是否有任何错误?