shell中执行hive指令以及hive中执行shell和hdfs指令语法

0. 简介

主要介绍了三种环境命令执行语法:

  1. shell中执行hive指令
  2. hive中执行shell指令
  3. hive中执行hdfs指令

1. shell中执行hive指令

语法:hive [-hiveconf x=y]* [<-i filename>]* [<-f filename> | <-e query-string>] [-S]

说明:

  1. -i 从文件初始化HQL。执行sql文件并留在hive交互窗口;hive -i sql_string.hql

    我们先创建一个sql_string.hql文件:vim sql_string.sql

    i切换到输入模式,输入如下sql,再按esc,输入:wq回车保存脚本文件;

    然后再执行hive -i指令

    输出后,停留再hive交互窗口

  2. -e从命令行执行指定的HQL;

    比如执行一条hive执行并将输出的数据重定向到当前工作目录:hive -e "use mydb;select appid,game_type from dim_game_info limit 10" > game_info.txt;当然也可以把hive参数设置语句配里边,多条语句英文分号分隔即可:hive -e "set mapred.reduce.tasks=10;select appid,game_type from mydb.dim_game_info limit 10;"参数配置只对该会话生效;

  3. -f 执行HQL脚本:hive -f sql_string.hql;这个sql_string.hql是一个包含hive命令的文本文件,案例类同hive -i指令。

  4. -v 输出执行的HQL语句到控制台

  5. -p connect to Hive Server on port number

  6. -hiveconf x=y Use this to set hive/hadoop configuration variables.可用来配置一些hive参数,比如:hive -hiveconf mapred.reduce.tasks=10;设置reduce个数为10个;

  7. -S 设置静默状态,不输出ok等消息;

2. hive中执行shell指令

只需要在指令最前方加上!号再以;结尾即可

比如查看/usr/local/hive/hive_data目录下的文件:!ls /usr/local/hive/hive_data;
清除窗口内容指令:!clear;

查看文本文件前两行:!head /usr/local/hive/hive_data/target_game.txt -n2;

需要注意的是,以下两类不可行:

  1. 重定向,比如(前两行保存到test.txt文件中):!head /usr/local/hive/hive_data/target_game.txt -n2 > test.txt
  2. 管道用法(查看前两行):!cat /usr/local/hive/hive_data/target_game.txt | head -n2

3. hive中执行hdfs指令

在Hive窗口中执行hdfs命令。直接Hadoop命令中的关键字"hadoop"去掉,指令以dfs开头,以分号;结尾即可,例如,

查看HDFS目录下的文件情况:dfs -ls /user/hive/warehouse/mydb.db/;

复制文件到本地系统:

!pwd;查看工作目录

复制到工作目录下:
dfs -get /user/hive/warehouse/mydb.db/ads_mg_core_target_value ./hive_data/target_name_get.txt;

再查看本地系统目录下的文件:!ls ./hive_data;出现了命名为target_name_get.txt的本地文件

更多hdfs命令:点我查看常用hdfs指令

相关推荐
Bug快跑-119 小时前
云原生微服务环境下分布式事务优化实践——提升系统一致性与高并发处理能力
hadoop
叡鳍19 小时前
Hive---案例7-6 列转行
数据仓库·hive·hadoop
❀͜͡傀儡师1 天前
docker-compose一键部署Hadoop集群
hadoop·docker·容器
干就完事了2 天前
Hive内置函数
数据仓库·hive·hadoop
布吉岛没有岛_2 天前
Hadoop学习_week1
大数据·hadoop
lijun_xiao20092 天前
Apache Hadoop-学习笔记1
hadoop·学习·apache
q***07142 天前
Spring Boot 从 2.7.x 升级到 3.3注意事项
数据库·hive·spring boot
阿杜杜不是阿木木2 天前
在 Hadoop 生态使用 JuiceFS,并为Hive提供HDFS存储安装指南
hive·hadoop·hdfs
小坏讲微服务3 天前
MaxWell中基本使用原理 完整使用 (第一章)
大数据·数据库·hadoop·sqoop·1024程序员节·maxwell
一只小青团3 天前
Hadoop之HDFS
大数据·hadoop·分布式