-
Notifications
You must be signed in to change notification settings - Fork 1
/
reduce.py
56 lines (50 loc) · 1.25 KB
/
reduce.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import pyspark.sql.functions as F
import pyspark.sql.types as T
from pyspark.sql import SparkSession
from pyspark import SparkConf
def process(sc, input_path):
# load data
df = (
# read file
sc.read.parquet(input_path)
# select columns
.select(
F.col("doi"),
F.col("hash")
)
# reduce chunks to individual documents
.rdd
.map(lambda x: (x[0], [int(y[0]) for y in x[1]]))
.reduceByKey(lambda a, b: a + b)
.toDF(schema=T.StructType([
T.StructField("doi", T.StringType(), False),
T.StructField("hash", T.ArrayType(T.IntegerType()), False)
]))
)
return df
def run(sc, args):
# args
input_path = args[0]
output_path = args[1]
# process
df = process(sc, input_path)
df.show()
df.write.mode("overwrite").parquet(output_path)
if __name__ == '__main__':
# args
INPUT = "stereo-hashed.parquet/*"
OUTPUT = "stereo-reduced.parquet"
# spark session
spark = (
SparkSession
.builder
.config(conf=SparkConf())
.getOrCreate()
)
# process
(
process(spark, INPUT)
.write
.mode('overwrite')
.parquet(OUTPUT)
)