-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoracle_to_doris_yaml.py
57 lines (48 loc) · 1.63 KB
/
oracle_to_doris_yaml.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
57
from pyspark.sql import SparkSession
from pyspark.sql.functions import col, regexp_replace, when, lit
import yaml
import argparse
spark = SparkSession.builder \
.appName("oracle_to_doris_yaml") \
.getOrCreate()
spark.sparkContext.setLogLevel("WARN")
# args
parser = argparse.ArgumentParser(description='setting yaml file.')
parser.add_argument('-f', '--file', type=str, required=True, help='yaml configuration file name')
args = parser.parse_args()
# setting yaml
with open(args.file, "r") as file:
config = yaml.safe_load(file)
source = config['source']
df = spark.read \
.format("jdbc") \
.option("driver", "oracle.jdbc.driver.OracleDriver") \
.option("url", source["url"]) \
.option("dbtable", source["dbtable"]) \
.option("user", source["user"]) \
.option("password", source["password"]) \
.load()
df.show()
process = config['process']
selected_columns = []
for field in process["fields"]:
# regex
column = regexp_replace(col(field['name']), r"(\r\n|\n|\r|\t|\r)", "").alias(field['alias'])
# default
if "default" in field:
column = when(column.isNull(), lit(field["default"])).otherwise(column)
column = column.alias(field['alias'])
selected_columns.append(column)
processed_df = df.select(*selected_columns)
# sink doris
sink = config['sink']
ds = processed_df \
.write \
.mode("append") \
.format("doris") \
.option("checkpointLocation", f"./checkpoint/{sink["table"]}") \
.option("doris.table.identifier", sink["table"]) \
.option("doris.fenodes", sink["feNodes"]) \
.option("user", sink["user"]) \
.option("password", sink["password"]) \
.save()