hadoop学习;block数据块;mapreduce实现样例;UnsupportedClassVersionError异常;关联项目源代码
对于开源的东东,尤其是刚出来不久,我认为最好的学习方式就是能够看源代码和doc,測试它的样例
为了方便查看源代码,关联导入源代码的项目
先前的项目导入源代码是关联了源代码文件
block数据块,在配置文件hdfs-default.xml中能够查看到,记住要改动不是在这里
block文件存储块是最主要的单位
查看block存放位置,配置文件里查看
假设文件大于64M会占两个块,meta文件是校验文件,第二个文件大于64M,删除文件后,则相应block不在
datanode存放文件,一个文件能够存放在不同机器上datanode
mapreduce本身有默认的类,当什么都不写的时候,原样输出
package com.kane.mr.minidefault;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class TestDefault {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
//GenericOptionsParser辅助工具类
//String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
String[] otherArgs = {"hdfs://centos:9000/kane/mini.txt","hdfs://centos:9000/kane/output"};
if (otherArgs.length != 2) {
System.err.println("Usage: wordcount <in> <out>");
System.exit(2);
}
Job job = new Job(conf, "word count");
job.setJarByClass(TestDefault.class);
//中间的内容省略就採用默认的类操作,应该是原样输出
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));//输入參数,相应hadoop jar 相应类执行时在后面加的第一个參数
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));//输出參数
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
然后到处该类为jar包,放到hadoop文件下,执行
接下来自己创建须要mr执行的源文件,并导入hdfs中
当我们执行hadoop命令执行时 可能出异常,由于你编写代码的jdk可能和hadoop用到的JVM不匹配
解决的办法事实上非常easy,仅仅要更改这个选项即可了。详细过程例如以下:
----------------------------------------------------------
1、右键点击project文件,选择属性(properties),
2、在属性窗体中选择 Build-->Java,在右边的选项中有四个下拉框,就能够看到编译选项了,
3、当中Compiler和Debug Option能够不用管,仅仅在Languege features和Target VM中选择对应的JDK版本号就能够了,然后确定,一切OK。
附件中是配置的图片。
-----------------------------------------------------------
假设在Target VM中选择了All Java SDKs,那么你的class文件在使用JDK1.1的VM上都能够执行(Jbuilder2006帮助中是这么说的,预计没几个人的机子上还在用JDK1.1吧 :-)
默认的mr程序原样输出
測试wordcount
package com.kane.mr;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class MapperClass extends Mapper<Object,Text,Text,IntWritable>{
public Text keyText=new Text("key");
public IntWritable intValue=new IntWritable(1);
@Override
protected void map(Object key, Text value,
Context context)
throws IOException, InterruptedException {
//获取输入的值
String str=value.toString();
//用什么分隔键值,默认空格或\t 或\n
StringTokenizer sTokenizer=new StringTokenizer(str);
//循环输出,假如是My name is kane 则分四次输出四个单词
while (sTokenizer.hasMoreElements()) {
Object object = (Object) sTokenizer.nextElement();
//这里每一个单词能够看做一个key
keyText.set(str);
context.write(keyText, intValue);//匹配一个就加value比如(“My”,1)
}
}
}
package com.kane.mr;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
//map传来的键值就是text 和 intwritable
public class ReducerClass extends Reducer<Text,IntWritable, Text,IntWritable>{
public IntWritable intValue= new IntWritable(0);
@Override
protected void reduce(Text key, Iterable<IntWritable> values,//假如name出现两次,这里得到的values是 name [1,1]
Context context)
throws IOException, InterruptedException {
int sum=0;
while (values.iterator().hasNext()) {
sum+=values.iterator().next().get();
}
//这里值用intwritable输出是由于非常多情况下一个mapreduce的输出是下一个mapreduce的输入
intValue.set(sum);
context.write(key, intValue);
}
}
package com.kane.mr;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class WordCounter {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println("Usage: wordcount <in> <out>");
System.exit(2);
}
Job job = new Job(conf, "word count");
job.setJarByClass(WordCounter.class);
job.setMapperClass(MapperClass.class);
//job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(ReducerClass.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));//输入參数,相应hadoop jar 相应类执行时在后面加的第一个參数
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));//输出參数
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
hadoop学习;block数据块;mapreduce实现样例;UnsupportedClassVersionError异常;关联项目源代码的更多相关文章
- hadoop得知;block数据块;mapreduce实现样例;UnsupportedClassVersionError变态;该项目的源代码相关联
对于开源的东西.特别是刚出来不久.我认为最好的学习方法是能够看到源代码,doc,样品测试 为了方便查看源代码,导入与项目相关的源代码 watermark/2/text/aHR0cDovL2Jsb2cu ...
- Hadoop hadoop 之hdfs数据块修复方法
hadoop 之hdfs数据块修复方法: .手动修复 hdfs fsck / #检查集群的健康状态 hdfs debug recoverLease -path 文件位置 -retries 重试次数 # ...
- Hadoop学习(4)-- MapReduce
MapReduce是一种用于大规模数据集的并行计算编程模型,由Google提出,主要用于搜索领域,解决海量数据的计算问题.其主要思想Map(映射)和Reduce(规约)都是从函数是编程语言中借鉴而来的 ...
- hadoop学习第三天-MapReduce介绍&&WordCount示例&&倒排索引示例
一.MapReduce介绍 (最好以下面的两个示例来理解原理) 1. MapReduce的基本思想 Map-reduce的思想就是“分而治之” Map Mapper负责“分”,即把复杂的任务分解为若干 ...
- Hadoop学习基础之三:MapReduce
现在是讨论这个问题的不错的时机,因为最近媒体上到处充斥着新的革命所谓“云计算”的信息.这种模式需要利用大量的(低端)处理器并行工作来解决计算问题.实际上,这建议利用大量的低端处理器来构建数据中心,而不 ...
- Hadoop学习之第一个MapReduce程序
期望 通过这个mapreduce程序了解mapreduce程序执行的流程,着重从程序解执行的打印信息中提炼出有用信息. 执行前 程序代码 程序代码基本上是<hadoop权威指南>上原封不动 ...
- Hdfs block数据块大小的设置规则
1.概述 hadoop集群中文件的存储都是以块的形式存储在hdfs中. 2.默认值 从2.7.3版本开始block size的默认大小为128M,之前版本的默认值是64M. 3.如何修改block块的 ...
- Hadoop学习笔记—4.初识MapReduce
一.神马是高大上的MapReduce MapReduce是Google的一项重要技术,它首先是一个编程模型,用以进行大数据量的计算.对于大数据量的计算,通常采用的处理手法就是并行计算.但对许多开发者来 ...
- Hadoop学习笔记(2) 关于MapReduce
1. 查找历年最高的温度. MapReduce任务过程被分为两个处理阶段:map阶段和reduce阶段.每个阶段都以键/值对作为输入和输出,并由程序员选择它们的类型.程序员还需具体定义两个函数:map ...
随机推荐
- SHA-1
https://en.wikipedia.org/wiki/SHA-1 In cryptography, SHA-1 (Secure Hash Algorithm 1) is a cryptograp ...
- 在try...catch语句中执行Response.End()后如何停止执行catch语句中的内容
在调用Response.End()时,会执行Thread.CurrentThread.Abort()操作. 如果将Response.End()放在try...catch中,catch会捕捉Thread ...
- jqGrid如何实现单选。
设置如下:multiselect:true // multi-select checkboxes appear multiboxonly:true // checkboxes act like rad ...
- I.MX6 U-boot Kernel backlight setting
/********************************************************************* * I.MX6 U-boot Kernel backlig ...
- 对 Azure 虚拟网络网关的改进
YU-SHUN WANG Azure 网络高级项目经理 在 2014 年欧洲 TechEd 大会上,我们宣布了对Azure 虚拟网络网关的多项改进: 1. 高性能网关 SKU 2. Azure 虚 ...
- 学习面试题Day03
1.Java中的注释有哪些? 如果不算Annotation,Java的注释有3种,即行注释.块注释和文档注释.它们往往适合于不同地方的注释,其中文档注释比较特殊,它的注释信息可以进入到javadoc文 ...
- git提交小结
git有工作区和暂存区的概念,工作区就是可以看到文件目录的地方,暂存区则是提交代码的地方 第一步,进入文件工作目录,终端输入命令 $ dir1/dir2: 第二步,查看哪些文件已经修改,输入命令 $ ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.9
(1). When $A$ is normal, the set $W(A)$ is the convex hull of the eigenvalues of $A$. For nonnormal ...
- ASCII编码:Linux&Windows
我们的服务器为linux系统,日志中的字段通常会用不同分隔符来做分隔,在不同操作系统编码格式下查看也会有不同的体现,甚至会出现所谓的乱码.我在xshell5下常用的编码格式Unicode(UTF-8) ...
- POJ 2435Navigating the City(bfs)
题意:给你一个地图,’+’代表十字路口,‘-’‘|’表示街道,‘.’表示建筑物,‘s’,’E’ 起点和终点.输出从起点到终点的的 最短路径(包括方向和沿该方向的经过的十字路口数) 分析:ans[i][ ...