<4> hive 执行命令方式,数据加载

本文详细介绍了Hive中执行SQL命令的各种方式,包括命令行、静默模式、详细模式及通过文件执行,同时深入解析了数据加载的方法,如创建表时加载、本地数据加载、HDFS数据加载等,特别关注了分区表的数据加载流程。

hive执行命令的方式
hive -e "hql" 命令行执行hsql
hive -e "select * from mydb2.c1"
hive -S 静默模式,控制台不输出Logging信息
hive -S -e "select * from mydb2.c1"
hive -v 详细模式,会把执行的sql打印出来
hive -v -e "select * from mydb2.c1"
hive -f "path" 执行文件
hive -f "/home/lz/createtable.txt"
hive -i 执行初始化xx自己实现

list jar;显示缓存了哪些jar包
list file;显示缓存了哪些文件

hive 数据加载
创建表时加载
create table c3 as select * from c1;
创建表时指定数据位置
create table c4(id int,name string) location 'path';
本地数据加载
load data local inpath 'path' overwrite into table c5;
加载hdfs数据
load data inpath 'path' overwrite into table c6;
查询语句加载数据
create table c4 like c2;(复制表结构)
1.insert into/overwrite table c4
select id,name
from c2
where id=1;
2.from c2
insert into/overwrite c5
select id,name
where id=2;

注意:字段对于和关系型数据库不一样的
hive在加载数据的时候不做字段检查和字段类型检查的
在查询的时候才做检查

分区表加载数据
同表加载数据一样,只是需要指定分区分区
注意:数据存放的路径层次要和表的分区一致
如果分区表没有新增分区,即使目标路径下已经有数据了,但依然查不到数据

本地加载数据
create table if not exists d1(
id int,
name string,
age int
)
partitioned by(dt string)
row format delimited fields terminated by ','
stored as textfile;

1,tom,24
2,jack,25
3,lc,27
4,ljc,28
load data local inpath 'path' overwrite/into table d1 partition(dt='20180416');
加载hdfs数据
load data inpath 'path' overwrite/into table tablename partition(dt='20180416');
由查询语句加载数据
create table if not exists d3(
id int,
name string
)
partitioned by(dt string)
row format delimited fields terminated by ','
stored as textfile;
insert overwrite table d3 partition(dt='20180815') select name,id from d1;
insert overwrite table d2 partition(dt='20180815') select id,name,age from d1;
注意:由查询语句加载数据,字段必须一样多,d2表三个字段,那么select d1表的三个字段才能插入

hive数据加载需要注意的问题
1.分隔符问题,分隔符默认是单个字符(因为默认只取第一个字符)
2.load数据字段类型不能互相转化是,查询返回NULL
create table if not exists e1(id int,name string) row format delimited fields terminated by ',' stored as textfile;
1,tom
2,jack
load data local inpath '/home/lz/e1.txt' overwrite into table e1;
create table e2 like e1;
load data local inpath 'path' overwrite into table e2;
(这个查询那么字段为NULL,string转不了int)
3.select查询插入,字段类型不能互相转化时,插入数据为NULL
insert into table e2 select name,id from e1;
(这个id字段为NULL,string转不了int)
4.select查询插入分区数据,字段值顺序要预表中字段顺序一致,名称可不一致
5.外部分区表需要添加分区才能看到数据(alter table add partition(xx=xx))

一、设置内嵌Derby模式 # 上传解压安装包 cd /export/server/ tar zxvf apache-hive-3.1.2-bin.tar.gz mv apache-hive-3.1.2-bin hive #解决 hadoop、hive 之间 guava 版本差异 cd /export/server/hive rm -rf lib/guava-19.0.jar cp /export/server/hadoop-3.1.4/share/hadoop/common/lib/guava-27.0-jre.jar ./lib/ #修改 hive 环境变量文件 添加 Hadoop_HOME cd /export/server/hive/conf/ mv hive-env.sh.template hive-env.sh vim hive-env.sh export HADOOP_HOME=/export/server/hadoop export HIVE_CONF_DIR=/export/server/hive/conf export HIVE_AUX_JARS_PATH=/export/server/hive/lib #初始化 metadata cd /export/server/hive bin/schematool -dbType derby -initSchema #启动 hive 服务 bin/hive 二、设置直连数据库模式 略 三、设置远程服务器模式 1.安装MySQL 2.hive的安装 # 上传解压安装包 cd /export/server/ tar zxvf apache-hive-3.1.2-bin.tar.gz mv apache-hive-3.1.2-bin hive #解决 hadoop、hive 之间 guava 版本差异 cd /export/server/hive rm -rf lib/guava-19.0.jar cp /export/server/hadoop-3.1.4/share/hadoop/common/lib/guava-27.0-jre.jar ./lib/ #添加 mysql jdbc 驱动到 hive 安装包 lib/文件下 mysql-connector-java-5.1.32.jar #修改 hive 环境变量文件 添加 Hadoop_HOME cd /export/server/hive/conf/ mv hive-env.sh.template hive-env.sh vim hive-env.sh export HADOOP_HOME=/export/server/hadoop export HIVE_CONF_DIR=/export/server/hive/conf export HIVE_AUX_JARS_PATH=/export/server/hive/lib #新增 hive-site.xml 配置 mysql 等相关信息 vim hive-site.xml #初始化 metadata cd /export/server/hive bin/schematool -initSchema -dbType mysql -verbos #初始化成功会在 mysql 中创建 74 张表 3. hive-site.xml 配置 mysql 等相关信息 <configuration> <!-- 存储元数据 mysql 相关配置 --> <property> <name>javax.jdo.option.ConnectionURL</name> <value> jdbc:mysql://Master:3306/hive?createDatabaseIfNotExist=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8</value> </property> <property> <name>javax.jdo.option.ConnectionDriverName</name> <value>com.mysql.jdbc.Driver</value> </property> <property> <name>javax.jdo.option.ConnectionUserName</name> <value>root</value> </property> <property> <name>javax.jdo.option.ConnectionPassword</name> <value>123456</value> </property> <!-- H2S 运行绑定 host --> <property> <name>hive.server2.thrift.bind.host</name> <value>Master</value> </property> <!-- 远程模式部署 metastore 服务地址 --> <property> <name>hive.metastore.uris</name> <value>thrift://Master:9083</value> </property> <!-- 关闭元数据存储授权 --> <property> <name>hive.metastore.event.db.notification.api.auth</name> <value>false</value> </property> <!-- 关闭元数据存储版本的验证 --> <property> <name>hive.metastore.schema.verification</name> <value>false</value> </property> </configuration> 4.初始化 metadata cd /export/server/hive bin/schematool -initSchema -dbType mysql -verbos 若格式化失败 Hive 重新格式化 1. 删除用户目录:首先,需要删除用户目录中的所有内容。hdfs dfs -rm -r /user/ 2. 删除 Hive 数据库:接下来,删除 Hive 格式化过程中创建的 Hive 数据库。 mysql> drop database hive; 3. 重新格式化:最后,使用 schematool 命令重新初始化 Hive 的元数据。 5.远程模式 Metastore 服务手动启动 前台启动 关闭 ctrl+c /export/server/hive/bin/hive --service metastore #后台启动 进程挂起 关闭使用 jps + kill #输入命令回车执行 再次回车 进程将挂起后台 nohup /export/server/hive/bin/hive --service metastore & #前台启动开启 debug 日志 /export/server/hive/bin/hive --service metastore --hiveconf hive.root.logger=DEBUG,console 6.Hive Client 使用 /export/server/hive/bin/hive 7.其他机器上通过 bin/hive 访问 hive metastore 服务 如果需要在其他机器上通过 bin/hive 访问 hive metastore 服务,只需要在 该机器的 hive-site.xml 配置中添加 metastore 服务地址即可。 #前提步骤 将hive安装包发送各节点,并配置环境变量 scp -r /export/server/hive slave1:/export/server scp -r /export/server/hive slave2:/export/server #将环境变量发送到slave1、slave2,去对应节点source /etc/profile scp -r /etc/profile slave2:/etc scp -r /etc/profile slave3:/etc 修改salve1和slave2的配置文件 <configuration> <property> <name>hive.metastore.uris</name> <value>thrift://Master:9083</value> </property> </configuration> 测试是否能够访问 cd …/ /export/server/hive/bin/hive show databases我现在弄到第七步,要怎么弄,其他的都弄好了,以下是我的虚拟机信息. 请帮我根据文档要求完成操作,以下是我的虚拟机信息. 用户名为:ma 虚拟机master IP为192.168.81.130 虚拟机slaver1 IP为192.168.81.131 虚拟机slaver2 IP为192.168.81.132 虚拟机slaver3 IP为192.168.81.133 hive路径: /export/server/hive hive版本: 3.1.2 hadoop路径: /export/server/hadoop hadoop版本: 3.1.3 jdk路径: /export/server/jdk jdk版本: 1.8.0_241 mysql路径: /export/server/mysql mysql版本: 5.7.43 目前我在master虚拟机,当前所在路径:/export/server 所有操作均在root下完成 请给我最详细的操作方法及代码,务必详细,谢谢
最新发布
10-31
```markdown ## 文件类型判断 该文件为“普通文本型”。 --- ## 如何打开 Hive 命令行(CLI) 要打开 Hive 命令行界面(即 `hive>` 提示符),请根据您当前的环境选择以下任意一种方式。 --- ### ✅ 方法一:使用完整路径启动(推荐初学者) ```bash /export/server/hive/bin/hive ``` > 这是最稳妥的方式,无需依赖环境变量,直接进入 Hive CLI。 成功后会出现: ``` hive> ``` --- ### ✅ 方法二:配置环境变量后直接使用 `hive` 命令 #### 1. 配置全局环境变量 ```bash vim /etc/profile.d/hive.sh ``` 写入以下内容: ```bash export HIVE_HOME=/export/server/hive export PATH=$PATH:$HIVE_HOME/bin ``` #### 2. 使配置立即生效 ```bash source /etc/profile.d/hive.sh ``` #### 3. 直接启动 Hive ```bash hive ``` > 只要配置正确,任何目录下都可以执行命令。 --- ### 📌 注意事项 - 启动前请确保 **Hadoop 已正常运行**,因为 Hive 依赖 HDFS。 - 建议在固定目录(如 `/export/server/hive`)中启动 Hive,避免内嵌 Derby 元数据路径错乱。 - 若提示权限问题,请确认 `hive` 脚本是否可执行: ```bash chmod +x /export/server/hive/bin/hive ``` --- ### 🔁 验证是否能打开 Hive 执行: ```bash which hive ``` 预期输出: ``` /export/server/hive/bin/hive ``` 然后运行: ```bash hive ``` 看到 `hive>` 提示符即表示打开成功! --- ### 💡 示例流程汇总 ```bash # 1. (可选)进入 Hive 安装目录 cd /export/server/hive # 2. 打开 Hive 命令hive # 3. 成功进入 hive> SHOW DATABASES; # 4. 退出 hive> quit; ``` --- ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值