1. 程式人生 > >Hadoop原始碼詳解之DBOutputFormat類

Hadoop原始碼詳解之DBOutputFormat類

Hadoop 原始碼詳解之 DBOutputFormat

1. 類釋義

A OutputFormat that sends the reduce output to a SQL table.
一種將Reduce 輸出到一個SQL表中的輸出格式。

DBOutputFormat accepts <key,value> pairs, where key has a type extending DBWritable. Returned RecordWriter writes only the key to the database with a batch SQL query.
DBOuputFormat

接收<key,value>對,這其中的key實現了 DBWritable 介面。返回的RecordWriter 使用一個批處理的SQL 查詢語句 僅僅把鍵寫到database中。

2. 類原始碼

3. 方法詳解

3.1 setOutput()方法
  • 方法釋義

Initializes the reduce-part of the job with the appropriate output settings
使用合適的輸出設定初始化job的 reduce部分。

  • 方法原始碼
/**
   * @param job The job
   * @param tableName The table to insert data into
   * @param fieldNames The field names in the table.
   */
public static void setOutput(Job job, String tableName, String... fieldNames) throws IOException { if(fieldNames.length > 0 && fieldNames[0] != null) { DBConfiguration dbConf = setOutput(job, tableName); dbConf.setOutputFieldNames(fieldNames); } else { if
(fieldNames.length > 0) { setOutput(job, tableName, fieldNames.length); } else { throw new IllegalArgumentException( "Field names must be greater than 0"); } } }

注意上述的輸出欄位結果可以是多個列,所以其引數使用的是String ... fieldName