mysql - 将 Apache Hadoop 数据输出存储到 Mysql 数据库

标签 mysql hadoop mapreduce database-connection bigdata

我需要将map-reduce程序的输出存入数据库,请问有什么办法吗?

如果是这样,是否可以根据需要将输出存储到多个列和表中?

请给我一些解决方案。

谢谢你..

最佳答案

展示了很好的例子on this blog ,我试过了,效果非常好。我引用了代码中最重要的部分。

首先,您必须创建一个表示您要存储的数据的类。该类必须实现 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);
   }
}

在您的 Reducer 中创建先前定义的类的对象:

public class Reduce extends Reducer<Text, IntWritable, DBOutputWritable, NullWritable> {

   protected void reduce(Text key, Iterable<IntWritable> 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();
     }
   }
}

最后,您必须配置与您的数据库的连接(不要忘记在类路径中添加您的数据库连接器)并注册您的映射器和缩减器的输入/输出数据类型。

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

关于mysql - 将 Apache Hadoop 数据输出存储到 Mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18351475/

相关文章:

java - Reducer节点需要很长时间才能接收其记录

php - 如何在 laravel 的编辑表单中获取可选值

php - 根据到当前 GPS 位置的距离重新排序 <select> 位置列表

hadoop - Microsoft HDInsight仪表板服务不可用

java - job.setOutputKeyClass 和 job.setOutputReduceClass 指的是哪里?

尽管安装了最新版本,Hadoop 仍显示旧版本

mysql - ER_CON_COUNT_ERROR : Too many connections knex and bookshelf

php - 使用日期范围搜索时优化 mysql 查询

node.js - 如何在MongoDB中为共同的 friend 建模

hadoop - 在 Cassandra 表上运行 PIG 脚本