0

我正在尝试使用 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)
4

1 回答 1

4

根据您的问题,我假设 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;

请注意,分隔符和输入文件格式在创建表时明确给出。

表也​​以 TEXTFILE 格式存储。这是由于输入文件的格式。

如果你想要 ORC 表,那么输入文件应该是 ORC 格式(Hive 'load data' 命令只是将文件复制到 hive 数据文件并且不对数据进行任何转换)。一种可能的解决方法是使用 STORED AS TEXTFILE 创建一个临时表,将数据加载到其中,然后将数据从该表复制到 ORC 表。

使用“加载”命令加载数据。

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/

于 2018-12-21T17:46:54.873 回答