-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspark_test.py
186 lines (157 loc) · 6.77 KB
/
spark_test.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import sys
import time
import MySQLdb
from pyspark.sql import HiveContext
from pyspark import SparkConf, SparkContext, SQLContext
# /usr/bin/spark-submit --jars "/home/engyne/spark/ojdbc7.jar" --master local /home/engyne/spark/spark_test.py
conf = SparkConf().setAppName('inc_dd_openings')
sc = SparkContext(conf=conf)
sqlContext = HiveContext(sc)
reload(sys)
sys.setdefaultencoding("utf-8")
mysql_url = "192.168.1.111"
mysql_user = "root"
mysql_password = "xxx"
mysql_db = "xxx"
get_df_url = "jdbc:oracle:thin:@//192.168.1.xxx:1521/ORCLPDB"
get_df_driver = "oracle.jdbc.driver.OracleDriver"
get_df_user = "xxx"
get_df_password = "xxx"
# insert update delete
def conMysqlDB_exec(sqlStr):
db = MySQLdb.connect(mysql_url, mysql_user, mysql_password, mysql_db, charset='utf8' )
cursor = db.cursor()
try:
cursor.execute(sqlStr)
db.commit()
result = True
except:
print("---->MySqlError: execute error")
result = False
db.rollback()
db.close
return result
# select
def conMysqlDB_fetchall(sqlStr):
db = MySQLdb.connect(mysql_url, mysql_user, mysql_password, mysql_db, charset='utf8' )
cursor = db.cursor()
results = []
try:
cursor.execute(sqlStr)
results = cursor.fetchall()
except:
print("---->MySqlError: unable to fecth data")
db.close
return results
# import
def importData(type):
time_start = time.time()
findJobSql = "SELECT * FROM job where status=1"
result = conMysqlDB_fetchall(findJobSql)
resultInfoList = []
for i, val in enumerate(result):
databaseName = val[1]
tableName = val[2]
partitionColumnName = val[3]
partitionColumnDesc = val[4]
checkColumn = val[5]
lastValue = val[6]
sqlContext.sql("use %s" % databaseName)
if type == "append":
df = getDF("(select * from %s where to_char(%s, 'yyyy-MM-dd')>'%s')" % (tableName, checkColumn, lastValue))
try:
nowLastValue = df.rdd.reduce(max)[checkColumn]
o2hBase(df, databaseName, tableName, partitionColumnName, partitionColumnDesc, True, "")
updataJobSql = "UPDATE job SET last_value='%s' WHERE table_name='%s'" % (nowLastValue, tableName)
if conMysqlDB_exec(updataJobSql):
print("---->SUCCESS: incremental import success")
resultInfoList.append("SUCCESS: %s import success" % tableName)
except ValueError:
print("---->INFO: No new data added!")
resultInfoList.append("INFO: %s ValueError(No new data added!)" % tableName)
pass
except:
print("---->ERROR: other error")
resultInfoList.append("ERROR: %s has other error" % tableName)
pass
else:
df = getDF(tableName)
try:
o2hBase(df, databaseName, tableName, partitionColumnName, partitionColumnDesc, False, type)
print("---->INFO: import success")
resultInfoList.append("SUCCESS: %s import success" % tableName)
except:
print("---->ERROR: import error")
resultInfoList.append("ERROR: %s import error" % tableName)
pass
print("RESULT:")
for i, val in enumerate(resultInfoList):
print(val)
time_end = time.time()
print("---->INFO: time cost", (time_end - time_start)/60, "m")
def max(a, b):
if a>b:
return a
else:
return b
def getDF(tableName):
try:
df = sqlContext.read.format("jdbc") \
.option("url", get_df_url) \
.option("driver", get_df_driver) \
.option("dbtable", tableName) \
.option("user", get_df_user).option("password", get_df_password) \
.load()
except:
print("---->DF_ERROR: get df error")
return df
# o2h
def o2hBase(df, databaseName, tableName, partitionColumnName, partitionColumnDesc, isIncremental, type):
sqlContext.sql("use %s" % databaseName)
schema = df.schema
cnSql, columnNameSql = getTableDesc(df, partitionColumnName)
# rdd = df.map(lambda x : [x[processLineList[i]].replace("\n","").replace("\r","") for i in range(len(processLineList))])
rdd = df.map(lambda x : [(x[i].replace("\n","").replace("\r","") if isinstance(x[i], unicode) or isinstance(x[i], str) else x[i]) for i in range(len(x))])
df = sqlContext.createDataFrame(rdd, schema)
df.registerTempTable("temp%s" % tableName)
if partitionColumnName == "":
if isIncremental:
saveSql = "insert into table %s select * from temp%s" % (tableName, tableName)
else:
if type == "import-overwrite":
saveSql = "insert overwrite table %s select * from temp%s" % (tableName, tableName)
else:
saveSql = "create table %s as select * from temp%s" % (tableName, tableName)
else:
sqlContext.sql("set hive.exec.dynamic.partition=true")
sqlContext.sql("set hive.exec.dynamic.partition.mode=nonstrict")
sqlContext.sql("SET hive.exec.max.dynamic.partitions=100000")
sqlContext.sql("SET hive.exec.max.dynamic.partitions.pernode=100000")
if isIncremental:
saveSql = "insert into table %s partition(%s) SELECT %s,%s FROM temp%s" % (tableName, partitionColumnName, cnSql, partitionColumnName, tableName)
else:
if type != "import-overwrite":
# dynamic partition create table
createTableSql = "create table %s (%s)PARTITIONED BY (%s %s) row format delimited fields terminated by '\t' LINES TERMINATED BY '\n'" % (tableName, columnNameSql, partitionColumnName, partitionColumnDesc)
sqlContext.sql(createTableSql)
print("---->INFO: dynamic partition create success")
saveSql = "insert overwrite table %s partition(%s) SELECT %s,%s FROM temp%s" % (tableName, partitionColumnName, cnSql, partitionColumnName, tableName)
sqlContext.sql(saveSql)
sqlContext.dropTempTable("temp%s" % tableName)
def getTableDesc(sqlDF, partitionColumnName):
columnNameSql = ""
cnSql = ""
dfTypeList = sqlDF.dtypes
for i, val in enumerate(dfTypeList):
if (val[0] != partitionColumnName):
columnNameSql = columnNameSql + val[0] + " " + val[1]
cnSql = cnSql + val[0]
if i + 1 != len(dfTypeList):
columnNameSql = columnNameSql + ","
cnSql = cnSql + ","
return cnSql, columnNameSql
if __name__ == '__main__':
# type : import #General import
# import-overwrite, #General import overwrite table
# append #Incremental import
importData("import")