3.格式化HDFS文件系统
1)命令: Hadoop namenode -format
4.启动守护进程(dfs和mapred,需要正确设置好前面三个文件,否则易出错)
$ start-dfs.sh
$ start-mapred.sh
5.编写CPP文件,源码如下:
- #include <algorithm>
- #include <limits>
- #include <stdint.h>
- #include <string>
- #include "hadoop/Pipes.hh"
- #include "hadoop/TemplateFactory.hh"
- #include "hadoop/StringUtils.hh"
- class MaxTemperatureMapper : public HadoopPipes::Mapper {
- public:
- MaxTemperatureMapper(HadoopPipes::TaskContext& context) {
- }
- void map(HadoopPipes::MapContext& context) {
- std::string line = context.getInputValue();
- std::string year = line.substr(15, 4);
- std::string airTemperature = line.substr(87, 5);
- std::string q = line.substr(92, 1);
- if (airTemperature != "+9999" &&
- (q == "0" || q == "1" || q == "4" || q == "5" || q == "9")) {
- context.emit(year, airTemperature);
- }
- }
- };
- class MapTemperatureReducer : public HadoopPipes::Reducer {
- public:
- MapTemperatureReducer(HadoopPipes::TaskContext& context) {
- }
- void reduce(HadoopPipes::ReduceContext& context) {
- int maxValue = INT_MIN;
- while (context.nextValue()) {
- maxValue = std::max(maxValue, HadoopUtils::toInt(context.getInputValue()));
- }
- context.emit(context.getInputKey(), HadoopUtils::toString(maxValue));
- }
- };
- int main(int argc, char *argv[]) {
- return HadoopPipes::runTask(HadoopPipes::TemplateFactory<MaxTemperatureMapper,
- MapTemperatureReducer>());
- }
