受够了百度一大堆抄书党,书里没有明确给出的一些类,可以去翻作者的github,地址是https://github.com/tomwhite/hadoop-book/
一些通用的类放在common包下。
下面给出来这个9-12的例子所引用的类——NcdcStationMetadataParser,这里用到的实例数据《气象站ID-气象站名》可以从ftp://ftp.ncdc.noaa.gov/pub/data/noaa/下载
具体是isd-history.txt这个文件,里面说的挺详细的。
我自己照着格式写了之后再参考了原作者的思路之后,代码如下
import org.apache.hadoop.io.Text;
public class NcdcStationMetadataParser {
private String stationId;//观察站编号
private String stationName;//观察站名称
public boolean parse(String record){
String usaf = record.substring(0, 6);
String wban = record.substring(7, 12);
stationId=usaf+"-"+wban;
stationName=record.substring(13,42);
stationName=stationName.trim();//去除前后空格
if(stationName.equals("")){//去除无效数据
return false;
}else {
try{
Integer.parseInt(usaf);//usaf是纯数字标识,转换不过来就说明是有问题
}catch (NumberFormatException e){
return false;
}
}
return true;
}
public boolean parse(Text text){
return parse(text.toString());
}
public String getStationId() {
return stationId;
}
public String getStationName() {
return stationName;
}
}
抄书党越多,越发感觉这门技术是不是太过时或者太冷门了。