-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
49 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
./idea | ||
./idea | ||
.DS_Store |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from pyspark.sql import SparkSession | ||
from pyspark.sql.functions import col,date_format | ||
|
||
def init_spark(): | ||
sql = SparkSession.builder\ | ||
.appName("trip-app")\ | ||
.config("spark.jars", "/opt/spark-apps/postgresql-42.2.22.jar")\ | ||
.getOrCreate() | ||
sc = sql.sparkContext | ||
return sql,sc | ||
|
||
def main(): | ||
url = "jdbc:postgresql://storage-postgres:5432/postgres" | ||
properties = { | ||
"user": "postgres", | ||
"password": "root", | ||
"driver": "org.postgresql.Driver" | ||
} | ||
file = "/opt/spark-data/MTA_2014_08_01.csv" | ||
sql,sc = init_spark() | ||
|
||
df = sql.read.load(file,format = "csv", inferSchema="true", sep="\t", header="true") \ | ||
.withColumn("report_hour",date_format(col("time_received"),"yyyy-MM-dd HH:00:00")) \ | ||
.withColumn("report_date",date_format(col("time_received"),"yyyy-MM-dd")) | ||
|
||
# Filter invalid coordinates | ||
df.where("latitude <= 90 AND latitude >= -90 AND longitude <= 180 AND longitude >= -180") \ | ||
.where("latitude != 0.000000 OR longitude != 0.000000 ") \ | ||
.write \ | ||
.jdbc(url=url, table="mta_reports", mode='append', properties=properties) \ | ||
.save() | ||
|
||
if __name__ == '__main__': | ||
main() |
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
spark.eventLog.dir file:/tmp/spark-events | ||
spark.eventLog.enabled true | ||
spark.history.fs.logDirectory file:/tmp/spark-events |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.csv |
This file contains 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