forked from CharityNavigator/990_long
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.py
More file actions
44 lines (34 loc) · 1.4 KB
/
Copy pathexamples.py
File metadata and controls
44 lines (34 loc) · 1.4 KB
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
import argparse
from pyspark.sql import SparkSession
from pyspark.sql.types import *
from pyspark.sql.functions import *
from lxml import etree
import re
import time
import datetime
import sys
spark = SparkSession.builder.getOrCreate()
sc = spark.sparkContext
### SO 25407550
log4jLogger = sc._jvm.org.apache.log4j
LOGGER = log4jLogger.LogManager.getLogger(__name__)
### Handle command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("--input", action="store", help="Path to Parquet file containing xpath-value pairs.", default = "990_long/parsed")
parser.add_argument("--output", action="store", help="Path in which to store CSVs. Can be local or S3. Local recommended.", default = "990_long/examples")
parser.add_argument("--timestamp", action="store_true", help="If true, append the timestamp to the output path.")
args = parser.parse_args()
if args.timestamp:
timestamp = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d-%H-%M-%S')
suffix = "/%s" % timestamp
else:
suffix = ""
outputPath = args.output + suffix
raw = spark.read.parquet(args.input) \
.withColumn("rowId", monotonically_increasing_id())
raw.createOrReplaceTempView("raw")
query = "SELECT * FROM raw x, (SELECT MIN(rowId) as rowId FROM raw GROUP BY xpath) y WHERE x.rowId = y.rowId"
spark.sql(query) \
.coalesce(1) \
.write.csv(outputPath, header=True)
print "*** Process complete."