-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocal_load.py
63 lines (50 loc) · 1.56 KB
/
local_load.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
#!/usr/bin/env python3
import pymysql
import os
import requests
import time
path="./tsv"
cnx = pymysql.connect(user='wubx', password='wubxwubx',
host='192.168.1.100',
port = 3307,
database='default')
cursor = cnx.cursor()
copy_sql="copy into ontime from @~ files=('%s') file_format=(type='tsv' compression='auto')"
print("1. Get file lists")
os.chdir(path)
files=os.listdir()
files.sort()
print("2. Upload file lists")
for i in files:
t1= time.time()
sql="presign upload @~/%s"%(i)
cursor.execute(sql)
url = cursor.fetchone()[2]
with open(i, 'rb') as f:
data =f.read()
response=requests.put(url, data=data)
t2=time.time()
print("Upload %s use: %s"%(i, (t2-t1)))
print("3. End upload file lists")
sql="select value from system.settings where name='max_threads'"
cursor.execute(sql)
max_threads=int(cursor.fetchone()[0])
if max_threads >16:
max_threads=16
print("4. Use copy into load data")
for i in range(0, len(files), max_threads):
t1=time.time()
f=files[i:i+max_threads]
l=','.join("'{0}'".format(x) for x in f)
copy_sql="copy into ontime from @~ files=(%s) file_format=(type='tsv' compression='auto') purge=true"%(l)
#print(copy_sql)
cursor.execute(copy_sql)
t2=time.time()
print("Load %s into databend use: %s s"%(l,(t2-t1)))
print("5. End load data")
print("6. count table")
sql="select count(*) from ontime"
cursor.execute(sql)
print("ontime table rows: %d"%(cursor.fetchone()[0]))
cursor.close()
cnx.close()