写个Hadoop Map-Reduce分析NCDC的天气数据

我是Hadoop小白,写本文是总结笔记,怕之后忘了。

搭个Hadoop Cluster

因为手头有几个Raspberry Pi 3的板子,就花了点时间搭了一个小的Hadoop Cluster的集群,四个Slave with Raspbian, Master server在一个虚拟机里安装了Ubuntu 18.04。关于如何配置,IBM上的这篇Blog讲得非常详细。

NCDC的天气数据

在《Hadoop Definitive Guide》上讲到使用NCDC(National Climatic Data Center)的天气数据来分析每年最高的气温。这本书在Github上有1901年和1902年的数据,但如果你想试试更大的数据集就需要自己下载了。

这里有一个Shell Script可以帮你从NCDC的FTP上下载天气数据。你只需要给出起始和终止年份。

下载之后,天气数据会分到不同的zip文件里,但如果上传到HDFS上还要把它放到一个文件里。下面这段Python代码就做这个事情。

要注意的就是,这段代码要运行在天气的Zip同一个文件夹里。

我下载了1901年到1935年的数据,大约400多M,使用hadoop fs的命令上传到HDFS上,

然后就可以写一个简单的Map – Reduce了。

因为HDFS一个Block是128M,所以400M需要4个Block。

Map-Reduce

首先是Mapper,其实就是分析天气文件的每一行,从里面取出温度。

Reducer则是从前面得到的年份和温度的信息汇总在一些,求出当年里最高的气温。

这个过程很像Java 8里引入的Stream。

之后,还要实现一个Job来执行这个Map-Reduce任务。

这个程序需要两个路径,一是NCDC的数据文件,一是结果的输出路径。Job还要需要明确Mapper和Recuder的类名。

生成Jar文件后,使用下面的命令行运行Map-Reduce任务

结果会被发到output4文件夹里,几分钟后,就可以看到结果了。

好了,Hadoop里的HelloWorld算是完成了!

References

 

[VS Code Tips] How to Make AssertJ Be Static Imported in VS Code

After installing the famous Java plugin written by RedHat, VS Code will become into a powerful Java IDE, which is far beyond other editors, such as Sublime, Notepad++.

Today’s tip will show you how to static import AssertJ in VS Code. AssertJ helps you to write assertions in unit testing. Compared to Hamcrest, someone, like me, think AssertJ’s API and syntax make more sense.

First, you need to introduce it. In maven, it’s very straightforward. Simply define a new dependency in pom.xml.

Next step is to update a configuration “java.completion.favoriteStaticMembers“. Otherwise, assertThat() will be the one in Hamcrest in the auto-completion. Remove “org.junit.Assert.*” from the user configuration and add “org.assertj.core.api.Assertions.*” instead.

Now, AssertJ will be statically imported into your unit testing.

Use Atlassian Confluence API to Copy and Create New Pages

For team management, we have a Confluence space to do weekly reporting. In that space, usually, one page is for a week and each page has the same template. So to copy these pages becomes a very boring work. No worries, there are Confluence APIs to help.

Installation and Connection to Confluence

Very simple and pip will help to finish everything.

After installation, use the following code to connect to Confluence.

Copy and Create New Page

Before creating new page, you need to get the body content of the template page.

Pay attention to the expand=’body.storage’. Without it, the returned page only contains the basic information, no body HTML.

Now you have the body and the rest work is a just for-loop.

Also you can use confluence.update_page() to update some existing pages.

References

《老人与海》读后

去年,在去夏威夷的飞机,拜读了一下海明威的《老人与海》。小说不长,5个多小时的飞行绰绰有余。读完后,最难忘的是老人的那份坚持。

不就是捕鱼嘛,管它是大还是小,为什么要这么拼命?!老人坚定地用毕生积累的经验和那条大鱼斗到底,哪怕是最后鱼死网破。不能输给一条鱼!这是一个渔夫的惯性思维?老人在捍卫着他的尊严,那是他所剩无几的东西。想想自己,有没有捍卫的“鱼”?应该有,自己好像一直在别人眼中简单且微不足道的技术或项目上花精力和时间,自己好像也进入了一个惯性的思维。捕鱼自然没有那么简单,那些项目其实也充满挑战。怕的是,自己进入一种惯性。自己是否真的喜欢捕鱼,也许不!好在,自己还有很多时间可以学习别的领域,可以去尝试。需要的只是一份勇气吧!

老人和大鱼的争斗非常惊心动魄,但很庆幸,老人最终可以回到那个属于他的港湾,有那个等他回家的男孩。希望将来的自己在一番拼杀后,也能回到一个安静的港湾吧!