forked from apache/iotdb
/
TSFInputSplit.java
273 lines (232 loc) · 7.83 KB
/
TSFInputSplit.java
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.iotdb.hadoop.tsfile;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapred.FileSplit;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
* This is tsfile <code>InputSplit</code>.<br>
* Each <code>InputSplit</code> will be processed by individual
* <code>Mapper</code> task.
*/
public class TSFInputSplit extends FileSplit implements Writable, org.apache.hadoop.mapred.InputSplit {
private Path path;
private String[] hosts;
private long length;
private List<ChunkGroupInfo> chunkGroupInfoList;
public TSFInputSplit() {
super();
}
public TSFInputSplit(Path path, String[] hosts, long length, List<ChunkGroupInfo> chunkGroupInfoList) {
super(path, 0, length, hosts);
this.path = path;
this.hosts = hosts;
this.length = length;
this.chunkGroupInfoList = chunkGroupInfoList;
}
/**
* @return the path
*/
public Path getPath() {
return path;
}
/**
* @param path the path to set
*/
public void setPath(Path path) {
this.path = path;
}
@Override
public long getLength() {
return this.length;
}
@Override
public String[] getLocations() throws IOException {
return this.hosts;
}
public List<ChunkGroupInfo> getChunkGroupInfoList() {
return chunkGroupInfoList;
}
public void setChunkGroupInfoList(List<ChunkGroupInfo> chunkGroupInfoList) {
this.chunkGroupInfoList = chunkGroupInfoList;
}
@Override
public void write(DataOutput out) throws IOException {
out.writeUTF(path.toString());
out.writeLong(length);
out.writeInt(hosts.length);
for (String string : hosts) {
out.writeUTF(string);
}
out.writeInt(chunkGroupInfoList.size());
ChunkGroupInfo.convertToThrift(chunkGroupInfoList, out);
}
@Override
public void readFields(DataInput in) throws IOException {
path = new Path(in.readUTF());
length = in.readLong();
int len = in.readInt();
hosts = new String[len];
for (int i = 0; i < len; i++) {
hosts[i] = in.readUTF();
}
int numOfChunkGroupInfo = in.readInt();
chunkGroupInfoList = ChunkGroupInfo.readFromThrift(numOfChunkGroupInfo, in);
}
@Override
public String toString() {
return "TSFInputSplit [path=" + path + ", chunkGroupInfoList=" + chunkGroupInfoList
+ ", length=" + length + ", hosts=" + Arrays.toString(hosts) + "]";
}
/**
* Used to store the key info in chunkGroupMetadata
*/
public static class ChunkGroupInfo {
/**
* Name of device.
*/
private String deviceId;
/**
* Name Of all the sensors in the device.
*/
private String[] measurementIds;
/**
* Byte offset of the corresponding data in the file
* Notice: include the chunk group marker.
*/
private long startOffset;
/**
* End Byte position of the whole chunk group in the file
* Notice: position after the chunk group footer.
*/
private long endOffset;
public ChunkGroupInfo() {
}
public ChunkGroupInfo(String deviceId, String[] measurementIds, long startOffset, long endOffset) {
this.deviceId = deviceId;
this.measurementIds = measurementIds;
this.startOffset = startOffset;
this.endOffset = endOffset;
}
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
public String[] getMeasurementIds() {
return measurementIds;
}
public void setMeasurementIds(String[] measurementIds) {
this.measurementIds = measurementIds;
}
public long getStartOffset() {
return startOffset;
}
public void setStartOffset(long startOffset) {
this.startOffset = startOffset;
}
public long getEndOffset() {
return endOffset;
}
public void setEndOffset(long endOffset) {
this.endOffset = endOffset;
}
@Override
public String toString() {
return "ChunkGroupInfo{" +
"deviceId='" + deviceId + '\'' +
", measurementIds=" + Arrays.toString(measurementIds) +
", startOffset=" + startOffset +
", endOffset=" + endOffset +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ChunkGroupInfo that = (ChunkGroupInfo) o;
return startOffset == that.startOffset &&
endOffset == that.endOffset &&
deviceId.equals(that.deviceId) &&
Arrays.equals(measurementIds, that.measurementIds);
}
@Override
public int hashCode() {
int result = Objects.hash(deviceId, startOffset, endOffset);
result = 31 * result + Arrays.hashCode(measurementIds);
return result;
}
/**
*
* @param chunkGroupInfoList List of ChunkGroupInfo need to be written into DataOutput
* @param out DataOutput
* @throws IOException
*/
static void convertToThrift(List<ChunkGroupInfo> chunkGroupInfoList, DataOutput out) throws IOException {
for (ChunkGroupInfo chunkGroupInfo : chunkGroupInfoList) {
out.writeInt(chunkGroupInfo.deviceId.getBytes(StandardCharsets.UTF_8).length);
out.write(chunkGroupInfo.deviceId.getBytes(StandardCharsets.UTF_8));
out.writeInt(chunkGroupInfo.measurementIds.length);
for (String measurementId : chunkGroupInfo.measurementIds) {
out.writeInt(measurementId.getBytes(StandardCharsets.UTF_8).length);
out.write(measurementId.getBytes(StandardCharsets.UTF_8));
}
out.writeLong(chunkGroupInfo.startOffset);
out.writeLong(chunkGroupInfo.endOffset);
}
}
/**
* Read the specific number of ChunkGroupInfo from DataInput
* @param num number of ChunkGroupInfo that need to be read
* @param in DataInput
* @return The List of ChunkGroupInfo read from DataInput
* @throws IOException
*/
static List<ChunkGroupInfo> readFromThrift(int num, DataInput in) throws IOException {
List<ChunkGroupInfo> chunkGroupInfoList = new ArrayList<>();
for (int i = 0; i < num; i++) {
int lenOfDeviceId = in.readInt();
byte[] b = new byte[lenOfDeviceId];
in.readFully(b);
String deviceId = new String(b, StandardCharsets.UTF_8);
int numOfMeasurement = in.readInt();
String[] measurementIds = new String[numOfMeasurement];
for (int j = 0; j < numOfMeasurement; j++) {
int lenOfMeasurementId = in.readInt();
byte[] c = new byte[lenOfMeasurementId];
in.readFully(c);
measurementIds[j] = new String(c, StandardCharsets.UTF_8);
}
long startOffset = in.readLong();
long endOffset = in.readLong();
chunkGroupInfoList.add(new ChunkGroupInfo(deviceId, measurementIds, startOffset, endOffset));
}
return chunkGroupInfoList;
}
}
}