溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MapReduce操作Mysql的方法是什么

發(fā)布時間:2021-12-08 09:10:43 來源:億速云 閱讀:145 作者:iii 欄目:云計算

這篇文章主要講解了“MapReduce操作Mysql的方法是什么”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“MapReduce操作Mysql的方法是什么”吧!

hadoop技術推出一首曾遭到關系數(shù)據(jù)庫研究者的挑釁和批評,認為MapReduce不具有關系數(shù)據(jù)庫中的結構化數(shù)據(jù)存儲和處理能力。為此,hadoop社區(qū)和研究人員做了多的努力,在hadoop0.19版支持MapReduce訪問關系數(shù)據(jù)庫,如:mysql,MySQL、PostgreSQL、Oracle 等幾個數(shù)據(jù)庫系統(tǒng)。

1. 從Mysql讀出數(shù)據(jù)

Hadoop訪問關系數(shù)據(jù)庫主要通過一下接口實現(xiàn)的:DBInputFormat類,包所在位置:org.apache.hadoop.mapred.lib.db 中。DBInputFormat 在 Hadoop 應用程序中通過數(shù)據(jù)庫供應商提供的 JDBC接口來與數(shù)據(jù)庫進行交互,并且可以使用標準的 SQL 來讀取數(shù)據(jù)庫中的記錄。學習DBInputFormat首先必須知道二個條件。

  1. 在使用 DBInputFormat 之前,必須將要使用的 JDBC 驅動拷貝到分布式系統(tǒng)各個節(jié)點的$HADOOP_HOME/lib/目錄下。

  2. MapReduce訪問關系數(shù)據(jù)庫時,大量頻繁的從MapReduce程序中查詢和讀取數(shù)據(jù),這大大的增加了數(shù)據(jù)庫的訪問負載,因此,DBInputFormat接口僅僅適合讀取小數(shù)據(jù)量的數(shù)據(jù),而不適合處理數(shù)據(jù)倉庫。要處理數(shù)據(jù)倉庫的方法有:利用數(shù)據(jù)庫的Dump工具將大量待分析的數(shù)據(jù)輸出為文本,并上傳的Hdfs中進行處理

 DBInputFormat 類中包含以下三個內置類

  1. protected class DBRecordReader implementsRecordReader<LongWritable, T>:用來從一張數(shù)據(jù)庫表中讀取一條條元組記錄。

  2. 2.public static class NullDBWritable implements DBWritable,Writable:主要用來實現(xiàn) DBWritable 接口。DBWritable接口要實現(xiàn)二個函數(shù),第一是write,第二是readFileds,這二個函數(shù)都不難理解,一個是寫,一個是讀出所有字段。原型如下:

    public void write(PreparedStatement statement) throwsSQLException;public void readFields(ResultSet resultSet) throws SQLException;
  3. protected static class DBInputSplit implements InputSplit:主要用來描述輸入元組集合的范圍,包括 start 和 end 兩個屬性,start 用來表示第一條記錄的索引號,end 表示最后一條記錄的索引號.

下面對怎樣使用 DBInputFormat 讀取數(shù)據(jù)庫記錄進行詳細的介紹,具體步驟如下:

  1. DBConfiguration.configureDB (JobConf job, StringdriverClass, String dbUrl, String userName, String passwd)函數(shù),配置JDBC 驅動,數(shù)據(jù)源,以及數(shù)據(jù)庫訪問的用戶名和密碼。MySQL 數(shù)據(jù)庫的 JDBC 的驅動為“com.mysql.jdbc.Driver”,數(shù)據(jù)源為“jdbc:mysql://localhost/testDB”,其中testDB為訪問的數(shù)據(jù)庫。useName一般為“root”,passwd是你數(shù)據(jù)庫的密碼。

  2. DBInputFormat.setInput(JobConf job, Class<?extends DBWritable> inputClass, String tableName, String conditions,String orderBy, String... fieldNames),這個方法的參數(shù)很容易看懂,inputClass實現(xiàn)DBWritable接口。,string tableName表名, conditions表示查詢的條件,orderby表示排序的條件,fieldNames是字段,這相當與把sql語句拆分的結果。當然也可以用sql語句進行重載。etInput(JobConf job, Class<?extends DBWritable> inputClass, String inputQuery, StringinputCountQuery)。

  3. 編寫MapReduce函數(shù),包括Mapper 類、Reducer 類、輸入輸出文件格式等,然后調用JobClient.runJob(conf)。

上面講了理論,下面舉個例子:假設 MySQL 數(shù)據(jù)庫中有數(shù)據(jù)庫student,假設數(shù)據(jù)庫中的字段有“id”,“name”,“gender","number"。

第一步要實現(xiàn)DBwrite和write數(shù)據(jù)接口。代碼如下:

        public class StudentRecord implements Writable, DBWritable{            int id;
            String name;
            String gender;
            String number;
            @Override        public void readFields(DataInput in) throws IOException {            // TODO Auto-generated method stub
            this.id = in.readInt();            this.gender = Text.readString(in);            this.name = in.readString();            this.number = in.readString();
        }
            @Override        public void write(DataOutput out) throws IOException {            // TODO Auto-generated method stub
            out.writeInt(this.id);
            Text.writeString(out,this.name);            out.writeInt(this.gender);            out.writeInt(this.number);
        }
            
            @Override        public void readFields(ResultSet result) throws SQLException {            // TODO Auto-generated method stub
            this.id = result.getInt(1);            this.name = result.getString(2);            this.gender = result.getString(3);            this.number = result.getString(4);
        }
            
            @Override        public void write(PreparedStatement stmt) throws SQLException{            // TODO Auto-generated method stub
            stmt.setInt(1, this.id);
            stmt.setString(2, this.name);
            stmt.setString(3, this.gender);
            stmt.setString(4, this.number);
        }
            @Override        public String toString() {            // TODO Auto-generated method stub
            return new String(this.name + " " + this.gender + " " +this.number);
        }

第二步,實現(xiàn)Map和Reduce類

    public class DBAccessMapper extends MapReduceBase implements
            Mapper<LongWritable, TeacherRecord, LongWritable, Text> {
        @Override        public void map(LongWritable key, TeacherRecord value,
                OutputCollector<LongWritable, Text> collector, Reporter reporter)
                throws IOException {            // TODO Auto-generated method stub
            new collector.collect(new LongWritable(value.id), new Text(value
                    .toString()));
        }
    }

第三步:主函數(shù)的實現(xiàn),函數(shù)

public class DBAccessReader {    public static void main(String[] args) throws IOException {
        JobConf conf = new JobConf(DBAccessReader.class);
        conf.setOutputKeyClass(LongWritable.class);
        conf.setOutputValueClass(Text.class);
        conf.setInputFormat(DBInputFormat.class);
        FileOutputFormat.setOutputPath(conf, new Path("dboutput"));
        DBConfiguration.configureDB(conf,"com.mysql.jdbc.Driver",        "jdbc:mysql://localhost/school","root","123456");
        String [] fields = {"id", "name", "gender", "number"};
        DBInputFormat.setInput(conf, StudentRecord.class,"Student",null "id", fields);
        conf.setMapperClass(DBAccessMapper.class);
        conf.setReducerClass(IdentityReducer.class);
        JobClient.runJob(conf);
        }

}

2.寫數(shù)據(jù)

 往往對于數(shù)據(jù)處理的結果的數(shù)據(jù)量一般不會太大,可能適合hadoop直接寫入數(shù)據(jù)庫中。hadoop提供了相應的數(shù)據(jù)庫直接輸出的計算發(fā)結果。

  1. DBOutFormat: 提供數(shù)據(jù)庫寫入接口。

  2. DBRecordWriter:提供向數(shù)據(jù)庫中寫入的數(shù)據(jù)記錄的接口。

  3. DBConfiguration:提供數(shù)據(jù)庫配置和創(chuàng)建鏈接的接口。

DBOutFormat提供一個靜態(tài)方法setOutput(job,String table,String ...filedNames);該方法的參數(shù)很容易看懂。假設要插入一個Student的數(shù)據(jù),其代碼為

    public static void main(String[] args) throws IOException {
        Configuration conf = new Configuration();
        JobConf conf = new JobConf();
        conf.setOutputFormat(DBOutputFormat.class);
        DBConfiguration.configureDB(conf,"com.mysql.jdbc.Driver",                "jdbc:mysql://localhost/school","root","123456");
        DBOutputFormat.setOutput(conf,"Student", 456, "liqizhou", "man", "20004154578");
        JobClient.runJob(conf);

感謝各位的閱讀,以上就是“MapReduce操作Mysql的方法是什么”的內容了,經(jīng)過本文的學習后,相信大家對MapReduce操作Mysql的方法是什么這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI