#建表

create table sign_in (uri string , test string) row format delimited fields terminated by ‘|’;

#假设不存在表则创建

create table if not exists mytable (id bigint,name string);

#创建外部表

hive> create external table lss_sign_in  (uri string,token string)  row format delimited fields terminated by '\t’  ;

#从HDFS载入数据到表

hive> load data inpath 'hdfs:/user/root/input/sign_in/sign.uri' overwrite into table lss_sign_in;

#本地文件载入到hive 仓库:

hive> LOAD DATA LOCAL INPATH '/user/test.txt' OVERWRITE INTO TABLE lss;

#从HDFD载入到HIVE 仓库(注意这里记载完毕后会删除掉HDFS上的文件)

hive> LOAD DATA INPATH 'hdfs:/user/root/input/test.txt' OVERWRITE INTO TABLE kevin;    

Loading data to table default.kevin

Deleted hdfs://ixx-test-124:9019/user/hive/warehouse/kevin

尽管源文件表面上看是在HDFS删除了。事实上文件被移到hive HDFS数据仓库中去了。

默认数据库仓库的文件保存在: /user/hive/warehouse/

当中kevin相应的HIVE中得元数据表名称。

test.txt就是源文件,从本地载入文件和HDFS载入文件类似。

./hadoop fs -text /user/hive/warehouse/kevin/test.txt  

#查看表结构

describe tableName;

id      int

name    string

age     int

也能够使用:desc tableName。

#显示全部函数

show functions;

#查看函数用法

hive> describe function substr;

substr(str, pos[, len]) - returns the substring of str that starts at pos and is of length len

#依据URI分组,count(uri) 能够统计URI的PV

hive> select uri,count(uri) from sign_in group by uri;

#统计UV

hive> select uri, count(distinct koudaitoken) from sign_in group by uri;    

#也相同支持 limit 

#改动表结构

hive> alter table sign_in_uri replace columns(uri string);         

#把Select结果插入到表中

insert overwrite table sign_in_uri    select uri from sign_in group by uri;

#链接查询

#左链接

 select sign_in.*,sign_in_uri.* from sign_in_uri left outer join sign_in on(sign_in_uri.uri = sign_in.uri);

#右链接

select sign_in.*,sign_in_uri.* from sign_in_uri right outer join sign_in on(sign_in_uri.uri = sign_in.uri) ;

#全链接

hive> select sign_in.*,sign_in_uri.* from sign_in_uri full outer join sign_in on(sign_in_uri.uri = sign_in.uri) limit 100; 

#in 查询(hive不支持IN)使用semi join 达到类似效果

hive> select sign_in_uri.* from sign_in_uri left semi join sign_in on(sign_in_uri.uri = sign_in.uri) limit 10;

使用正则

hive> select regexp_extract(koudaitoken,'\\[.*\\]',0) from sign_in limit 10;

#查看函数用法

hive> describe function regexp_extract  

regexp_extract(str, regexp[, idx]) - extracts a group that matches regexp

#注意写入HDFS或本地文件夹时会删除掉文件夹下的内容。  

#将Hive数据导出到本地文件夹

hive> insert overwrite local directory '/luanshoushen/hive' select * from sign_in_uri;

#将Hive数据导出到HDFS

hive> insert overwrite directory 'user/root/input/hive' select * from sign_in;

#使用一个查询将结果写入HDFS文件夹和本地文件夹

hive> from sign_in 

    > insert overwrite local directory '/luanshoushen/hive' select * 

    > insert overwrite directory '/user/root/input/' select * 

    > ;

hive HQL笔记的更多相关文章

  1. hive学习笔记之一:基本数据类型

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  2. hive学习笔记之三:内部表和外部表

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  3. hive学习笔记之四:分区表

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  4. hive学习笔记之五:分桶

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  5. hive学习笔记之六:HiveQL基础

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  6. hive学习笔记之七:内置函数

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  7. hive学习笔记之九:基础UDF

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  8. hive学习笔记之十:用户自定义聚合函数(UDAF)

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本文是<hive学习笔记>的第十 ...

  9. hive学习笔记之十一:UDTF

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

随机推荐

  1. matplotlib系列——折线图

    import numpy as np import matplotlib.pyplot as plt import matplotlib #数据准备 #numpy.linspace(start, st ...

  2. UVALive 4728 Squares(旋转卡壳)

    Squares The famous Korean IT company  plans to make a digital map of the Earth with help of wireless ...

  3. 62.Longest Valid Parentheses(最长的有效括号)

    Level:   Medium 题目描述: Given a string containing just the characters '(' and ')', find the length of ...

  4. 【目录】linux 编程

    随笔分类 - linux 编程 Linux编程 24 shell编程(结构化 if [ condition ] 数值比较,字符串比较) 摘要: 一.概述 接着上篇讲的结构化命令,最后讲到了test命令 ...

  5. C#用new和override来实现抽象类的重写区别

    一,抽象的实现 using System; using System.Collections.Generic; using System.Linq; using System.Text; namesp ...

  6. Logback配置,error和普通日志分离

    <?xml version="1.0" encoding="utf-8"?> <configuration> <springPro ...

  7. window杀死端口

    获取端口的pid:netstat  -aon|findstr "8382" 杀死pid : taskkill /pid [] -t -f

  8. java的BigDecimal比较大小

    java的BigDecimal比较大小 //前提为a.b均不能为null if(a.compareTo(b) == -1){ System.out.println("a小于b"); ...

  9. Django 与 Flask框架的比较

    Django Django恐怕是最有代表性的Python框架了.它是一个遵循MMVC架构模式的开源框架.它的名字来自Django Reinhardt,一个法国作曲家和吉他演奏家,很多人认为他是历史上最 ...

  10. 【leetcode】921. Minimum Add to Make Parentheses Valid

    题目如下: 解题思路:上周都在忙着参加CTF,没时间做题,今天来更新一下博客吧.括号问题在leetcode中出现了很多,本题的解题思路和以前的括号问题一样,使用栈.遍历Input,如果是'('直接入栈 ...