我花了相当多的时间研究使用Teradata Fastload上载csv文件的可行方法,但是文档简短,有限且不清楚。
给出特定的csv,如何将其上传到给定的数据库?
答案 0 :(得分:3)
我用Python类创建了一个Gist,该类包含使用pyodbc与Teradata进行通信的所有必要方法。
专门用于使用此方法上传csv文件的csv文件必须满足某些条件:
示例:
“ value1” | “ value2” | “ value3”
“ value1” | “ value2” | “ value3”
“ value1” | “ value2” | “ value3”
这可以通过使用以下命令实现: 将熊猫作为pd导入 从csv导入QUOTE_ALL data.to_csv('tmp.csv',index = False,sep ='|',quotechar ='“',引用= QUOTE_ALL,header = False)
之后,您可以使用此功能:
def upload_csv(database, csv_file, table, columns, user, password, verbose=True):
"""
This function uses Fastlaod utily to upload csv file delimited with "|" instead of ',' and where all values in
file are quoted. Ex: "value1" | "value2" | . . .
:param csv_file: csv file without columns names
:param table: Insertion table
:param columns: Column names
:param user: username
:param password:
:param verbose: True | False if output is required
"""
script_text = fastload_template.substitute(DATA_FILE=csv_file,
COLUMN_DEFINITIONS=',\n'.join(['"' + column + '" (varchar(2000))' for column in columns]),
VALUES=',\n'.join([':' + '"' + column + '"' for column in columns]),
DATABASE=database, TABLE=table, USER=user, PASSWORD=password)
tmp_file = csv_file[:-4]
script = open(tmp_file, "w")
script.writelines("%s\n" % script_text)
script.close()
try:
if verbose:
run(["fastload < " + tmp_file], check=True, shell=True)
else:
run(["fastload < " + tmp_file], check=True, shell=True, stdout=open(os.devnull, 'w'))
except CalledProcessError as e:
if e.args[0] != 8: # Fastload gives error 8 but insert is working.. so don't touch :)
raise e
os.remove(tmp_file)
答案 1 :(得分:0)
只需使用 Teradata 提供的 tdload
实用程序命令即可。
tdload -h teradata -u td_user -p td_pass \
-t TPT_Jobname1 \
-f filename.csv -d ',' --TargetWorkingDatabase target_db \
--TargetErrorLimit 100 target_db.staging_table1