sql >> データベース >  >> RDS >> Mysql

ApacheHadoopデータ出力をMysqlデータベースに保存する

    このすばらしい例は、このブログでに示されています 、試してみましたが、とてもうまくいきました。コードの最も重要な部分を引用します。

    最初に、保存するデータを表すクラスを作成する必要があります。クラスはDBWritableインターフェースを実装する必要があります:

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

    レデューサーで以前に定義したクラスのオブジェクトを作成します:

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

    最後に、DBへの接続を構成し(クラスパスにdbコネクターを追加することを忘れないでください)、マッパーとリデューサーの入出力データ型を登録する必要があります。

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



    1. 同じテーブル内の子レコードを削除するMySqlトリガー

    2. データベースの同じ列に行の複数の入力を保存するにはどうすればよいですか?

    3. MySQL:日付が現在の週と現在の月に該当するテーブルからデータを選択します

    4. MySQLの1対多の関係-モデルを構築する方法は?