<jupyter_start><jupyter_text>如果本机有多个java版本或scala版本，需要在执行前设定好pyspark所用的jdk版本，按照下面这个方式设定即可<jupyter_code>import os
from os.path import *
file_dir = os.path.abspath(".")
proj_dir = dirname(dirname(file_dir))
os.environ['HADOOP_HOME'] = r"C:\hadoop\hadoop-2.6.5"
os.environ['SPARK_HOME'] = r"C:\hadoop\spark-2.4.5-bin-hadoop2.6"
os.environ['JAVA_HOME'] = r"C:\Program Files\Java\jdk1.8.0_191"
os.environ["SCALA_HOME"]=r"C:\Program Files (x86)\scala"
os.environ
import pyspark
from pyspark import SparkContext, SparkConf
from pyspark.sql import SparkSession
sconf = SparkConf().setMaster("local[4]").setAppName("test")
sc = SparkContext(conf=sconf)
ss = SparkSession(sparkContext=sc)
ss
sc.parallelize(range(100)).collect()
base_dir = dirname(dirname(os.path.abspath(".")))
base_dir
# 测试SparkContext
rdd = sc.textFile("file:///"+base_dir+"/input/i_have_a_dream.data")
rdd.flatMap(lambda x: x.split(" ")).countByValue().items()
# 测试SparkSession
df = ss.read.csv(path="file:///"+base_dir+"/input/students.data",sep="\t")
df.show()
sc.stop()
print("end")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18511627906976744, 'fraction_numerical': 0.018604651162790697, 'mean_word_length': 7.40625, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '582124', 'license': 'MIT', 'n_tokens_mistral': 477, 'n_tokens_neox': 473, 'n_words': 55, 'path': 'c3-py-spark/1_rdd/1_local_run.ipynb', 'repo_name': 'wangyaomail/zzti-spark-learn', 'size': '1075'}