Hive基本操作

1. Hive中执行linux中的命令

!linux命令;

2. Hive中执行hdfs的操作

dfs -ls /;

3. Hive的模式

  • 本地模式

    • 开发阶段建议使用本地模式
    • set hive.exec.mode.local.auto=true;
  • 集群模式(默认)

    • 生产环境建议使用集群模式

4. Hive的访问

  • cli(命令行)
  • webui(很少用)
  • api

5. Hive中的常见操作

  • 查看数据库的列表
    • show databases;
  • 创建数据库
    • create database db1;
  • 显示当前使用的数据库
    • set hive.cli.print.current.db=true;
  • 删除数据库
    • drop database db1;

6. Hive中表的操作

  • 创建表

    • create table t1(id int,name string);
  • 删除表

    • drop table t1;
  • 增加列

    • alter table t1 add columns(age int);
  • 删除列(其实下面是错误的,不支持删除列)

    • alter table t1 drop cloumns(age)

7. 执行hive的脚本

  • 在linux中执行hive脚本

    • hive -f xxx.hql
  • 在hive中执行

    • source xxx.sql

8. Hive中的DQL

  • 给hive表添加数据
    • insert into t1 values(‘admin’,12)(不推荐使用);
    • 直接给hdfs中添加文件即可(不推荐使用) hdfs dfs -put users.csv /user/hive/warehouse/db1.db/t3(不推荐使用)
    • load data [local] inpath ‘/root/users.csv’ into table t3;(不推荐使用,但是开发阶段使用较多)

9. Hive中的复合数据类型

9.1 数组类型array
  • 数据格式

    admin,12,java1-python1-java

    admin1,11,java-python1-java

    admin2,312,java-python-java

  • 创建表

    • create table t4(name string,age int,loves array) row format delimited fields terminated by ‘,’ collection items terminated by ‘-’ lines terminated by ‘\n’ ;
  • 加载数据

    • load data local inpath ‘/root/users04.csv’ into table t4;
  • 查询array

select loves[0] from t4;
9.2 map类型
  • 数据格式

    小明,12,数学:20,男

    小红,21,数学:22,女

    大胖,25,数学:30,男

  • 创建表

    • create table t5(name string,age int,score map<string,int>) row format delimited fields terminated by ‘,’ map keys terminated by ‘:’ lines terminated by ‘\n’
  • 加载数据

    • load data local inpath ‘/root/users05.csv’ into table t5;
  • 查询

select score['数学'] from t5;
9.3 struct类型(相当于给数组给定了名称)
  • 数据格式

    小明,10:男

    小花,10:女

    大胖,10:男

  • 创建表

    create table t6(name string ,info struct<age:int,sex:string>) row format delimited  fie
    lds terminated by ',' collection items  terminated  by ':';
    
  • 加载数据

    • load data local inpath ‘/root/users06.csv’ into table t6;
  • 查询

    • select info.age from t6;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值