我通过java代码运行hive查询。 例如:
“SELECT * FROM table WHERE id> 100”
如何将结果导出到hdfs文件。
答案 0 :(得分:61)
以下查询将结果直接插入HDFS:
INSERT OVERWRITE DIRECTORY '/path/to/output/dir' SELECT * FROM table WHERE id > 100;
答案 1 :(得分:35)
此命令会将输出重定向到您选择的文本文件:
$hive -e "select * from table where id > 10" > ~/sample_output.txt
答案 2 :(得分:23)
这会将结果放在目录中的制表符分隔文件中:
INSERT OVERWRITE LOCAL DIRECTORY '/home/hadoop/YourTableDir'
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
STORED AS TEXTFILE
SELECT * FROM table WHERE id > 100;
答案 3 :(得分:2)
@sarath 如果我想从另一个表运行另一个select *命令并写入同一个文件,如何覆盖该文件?
INSERT OVERWRITE LOCAL DIRECTORY'/ home / training / mydata / outputs'
SELECT expl,count(expl)as total
FROM(
SELECT爆炸(拆分)作为探索
FROM(
SELECT拆分(单词,'')作为拆分
来自wordcount
)t2
)t3
GROUP BY探索;
这是sarath的问题的一个例子
以上是存储在本地目录中的输出文件中的字数统计作业 :)
答案 4 :(得分:1)
我同意tnguyen80的回应。请注意,当查询中存在特定字符串值时,更好地用双引号给出整个查询。
例如:
$hive -e "select * from table where city = 'London' and id >=100" > /home/user/outputdirectory/city details.csv
答案 5 :(得分:1)
理想的方法是使用“INSERT OVERWRITE DIRECTORY'/ pathtofile'select * from temp where id> 100”而不是“hive -e'select * from ...'> /filepath.txt “
答案 6 :(得分:1)
要直接将文件保存在HDFS中,请使用以下命令:
hive> insert overwrite directory '/user/cloudera/Sample' row format delimited fields terminated by '\t' stored as textfile select * from table where id >100;
这会将内容放在HDFS文件夹/ user / cloudera / Sample中。
答案 7 :(得分:1)
两种方法可以存储HQL查询结果:
INSERT OVERWRITE DIRECTORY "HDFS Path" ROW FORMAT DELIMITED FIELDS TERMINATED BY '|'
SELECT * FROM XXXX LIMIT 10;
$hive -e "select * from table_Name" > ~/sample_output.txt
$hive -e "select * from table where city = 'London' and id >=100" > /home/user/outputdirectory/city details.csv
答案 8 :(得分:0)
将此行输入Hive命令行界面:
SyntaxError: can't assign to function call
insert overwrite directory '/data/test' row format delimited fields terminated by '\t' stored as textfile select * from testViewQuery;
- 一些具体观点
答案 9 :(得分:0)
示例:
创建外部表以将查询结果存储在'/ user / myName / projectA_additionaData /'
CREATE EXTERNAL TABLE additionaData
(
ID INT,
latitude STRING,
longitude STRING
)
COMMENT 'Additional Data gathered by joining of the identified cities with latitude and longitude data'
ROW FORMAT DELIMITED FIELDS
TERMINATED BY ',' STORED AS TEXTFILE location '/user/myName/projectA_additionaData/';
将查询结果输入到临时表中
insert into additionaData
Select T.ID, C.latitude, C.longitude
from TWITER
join CITY C on (T.location_name = C.location);
删除临时表
drop table additionaData
答案 10 :(得分:0)
要设置输出目录和输出文件格式等,请尝试以下操作:
INSERT OVERWRITE [LOCAL] DIRECTORY directory1
[ROW FORMAT row_format] [STORED AS file_format]
SELECT ... FROM ...
示例:
INSERT OVERWRITE DIRECTORY '/path/to/output/dir'
ROW FORMAT DELIMITED
STORED AS PARQUET
SELECT * FROM table WHERE id > 100;