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指令

相关推荐
一张假钞1 小时前
Spark SQL读写Hive Table部署
hive·sql·spark
想做富婆2 小时前
Hive:窗口函数[ntile, first_value,row_number() ,rank(),dens_rank()]和自定义函数
数据仓库·hive·hadoop
好记性+烂笔头9 小时前
4 Hadoop 面试真题
大数据·hadoop·面试
B站计算机毕业设计超人1 天前
计算机毕业设计Python+CNN卷积神经网络考研院校推荐系统 考研分数线预测 考研推荐系统 考研爬虫 考研大数据 Hadoop 大数据毕设 机器学习
hadoop·python·机器学习·spark·网络爬虫·课程设计·数据可视化
字节全栈_rJF1 天前
Hive 整合 Spark 全教程 (Hive on Spark)
hive·hadoop·spark
好记性+烂笔头2 天前
2 MapReduce
大数据·hadoop·mapreduce
字节全栈_ZKt2 天前
Hadoop集群中Hbase的介绍、安装、使用_root@master001 hadoop]# start-hbase
大数据·hadoop·hbase
一张假钞2 天前
Sqoop源码修改:增加落地HDFS文件数与MapTask数量一致性检查
java·hadoop·hdfs·sqoop
weixin_307779132 天前
设计转换Apache Hive的HQL语句为Snowflake SQL语句的Python程序方法
数据仓库·hive·python·sql
想做富婆2 天前
Hive:窗口函数(1)
数据仓库·hive·hadoop