`
dajuezhao
  • 浏览: 60500 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Hadoop中DBInputFormat和DBOutputFormat使用

阅读更多

一、背景

为了方便MapReduce直接访问关系型数据库(Mysql,Oracle),Hadoop提供了DBInputFormat和DBOutputFormat两个类。通过

DBInputFormat类把数据库表数据读入到HDFS,根据DBOutputFormat类把MapReduce产生的结果集导入到数据库表中。

二、技术细节

1、DBInputFormat(Mysql为例),先创建表:
CREATE TABLE studentinfo (
id INTEGER NOT NULL PRIMARY KEY,
name VARCHAR(32) NOT NULL);
2、由于0.20版本对DBInputFormat和DBOutputFormat支持不是很好,该例用了0.19版本来说明这两个类的用法。
3、DBInputFormat用法如下:
public class DBInput {
// DROP TABLE IF EXISTS `hadoop`.`studentinfo`;
// CREATE TABLE studentinfo (
// id INTEGER NOT NULL PRIMARY KEY,
// name VARCHAR(32) NOT NULL);

public static class StudentinfoRecord implements Writable, DBWritable {
int id;
String name;
public StudentinfoRecord() {

}
public void readFields(DataInput in) throws IOException {
this.id = in.readInt();
this.name = Text.readString(in);
}
public void write(DataOutput out) throws IOException {
out.writeInt(this.id);
Text.writeString(out, this.name);
}
public void readFields(ResultSet result) throws SQLException {
this.id = result.getInt(1);
this.name = result.getString(2);
}
public void write(PreparedStatement stmt) throws SQLException {
stmt.setInt(1, this.id);
stmt.setString(2, this.name);
}
public String toString() {
return new String(this.id + " " + this.name);
}
}
public class DBInputMapper extends MapReduceBase implements
Mapper<LongWritable, StudentinfoRecord, LongWritable, Text> {
public void map(LongWritable key, StudentinfoRecord value,
OutputCollector<LongWritable, Text> collector, Reporter reporter)
throws IOException {
collector.collect(new LongWritable(value.id), new Text(value
.toString()));
}
}
public static void main(String[] args) throws IOException {
JobConf conf = new JobConf(DBInput.class);
DistributedCache.addFileToClassPath(new Path(
"/lib/mysql-connector-java-5.1.0-bin.jar"), conf);

conf.setMapperClass(DBInputMapper.class);
conf.setReducerClass(IdentityReducer.class);

conf.setMapOutputKeyClass(LongWritable.class);
conf.setMapOutputValueClass(Text.class);
conf.setOutputKeyClass(LongWritable.class);
conf.setOutputValueClass(Text.class);

conf.setInputFormat(DBInputFormat.class);
FileOutputFormat.setOutputPath(conf, new Path("/hua01"));
DBConfiguration.configureDB(conf, "com.mysql.jdbc.Driver",
"jdbc:mysql://192.168.3.244:3306/hadoop", "hua", "hadoop");
String[] fields = { "id", "name" };
DBInputFormat.setInput(conf, StudentinfoRecord.class, "studentinfo",
null, "id", fields);

JobClient.runJob(conf);
}
}

a)StudnetinfoRecord类的变量为表字段,实现Writable和DBWritable两个接口。

实现Writable的方法:
public void readFields(DataInput in) throws IOException {
this.id = in.readInt();
this.name = Text.readString(in);
}
public void write(DataOutput out) throws IOException {
out.writeInt(this.id);
Text.writeString(out, this.name);
}

实现DBWritable的方法:
public void readFields(ResultSet result) throws SQLException {
this.id = result.getInt(1);
this.name = result.getString(2);
}
public void write(PreparedStatement stmt) throws SQLException {
stmt.setInt(1, this.id);
stmt.setString(2, this.name);
}

b)读入Mapper的value类型是StudnetinfoRecord。

c)配置如何连入数据库,读出表studentinfo数据。
DBConfiguration.configureDB(conf, "com.mysql.jdbc.Driver",
"jdbc:mysql://192.168.3.244:3306/hadoop", "hua", "hadoop");
String[] fields = { "id", "name" };
DBInputFormat.setInput(conf, StudentinfoRecord.class, "studentinfo", null, "id", fields);

4、DBOutputFormat用法如下:

public class DBOutput {

public static class StudentinfoRecord implements Writable, DBWritable {
int id;
String name;
public StudentinfoRecord() {

}
public void readFields(DataInput in) throws IOException {
this.id = in.readInt();
this.name = Text.readString(in);
}
public void write(DataOutput out) throws IOException {
out.writeInt(this.id);
Text.writeString(out, this.name);
}
public void readFields(ResultSet result) throws SQLException {
this.id = result.getInt(1);
this.name = result.getString(2);
}
public void write(PreparedStatement stmt) throws SQLException {
stmt.setInt(1, this.id);
stmt.setString(2, this.name);
}
public String toString() {
return new String(this.id + " " + this.name);
}
}

public static class MyReducer extends MapReduceBase implements
Reducer<LongWritable, Text, StudentinfoRecord, Text> {
public void reduce(LongWritable key, Iterator<Text> values,
OutputCollector<StudentinfoRecord, Text> output, Reporter reporter)
throws IOException {
String[] splits = values.next().toString().split("\t");
StudentinfoRecord r = new StudentinfoRecord();
r.id = Integer.parseInt(splits[0]);
r.name = splits[1];
output.collect(r, new Text(r.name));
}
}

public static void main(String[] args) throws IOException {
JobConf conf = new JobConf(DBOutput.class);
conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(DBOutputFormat.class);

FileInputFormat.setInputPaths(conf, new Path("/hua/hua.bcp"));
DBConfiguration.configureDB(conf, "com.mysql.jdbc.Driver",
"jdbc:mysql://192.168.3.244:3306/hadoop", "hua", "hadoop");
DBOutputFormat.setOutput(conf, "studentinfo", "id", "name");

conf.setMapperClass(org.apache.hadoop.mapred.lib.IdentityMapper.class);
conf.setReducerClass(MyReducer.class);

JobClient.runJob(conf);
}

}

a)StudnetinfoRecord类的变量为表字段,实现Writable和DBWritable两个接口,同.DBInputFormat的StudnetinfoRecord类。

b)输出Reducer的key/value类型是StudnetinfoRecord。

c)配置如何连入数据库,输出结果到表studentinfo。
DBConfiguration.configureDB(conf, "com.mysql.jdbc.Driver",
"jdbc:mysql://192.168.3.244:3306/hadoop", "hua", "hadoop");
DBOutputFormat.setOutput(conf, "studentinfo", "id", "name");
三、总结

运行MapReduce时候报错:java.io.IOException: com.mysql.jdbc.Driver,一般是由于程序找不到mysql驱动包。解决方法是让每个

tasktracker运行MapReduce程序时都可以找到该驱动包。

添加包有两种方式:

1.在每个节点下的${HADOOP_HOME}/lib下添加该包。重启集群,一般是比较原始的方法。

2.a)把包传到集群上: hadoop fs -put mysql-connector-java-5.1.0- bin.jar /lib

b)在mr程序提交job前,添加语句:istributedCache.addFileToClassPath(new Path("/lib/mysql- connector-java- 5.1.0-bin.jar"), conf);

3、虽然API用的是0.19的,但是使用0.20的API一样可用,只是会提示方法已过时而已。

分享到:
评论

相关推荐

    hadoop中Map-Reduce使用示例,输入(DBInputFormat),输出(DBOu-MR_HBase.zip

    这个示例,"MR_HBase-Hadoop中的MapReduce使用示例,输入(DBInputFormat),输出(DBOutputFormat)",主要展示了如何利用MapReduce与HBase进行交互,进行数据的读取和写入。下面将详细介绍相关的知识点。 1. **...

    hadoop中文乱码问题

    解决Hadoop中文乱码问题的关键在于识别和匹配数据的正确编码,并在Hadoop组件和工具中设置相应的编码选项。在实际操作中,可能需要结合日志信息和源码调试来定位问题。同时,建立良好的编码规范,统一数据的编码格式...

    hadoop2.7.3的hadoop.dll和winutils.exe

    在IT行业中,Hadoop是一个广泛使用的开源框架,主要用于大数据处理和分布式存储。Hadoop 2.7.3是Hadoop发展中的一个重要版本,它包含了众多的优化和改进,旨在提高性能、稳定性和易用性。在这个版本中,`hadoop.dll`...

    hadoop的hadoop.dll和winutils.exe下载

    在Hadoop生态系统中,`hadoop.dll`和`winutils.exe`是两个关键组件,尤其对于Windows用户来说,它们在本地开发和运行Hadoop相关应用时必不可少。`hadoop.dll`是一个动态链接库文件,主要用于在Windows环境中提供...

    hadoop调试工具hadoop.dll和hadoop.exp和winutils.exe

    在Hadoop生态系统中,调试工具对于开发者和管理员来说至关重要,特别是在Windows环境中。本文将深入探讨三个关键组件:hadoop.dll、hadoop.exp和winutils.exe,以及它们在64位系统中的应用。这些组件主要用于在...

    hadoop.dll 和 winutils.exe

    在IT行业中,Hadoop是一个广泛使用的开源框架,主要用于大数据处理和分布式存储。它是由Apache软件基金会开发并维护的,旨在提供可靠、可扩展的数据处理能力。标题中的"hadoop.dll"和"winutils.exe"是Hadoop在...

    hadoop2.7.3 Winutils.exe hadoop.dll

    在IT行业中,Hadoop是一个广泛使用的开源框架,主要用于大数据处理和分布式存储。Hadoop 2.7.3是这个框架的一个稳定版本,它包含了多个改进和优化,以提高性能和稳定性。在这个版本中,Winutils.exe和hadoop.dll是两...

    hadoop2.7.2在windows环境中相关依赖文件hadoop.dll和winutils.exe

    在这个过程中,有两个关键的依赖文件:`hadoop.dll`和`winutils.exe`,它们对于在Windows上正确运行Hadoop至关重要。本文将详细介绍这两个文件的作用以及如何在Windows中解决相关问题。 `hadoop.dll`是Hadoop在...

    hadoop.dll winutils.exe

    本文将深入探讨与"Hadoop.dll"和"winutils.exe"相关的知识点,并解释它们在Hadoop生态系统中的作用。 1. Hadoop.dll:这是一个动态链接库文件,通常在Windows环境中与Hadoop相关联。在Windows上运行Hadoop时,由于...

    Hadoop安装使用教程0基础!!!易懂!!!

    Hadoop安装使用教程Hadoop安装使用教程Hadoop安装使用教程Hadoop安装使用教程Hadoop安装使用教程Hadoop安装使用教程Hadoop安装使用教程Hadoop安装使用教程Hadoop安装使用教程Hadoop安装使用教程Hadoop安装使用教程...

    Linux下Hadoop的分布式配置和使用.doc

    "Linux下Hadoop的分布式配置和使用" 本文档主要介绍了在 Linux 系统下 Hadoop 的分布式配置和使用,涵盖了从集群网络环境介绍到 Hadoop 集群配置和启动、Hadoop 使用等多个方面的知识点。 一、集群网络环境介绍 ...

    hadoop-eclipse-plugin插件和hadoop.dll和winutile.exe.zip

    综上所述,为了在Windows 10环境中使用Hadoop,你需要安装并配置好JDK,下载Hadoop 2.9.2并设置相应的环境变量,同时利用Eclipse 2020-03和Hadoop-Eclipse-Plugin插件进行开发。如果你已经启动了Eclipse,记得在安装...

    hadoop.dll & winutils.exe For hadoop-3.0.0

    标题中的“hadoop.dll & winutils.exe For hadoop-3.0.0”是指在Hadoop 3.0.0版本中使用的两个关键组件:hadoop.dll和winutils.exe。这两个文件对于在Windows环境中配置和运行Hadoop生态系统至关重要。 Hadoop是一...

    【2.7.5】hadoop.dll和winutils.exe

    这是一个动态链接库文件,主要在Windows操作系统中使用。在Hadoop的Windows版本中,`hadoop.dll`是Hadoop运行时环境的一部分,它包含了Hadoop在Windows上运行所需的特定功能和接口。由于Hadoop最初设计为在Linux环境...

    hadoop与mysql数据库相连读出数据.pdf

    从标题和描述中,我们可以了解到本文的主要知识点是Hadoop与MySQL数据库的集成,使用Hadoop读取MySQL数据库中的数据。 从标签中,我们可以看到本文没有提供任何标签信息。 从部分内容中,我们可以看到本文提供了...

    hadoop2.6.0版本hadoop.dll和winutils.exe

    在标题中提到的“hadoop2.6.0版本hadoop.dll和winutils.exe”是针对Windows环境下运行Hadoop的一些关键组件。 1. **Hadoop 2.6.0**: 这是Hadoop的一个主要版本,发布于2014年,带来了许多增强和改进。在Hadoop 2.x...

    eclipse连接hadoop所需要的hadoop.ddl和eclipse插件和hadoop运行案例

    关于`hadoop运行案例`,这可能是提供的一些示例代码或者教程,用于展示如何在Eclipse中使用Hadoop插件开发和运行实际的Hadoop项目。这些案例通常会涵盖基础的WordCount程序,以及其他更复杂的处理任务,如数据过滤、...

    hadoop2.6,hadoop.dll、winutils.exe下载

    在IT行业中,Hadoop是一个广泛使用的开源框架,主要用于大数据处理和分布式存储。Hadoop 2.6是Hadoop发展过程中的一个重要版本,它带来了许多性能优化和功能改进,旨在提高集群效率和稳定性。本资源提供了适用于64位...

    Hadoop2.7.1中文文档

    总的来说,Hadoop2.7.1中文文档是一个宝贵的资源,涵盖了Hadoop的基本概念、架构、配置、使用和最佳实践。无论你是初学者还是经验丰富的开发者,都能从中获取有价值的信息,提升自己在大数据领域的技能。通过深入...

Global site tag (gtag.js) - Google Analytics