-
Notifications
You must be signed in to change notification settings - Fork 1
/
analysis.py
49 lines (35 loc) · 1.68 KB
/
analysis.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
import argparse
from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, StructField, StringType
def merge_two_files(data_source_1,data_source_2,output_uri):
"""
Merge 2 files into 1
Usage
spark-submit analysis.py --data_source_1 x_list.txt --data_source_2 y_list.txt --output_uri /uri/output/folder/to/store/results
for example /Users/johnpaulbabu/Documents/pyspark/output
"""
spark=SparkSession.builder.appName("merge-two-files").getOrCreate()
x_schema = StructType([
StructField("ID_x", StringType(), True),
StructField("value_x", StringType(), True)])
y_schema = StructType([
StructField("ID_y", StringType(), True),
StructField("value_y", StringType(), True)])
if data_source_1 is not None:
x = spark.read.csv(data_source_1, sep='\t',header=False,schema=x_schema)
if data_source_2 is not None:
y = spark.read.csv(data_source_2, sep='\t', header=False, schema=y_schema)
res = x.join(y, x.ID_x == y.ID_y, how= "left")
res1 = res.drop(res.ID_y)
res1.show()
res1.write.option("header", "true").mode("overwrite").csv(output_uri)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
'--data_source_1', help="The URI for your input CSV data, like an S3 bucket location.")
parser.add_argument(
'--data_source_2', help="The URI for your input CSV data, like an S3 bucket location.")
parser.add_argument(
'--output_uri', help="The URI where output is saved, like an S3 bucket location.")
args = parser.parse_args()
merge_two_files(args.data_source_1, args.data_source_2,args.output_uri)