5)Hive(DML:数据操作语言)

数据导入
  1. 向表中装载数据(load):
    语法:
    load data [local] inpath '/opt/module/datas/student.txt' [overwrite] into table student partition(month=2019);
    ①load data:表示加载数据;
    ②local:表示从本地加载数据,否则从hdfs上加载数据到hive表;
    ③inpath:表示被加载数据的路径;
    ④into:向表中追加数据,加上overwrite表示先删除表中数据再加载新数据;
    ⑤table:表示加载到哪张表;
    ⑥student:表示具体的表;
    ⑦partition:表示上传到指定分区;

  2. 通过查询语句向表中插入数据(insert):
    示例:insert into table student partition(month='201909') values(1,'Curry');

  3. 查询语句中创建表并加载数据(as select):
    根据查询结果创建表(查询的结果会添加到新创建的表中):
    create table if not exists student as select id,name from student2;

  4. 创建表时通过Location指定加载数据路径:

    1. 创建表并指出表在hdfs的位置:
      create table if not exists stu(name string,age int) row format delimited fields terminated by '\t' location '/user/hive/warehouse/student5'
    2. 上传数据到hdfs上:
      dfs -put /opt/module/datas/student.txt /user/hive/warehouse/student5
  5. Import数据到指定Hive表中:
    注意:先用export导出后,再将数据导入
    import table student2 partition(month='201709') from '/user/hive/warehouse/export/student';

数据导出
  1. Insert导出
    1. 将查询的结果导出到本地
      insert overwrite local directory '/opt/module/hive/data/stu.txt' select * from stu;
    2. 将查询的结果格式化导出到本地
      insert overwrite local directory '/opt/module/hive/data/stu1.txt' row format delimited fields terminated by '\t' select * from stu;
    3. 将查询的结果导出到HDFS上(没有local)
      insert overwrite directory '/user/zy/stu,txt' row format delimited fields terminated by '\t' select * from stu;
  2. Hadoop命令导出到本地
    dfs -get '/user/hive/warehouse/student/month=201709/000000_0' /opt/module/datas/export/student3.txt;
  3. Hive Shell 命令导出
    基本语法:hive -f/-e 执行语句或脚本 > file_name
    示例:bin/hive -e 'select * from default.student;' > /opt/module/datas/export/student.txt;
  4. Export导出到HDFS上
    export table default.stu to '/user/hive/warehouse/export/student';
清除表中数据

truncate只能删除管理表,不能删除外部表中的数据
truncate table student;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值