您现在的位置是:热点 >>正文
「基础」SQL-Hive中的Select From 解析
热点94040550人已围观
简介...
你好,我是巡山猫!
今天我们来讲讲Hive中最常用的 select from 语句知识要点。
Hive系列文章预计10-20篇,主要讲数据分析中最基础的SQL技能。
01-查询表中的内容
查询指定的某一列或某几列,命令如下:
SELECT 列名1,列名2,…… FROM 表名;
查询表中的所有字段时,可以使用*代表所有字段。星号(*)是选取所有列的快捷方式。命令如下:
SELECT * FROM 表名;
如果我们想查询表 t_od_use_cnt 中的所有的user_id和use_cnt,具体命令如下:
SELECT user_id &nbs「基p; ,use_cnt FROM app.t_od_use_cnt;
备注:app是数据库名,如果当前查询表与当前使用数据库一致,可以省略不写
02-Hive严格模式
在Hive中这样写虽然语法正确(不加分区),但在实际工作中这样写很可能会报错。
因为Hive中的表一般数据量极大,为了防止用户误操作进行全表扫描,可以设置为查询分区表时必须加入分区限制。比如这里我们的分区字段是date_8这个日期字段,工作中的表会要求我们必须限定查询哪几天的分区数据。这里我们可以输入设置参数进行模拟,命令如下:
hive (app)> set hive.mapred.mode; hive.mapred.mode=nonstr础」ict hive (app)> set hive.mapred.mode=strict; hive (app)> set hive.mapred.mode; hive.mapred.mode=strict
然后我们重新执行上面的查询语句,报错如下:
hive (app)> SELECT user_id > ,use_cnt&SQnbsp; > FROM app.t_od_use_cnt; FAILED: SemanticException [Error 10041]: No partition predicate found for Alias "t_od_use_cnt" Table "t_od_use_cnt" hive (app)>
如下使用where语句限定分区即可解决这个问题:
SELL-ECT user_id ,use_cnt FROM app.t_od_use_cnt WHERE date_8 = '20210420'; 03-limit限制查询返回行数
上面的查询语句会返回查询到的所有数据,但有时候我们只是要确认一下表中的数据内容,或者要指定行数据,比如只要100行,这时只需要在查询语句后加上(limit 数字)即可。
查询表t_od_use_cnt中前5行数据,命令如下:
SELECT user_id ,useHi_cnt FROM app.t_od_use_cnt WHERE date_8 = 20190101 Limit 5;
运行结果如下:
hive (app)> SELECT user_id > ,use_cnt >&venbsp;FROM app.t_od_use_cnt > WHERE date_8 = 20210420 Limit 5; OK user_id use_cnt 10000 6 10001 49 10002 23 10003 1 10004 29 Time 中的taken: 0.829 seconds, Fetched: 5 row(s) hive (app)> 04-列四则运算
在查询时可以对数值类型的字段进行加减乘除和取余等四则运算
下面我们将表t_od_use_cnt中use_cnt列和is_active列相乘得到一个新列,其他用法依次类推。
hive (app)> SELECT user_id >  Se;,use_cnt > ,is_active > ,use_cnt * is_active >le FROM app.t_od_use_cnt > WHERE date_8 = 20210420 Limit 5; OK user_id use_cnt is_active _c3 10000 6 1 6 10001 49 1 49&nctbsp;10002 23 1 23 10003 1 0 0 10004 29 1 29 Time taken: 0.124 seconds, Fetched: 5 row(s) hive (app)> 05-列别名
可以看到上面的例子中我们通过两个列相乘人为制造出一个新列,系统默认将其列名起为_c3。通常有必要给这些新产生的列起一个别名。已有列的列名如果含义不清晰也可 F以通过起别名的方式进行更改。不过别名只在本条SQL语句中生效,不影响原表中的字段名。
这里顺便介绍一下字段命名规则:
1.不能和已有字段重复
2.只能包括小写字母(a-z)、数字(0-9)、下划线(_)
3.以字母开头
4.单词之间用下划线_分割
这里我们将别名起为active_use_cnt,在列后面加 AS active_use_cnt即可。另AS可以省略,只用空格分隔别名也可以生效
hive (app)> SELECT user_id > ro,use_cnt > ,is_active > ,use_cnt * is_active AS active_use_cnt m > FROM app.t_od_use_cnt > WHERE date_8 = 20210420 Limit 5; OK user_id use_cnt is_active active_use_cnt 10000 6 1 6 10001&nbs解析p; 49 1 49 10002 23 1 23 10003 1 0 0 10004 29 1 29 Time taken: 0.239 seconds, Fetched: 5 row(s) hive (app)>
Tags:
相关文章
标签: 亿咖通科技 施罗德 沃尔沃汽车 吉利瑞典控股
热点云梦级4月16日云梦级消息,全球出行科技企业亿咖通科技控股(纳斯达克股票代码:ECX)今日正式宣布,任命资深国际高管施罗德女士(Lone Fønss Schrøder)出任公司董事长,自即日起生效。...
【热点】
阅读更多标签: 百度 慧播星 百度一镜 数字人
热点云梦级6月8日云梦级消息,据新浪科技报道,百度 MEG(Mobile Ecosystem Group,移动生态事业群组,是百度的核心业务线)进行了新的一轮组织调整:合并商业部与电商事业部,成立大商业事...
【热点】
阅读更多Kubernetes 1.20正式发布
热点我们很高兴为大家带来Kubernetes 1.20版本,2020年中的第三个、也是最后一个版本!此版本中包含42项增强功能:包括11项增强功能的稳定版、15项增强功能的beta版以及16项增强功能的a...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.rostaing.com/catalogsearch/result/?cat=22&q=谷歌霸屏服务【tg:seogoogle987】,hyn
- https://www.urmc.rochester.edu/news/search?q=谷歌霸屏服务【tg:seogoogle987】,vDT
- https://www.umc.edu/search.aspx?ID=I4gnDKRS991MFcIDDzoYze4aWSAszAm7&q=谷歌霸屏服务【tg:seogoogle987】,Uh9
- https://www.trockenmittel.ch/catalogsearch/result/?iC=ZM3&q=谷歌霸屏服务【tg:seogoogle987】,M3x
- https://www.yli.ro/catalogsearch/result/?q=谷歌霸屏服务【tg:seogoogle987】,jHJ
- https://www.merchbar.com/search?q=谷歌霸屏服务【tg:seogoogle987】,sod
- https://www.polliwogs.com/search/谷歌霸屏服务【tg:seogoogle987】,c7c
- https://search.danawa.com/dsearch.php?query=谷歌霸屏服务【tg:seogoogle987】inv
- https://www.ushmm.org/search/results/?ID=8s0587Oi8UTv74zPPM4fuhwjNpe2hsEE&q=谷歌霸屏服务【tg:seogoogle987】,7bn
- https://www.losgatoschamber.com/list/search?action=SEARCH&q=谷歌霸屏服务【tg:seogoogle987】,JCC
- https://www.ucycle.com/search/=谷歌霸屏服务【tg:seogoogle987】,lTy
- https://www.searcychamber.com/list/search?AsfnA=DtkTQzs&q=谷歌霸屏服务【tg:seogoogle987】,q8y
- https://www.usslexington.com/?action=QUERY&s=谷歌霸屏服务【tg:seogoogle987】,sS2
- https://www.dba.dk/soeg/?soeg=谷歌霸屏服务【tg:seogoogle987】KbP
- https://m.shoppinghow.kakao.com/m/search/q/谷歌霸屏服务【tg:seogoogle987】EwU
- https://hsmoa.com/search?query=谷歌霸屏服务【tg:seogoogle987】cNt
- https://www.yr.no/en/search?q=谷歌霸屏服务【tg:seogoogle987】,qEz
- https://www.polar-tech.com/?action=QUERY&s=谷歌霸屏服务【tg:seogoogle987】,lrE
- https://www.remit.co.jp/en/?s=谷歌霸屏服务【tg:seogoogle987】,bxs
- https://www.kaliastyle.com/catalogsearch/result/?q=GOOGLE谷歌霸屏服务【tg:seogoogle987】,W6C
- https://www.quarterjacksurgery.co.uk/?AT=3rBy&s=谷歌霸屏服务【tg:seogoogle987】,x5b
- https://www.pearson.com/en-us/search.html?aq=谷歌霸屏服务【tg:seogoogle987】,eu2
- https://www.u-bourgogne.fr/?cat=117&s=谷歌霸屏服务【tg:seogoogle987】,ofj
- https://www.nctsn.org/search?cat=191&query=谷歌霸屏服务【tg:seogoogle987】,Vjp
- https://nicolazzi.it/en/?s=谷歌霸屏服务【tg:seogoogle987】,AoQ
- https://www.routesonline.com/search/?query=谷歌霸屏服务【tg:seogoogle987】,vCC
- https://www.temu.com/search_result.html?search_key=谷歌霸屏服务【tg:seogoogle987】Km7
- https://www.toy-people.com/?s=谷歌霸屏服务【tg:seogoogle987】,jzu
- https://www.maafushidive.com?collections?vendors?q=谷歌霸屏服务【tg:seogoogle987】,8PI
- https://www.tycoonstory.com/?s=谷歌霸屏服务【tg:seogoogle987】,NGp
- https://www.nyif.com/catalogsearch/result/?q=谷歌霸屏服务【tg:seogoogle987】,H83
- https://www.pierreherme.com/catalogsearch/result/?q=谷歌霸屏服务【tg:seogoogle987】,Zob
- https://www.revere.org/search?action=SEARCH&q=谷歌霸屏服务【tg:seogoogle987】,Lt7
- https://www.krisinformation.se/en/search?q=谷歌霸屏服务【tg:seogoogle987】,pvl
- https://www.world-architects.com/en/search?term=谷歌霸屏服务【tg:seogoogle987】,y6R
- https://www.nextavenue.org/?action=QUERY&s=谷歌霸屏服务【tg:seogoogle987】,Rv7
- https://www.teacherspayteachers.com/Browse/Search:谷歌霸屏服务【tg:seogoogle987】,ziE
- https://www.flickr.com/photos/tags/谷歌霸屏服务【tg:seogoogle987】ixX
- https://www.spicebar.de/catalogsearch/result/?q=谷歌霸屏服务【tg:seogoogle987】,h4U
- https://www.supersauna.de/catalogsearch/result/?q=谷歌霸屏服务【tg:seogoogle987】,47w
- https://www.randjleather.co.uk/index.php?route=product/search&tag=谷歌霸屏服务【tg:seogoogle987】,oOx
- https://www.lneg.pt/en/?s=谷歌霸屏服务【tg:seogoogle987】,34a
- https://www.symatoys.com/search.html?ID=UyMD6aO58bPrJvQAB1I0N69KzJBZeNPC&keyword=谷歌霸屏服务【tg:seogoogle987】,W10
- https://www.mesec.cz/vyhledavani/?qs=谷歌霸屏服务【tg:seogoogle987】,Ynz
- https://www.kubb-tool.bfs.admin.ch/en/search?action=CHECK&q=谷歌霸屏服务【tg:seogoogle987】,0xP
- https://www.riohondo.edu/?action=QUERY&s=谷歌霸屏服务【tg:seogoogle987】,fan
- https://www.shacknews.com/editorial-search?q=谷歌霸屏服务【tg:seogoogle987】,g2h
- https://www.seagrant.wisc.edu/?page=63&s=谷歌霸屏服务【tg:seogoogle987】,FW1
- https://www.tumblr.com/tagged/谷歌霸屏服务【tg:seogoogle987】,AKV
- https://www.youm7.com/Home/Search?allwords=谷歌霸屏服务【tg:seogoogle987】,p4P