实验6 熟悉Hive的基本操作

您所在的位置:网站首页 实验报告表7-1 实验6 熟悉Hive的基本操作

实验6 熟悉Hive的基本操作

2024-06-28 20:00:31| 来源: 网络整理| 查看: 265

一、实验目的

(1)理解 Hive 作为数据仓库在 Hadoop 体系结构中的角色。 (2)熟练使用常用的 HiveQL。

二、实验平台 操作系统:Ubuntu18.04(或Ubuntu16.04);Hadoop版本:3.1.3;Hive版本:3.1.2;JDK版本:1.8。 三、数据集

准备工作:

由《Hive编程指南》(O’Reilly系列,人民邮电出版社)提供,下载地址: https://raw.githubusercontent.com/oreillymedia/programming_hive/master/prog-hive-1st-ed-data.zip

备用下载地址: https://www.cocobolo.top/FileServer/prog-hive-1st-ed-data.zip

下载慢可参考我上传的资源:林子雨Hive数据集下载

解压后可以得到本实验所需的 stocks.csv 和 dividends.csv 两个文件。

进入你的 Downloads(下载)文件夹,右键解压刚下载的数据压缩包,进入 prog-hive-1st-ed-data 文件夹,右键打开终端:

cd ~/Downloads/prog-hive-1st-ed-data sudo cp ./data/stocks/stocks.csv /usr/local/hive sudo cp ./data/dividends/dividends.csv /usr/local/hive

进入 Hadoop 目录,启动 Hadoop:

cd /usr/local/hadoop sbin/start-dfs.sh

启动 MySQL:

service mysql start

切换到 Hive 目录下,启动 MySQL 和 Hive:

cd /usr/local/hive bin/hive 四、实验步骤

(1)创建一个内部表 stocks,字段分隔符为英文逗号,表结构如下所示:

stocks 表结构:

col_namedata_typeexchangestringsymbolstringymdstringprice_openfloatprice_highfloatprice_lowfloatprice_closefloatvolumeintprice_adj_closefloat

代码:

create table if not exists stocks ( `exchange` string, `symbol` string, `ymd` string, `price_open` float, `price_high` float, `price_low` float, `price_close` float, `volume` int, `price_adj_close` float ) row format delimited fields terminated by ',';

查看表:

hive> describe stocks; OK exchange string symbol string ymd string price_open float price_high float price_low float price_close float volume int price_adj_close float Time taken: 0.062 seconds, Fetched: 9 row(s) hive>

(2)创建一个外部分区表 dividends(分区字段为 exchange 和 symbol),字段分隔符为英文逗号,表结构如下所示:

dividends 表结构

col_namedata_typeymdstringdividendfloatexchangestringsymbolstring

代码:

create external table if not exists dividends ( `ymd` string, `dividend` float ) partitioned by(`exchange` string ,`symbol` string) row format delimited fields terminated by ',';

查看表:

hive> describe dividends; OK ymd string dividend float exchange string symbol string # Partition Information # col_name data_type comment exchange string symbol string Time taken: 0.106 seconds, Fetched: 9 row(s) hive>

(3)从 stocks.csv 文件向 stocks 表中导入数据:

代码:

load data local inpath '/usr/local/hive/stocks.csv' overwrite into table stocks;

(4) 创建一个未分区的外部表 dividends_unpartitioned,并从 dividends.csv 向其中导入数据,表结构如下所示:

dividends_unpartitioned 表结构

col_namedata_typeymdstringdividendfloatexchangestringsymbolstring

代码:

create external table if not exists dividends_unpartitioned ( `exchange` string , `symbol` string, `ymd` string, `dividend` float ) row format delimited fields terminated by ',';

导入数据:

load data local inpath '/usr/local/hive/dividends.csv' overwrite into table dividends_unpartitioned;

(5)通过对 dividends_unpartitioned 的查询语句,利用 Hive 自动分区特性向分区表 dividends 各个分区中插入对应数据。

代码:

set hive.exec.dynamic.partition=true; set hive.exec.dynamic.partition.mode=nonstrict; set hive.exec.max.dynamic.partitions.pernode=1000; insert overwrite table dividends partition(`exchange`,`symbol`) select `ymd`,`dividend`,`exchange`,`symbol` from dividends_unpartitioned;

(6)查询IBM公司(symbol = IBM)从 2000 年起所有支付股息的交易日(dividends 表中有对应记录)的收盘价(price_close)。

操作语句如下:

select s.ymd,s.symbol,s.price_close from stocks s LEFT SEMI JOIN dividends d ON s.ymd=d.ymd and s.symbol=d.symbol where s.symbol='IBM' and year(ymd)>=2000;

输出如下(折叠部分输出):

2010-02-08 IBM 121.88 2009-11-06 IBM 123.49 2009-08-06 IBM 117.38 ... 2000-05-08 IBM 109.75 2000-02-08 IBM 118.81 Time taken: 8.75 seconds, Fetched: 41 row(s)

(7)查询苹果公司(symbol = AAPL)2008 年 10 月每个交易日的涨跌情况,涨显示 rise,跌显示 fall,不变显示 unchange。

操作语句如下:

select ymd, case when price_close-price_open>0 then 'rise' when price_close-price_open 50;

输出如下:

2006 70.81063753105255 2007 128.27390423049016 2008 141.9790115054888 2009 146.81412711976066 2010 204.72159912109376 Time taken: 2.347 seconds, Fetched: 5 row(s)

(10)查询每年年平均调整后收盘价(price_adj_close)前三名的公司的股票代码及年平均调整后收盘价。

操作语句如下:

select t2.`year`,symbol,t2.avg_price from ( select *,row_number() over(partition by t1.`year` order by t1.avg_price desc) as `rank` from ( select year(ymd) as `year`, symbol, avg(price_adj_close) as avg_price from stocks group by year(ymd),symbol )t1 )t2 where t2.`rank`


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭