用mapreduce實現將mysql資料匯出到HDFS上

來源:互聯網
上載者:User

標籤:des   style   blog   java   color   os   資料   io   

因為業務需要,需要將一批mysql資料匯入到HBASE,現在先將資料從Mysql匯出到HDFS。

版本:hadoop CDH4.5,Hbase-0.946

  1、實體類

    YqBean 是我的實體類,請根據自己需要修改,實體類需要 implements Writable, DBWritable。

  2、MR實現

import java.io.IOException;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.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.DBInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;/** * @author * @version 建立時間:Jul 24, 2014 2:09:22 AM * 類說明 */public class AccessData {    public static class DataAccessMap extends Mapper<LongWritable,YqBean,Text,Text>{        @Override        protected void map(LongWritable key, YqBean value,Context context)                throws IOException, InterruptedException {                System.out.println(value.toString());                context.write(new Text(), new Text(value.toString()));        }    }        public static class DataAccessReducer extends Reducer<Text,Text,Text,Text>{        protected void reduce(Text key, Iterable<Text> values,                Context context)                throws IOException, InterruptedException {             for(Iterator<Text> itr = values.iterator();itr.hasNext();)                {                         context.write(key, itr.next());                 }           }    }    public static void main(String[] args) throws Exception {            Configuration conf = new Configuration();           //mysql的jdbc驅動        DBConfiguration.configureDB(conf,"com.mysql.jdbc.Driver", "jdbc:mysql://ip:3306/tablename?useUnicode=true&characterEncoding=utf8", "username", "passwd");            Job job = new Job(conf,"test mysql connection");            job.setJarByClass(AccessData.class);                        job.setMapperClass(DataAccessMap.class);            job.setReducerClass(DataAccessReducer.class);                        job.setOutputKeyClass(Text.class);            job.setOutputValueClass(Text.class);                        job.setInputFormatClass(DBInputFormat.class);         FileOutputFormat.setOutputPath(job, new Path("hdfs://ip:9000/hdfsFile"));                        //對應資料庫中的列名(實體類欄位)           String[] fields = {"id","title","price","author","quantity","description","category_id","imgUrl"};          DBInputFormat.setInput(job, YqBean.class,"tablename", "sql語句 ", "title", fields);              System.exit(job.waitForCompletion(true)? 0 : 1);        }}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.