Created
January 19, 2020 22:24
-
-
Save aarroyoc/62f9145136165ff6398c665248e98f74 to your computer and use it in GitHub Desktop.
PySpark example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ~/spark-3.0xxxx/bin/spark-submit sparky.py | |
| from pyspark.sql import SparkSession | |
| spark = SparkSession.builder.appName("test").getOrCreate() | |
| sc = spark.sparkContext | |
| # Word Count | |
| inputfile = sc.textFile("input.txt") | |
| counts = inputfile.flatMap(lambda x: x.split(" ")).map(lambda x: (x, 1)).reduceByKey(lambda x,y: x+y) | |
| counts.saveAsTextFile("output") | |
| accum = sc.accumulator(0) | |
| def ac(x): | |
| global accum | |
| accum += x | |
| sc.parallelize([1,2,3,4]).foreach(ac) | |
| print(accum.value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment