-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathFixedWidthRecordReader.java
More file actions
151 lines (131 loc) · 4.37 KB
/
Copy pathFixedWidthRecordReader.java
File metadata and controls
151 lines (131 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.spark.fixedlength;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileSplit;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.RecordReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class FixedWidthRecordReader implements
RecordReader<LongWritable, Text> {
private static final int DEFAULT_BUFFER_SIZE = 64 * 1024;
String charsetName = "UTF-8";
String quote;
StringBuilder stringBuilder;
long start;
long end;
long pos;
FileSystem fs;
FSDataInputStream fileIn;
private String[] lengthsAndDelimiters;
final Path file;
InputStreamReader inputStreamReader;
char[] singleChar, multipleChars;
boolean isQuotePresent = false;
public FixedWidthRecordReader(JobConf conf, FileSplit split)
throws IOException {
lengthsAndDelimiters = FixedWidthHelper
.modifyIdentifier(conf.get("lengthsAndDelimiters").split(","));
quote = conf.get("quote");
charsetName = conf.get("charsetName");
start = split.getStart();
pos = start;
end = start + split.getLength();
file = split.getPath();
fs = file.getFileSystem(conf);
fileIn = fs.open(split.getPath());
fileIn.seek(start);
inputStreamReader = new InputStreamReader(fileIn, charsetName);
singleChar = new char[1];
stringBuilder = new StringBuilder();
}
@Override
public void close() throws IOException {
inputStreamReader.close();
fileIn.close();
}
@Override
public LongWritable createKey() {
return new LongWritable();
}
@Override
public Text createValue() {
return new Text("");
}
@Override
public synchronized float getProgress() throws IOException {
if (pos == end)
return 0.0f;
else {
return Math.min(1.0f, (pos - start) / (float) (end - start));
}
}
@Override
public synchronized boolean next(LongWritable key, Text value)
throws IOException, RuntimeException {
boolean fieldNotFound, isMatchingDelimiterInProgress = false, isSecondLastCharNewline = false, isThirdLastCharNewline = false;
boolean quoteCharFound = false;
int fieldLength, delimiterCharCounter;
stringBuilder.setLength(0);
if (!isEOFEncountered() && !isSecondLastCharNewline
&& !isThirdLastCharNewline) {
for (int i = 0; i < lengthsAndDelimiters.length
&& !isSecondLastCharNewline && !isThirdLastCharNewline; i++) {
fieldLength = Integer.parseInt(lengthsAndDelimiters[i]);
if (!(pos + fieldLength > end)) {
multipleChars = new char[fieldLength];
inputStreamReader.read(multipleChars);
pos += new String(multipleChars).getBytes(charsetName).length;
stringBuilder.append(multipleChars);
} else if ((isSecondLastChar() && isSecondLastCharNewline())
|| (isThirdLastChar() && isThirdLastCharNewline())) {
stringBuilder.setLength(0);
isSecondLastCharNewline = true;
isThirdLastCharNewline = true;
} else {
String message = "The input data is not according to specified schema. Expected data with delimiters or lengths as "
+ Arrays.toString(lengthsAndDelimiters)
+ ", got: " + stringBuilder.toString();
throw new RuntimeException(message);
}
}
} else {
return false;
}
if (!isThirdLastCharNewline && !isSecondLastCharNewline) {
value.set(stringBuilder.toString());
return true;
} else {
return false;
}
}
private boolean isThirdLastCharNewline() throws IOException {
inputStreamReader.read(singleChar);
pos += new String(singleChar).getBytes(charsetName).length;
stringBuilder.append(singleChar);
return stringBuilder.toString().contentEquals("\r");
}
private boolean isThirdLastChar() {
return pos == end - 2;
}
private boolean isSecondLastCharNewline() throws IOException {
inputStreamReader.read(singleChar);
pos += new String(singleChar).getBytes(charsetName).length;
stringBuilder.append(singleChar);
return stringBuilder.toString().contentEquals("\n");
}
private boolean isSecondLastChar() {
return pos == end - 1;
}
@Override
public synchronized long getPos() throws IOException {
return pos;
}
private boolean isEOFEncountered() {
return pos >= end;
}
}