-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjob.py
34 lines (27 loc) · 811 Bytes
/
job.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Databricks notebook source
# COMMAND ----------
import pyspark
from pyspark.sql import SparkSession
from pyspark.sql.types import StringType
import pyspark.sql.functions as F
from functools import reduce
import requests
spark = SparkSession \
.builder \
.master("local[*]") \
.appName('why-is-the-example-always-wordcount') \
.getOrCreate()
sc = spark.sparkContext
moby_dick = requests.get('https://www.gutenberg.org/files/2701/old/moby10b.txt').text
words = reduce(
lambda words, line: words + [w.lower() for w in line.split(' ')],
moby_dick.splitlines(),
[]
)
df = spark.createDataFrame(words, StringType())
df \
.withColumnRenamed(df.columns[0], "word") \
.groupBy("word") \
.agg(F.count("word").alias("count")) \
.orderBy(F.desc("count")) \
.show()