Hive创建表的几种方式

    hive执行的三种方式:

    1. 用hive CLI

    2. 终端执行hive命令: hive -e hive语句

    3. 终端执行hive脚本: hive -f  hive脚本

    如果需要通过jdbc来连接hive,需要在终端开启hiveserver2服务

nohup hive --service hiveserver2 &

netstat -ntpl | grep 10000  // 查看该进程是否启动

    创建表的几种方式:

    1. 创建内表:

create table customer(
    customerID int,
    firstName string,
    lastName string,
    birthday timestamp)
row format delimited fields terminated by ',';
    向表中导入linux本地数据:

load data local inpath '/opt/custorm' into table customer;
    2. 创建外表:

create external table salaries(
    gender string,
    age int,
    salary double,
    zip int)
row format delimited fields terminated by ',';
    3. 创建静态分区表:

    

create table employees(
    id int,
    name string)
partitioned by (dept string)
row format delimited fields terminated by ',';

    向表中导入HDFS上的文件:
load data inpath '/user/root/employees.txt' overwrite into table employees;
    4. 创建动态分区表:

    参数设置(直接在CLI下输入): 

set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
    创建动态分区表的代码与静态分区表一样,只是多了上面两行设置。

create table student_dynamic(
    id int,
    name string,
    classes string)
partitioned by (class string)
row format delimited fields terminated by '\t';
    数据导入动态分区表:

insert overwrite table student_dynamic_partition (class) select *, classes from student;
    上面的代码,可以自动将classes中不同类别的数据自动分区
    5. 创建带有数据的表:

create table stucopy as select id,name,score from student;


    
    数据导入的三种方式:

    1. 从本地导入数据到Hive表

    2. 从HDFS导入数据到HIve表(会将HDFS上的文件mv到建表时指定的目录下,而不是copy)

    3. 从其他表中查询并导入
    4. 多表插入

from emp insert into table empcopy1
select id, name limit 5
insert into table empcopy2
select name,salary where salary > 10000.0;
    数据导出到本地:

insert overwrite local directory '/opt/customer_exp' 
row format delimited fields terminated by ','
select * from employees limit 5;  
    数据导出到HDFS:

</pre><pre name="code" class="plain">insert overwrite directory '/user/root/customer_exp' 
row format delimited fields terminated by ','
select * from employees limit 5;
    Hive中不允许删除存在表的数据库,需要强制删除:

drop database if exists test cascate;









  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值