首页 > 其他分享 >Hive(二)DML数据操作语言

Hive(二)DML数据操作语言

时间:2024-09-02 14:36:34浏览次数:3  
标签:opt 语言 partition DML Hive month dept hive table

DML数据操作

一、数据导入

1、向表中装载数据

hive> load data [local] inpath '路径' [overwrite] into table 表名 [partition (partcol1=val1,…)];

(1)load data:表示加载数据
(2)local:表示从本地加载数据到hive表;否则从HDFS加载数据到hive表
(3)inpath:表示加载数据的路径
(4)overwrite:表示覆盖表中已有数据,否则表示追加
(5)into table:表示加载到哪张表
(6)表名:表示具体的表
(7)partition:表示上传到指定分区
2、通过查询语句向表中插入数据
(1)基本插入

insert into table  student partition(month='201709') values(1,'wangwu');
insert overwrite table student partition(month='201708') select id, name from student where month='201709';
  • 从student表中选择所有month分区为2017年9月的记录的id和name字段,然后将这些数据插入到(或覆盖)month分区为2017年8月的student表分区中。

(2)多插入

from dept_partition
              insert overwrite table dept_partition partition(month='201707')
              select deptno,dname,loc where month='201709'
              insert overwrite table dept_partition partition(month='201706')
              select deptno,dname,loc  where month='201709';

(3)查询语句中创建表并加载数据
根据查询结果创建表,查询的结果会添加到新创建的表中

create table if not exists student3 as select id, name from student;

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

  • 指定在HDFS上的位置
create table if not exists student5(
id int, name string)
row format delimited fields terminated by '\t'
location '/user/hive/warehouse/student5';
  • 上传数据到HDFS
dfs -put /opt/datas/student.txt /opt/hive/warehouse/student5;

(5)将数据导入到指定Hive表中
先用export导出后,再讲数据导入

import table student2 partition(month='201709') from '/opt/hive/warehouse/export/student';

从HDFS上的/opt/hive/warehouse/export/student目录中,导入数据到Hive表student2的month='201709'分区。

2、数据导出
(1)insert导出

  • 将查询的结果导出到本地
insert overwrite local directory '/opt/datas' select * from dept_partition;
  • 将查询的结果格式化导出到本地
insert overwrite local directory '/opt/datas/dept1'
row format delimited
fields terminated by '|'
select * from dept_partition;
  • 将查询的结果导出到HDFS上
insert overwrite directory '/opt/datas/dept'
row format delimited
fields terminated by '|'
select * from dept_partition;

(2)Hadoop命令导出到本地

dfs -get /opt/hive/warehouse/employee/employee.txt /opt/datas/dept2/dept.txt;
  • 源文件路径:/opt/hive/warehouse/employee/employee.txt
  • 目标路径:/opt/datas/dept2/dept.txt

(3)Hive Shell命令导出

  • 基本语法:hive -f/-e 执行语句或者脚本 > file
hive -e 'select * from hivetest.dept_partition;' > /opt/datas/dept3/dept.txt;

在shell窗口执行,需要库名.表名,需要本地文件夹存在

(4)export导出到HDFS上

export table hivetest.dept_partition to '/opt/datas/dept2';

3、清除表中数据

truncate table student;

只能删除管理表,不能删除外部表中的数据

标签:opt,语言,partition,DML,Hive,month,dept,hive,table
From: https://www.cnblogs.com/shihongpin/p/18392673

相关文章

  • 【华为OD机试真题E卷 Python语言】494、猜字谜 | 机试真题+思路参考+代码解析(E卷复用)
    文章目录一、题目......
  • Hive(一)数据类型以及DDL数据定义
    Hive数据类型一、基本数据类型Hive数据类型Java数据类型TINYINTbyteSMALINTshortINTintBIGINTlongBOOLEANbooleanFLOATfloatDOUBLEdoubleSTRINGstringTIMESTAMPBINARY对于Hive的String类型相当于数据库的varchar类型,该类型是......
  • C语言函数递归(含扫雷进阶思路)
    文章目录一、什么是递归二、递归的使用思路和限制条件1.递归的使用思路2.递归的限制条件三、递归的举例举例1:求n的阶乘2.举例2:顺序打印⼀个整数的每⼀位四、递归与迭代对比五、递归与迭代对比举例七、扫雷进阶思路一、什么是递归  递归是学习C语⾔函数绕不开的......
  • 基于C语言的选择排序算法
    一、选择排序算法的基本原理        选择排序算法是一种简单直观的排序算法。其基本原理为:        首先,将待排序的数组划分为已排序和未排序两部分。初始时,已排序部分为空,未排序部分为整个数组。        在每一轮排序中,从未排序部分找出最小(或最大)......
  • 基于C语言的归并排序算法
    一、归并排序的基本概念        归并排序(MergeSort)是一种基于分治法思想的高效稳定排序算法。其基本原理是将一个待排序的数组不断地分割成较小的子数组,直到每个子数组只包含一个元素,此时每个子数组都被认为是有序的。然后,再将这些有序的子数组合并成一个更大的有序......
  • JS设计模式之“语言之魂” - 原型模式
    前言当我们学习JavaScript的时候,经常会听到原型(prototype)、原型链(prototypechain)和原型模式(prototypepattern)这些概念,它们之间有什么关联呢?怎么样才能使用好原型模式呢?一.“语言之魂”-原型模式概念原型模式是JavaScript语言的核心机制之一,是JavaScript中最基本......
  • 模拟实现strlen函数(C语言)
    #include<stdio.h>//strlen实现intStrlen(chararr[]){ inti=0; intnum=0;//长度的数值 for(i=0;arr[i]!='\0';i++)//当arr[i]不为\0时继续 { num++;//长度增加 } returnnum;//返回长度的值}intmain(){ //创建一个数组 chararr[100]=......
  • C语言中的#和##
    在C语言中,#和##是预处理器运算符,具有特定的功能。一、#运算符(字符串化运算符)概念:#运算符被称为字符串化运算符。它的作用是将其后面的参数转换为字符串常量。作用:在宏定义中,#可以将传入的参数转换为字符串,方便输出调试信息或者构建特定的字符串。代码例子:#incl......
  • C程序设计语言(第2版·新版)练习题1-18
    练习1-18 编写一个程序,删除每个输入行末尾的空格及制表符,并删除完全是空格的行。#include<stdio.h>#defineMAXLINE1000intgetline(chars[],intlim);intremove_tail(chars[]);intmain(intargc,char*argv[]){(void)argc;(void)argv;......
  • 逆序一句话如:you like her 变为 reh ekil uoy(C语言)
    #include<stdio.h>#include<string.h>//逆序一句话如://youlikeher变为rehekiluoyintmain(){ //创建一个字符串 chararr[100]={0}; //输入字符串内容 gets(arr); //逆序整句话(即把ilike变为ekili) intsz=strlen(arr)-1; intleft=0,righ......