大数据基础编程、实验和教程案例(实验六)

大数据基础编程、实验和教程案例(实验六)

14.6 实验六 熟悉 Hive 的基本操作

本实验对应第 8 章的内容。

14.6.1 实验目的

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

14.6.2 实验平台

操作系统Linux
Hadoop 版本3.1.3
Hive 版本3.1.2
JDK 版本1.8

14.6.3 数据集

由《Hive 编程指南》(O’Reilly 系列,人民邮电出版社)提供,下载地址:
https://raw.githubusercontent.com/oreillymedia/programming_hive/master/prog-hive-1sted-data.zip
备用下载地址:
https://www.cocobolo.top/FileServer/prog-hive-1st-ed-data.zip
解压后可以得到本实验所需的 stocks.csv 和 dividends.csv 两个文件。

14.6.4 实验步骤

(1)创建一个内部表 stocks,字段分隔符为英文逗号

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 ',';

在这里插入图片描述
(2)创建一个外部分区表 dividends(分区字段为 exchange 和 symbol),字段分隔符为英文逗号,表结构如表 A-7 所示。

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

在这里插入图片描述
(3)从 stocks.csv 文件向 stocks 表中导入数据。

load data local inpath '/home/hadoop/data/stocks/stocks.csv' overwrite into table stocks;

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

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 '/home/hadoop/data/dividends/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;

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

select ymd,
case
 when price_close-price_open>0 then 'rise'
 when price_close-price_open<0 then 'fall'
 else 'unchanged'
end as situation
from stocks
where symbol='AAPL' and substring(ymd,0,7)='2008-10';

(8)查询 stocks 表中收盘价(price_close)比开盘价(price_open)高得最多的那条记录的交易所(exchange)、股票代码(symbol)、日期(ymd)、收盘价、开盘价及二者差价。

select `exchange`,symbol,ymd,price_close-price_open as `diff`
from
(
 select *
 from stocks
 order by price_close-price_open desc
 limit 1
)t;

(9)从 stocks 表中查询苹果公司(symbol=AAPL)年平均调整后收盘价(price_adj_close) 大于 50 美元的年份及年平均调整后收盘价。

select
 year(ymd) as `year`,
 avg(price_adj_close) as avg_price from stocks
where `exchange`='NASDAQ' and symbol='AAPL'
group by year(ymd)
having avg_price > 50;

(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`<=3;
  • 1
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实验三:HDFS操作方法和基础编程实验 1. 实验目的 了解HDFS的基本操作方法和基础编程实验,掌握Hadoop文件系统的操作。 2. 实验环境 - 操作系统:Windows 10 - 虚拟机软件:VMware Workstation 15 Pro - 虚拟机操作系统:CentOS 7 - Hadoop版本:2.7.7 3. 实验步骤 3.1 HDFS操作方法 3.1.1 启动HDFS服务 在CentOS 7中打开终端,输入以下命令启动HDFS服务: ``` start-dfs.sh ``` 3.1.2 创建文件夹 HDFS中的文件夹称为目录,使用以下命令在HDFS中创建一个目录: ``` hadoop fs -mkdir /test ``` 3.1.3 上传文件 使用以下命令将本地文件上传到HDFS中的目录: ``` hadoop fs -put /opt/test.txt /test ``` 3.1.4 下载文件 使用以下命令将HDFS中的文件下载到本地: ``` hadoop fs -get /test/test.txt /opt ``` 3.1.5 查看文件 使用以下命令查看HDFS中的文件: ``` hadoop fs -ls /test ``` 3.1.6 删除文件 使用以下命令删除HDFS中的文件: ``` hadoop fs -rm /test/test.txt ``` 3.2 基础编程实验 3.2.1 实验要求 编程实现一个完整的Hadoop MapReduce程序,实现词频统计功能。 3.2.2 实验步骤 3.2.2.1 编写Mapper类 在Eclipse中新建一个Java项目,创建Mapper类,代码如下: ``` public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer tokenizer = new StringTokenizer(line); while (tokenizer.hasMoreTokens()) { word.set(tokenizer.nextToken()); context.write(word, one); } } } ``` 3.2.2.2 编写Reducer类 创建Reducer类,代码如下: ``` public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> { private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } } ``` 3.2.2.3 编写Driver类 创建Driver类,代码如下: ``` public class WordCount { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(WordCountMapper.class); job.setCombinerClass(WordCountReducer.class); job.setReducerClass(WordCountReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` 3.2.2.4 打包 右键项目,选择Export,选择JAR file,点击Next,选择要导出的项目和要导出的类,点击Next,选择要导出的JAR文件路径和文件名,点击Finish,即可生成JAR文件。 3.2.2.5 运行 使用以下命令在Hadoop集群上运行程序: ``` hadoop jar /opt/wordcount.jar WordCount /input /output ``` 其中,/input是输入文件所在的目录,/output是输出文件所在的目录。 4. 实验结果与结论 经过以上步骤,我们可以成功地完成HDFS操作方法和基础编程实验,从而掌握了Hadoop文件系统的操作。同时,我们还通过编写MapReduce程序实现了词频统计功能,进一步加深了对Hadoop的理解和应用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值