我正在尝试将csv导入aws redshift( postgresql 8.x)。
数据流是: mysql、->、parquet文件、s3、->、csv文件、s3 ->红移文件。
表结构
mysql表sql:
create table orderitems
(
id char(36) collate utf8_bin not null
primary key,
store_id char(36) collate utf8_bin not null,
ref_type int not null,
ref_id char(36) collate utf8_bin not null,
store_product_id char(36) collate utf8_bin not null,
product_id char(36) collate utf8_bin not null,
product_name varchar(50) null,
main_image varchar(200) null,
price int not null,
count int not null,
logistics_type int not null,
time_create bigint not null,
time_update bigint not null,
...
);我使用相同的sql在redshift中创建表,但它在导入csv时出错。
我的代码将csv导入redshift (python)
# parquet is dumpy by sqoop
p2 = 'xxx'
df = pd.read_parquet(path)
with smart_open.smart_open(p2, 'w') as f:
df.to_csv(f, index=False) # python3 default encoding is utf-8
conn = psycopg2.connect(CONN_STRING)
sql="""COPY %s FROM '%s' credentials 'aws_iam_role=%s' region 'cn-north-1'
delimiter ',' FORMAT AS CSV IGNOREHEADER 1 ; commit ;""" % (to_table, p2, AWS_IAM_ROLE)
print(sql)
cur = conn.cursor()
cur.execute(sql)
conn.close()得到误差
通过检查STL_LOAD_ERRORS在product_name列上找到的错误

real_value是伊利畅轻蔓越莓奇亚籽风味发酵乳215g/瓶(中文)。
所以看起来像是编码问题。因为mysql是utf-8,csv也是utf-8,所以我不知道出了什么问题。
发布于 2019-01-09 08:21:34
您的列是varchar数据类型,长度为50。那是50个字节,不是50个字符。您给出的字符串示例看起来大约是16个汉字,每个字符大概是3个字节(UTF-8)和4个ASCII字符(每个字符是一个字节),因此大约是52个字节。这比列的字节长度长,因此导入失败。
https://stackoverflow.com/questions/54102184
复制相似问题