将管道加载到配置单元

t3psigkw  于 2021-06-27  发布在  Hive
关注(0)|答案(1)|浏览(286)

我尝试在配置单元表中使用python加载管道分隔的csv文件,但没有成功。请帮忙。
完整代码:

from pyhive import hive 
host_name = "192.168.220.135" 
port = 10000 
user = "cloudera" 
password = "cloudera" 
database = "default" 
conn = hive.Connection(host=host_name, port=port, username=user, database=database) 
print('Connected to DB: {}'.format(host_name)) 
cursor = conn.cursor() 
Query = """LOAD DATA LOCAL inpath '/home/cloudera/Desktop/ccna_test/RERATING_EMMCCNA.csv' INTO TABLE python_testing fields terminated by '|' lines terminated by '\n' """ 
cursor.execute(Query)
v7pvogib

v7pvogib1#

根据您的问题,我假设csv格式如下所示,您希望查询将数据加载到配置单元表中。
值1 |值2 |值3
值4 |值5 |值6
值7 |值8 |值9
首先应该有一个配置单元表,可以使用下面的查询创建。 create table python_testing ( col1 string, col2 string, col3 string ) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde' with SERDEPROPERTIES ( "separatorChar" = "|") stored as textfile; 请注意,分隔符和输入文件格式是在表创建时显式给出的。
表也以文本文件格式存储。这是由于输入文件的格式。
如果需要orc表,则输入文件应为orc格式(hive“load data”命令仅将文件复制到hive数据文件,不对数据进行任何转换)。一种可能的解决方法是创建一个临时表,其中存储为textfile,将数据加载到其中,然后将数据从此表复制到orc表。
使用“load”命令加载数据。 load data local inpath '/home/hive/data.csv' into table python_testing; /home/hive/data.csv应该是您的文件路径。
欲了解更多详情,请访问博客帖子-http://forkedblog.com/load-data-to-hive-database-from-csv-file/

相关问题