Sqoop切分數據的思想概況


Sqoop通過--split-by指定切分的字段,--m設置mapper的數量。通過這兩個參數分解生成m個where子句,進行分段查詢。因此sqoop的split可以理解為where子句的切分。

第一步,獲取切分字段的MIN()和MAX()

為了根據mapper的個數切分table,sqoop首先會執行一個sql,用於獲取table中該字段的最小值和最大值,源碼片段為org.apache.sqoop.mapreduce.DataDrivenImportJob 224行,大體為:

private String buildBoundaryQuery(String col, String query) {
    ....
    return "SELECT MIN(" + qualifiedName + "), MAX(" + qualifiedName + ") "
        + "FROM (" + query + ") AS " + alias;
  }

獲取到最大值和最小值,就可以根據不同的字段類型進行切分。

第二步,根據MIN和MAX不同的類型采用不同的切分方式

支持有Date,Text,Float,Integer,Boolean,NText,BigDecimal等等。

數字都是一個套路,就是

步長=(最大值-最小值)/mapper個數

,生成的區間為

[最小值,最小值+步長)
[最小值+2*步長,最小值+3*步長)
...
[最大值-步長,最大值]

可以參考下面的代碼片段org.apache.sqoop.mapreduce.db.FloatSplitter 43行

    List<InputSplit> splits = new ArrayList<InputSplit>();
    ...
    int numSplits = ConfigurationHelper.getConfNumMaps(conf);
    double splitSize = (maxVal - minVal) / (double) numSplits;
...
    double curLower = minVal;
    double curUpper = curLower + splitSize;

    while (curUpper < maxVal) {
        splits.add(new DataDrivenDBInputFormat.DataDrivenDBInputSplit(
          lowClausePrefix + Double.toString(curLower),
          highClausePrefix + Double.toString(curUpper)));
        curLower = curUpper;
        curUpper += splitSize;
    }

這樣最后每個mapper會執行自己的sql語句,比如第一個mapper執行:

select * from t where splitcol >= min and splitcol < min+splitsize

第二個mapper又會執行

select * from t where splitcol >= min+splitsize and splitcol < min+2*splitsize

其他字段類型

對於日期,會轉變成時間戳,同樣采用數字這種套路。

復雜的是字符串這種類型,最簡單的方式就是m小於26的時候,比如2,那么按照開頭字母就可以切分,[A,M),[M,Z].但是對於hello,helaa這種就只能到第四個字母才能切分了。因此字符串采用的算法是下面這種:

The algorithm used is as follows:
Since there are 2**16 unicode characters, we interpret characters as digits in base 65536. Given a string 's' containing characters s_0, s_1.. s_n, we interpret the string as the number: 0.s_0 s_1 s_2.. s_n in base 65536. Having mapped the low and high strings into floating-point values, we then use the BigDecimalSplitter to establish the even split points, then map the resulting floating point values back into strings.

實在看不懂英文!等再細致研究下在分享。

參考

Hdfs InputSplit切片詳解


免責聲明!

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



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