將MapReduce的結果輸出至Mysql數據庫


package com.sun.mysql;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.db.DBConfiguration;
import org.apache.hadoop.mapreduce.lib.db.DBOutputFormat;
import org.apache.hadoop.mapreduce.lib.db.DBWritable;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;


/**
 * 將mapreduce的結果數據寫入mysql中
 * @author asheng
 */
public class WriteDataToMysql {
/**
     * 重寫DBWritable
     * @author asheng
     * TblsWritable需要向mysql中寫入數據
     */
    public static class TblsWritable implements Writable, DBWritable
    {  
            String tbl_name;  
            String tbl_type;  
            public TblsWritable()
            {  
            
            }  
            public TblsWritable(String tbl_name,String tab_type)
            {  
            this.tbl_name = tbl_name;
            this.tbl_type = tab_type;
            }  
            @Override  
            public void write(PreparedStatement statement) throws SQLException
            {
                    statement.setString(1, this.tbl_name);  
                    statement.setString(2, this.tbl_type);  
            }  
            @Override  
            public void readFields(ResultSet resultSet) throws SQLException
            {  
                    this.tbl_name = resultSet.getString(1);  
                    this.tbl_type = resultSet.getString(2);  
            }  
            @Override  
            public void write(DataOutput out) throws IOException
            {  
                    out.writeUTF(this.tbl_name);
                    out.writeUTF(this.tbl_type);
            }  
            @Override  
            public void readFields(DataInput in) throws IOException
            {  
                    this.tbl_name = in.readUTF();  
                    this.tbl_type = in.readUTF();  
            }  
            public String toString()
            {  
                return new String(this.tbl_name + " " + this.tbl_type);  
            }  
    }
    public static class ConnMysqlMapper extends Mapper<LongWritable,Text,Text,Text>
    //TblsRecord是自定義的類型,也就是上面重寫的DBWritable類
   {  
        public void map(LongWritable key,Text value,Context context)throws IOException,InterruptedException
        {  
        //<首字母偏移量,該行內容>接收進來,然后處理value,將abc和x作為map的輸出
        //key對於本程序沒有太大的意義,沒有使用
        String name = value.toString().split(" ")[0];
        String type = value.toString().split(" ")[1];
                context.write(new Text(name),new Text(type));  
        }  
   }  
    public static class ConnMysqlReducer extends Reducer<Text,Text,TblsWritable,TblsWritable>
    {  
        public void reduce(Text key,Iterable<Text> values,Context context)throws IOException,
                                                                                                                InterruptedException
        {  
        //接收到的key value對即為要輸入數據庫的字段,所以在reduce中:
        //wirte的第一個參數,類型是自定義類型TblsWritable,利用key和value將其組合成TblsWritable,
                                                                                                                    然后等待寫入數據庫
        //wirte的第二個參數,wirte的第一個參數已經涵蓋了要輸出的類型,所以第二個類型沒有用,設為null
        for(Iterator<Text> itr = values.iterator();itr.hasNext();)
                 {  
                     context.write(new TblsWritable(key.toString(),itr.next().toString()),null);
                 }  
        }  
    }  
    public static void main(String args[]) throws IOException, InterruptedException, ClassNotFoundException
    {
        Configuration conf = new Configuration();

        DBConfiguration.configureDB(conf, "com.mysql.jdbc.Driver","jdbc:mysql://127.0.0.1:3306/mapreduce_test","root", "root");    
        Job job = new Job(conf,"test mysql connection");  
        job.setJarByClass(ReadDataFromMysql.class);  
          
        job.setMapperClass(ConnMysqlMapper.class);  
        job.setReducerClass(ConnMysqlReducer.class);  
          
        job.setOutputKeyClass(Text.class);  
        job.setOutputValueClass(Text.class);
        
        job.setInputFormatClass(TextInputFormat.class);  
        job.setOutputFormatClass(DBOutputFormat.class);
        FileInputFormat.addInputPath(job, new Path(args[0]));
        
        DBOutputFormat.setOutput(job, "lxw_tabls", "TBL_NAME","TBL_TYPE");
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}
//執行輸入參數為/home/asheng/hadoop/in/test3.txt
//test3.txt中的內容為
/*
abc x
def y
chd z
*/
//即將abc x分別做為TBL_NAME,和TBL_TYPE插入數據庫中


//輸出結果在mysql數據庫中查看
//select * from lxw_tabls;
//發現新增三行
/*
abc x
def y
chd z
*/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM