hive的排序,分組练习

数据:

添加表和插入数据(数据在Linux本地中)

create table if not exists tab1(
IP string,
SOURCE string,
TYPE string
)
row format delimited fields terminated by '|'
stored as textfile; load data local inpath '/home/data/data1.txt' into table tab1;

1.问题:(top10)按照来源排序,访问量高的排最上面

select source,count(*) num
from tab1
group by source
order by num desc;

select 查询在order by 前

2.问题:(推荐系统)给一个用户ip地址,得到用户经常访问的应用类型后,推荐用户同种类型的其他应用

数据二:

建表,填数据:

create table if not exists tab2(
id string,
name string,
url string,
pid string
)
row format delimited fields terminated by '\t'
stored as textfile; load data local inpath '/home/data/data2.txt' into table tab2;

1.问题:(数据清洗)合并name与url,格式为 NAME:name|URL:url

select concat('NAME:',name,'|','URL:',url)
from tab2
where name is not null and url is not null;

数据三:

表的建立和数据插入

create table if not exists tab4(
no string,
province string,
city string,
pid string,
cid string
)
row format delimited fields terminated by '\t'
stored as textfile; load data local inpath '/home/data/data4.txt' into table tab4;

1.问题:从源数据中筛出pid与省份、cid与城市,并且创建新表保存 去重 distinct

这里使用加行键的方法,实行唯一标识。

select pid,province
from tab4
group by pid,province
limit 1 select t1.cid,t1.city,t1.rank
from(
select cid,city,row_number() over (partition by cid order city) rank
from tab4
group by cid,city) t1
where t1.rank=1;

数据四:

新建表以及添加数据

create table if not exists tab5(
lac string,
cellid string,
cell_name string,
longitude string,
latitude string
)
row format delimited fields terminated by '\t'
stored as textfile; load data local inpath '/home/data/data5.txt' into table tab5;

1.问题:从cell_name列中截取需要的部分,例如 HZD0090,截取D0090杭州四季青蔬菜公司,截取D0090

数据五:

建表+添加数据

create table if not exists tab6(
id string,
service string
)
row format delimited fields terminated by '\t'
stored as textfile; load data local inpath '/home/data/data6.txt' into table tab6;

问题:去除所有父类服务,只要子类服务(id 是字符串类型)

select * from tab6 where id>100


数据六:

添加数据:

create table if not exists tab7(
id string,
type string,
sagem string
) row format delimited fields terminated by '\t'
stored as textfile; load data local inpath '/home/data/data7.txt' into table tab7;

问题:按照设备类型,统计出现的频率

select type,count(*)
from tab7
group by type;

数据七:

问题:去重后存入到新表中

select col,row_number() over (partition by col order by col) rank
from tab10
group by col;

hive的排序,分組练习的更多相关文章

  1. Hive为什么要分桶

    对于每一个表(table)或者分区, Hive可以进一步组织成桶,也就是说桶是更为细粒度的数据范围划分.Hive也是针对某一列进行桶的组织.Hive采用对列值哈希,然后除以桶的个数求余的方式决定该条记 ...

  2. Hive学习笔记——Hive中的分桶

    对于每一个表(table)或者分区, Hive可以进一步组织成桶,也就是说桶是更为细粒度的数据范围划分.Hive也是针对某一列进行桶的组织.Hive采用对列值哈希,然后除以桶的个数求余的方式决定该条记 ...

  3. Hive 学习之路(五)—— Hive 分区表和分桶表

    一.分区表 1.1 概念 Hive中的表对应为HDFS上的指定目录,在查询数据时候,默认会对全表进行扫描,这样时间和性能的消耗都非常大. 分区为HDFS上表目录的子目录,数据按照分区存储在子目录中.如 ...

  4. Hive 系列(五)—— Hive 分区表和分桶表

    一.分区表 1.1 概念 Hive 中的表对应为 HDFS 上的指定目录,在查询数据时候,默认会对全表进行扫描,这样时间和性能的消耗都非常大. 分区为 HDFS 上表目录的子目录,数据按照分区存储在子 ...

  5. 入门大数据---Hive分区表和分桶表

    一.分区表 1.1 概念 Hive 中的表对应为 HDFS 上的指定目录,在查询数据时候,默认会对全表进行扫描,这样时间和性能的消耗都非常大. 分区为 HDFS 上表目录的子目录,数据按照分区存储在子 ...

  6. Crystal Report分組中的序號重新遞增

    客戶要批次列印發票,也就是報表需要按照發票號碼(INV_NO)進行分組,每個發票里還有明細的item,之前因為直接抓RecordNumber,所以該欄位只能從1開始計數,遇到新的發票發號不會重新從1開 ...

  7. hive 分组排序,topN

    hive 分组排序,topN 语法格式:row_number() OVER (partition by COL1 order by COL2 desc ) rankpartition by:类似hiv ...

  8. hive -- 分区,分桶(创建,修改,删除)

    hive -- 分区,分桶(创建,修改,删除) 分区: 静态创建分区: 1. 数据: john doe 10000.0 mary smith 8000.0 todd jones 7000.0 boss ...

  9. laravel 路由分組

    laravel 路由分組 Route::group(['prefix' => 'admin'], function () { $namespacePrefix="\\App\\Http ...

随机推荐

  1. ubuntu上传到百度网盘

    1 2 亲测可以上传

  2. Centos7上安装、破解bamboo6.0.3

    1.下载bamboo安装包,地址:https://www.atlassian.com/software/bamboo/download?_ga=2.65378349.245489969.1512876 ...

  3. [转载]C# TimeSpan 计算时间差(时间间隔)

    TimeSpan 结构  表示一个时间间隔. 命名空间:System 程序集:mscorlib(在 mscorlib.dll 中) 说明: 1.DateTime值类型代表了一个从公元0001年1月1日 ...

  4. 为KindEditor 添加“一键去除空格功能”

    环境说明:KindEditor  4.1.11 一.确定你在使用KindEditor时,引用的是kindEditor-all.js,找到任何一个已经存在的功能,例如,清除HTML代码,我在做的时候本来 ...

  5. 【题解】Luogu P4867 Gty的二逼妹子序列

    原题传送门 同Luogu P4396 [AHOI2013]作业 询问多了10倍,但还能跑过(smog #include <bits/stdc++.h> #define N 100005 # ...

  6. Oracle rac 监听状态异常远程连接问题解决(TNS-12541 TNS-12560 TNS-00511 Linux Error:111 ORA-12502)

    问题1现象 数据导出脚本执行失败,报错如下 ORA-12537 到服务器上查看,报错: [oracle@test ~]$ lsnrctl status LSNRCTL - Production on ...

  7. No Directionality widget found.错误记录。

    import 'package:flutter/material.dart'; void main() => runApp(new Center(child: new Text('Hello, ...

  8. (转)A Recipe for Training Neural Networks

    A Recipe for Training Neural Networks Andrej Karpathy blog  2019-04-27 09:37:05 This blog is copied ...

  9. 2、Docker基础用法

    容器镜像:https://hub.docker.com/ Docker架构图:  https://ruby-china.org/topics/22004 Docker使用客户端-服务器(client- ...

  10. css浮动与清除浮动

    css浮动 首先,我们要知道,css中块级元素在页面中是独占一行的,自上而下排列,也就是我们所说的流,通常称为标准流. 以div为例,div是块级元素,如下: 可以清楚地看到,div是独占一行的,di ...