我刚开始和hadoop合作。我做了一些基本的应用,比如word coutnt。但是现在我需要在Hadoop、HDFS和Mysql数据库之间传输数据。
我试着遵循代码,但没有得到正确的结果。
DbInputWritable.java
public class DBInputWritable implements Writable, DBWritable
{
private int id;
private String name;
public void readFields(DataInput in) throws IOException { }
public void readFields(ResultSet rs) throws SQLException
//Resultset object represents the data returned from a SQL statement
{
id = rs.getInt(1);
name = rs.getString(2);
}
public void write(DataOutput out) throws IOException { }
public void write(PreparedStatement ps) throws SQLException
{
ps.setInt(1, id);
ps.setString(2, name);
}
public int getId()
{
return id;
}
public String getName()
{
return name;
}
}DBOutputWritable.java
public class DBOutputWritable implements Writable, DBWritable
{
private String name;
private int count;
public DBOutputWritable(String name, int count)
{
this.name = name;
this.count = count;
}
public void readFields(DataInput in) throws IOException { }
public void readFields(ResultSet rs) throws SQLException
{
name = rs.getString(1);
count = rs.getInt(2);
}
public void write(DataOutput out) throws IOException { }
public void write(PreparedStatement ps) throws SQLException
{
ps.setString(1, name);
ps.setInt(2, count);
}
}请任何人帮忙。
发布于 2014-01-27 09:23:08
也许你可以给我们看看这个例外的细节。您的Hadoop库中有mysql驱动程序吗?顺便说一句,sqoop是在Hadoop和DB之间传输数据的一个很好的工具。如果dbdata不是很大,可以使用它。
发布于 2014-01-27 09:07:22
我不知道您的代码到底出了什么问题,但是您试过斯库普吗?
Sqoop非常容易将数据从sql数据库导入到hdfs,反之亦然。一旦hdfs中有了数据,就不必实现任何自定义输入。
编辑:您不需要与Hadoop集成,只需使用sqoop从mysql数据库中加载数据,并且一旦在hdfs中获得数据,就编写映射器代码,并在作业配置中设置hdfs上文件的路径。您可以遵循多个mapreduce教程,只需将要读取的文件更改为以前用Sqoop导入的文件。
发布于 2014-01-27 09:17:29
https://stackoverflow.com/questions/21376488
复制相似问题