Hadoop实战第一篇
前言:
都说现在是草根为尊的时代,近年来hadoop及spark技术在国内越来越流行。而且渐渐现成为企业的新宠。在DT时代全面来临之前,能提早接触大数据的技术必然能先人一步。本文作为Hadoop系列的第一篇,将HDFS和MapRed两个技术核心用2个实例简单实现一些,希望能供hadoop入门的朋友些许参考。
--HDFS
import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path; public class HDFStest {
final static String P_IN="hdfs://hadoop0:9000/data";
final static String P_F1="hdfs://hadoop0:9000/a.txt"; public static void main(String[] args) throws IOException { FileSystem fileSystem = FileSystem.get(new Configuration());
System.out.println("make diretory:");
fileSystem.mkdirs(new Path(P_IN));
System.out.println("judgy if exist 'File':");
System.out.println(fileSystem.exists(new Path(P_F1))); } }
--MapReduce
实现文本单词出现次数的统计:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class WC { static String INPUT="hdfs://hadoop0:9000/hello";
static String OUTPUT="hdfs://hadoop0:9000/output"; public static void main(String[] args) throws Exception{ Job job = new Job(new Configuration(),WC.class.getSimpleName());
job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReducer.class);
job.setJarByClass(WC.class);
//输出结果格式
job.setMapOutputKeyClass(Text.class);;
job.setMapOutputValueClass(LongWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
//路径设置
FileInputFormat.setInputPaths(job, INPUT);
FileOutputFormat.setOutputPath(job, new Path(OUTPUT));
//waitfor
job.waitForCompletion(true); } static class MyMapper extends Mapper<LongWritable, Text,Text,LongWritable >{ @Override
protected void map(LongWritable key, Text value,
Mapper<LongWritable, Text, Text, LongWritable>.Context context)
throws IOException, InterruptedException { String[] words = value.toString().split(" ");
for(String word:words){
context.write(new Text(word), new LongWritable(1));
}
}
}
static class MyReducer extends Reducer<Text, LongWritable, Text, LongWritable>{ @Override
protected void reduce(Text arg0, Iterable<LongWritable> arg1,Context context)
throws IOException, InterruptedException { Long sum=0L;
for(LongWritable c:arg1){
sum += c.get();
}
context.write(arg0,new LongWritable(sum));
}
}
}
以上代码相对简单,map读取到一行“Text”之后通过字符串切分函数split()得到各个单词,每个单词出现一次计数为1:
Reduce操作,实际就是一个集合元素累计的操作:
Hadoop实战第一篇的更多相关文章
- spring boot实战(第一篇)第一个案例
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] spring boot实战(第一篇)第一个案例 前言 写在前面的话 一直想将spring boot相关内容写成一个系列的 ...
- vue+uni-app商城实战 | 第一篇:【有来小店】微信小程序快速开发接入Spring Cloud OAuth2认证中心完成授权登录
一. 前言 本篇通过实战来讲述如何使用uni-app快速进行商城微信小程序的开发以及小程序如何接入后台Spring Cloud微服务. 有来商城 youlai-mall 项目是一套全栈商城系统,技术栈 ...
- (python3爬虫实战-第一篇)利用requests+正则抓取猫眼电影热映口碑榜
今天是个值得纪念了日子,我终于在博客园上发表自己的第一篇博文了.作为一名刚刚开始学习python网络爬虫的爱好者,后期本人会定期发布自己学习过程中的经验与心得,希望各位技术大佬批评指正.以下是我自己做 ...
- Canal 实战 | 第一篇:SpringBoot 整合 Canal + RabbitMQ 实现监听 MySQL 数据库同步更新 Redis 缓存
一. Canal 简介 canal [kə'næl],译意为水道/管道/沟渠,主要用途是基于 MySQL 数据库增量日志解析,提供增量数据订阅和消费 早期阿里巴巴因为杭州和美国双机房部署,存在跨机房同 ...
- Hadoop优化 第一篇 : HDFS/MapReduce
比较惭愧,博客很久(半年)没更新了.最近也自己搭了个博客,wordpress玩的还不是很熟,感兴趣的朋友可以多多交流哈!地址是:http://www.leocook.org/ 另外,我建了个QQ群:3 ...
- cmake实战第一篇:初试 cmake
1.准备工作: 首先,在/code_test 目录下建立一个 cmake 目录,用来放置我们学习过程中的所有练习.(如果以下命令出现xxx: cannot create directory ‘x’: ...
- SpringCloud实战 | 第一篇:Windows搭建Nacos服务
前言 为什么放弃eureka选择nacos?本地开发环境需要搭建nacos-server,想着是很简单的事但是被一些文章(少了关键必要的步骤)给带偏了,所以亲测成功后写了这篇文章. 搭建nacos-s ...
- Docker实战 | 第一篇:Centos8 安装 Docker
1. 安装依赖包 yum install -y yum-utils device-mapper-persistent-data lvm2 2. 配置镜像源 yum config-manager --a ...
- Spring Cloud实战 | 最终篇:Spring Cloud Gateway+Spring Security OAuth2集成统一认证授权平台下实现注销使JWT失效方案
一. 前言 在上一篇文章介绍 youlai-mall 项目中,通过整合Spring Cloud Gateway.Spring Security OAuth2.JWT等技术实现了微服务下统一认证授权平台 ...
随机推荐
- 实现gridview空白处的点击事件
今天做了一个girdview,要求长按item出现删除按钮,点击空白处取消,长按出现按钮可以,但是点击空白处有问题,如果点击到书籍的空白处 可以用适配器的布局点击事件处理,但是空白区域不是item,不 ...
- lua 模块与环境
编写一个模块的最简单方法: -- complex.lua -- 模块实际上是一个表 complex = {} -- 定义模块函数 function complex.add(c1,c2) ... end ...
- request.getRequestDispather().forward()与response.sendRedirect()
request.getRequestDispather().forward(),是服务器端的跳转,地址栏无变化. response.sendRedirect()是客户端的跳转,地址栏发生变化.
- switch2osm使用open street map离线地图中文乱码方框解决办法
----------written by shenwenkai------------- ubuntu linux环境下,按照网址(https://switch2osm.org/serving-til ...
- P1047 校门外的树
P1047 校门外的树 题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0 ...
- QT 网络编程三(TCP版)
QT客户端 //widget.h #ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QTcpSocket& ...
- codevs2806 红与黑
难度等级:白银 codevs2806 红与黑 题目描述 Description 有一个矩形房间,覆盖正方形瓷砖.每块瓷砖涂成了红色或黑色.一名男子站在黑色的瓷砖上,由此出发,可以移到四个相邻瓷砖之一, ...
- es6+移动轮播插件
前言:之前赶项目,都是直接用框架,对于touch事件是模拟两可,趁着有心情,用es6写一个原生移动轮播插件. 用了es6的新特性,确实挺爽的,说到es6,就不得不说到babel,博主已经码好了,直接用 ...
- eval解析JSON字符串的一个小问题
之前写过一篇 关于 JSON 的介绍文章,里面谈到了 JSON 的解析.我们都知道,高级浏览器可以用 JSON.parse() API 将一个 JSON 字符串解析成 JSON 数据,稍微欠妥点的做法 ...
- linux命令行安装使用KVM
一.说明 本篇文章介绍的是基于centos环境来安装的,ip地址192.168.4.233 二.检查CPU是否支持虚拟技术 egrep 'vmx|svm' /proc/cpuinfo 如果有输出内容表 ...