Hadoop Streaming 讀ORC檔案
【背景】
hadoop Streaming的處理流程是先通過inputFormat讀出輸入檔案內容,將其傳遞mapper,再將mapper返回的key,value傳給reducer,最後將reducer返回的值通過outputformat寫入輸出檔案。
目前有個需求是通過hadoop streaming讀取roc檔案。使用正常的org.apache.orc.mapred.OrcInputFormat讀orc檔案時每行返回的值是:
null {"name":"123","age":"456"}
null {"name":"456","age":"789"}
返回這種資料的原因是OrcInputFormat讀取檔案返回的值是<NullWritable, OrcStruct>, NullWritable toString的返回值是null, OrcStruct toString的返回值是一個json串。
需要開發一個轉換器,只返回OrcInputFormat返回的json串的value即可。即返回:
123 456
456 789
【重寫InputFormat,單檔案讀取】
package is.orc;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.ql.io.sarg.SearchArgument;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.mapred.*;
import org.apache.orc.TypeDescription;
import org.apache.orc.mapred.OrcInputFormat;
import org.apache.orc.mapred.OrcMapredRecordReader;
import org.apache.orc.mapred.OrcStruct;
import org.apache.orc.Reader;
import org.apache.orc.Reader.Options;
import java.io.IOException;
public class OrcInputAsTextInputFormat extends org.apache.hadoop.mapred.FileInputFormat<Text, Text> {
//真正讀檔案的還是OrcInputFormat
protected OrcInputFormat<OrcStruct> orcInputFormat = new OrcInputFormat();
public RecordReader<Text, Text> getRecordReader(InputSplit split, JobConf job, Reporter reporter) throws IOException {
OrcMapredRecordReader realReader = (OrcMapredRecordReader) orcInputFormat.getRecordReader(split, job, reporter);
return new TextRecordReaderWrapper
(realReader);
}
public static boolean[] parseInclude(TypeDescription schema, String columnsStr) {
return OrcInputFormat.parseInclude(schema, columnsStr);
}
public static void setSearchArgument(Configuration conf, SearchArgument sarg, String[] columnNames) {
OrcInputFormat.setSearchArgument(conf, sarg, columnNames);
}
public static Options buildOptions(Configuration conf, Reader reader, long start, long length) {
return OrcInputFormat.buildOptions(conf, reader, start, length);
}
protected static class TextRecordReaderWrapper implements RecordReader<Text, Text> {
private OrcMapredRecordReader realReader;
private OrcStruct orcVal ;
private StringBuilder buffer;
private final int numOfFields;
public TextRecordReaderWrapper(OrcMapredRecordReader realReader) throws IOException{
this.realReader = realReader;
this.orcVal = (OrcStruct)realReader.createValue();
this.buffer = new StringBuilder();
this.numOfFields = this.orcVal.getNumFields();
}
public boolean next(Text key, Text value) throws IOException {
// 將第一個欄位作為key,剩餘的欄位以\t為分隔符組成字串作為value
if (realReader.next(NullWritable.get(), orcVal)){
buffer.setLength(0); //清空buffer
key.set(orcVal.getFieldValue(0).toString());
//以\t為分隔符,組裝返回值
for(int i = 1; i < numOfFields; ++i) {
buffer.append("\t");
WritableComparable curField = orcVal.getFieldValue(i);
if (curField != null && ! curField.equals(NullWritable.get())){
buffer.append(curField.toString());
}
}
value.set(buffer.substring(1)); //去掉開始新增的\t
return Boolean.TRUE;
}
return Boolean.FALSE;
}
public Text createKey() {
return new Text();
}
public Text createValue() {
return new Text();
}
public long getPos() throws IOException {
return realReader.getPos();
}
public void close() throws IOException {
realReader.close();
}
public float getProgress() throws IOException {
return realReader.getProgress();
}
}
}
【多檔案讀取】
MapReduce在讀資料的時候可以通過合併小檔案的方式減少map個數,比如說CombineSequenceFileInputFormat。如果不合並小檔案,可能出現map數過大的情況,資源消耗過多,且執行效率很慢。對應到orc格式時沒找到官方提供的包,只能自己寫一個。具體程式碼如下:
package is.orc;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.mapred.lib.CombineFileInputFormat;
import org.apache.hadoop.mapred.lib.CombineFileRecordReader;
import org.apache.hadoop.mapred.lib.CombineFileRecordReaderWrapper;
import org.apache.hadoop.mapred.lib.CombineFileSplit;
import java.io.IOException;
public class CombineOrcInputAsTextInputFormat
extends CombineFileInputFormat<org.apache.hadoop.io.Text, org.apache.hadoop.io.Text> {
@SuppressWarnings({ "rawtypes", "unchecked" })
public RecordReader<org.apache.hadoop.io.Text, org.apache.hadoop.io.Text> getRecordReader(InputSplit split, JobConf conf,
Reporter reporter) throws IOException {
return new CombineFileRecordReader(conf, (CombineFileSplit)split, reporter,
ORCFileRecordReaderWrapper.class);
}
/**
* A record reader that may be passed to <code>CombineFileRecordReader</code>
* so that it can be used in a <code>CombineFileInputFormat</code>-equivalent
* for <code>SequenceFileInputFormat</code>.
*
* @see CombineFileRecordReader
* @see CombineFileInputFormat
* @see SequenceFileInputFormat
*/
private static class ORCFileRecordReaderWrapper
extends CombineFileRecordReaderWrapper<org.apache.hadoop.io.Text, org.apache.hadoop.io.Text> {
// this constructor signature is required by CombineFileRecordReader
public ORCFileRecordReaderWrapper(CombineFileSplit split,
Configuration conf, Reporter reporter, Integer idx) throws IOException {
//只需配置此處的InputFormat為第一部分編寫的OrcInputAsTextInputFormat即可。具體的合併操作,CombineFileInputFormat已幫我們實現
super(new OrcInputAsTextInputFormat(), split, conf, reporter, idx);
}
}
}
相關文章
- Hive Streaming 追加 ORC 檔案Hive
- Hadoop之HDFS檔案讀寫流程說明Hadoop
- Hadoop-Streaming(Python篇)HadoopPython
- hive orc表'orc.create.index'='true'與'orc.create.index'='false'HiveIndexFalse
- 大資料檔案格式比較:AVRO vs. PARQUET vs. ORC大資料VR
- Hadoop檢視檔案///hadoop 清洗檔案出現亂碼Hadoop
- 我的讀取hadoop Sequence格式的檔案的程式碼Hadoop
- Spark Streaming監聽HDFS檔案(Spark-shell)Spark
- Serverless Streaming:毫秒級流式大檔案處理探秘Server
- hadoop 配置檔案簡析Hadoop
- hadoop叢集安裝檔案Hadoop
- Hadoop VERSION檔案誤刪Hadoop
- 檔案排版(文字檔案讀寫)
- 檔案讀寫
- hadoop基礎學習三十一(spark-streaming)HadoopSpark
- MR hadoop streaming job的學習 combinerHadoop
- ORC International:資料解讀亞太旅客如何使用社交媒體?
- Hadoop系列,執行jar檔案命令HadoopJAR
- Hadoop小檔案的處理方式Hadoop
- Golang 讀、寫檔案Golang
- Java 讀取檔案Java
- keras讀寫檔案Keras
- perl 讀寫檔案
- 檔案讀寫IO
- trace檔案閱讀
- tiff檔案讀取
- 任意檔案讀取
- 檔案的讀寫
- pandas讀 .sql檔案SQL
- VBA建立文字檔案、讀寫文字檔案
- 檔案操作之按照行讀寫檔案
- 比對檔案sam檔案的解讀
- HDFS讀檔案過程分析:讀取檔案的Block資料BloC
- hadoop streaming 按欄位排序與輸出分割詳解Hadoop排序
- ASP.NET工程檔案(.csproj)檔案解讀ASP.NET
- 讀取檔案流並寫入檔案流
- python讀取檔案——python讀取和儲存mat檔案Python
- hadoop之 解析HDFS的寫檔案流程Hadoop