歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> RCFileInputFormat的使用方法

RCFileInputFormat的使用方法

日期:2017/3/1 9:57:19   编辑:Linux編程

在使用RCFileInputFormat時可以設置需要讀取的列的序號:

/**

* Sets read columns' ids(start from zero) for RCFile's Reader. Once a column

* is included in the list, RCFile's reader will not skip its value.

*

*/

public static void setReadColumnIDs(Configuration conf, ArrayList<Integer> ids) {

String id = toReadColumnIDString(ids);

setReadColumnIDConf(conf, id);

}

如果使用RCFileInputFormat,在map當中傳入的value為數據庫表的一行。如要讀取該行的每一列可以使用如下方法:

String[] columns = new String[columnNum];

for(int i=0;i<columnNum;i++){

columns[i]= new String(value.get(i).getData(),value.get(i).getStart(),

Value.get(i).getLength());

}

其中,columnNum為讀取的列數,然後在for循環中構造每一列。在這裡可能會有疑問的地方,為什麼每一列還需要start和length呢?因為value.get(i).getData()獲取的是該行所有列的數據,不是針對該列的數據,所以需要“開始位置”和“長度”。在這裡我們或許還可以推定:該行所有列value.get(i).getData()的方法其實是共享了一個byte數組。

更多Hadoop相關信息見Hadoop 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=13

Copyright © Linux教程網 All Rights Reserved