Hive个人心得笔记之基础指令 川长思鸟来 2021-12-14 06:21 347阅读 0赞 # Hive个人心得笔记之基础指令 # -------------------- # 一.基础指令 # **desc 表 查看表结构** ![20190708113624901.png][] 没有主键,可以重复提交 手动提交 效率过低,可以通过文件形式传输 <table> <tbody> <tr> <td style="border-color:#a3a3a3;vertical-align:top;width:2.502in;"> <p style="margin-left:0in;">load data local inpath '/home/software/1.txt' into table stu;</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:2.0895in;"> <p style="margin-left:0in;">通过加载文件数据到指定的表里</p> </td> </tr> </tbody> </table> 但是,如果不指定分割格式,无法正常传输 ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdVk1MjE_size_16_color_FFFFFF_t_70][] ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdVk1MjE_size_16_color_FFFFFF_t_70 1][] ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdVk1MjE_size_16_color_FFFFFF_t_70 2][] <table> <tbody> <tr> <td style="border-color:#a3a3a3;vertical-align:top;width:2.502in;"> <p style="margin-left:0in;"><span style="color:#ffffff;">命令</span></p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:2.0895in;"> <p style="margin-left:0in;"><span style="color:#ffffff;">作用</span></p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:6.327in;"> <p style="margin-left:0in;"><span style="color:#ffffff;">额外说明</span></p> </td> </tr> <tr> <td style="border-color:#a3a3a3;vertical-align:top;width:2.502in;"> <p style="margin-left:0in;">show databases;</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:2.0895in;"> <p style="margin-left:0in;">查看都有哪些数据库</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:6.327in;"> <p style="margin-left:0in;"> </p> </td> </tr> <tr> <td style="border-color:#a3a3a3;vertical-align:top;width:2.502in;"> <p style="margin-left:0in;">create database park;</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:2.0895in;"> <p style="margin-left:0in;">创建park数据库</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:6.327in;"> <p style="margin-left:0in;">创建的数据库,实际是在Hadoop的HDFS文件系统里创建一个目录节点,统一存在: /user/hive/warehouse 目录下</p> </td> </tr> <tr> <td style="border-color:#a3a3a3;vertical-align:top;width:2.502in;"> <p style="margin-left:0in;">use park;</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:2.0895in;"> <p style="margin-left:0in;">进入park数据库</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:6.327in;"> <p style="margin-left:0in;"> </p> </td> </tr> <tr> <td style="border-color:#a3a3a3;vertical-align:top;width:2.502in;"> <p style="margin-left:0in;">show tables;</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:2.0895in;"> <p style="margin-left:0in;">查看当前数据库下所有表</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:6.327in;"> <p style="margin-left:0in;"> </p> </td> </tr> <tr> <td style="border-color:#a3a3a3;vertical-align:top;width:2.502in;"> <p style="margin-left:0in;">create table stu</p> <p style="margin-left:0in;"> (id int,name string);</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:2.0895in;"> <p style="margin-left:0in;">创建stu表,以及相关的两个字段</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:6.327in;"> <ul style="margin-left:.2562in;"> <li>hive里,表示字符串用的是string,不用char和varchar</li> <li>所创建的表,也是HDFS里的一个目录节点</li> </ul></td> </tr> <tr> <td style="border-color:#a3a3a3;vertical-align:top;width:2.502in;"> <p style="margin-left:0in;">insert into stu values(1,'zhang')</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:2.0895in;"> <p style="margin-left:0in;">向stu表插入数据</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:6.327in;"> <ul style="margin-left:.2562in;"> <li>HDFS不支持数据的修改和删除,因此已经插入的数据不能够再进行任何的改动</li> <li>在Hadoop2.0版本后支持了数据追加。实际上,insert into 语句执行的是追加操作</li> <li>hive支持查询,行级别的插入。不支持行级别的删除和修改</li> <li>hive的操作实际是执行一个job任务,调用的是Hadoop的MR</li> <li>插入完数据之后,发现HDFS stu目录节点下多了一个文件,文件里存了插入的数据,因此,hive存储的数据,是通过HDFS的文件来存储的。</li> </ul></td> </tr> <tr> <td style="border-color:#a3a3a3;vertical-align:top;width:2.502in;"> <p style="margin-left:0in;">select * from stu</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:2.0895in;"> <p style="margin-left:0in;">查看表数据</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:6.327in;"> <p style="margin-left:0in;">也可以根据字段来查询,比如select id from stu</p> </td> </tr> <tr> <td style="border-color:#a3a3a3;vertical-align:top;width:2.502in;"> <p style="margin-left:0in;">drop table stu</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:2.0895in;"> <p style="margin-left:0in;">删除表</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:6.327in;"> <p style="margin-left:0in;"> </p> </td> </tr> <tr> <td style="border-color:#a3a3a3;vertical-align:top;width:2.502in;"> <p style="margin-left:0in;">select * from stu</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:2.0895in;"> <p style="margin-left:0in;">查询stu表数据</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:6.327in;"> <p style="margin-left:0in;"> </p> </td> </tr> <tr> <td style="border-color:#a3a3a3;vertical-align:top;width:2.502in;"> <p style="margin-left:0in;">load data local inpath '/home/software/1.txt' into table stu;</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:2.0895in;"> <p style="margin-left:0in;">通过加载文件数据到指定的表里</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:6.327in;"> <p style="margin-left:0in;"> </p> <ul style="margin-left:.2562in;"> <li>在执行完这个指令之后,发现hdfs stu目录下多了一个1.txt文件。由此可见,hive的工作原理实际上就是在管理hdfs上的文件,把文件里数据抽象成二维表结构,然后提供hql语句供程序员查询文件数据</li> <li>可以做这样的实验:不通过load 指令,而通过插件向stu目录下再上传一个文件,看下hive是否能将数据管理到stu表里。</li> </ul></td> </tr> <tr> <td style="border-color:#a3a3a3;vertical-align:top;width:2.502in;"> <p style="margin-left:0in;">create table stu1(id int,name string) row format delimited fields terminated by ' ';</p> <p style="margin-left:0in;"> </p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:2.0895in;"> <p style="margin-left:0in;">创建stu1表,并指定分割符 空格。</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:6.327in;"> <p style="margin-left:0in;"> </p> </td> </tr> <tr> <td style="border-color:#a3a3a3;vertical-align:top;width:2.502in;"> <p style="margin-left:0in;">desc stu</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:2.0895in;"> <p style="margin-left:0in;">查看 stu表结构</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:6.327in;"> <p style="margin-left:0in;"> </p> </td> </tr> <tr> <td style="border-color:#a3a3a3;vertical-align:top;width:2.502in;"> <p style="margin-left:0in;">create table stu2 like stu</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:2.0895in;"> <p style="margin-left:0in;">创建一张stu2表,表结构和stu表结构相同</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:6.327in;"> <p style="margin-left:0in;"> like只复制表结构,不复制数据</p> </td> </tr> <tr> <td style="border-color:#a3a3a3;vertical-align:top;width:2.502in;"> <p style="margin-left:0in;">insert overwrite table stu2 select * from stu</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:2.0895in;"> <p style="margin-left:0in;">把stu表数据插入到stu2表中</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:6.327in;"> <p style="margin-left:0in;"> </p> </td> </tr> <tr> <td style="border-color:#a3a3a3;vertical-align:top;width:2.502in;"> <p style="margin-left:0in;">insert overwrite local directory '/home/stu' row format delimited fields terminated by ' ' select * from stu;</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:2.0895in;"> <p style="margin-left:0in;">将stu表中查询的数据写到本地的/home/stu目录下</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:6.327in;"> <p style="margin-left:0in;"> </p> </td> </tr> <tr> <td style="border-color:#a3a3a3;vertical-align:top;width:2.502in;"> <p style="margin-left:0in;">insert overwrite directory '/stu' row format delimited fields terminated by ' ' select * from stu;</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:2.0895in;"> <p style="margin-left:0in;">将stu表中查询的数据写到HDFS的stu目录下</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:6.327in;"> <p style="margin-left:0in;"> </p> </td> </tr> <tr> <td style="border-color:#a3a3a3;vertical-align:top;width:2.502in;"> <p style="margin-left:0in;">from stu insert overwrite table stu1 select * insert overwrite table stu2 select *;</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:2.0895in;"> <p style="margin-left:0in;">将stu表中查询的数据写到stu1以及stu2两张表中</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:6.327in;"> <p style="margin-left:0in;"> </p> </td> </tr> <tr> <td style="border-color:#a3a3a3;vertical-align:top;width:2.502in;"> <p style="margin-left:0in;">alter table stu rename to stu2</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:2.0895in;"> <p style="margin-left:0in;">为表stu重命名为stu2</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:6.327in;"> <p style="margin-left:0in;"> </p> <p style="margin-left:0in;"> </p> </td> </tr> <tr> <td style="border-color:#a3a3a3;vertical-align:top;width:2.502in;"> <p style="margin-left:0in;">alter table stu add columns (age int);</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:2.0895in;"> <p style="margin-left:0in;">为表stu增加一个列字段age,类型为int</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:6.327in;"> <p style="margin-left:0in;"> </p> </td> </tr> <tr> <td style="border-color:#a3a3a3;vertical-align:top;width:2.502in;"> <p style="margin-left:0in;">exit</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:2.0895in;"> <p style="margin-left:0in;">退出hive</p> </td> <td style="border-color:#a3a3a3;vertical-align:top;width:6.327in;"> <p style="margin-left:0in;"> </p> </td> </tr> </tbody> </table> [20190708113624901.png]: /images/20211213/4f85f223245a4c32b9379080635ff942.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdVk1MjE_size_16_color_FFFFFF_t_70]: /images/20211213/c991342048964fd3ae44dbe688cbded7.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdVk1MjE_size_16_color_FFFFFF_t_70 1]: /images/20211213/22add205585f43b490f8b6f3f0d7bd48.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdVk1MjE_size_16_color_FFFFFF_t_70 2]: /images/20211213/9e791003790f4868b1795b73075d0668.png
相关 Hive个人心得笔记之数据库和数据仓库 Hive个人心得笔记之数据库和数据仓库 一、数据仓库和数据库对比 <table> <tbody> <tr> <td 青旅半醒/ 2021年12月14日 08:51/ 0 赞/ 698 阅读
相关 Hive个人心得笔记之基础指令 Hive个人心得笔记之基础指令 -------------------- 一.基础指令 desc 川长思鸟来/ 2021年12月14日 06:21/ 0 赞/ 348 阅读
相关 Hive个人心得笔记之Hive优化 Hive个人心得笔记之Hive优化 一.Hive的优化 Hive的优化 1. 小表缓存:将小表的放入内存 悠悠/ 2021年12月11日 23:49/ 0 赞/ 429 阅读
相关 Hive个人心得笔记之内置函数、UDF Hive个人心得笔记之内置函数 目录 Hive个人心得笔记之内置函数 一.内置函数 淩亂°似流年/ 2021年12月11日 23:15/ 0 赞/ 624 阅读
相关 Hive个人心得笔记之表结构,数据类型,join Hive个人心得笔记之表结构,数据类型,join 目录 Hive个人心得笔记之表结构,数据类型 叁歲伎倆/ 2021年12月11日 22:55/ 0 赞/ 397 阅读
还没有评论,来说两句吧...