一、hadoop简介
hadoop是一个开源的分布式存储和计算框架,能够处理大规模数据集,并且具有高可靠性和高扩展性。它由apache软件基金会开发,采用java编程语言编写,提供了一个可靠、高效的分布式系统基础架构。
二、hadoop核心组件
-
hadoop分布式文件系统(hdfs):hdfs是hadoop的核心组件之一,用于存储大规模数据集。它将数据分布式存储在集群的多个节点上,并提供了高容错性。
-
hadoop mapreduce:mapreduce是hadoop的另一个核心组件,用于并行处理大规模数据集。它将计算任务分成多个步骤(map和reduce),并在集群中的多个节点上并行执行。
-
yarn:yet another resource negotiator(yarn)是hadoop的资源管理器,负责集群资源的分配和调度。它允许多个数据处理框架共享集群资源,提高了集群的利用率。
三、hadoop生态系统
除了核心组件之外,hadoop还有许多相关项目和工具,构成了一个完整的生态系统,包括但不限于:
- apache hive:用于数据仓库查询和分析的数据仓库框架。
- apache pig:用于大规模数据分析的高级数据流语言和执行框架。
- apache hbase:一个分布式、面向列的数据库,用于实时读写大规模数据。
- apache spark:一个通用的、基于内存的大规模数据处理引擎,比mapreduce更快。
- apache kafka:一个分布式的流数据平台,用于构建实时数据管道和应用程序。
- apache flume:用于高可用性、大规模日志数据收集、聚合和传输的分布式系统。
四、hadoop项目实践
下面是一个简单的hadoop mapreduce示例,用于统计文本文件中每个单词出现的次数。我们将使用java编写mapreduce作业,并在hadoop集群上执行。
public class wordcountmapper extends mapper<longwritable, text, text, intwritable> {
private final static intwritable one = new intwritable(1);
private text word = new text();
public void map(longwritable key, text value, context context) throws ioexception, interruptedexception {
string line = value.tostring();
stringtokenizer tokenizer = new stringtokenizer(line);
while (tokenizer.hasmoretokens()) {
word.set(tokenizer.nexttoken());
context.write(word, one);
}
}
}
wordcountmapper类继承自org.apache.hadoop.mapreduce.mapper
,它定义了map阶段的逻辑。在这个例子中,mapper的输入键值对类型是longwritable
和text
,输出键值对类型是text
和intwritable
。
-
map
方法是mapper的核心,它接受一行文本作为输入,然后使用stringtokenizer
将这一行分割成单词。 -
对于每一个单词,它会创建一个键值对,其中键是单词本身(转换为
text
类型),值是常量1
(intwritable
类型)。 -
这些键值对会被写入到context对象中,以便后续的shuffle和sort阶段处理。
public class wordcountreducer extends reducer<text, intwritable, text, intwritable> {
public void reduce(text key, iterable<intwritable> values, context context) throws ioexception, interruptedexception {
int sum = 0;
for (intwritable value : values) {
sum += value.get();
}
context.write(key, new intwritable(sum));
}
}
wordcountreducer类继承自org.apache.hadoop.mapreduce.reducer
,它定义了reduce阶段的逻辑。reduce的输入键值对类型是text
和iterable<intwritable>
,输出键值对类型是text
和intwritable
。
-
reduce
方法是reducer的核心,它接受一组具有相同键的值。 -
遍历这些值,将它们累加起来得到总和。
-
最后,它将这个总和作为一个新的键值对写入到context对象中,这个键值对的键是单词,值是该单词的总出现次数。
public class wordcountdriver {
public static void main(string[] args) throws ioexception, interruptedexception ,classnotfoundexception {
//1.获取job
configuration conf = new configuration();
job job = job.getinstance(conf);
//2.设置jar路径
job.setjarbyclass(wordcountdriver.class);
//3.关联mapper和reducer
job.setmapperclass(wordcountmap.class);
job.setreducerclass(wordcountreduce.class);
//4.设置map输出key,value类型
job.setmapoutputkeyclass(text.class);
job.setmapoutputvalueclass(intwritable.class);
//5.设置最终输出的key,value的类型
job.setoutputkeyclass(text.class);
job.setmapoutputvalueclass(intwritable.class);
//6.设置输入路径和输出路径
// d:\hadoop\hadoop\hadoop\input\inputword.txt
fileinputformat.setinputpaths(job, new path(job, new path("args[0]"));
fileoutputformat.setoutputpath(job,new path(job, new path("args[1]"));
//7.提交job
boolean result =job.waitforcompletion(true);
system.exit(result ? 0 : 1);
}
}
wordcountdriver类是整个作业的驱动程序,它负责设置和运行mapreduce作业。
main
方法是入口点,它首先创建一个configuration对象,然后使用这个对象创建一个job实例。- 设置了作业的名称、jar文件的路径、mapper、combiner(在本例中与reducer相同)和reducer类。
- 还指定了输出键值对的类型。
- 通过
fileinputformat.addinputpath
和fileoutputformat.setoutputpath
方法,它设置了输入和输出的路径。 - 最后,它使用
job.waitforcompletion(true)
来提交作业并等待其完成。如果作业成功完成,则退出状态码为0,否则为1。
执行wordcountdrive类,代码运行如下成功即可打包成jar文件 ,同时准备一个txt文件,随便输入几行单词,同时在hdfs上创建input这个文件夹,将txt文件上传到hdfs的input文件上,提交到集群运行,使用cd ..命令回到虚拟机上自己构建的jar包的路径里,输入以下命令:
hadoop jar ./test/syadhsaj.jar com.hadoop.mapreduce.wordcountlinux.wordcountdrive /input /output
./test/syadhsaj.jar替换到自己的路径,com.hadoop.mapreduce.wordcountlinux.wordcountdrive复制自己的wordcountdrive类的全类名。
提交运行后,使用命令hdfs dfs -cat /output查看结果
五、项目实践步骤
- 编写mapreduce作业:编写mapper、reducer和driver类。
- 打包项目:将java源代码打包成jar文件。
- 准备输入数据:准备要处理的文本文件,将其上传到hdfs。
- 运行作业:在hadoop集群上提交mapreduce作业,并监视作业的执行情况。
- 查看结果:查看作业执行完成后生成的输出文件,即单词计数结果。
六、结论
hadoop mapreduce是一个强大的工具,适用于处理大规模数据集的并行计算任务。它的并行处理能力、容错性设计、灵活性和可扩展性使其成为处理大数据的首选技术之一。然而,开发者需要投入时间和精力去学习和掌握这一技术,以便充分利用其潜力。随着技术的发展,hadoop mapreduce也在不断进化,与其他新兴技术结合,以满足更多样化的数据处理需求。
发表评论