hadoop 输出结果保存mysql,将Apache Hadoop数据输出存储到Mysql数据库

I need to store output of map-reduce program into database, so is there any way?

If so, is it possible to store output into multiple columns & tables based on requirement??

please suggest me some solutions.

Thank you..

解决方案

The great example is shown on this blog, I tried it and it goes really well. I quote the most important parts of the code.

At first, you must create a class representing data you would like to store. The class must implement DBWritable interface:

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);

}

}

Create objects of previously defined class in your Reducer:

public class Reduce extends Reducer {

protected void reduce(Text key, Iterable values, Context ctx) {

int sum = 0;

for(IntWritable value : values) {

sum += value.get();

}

try {

ctx.write(new DBOutputWritable(key.toString(), sum), NullWritable.get());

} catch(IOException e) {

e.printStackTrace();

} catch(InterruptedException e) {

e.printStackTrace();

}

}

}

Finally you must configure a connection to your DB (do not forget to add your db connector on the classpath) and register your mapper's and reducer's input/output data types.

public class Main

{

public static void main(String[] args) throws Exception

{

Configuration conf = new Configuration();

DBConfiguration.configureDB(conf,

"com.mysql.jdbc.Driver", // driver class

"jdbc:mysql://localhost:3306/testDb", // db url

"user", // username

"password"); //password

Job job = new Job(conf);

job.setJarByClass(Main.class);

job.setMapperClass(Map.class); // your mapper - not shown in this example

job.setReducerClass(Reduce.class);

job.setMapOutputKeyClass(Text.class); // your mapper - not shown in this example

job.setMapOutputValueClass(IntWritable.class); // your mapper - not shown in this example

job.setOutputKeyClass(DBOutputWritable.class); // reducer's KEYOUT

job.setOutputValueClass(NullWritable.class); // reducer's VALUEOUT

job.setInputFormatClass(...);

job.setOutputFormatClass(DBOutputFormat.class);

DBInputFormat.setInput(...);

DBOutputFormat.setOutput(

job,

"output", // output table name

new String[] { "name", "count" } //table columns

);

System.exit(job.waitForCompletion(true) ? 0 : 1);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值