pycharm配置spark
pycharm 配置spark
在Run->Configuration->Defaults->Python->Environment variables下,配置:
PYTHONPATH /home/xxx/software/spark-2.1.0-bin-hadoop2.7/python:/home/xxx/software/spark-2.1.0-bin-hadoop2.7/python/lib/py4j-0.10.4-src.zipSPARK_HOME /home/xxx/software/spark-2.1.0-bin-hadoop2.7
sudo vi /etc/hosts
127.0.0.1 [email protected]test.py
import os
import sys
# Path for spark source folder
os.environ['SPARK_HOME'] = "/home/xxx/software/spark-2.1.0-bin-hadoop2.7"
# You might need to enter your local IP
# os.environ['SPARK_LOCAL_IP']="192.168.2.138"
# Path for pyspark and py4j
sys.path.append("/home/xxx/software/spark-2.1.0-bin-hadoop2.7/python")
sys.path.append("/home/xxx/software/spark-2.1.0-bin-hadoop2.7/python/lib/py4j-0.10.4-src.zip")
try:
from pyspark import SparkContext
from pyspark import SparkConf
print ("Successfully imported Spark Modules")
except ImportError as e:
print ("Can not import Spark Modules", e)
sys.exit(1)
logFile = "/home/xxx/software/spark-2.1.0-bin-hadoop2.7/README.md"
sc = SparkContext("local", "Simple App")
logData = sc .textFile(logFile).cache()
numAs = logData.filter(lambda s: 'a' in s).count()
numBs = logData.filter(lambda s: 'b' in s).count()
print("Lines with a: %i, lines with b: %i"%(numAs, numBs))