WordCount 到 Hdfs
来源:互联网 发布:淘宝优惠微信群 编辑:程序博客网 时间:2024/06/08 04:47
import
java.io.IOException;
import
org.apache.hadoop.conf.Configuration;
import
org.apache.hadoop.fs.Path;
import
org.apache.hadoop.io.IntWritable;
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;
import
org.apache.hadoop.util.GenericOptionsParser;
public
class
WordCount {
//计数变量
private
static
final
IntWritable ONE =
new
IntWritable(
1
);
/**
*
* @author 汤高
* Mapper<longwritable, intwritable="">中 LongWritable,IntWritable是Hadoop数据类型表示长整型和整形
*
* LongWritable, Text表示输入类型 (比如本应用单词计数输入是 偏移量(字符串中的第一个单词的其实位置),对应的单词(值))
* Text, IntWritable表示输出类型 输出是单词 和他的个数
* 注意:map函数中前两个参数LongWritable key, Text value和输出类型不一致
* 所以后面要设置输出类型 要使他们一致
*/
//Map过程
public
static
class
WordCountMapper
extends
Mapper<longwritable, intwritable=
""
> {
/***
*
*/
@Override
protected
void
map(LongWritable key, Text value, Mapper<longwritable, intwritable=
""
>.Context context)
throws
IOException, InterruptedException {
//默认的map的value是每一行,我这里自定义的是以空格分割
String[] vs = value.toString().split(
"\\s"
);
for
(String v : vs) {
//写出去
context.write(
new
Text(v), ONE);
}
}
}
//Reduce过程
/***
* @author 汤高
* Text, IntWritable输入类型,从map过程获得 既map的输出作为Reduce的输入
* Text, IntWritable输出类型
*/
public
static
class
WordCountReducer
extends
Reducer<text, intwritable=
""
>{
@Override
protected
void
reduce(Text key, Iterable<intwritable> values,
Reducer<text, intwritable=
""
>.Context context)
throws
IOException, InterruptedException {
int
count=
0
;
for
(IntWritable v:values){
count+=v.get();
//单词个数加一
}
context.write(key,
new
IntWritable(count));
}
}
public
static
void
main(String[] args) {
Configuration conf=
new
Configuration();
try
{
//得到一个Job 并设置名字
Job job=Job.getInstance(conf,
"wordcount1"
);
//设置Jar 使本程序在Hadoop中运行
job.setJarByClass(WordCount.
class
);
//设置Map处理类
job.setMapperClass(WordCountMapper.
class
);
//设置map的输出类型,因为不一致,所以要设置
job.setMapOutputKeyClass(Text.
class
);
job.setMapOutputValueClass(IntWritable.
class
);
//设置Reduce处理类
job.setReducerClass(WordCountReducer.
class
);
//设置输入和输出目录
FileInputFormat.addInputPath(job,
new
Path(
"hdfs://192.168.52.140:9000/in_2/"
));
FileOutputFormat.setOutputPath(job,
new
Path(
"hdfs://192.168.52.140:9000/myhbase"
+System.currentTimeMillis()));
//启动运行
System.exit(job.waitForCompletion(
true
) ?
0
:
1
);
}
catch
(IOException e) {
e.printStackTrace();
}
catch
(ClassNotFoundException e) {
e.printStackTrace();
}
catch
(InterruptedException e) {
e.printStackTrace();
}
}
0 0
- WordCount 到 Hdfs
- HADOOP -hdfs of wordcount
- Spark Run WordCount On Hdfs using Scala
- SparkStreaming实现HDFS的wordCount(java版)
- Spark WordCount 读写hdfs文件 (read file from hadoop hdfs and write output to hdfs)
- hadoop hdfs搭建 mapreduce环境搭建 wordcount程序简单注释
- spark从hdfs上读取文件运行wordcount
- spark从hdfs上读取文件运行wordcount
- Spark读取HDFS文件,文件格式为GB2312,实现WordCount示例
- 在Ubuntu中使用eclipse操作HDFS跑wordcount程序
- mapreduce Wordcount输入文件在hdfs上的实例
- mapreduce入门 cookbook note 1 【wordcount|hdfs|yarn简介】
- 配置Hadoop2.x的HDFS、MapReduce来运行WordCount程序
- 运行hdfs的wordcount程序时出现的错误
- hadoop经典wordcount和hdfs增加删除追加
- HDFS文件上传,命令行模式执行WordCount自带实例
- spark1.6从hdfs上读取文件运行wordcount
- Spark及HDFS环境下使用python的wordcount实例
- poj 2823 Sliding Window 单调队列
- Spring事务管理(详解+实例)
- 第五章
- StringUtils工具类
- Log4的配置
- WordCount 到 Hdfs
- Axure8.0基础教程(41-50)新手必须掌握的基础操作
- Android处理未捕获的异常Application应用生命周期变量
- Android 中获取设备的宽高
- 第三篇 Android Bootloader 之 U-boot
- PYQT笔记——QTableView
- Ubuntu交叉工具链配置
- 5.2.2中国剩余定理
- kali 简单安装中文输入法