-
Notifications
You must be signed in to change notification settings - Fork 3k
Implement Hive input format #1192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7c93671
207a416
e519318
6f4fbe2
3b76337
f535af0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * 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.iceberg.mr.hive; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.hadoop.fs.Path; | ||
| import org.apache.hadoop.hive.ql.io.CombineHiveInputFormat; | ||
| import org.apache.hadoop.mapred.InputSplit; | ||
| import org.apache.hadoop.mapred.JobConf; | ||
| import org.apache.hadoop.mapred.RecordReader; | ||
| import org.apache.hadoop.mapred.Reporter; | ||
| import org.apache.iceberg.Schema; | ||
| import org.apache.iceberg.SchemaParser; | ||
| import org.apache.iceberg.Table; | ||
| import org.apache.iceberg.data.Record; | ||
| import org.apache.iceberg.mr.InputFormatConfig; | ||
| import org.apache.iceberg.mr.mapred.Container; | ||
| import org.apache.iceberg.mr.mapred.MapredIcebergInputFormat; | ||
| import org.apache.iceberg.mr.mapred.TableResolver; | ||
| import org.apache.iceberg.mr.mapreduce.IcebergSplit; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
|
|
||
| public class HiveIcebergInputFormat extends MapredIcebergInputFormat<Record> | ||
| implements CombineHiveInputFormat.AvoidSplitCombination { | ||
|
|
||
| private transient Table table; | ||
| private transient Schema schema; | ||
|
|
||
| @Override | ||
| public InputSplit[] getSplits(JobConf job, int numSplits) throws IOException { | ||
| table = TableResolver.resolveTableFromConfiguration(job); | ||
| schema = table.schema(); | ||
|
|
||
| forwardConfigSettings(job); | ||
|
|
||
| return Arrays.stream(super.getSplits(job, numSplits)) | ||
| .map(split -> new HiveIcebergSplit((IcebergSplit) split, table.location())) | ||
| .toArray(InputSplit[]::new); | ||
| } | ||
|
|
||
| @Override | ||
| public RecordReader<Void, Container<Record>> getRecordReader(InputSplit split, JobConf job, | ||
| Reporter reporter) throws IOException { | ||
| // Since Hive passes a copy of `job` in `getSplits`, we need to forward the conf settings again. | ||
| forwardConfigSettings(job); | ||
| return super.getRecordReader(split, job, reporter); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean shouldSkipCombine(Path path, Configuration conf) { | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Forward configuration settings to the underlying MR input format. | ||
| */ | ||
| private void forwardConfigSettings(JobConf job) { | ||
| Preconditions.checkNotNull(table, "Table cannot be null"); | ||
rdsr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Preconditions.checkNotNull(schema, "Schema cannot be null"); | ||
|
|
||
| // Once mapred.TableResolver and mapreduce.TableResolver use the same property for the location of the table | ||
| // (TABLE_LOCATION vs. TABLE_PATH), this line can be removed: see https://github.com/apache/iceberg/issues/1155. | ||
| job.set(InputFormatConfig.TABLE_PATH, table.location()); | ||
| job.set(InputFormatConfig.TABLE_SCHEMA, SchemaParser.toJson(schema)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /* | ||
| * 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.iceberg.mr.hive; | ||
|
|
||
| import java.io.DataInput; | ||
| import java.io.DataOutput; | ||
| import java.io.IOException; | ||
| import org.apache.hadoop.fs.Path; | ||
| import org.apache.hadoop.mapred.FileSplit; | ||
| import org.apache.iceberg.mr.SerializationUtil; | ||
| import org.apache.iceberg.mr.mapreduce.IcebergSplit; | ||
| import org.apache.iceberg.mr.mapreduce.IcebergSplitContainer; | ||
|
|
||
| // Hive requires file formats to return splits that are instances of `FileSplit`. | ||
| public class HiveIcebergSplit extends FileSplit implements IcebergSplitContainer { | ||
rdsr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private IcebergSplit innerSplit; | ||
|
|
||
| // Hive uses the path name of a split to map it back to a partition (`PartitionDesc`) or table description object | ||
| // (`TableDesc`) which specifies the relevant input format for reading the files belonging to that partition or table. | ||
| // That way, `HiveInputFormat` and `CombineHiveInputFormat` can read files with different file formats in the same | ||
| // MapReduce job and merge compatible splits together. | ||
| private String tableLocation; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like what's happening is the table location is used as the split's path so that Hive associates all splits with the same
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hive uses the path name of the split to map it back to a I'll update the comment. |
||
|
|
||
| // public no-argument constructor for deserialization | ||
| public HiveIcebergSplit() {} | ||
|
|
||
| HiveIcebergSplit(IcebergSplit split, String tableLocation) { | ||
| this.innerSplit = split; | ||
| this.tableLocation = tableLocation; | ||
| } | ||
|
|
||
| @Override | ||
| public IcebergSplit icebergSplit() { | ||
| return innerSplit; | ||
| } | ||
|
|
||
| @Override | ||
| public long getLength() { | ||
| return innerSplit.getLength(); | ||
| } | ||
|
|
||
| @Override | ||
| public String[] getLocations() { | ||
| return innerSplit.getLocations(); | ||
| } | ||
|
|
||
| @Override | ||
| public Path getPath() { | ||
| return new Path(tableLocation); | ||
| } | ||
|
|
||
| @Override | ||
| public long getStart() { | ||
| return 0; | ||
| } | ||
|
|
||
| @Override | ||
| public void write(DataOutput out) throws IOException { | ||
| byte[] bytes = SerializationUtil.serializeToBytes(tableLocation); | ||
| out.writeInt(bytes.length); | ||
| out.write(bytes); | ||
|
|
||
| innerSplit.write(out); | ||
| } | ||
|
|
||
| @Override | ||
| public void readFields(DataInput in) throws IOException { | ||
| byte[] bytes = new byte[in.readInt()]; | ||
| in.readFully(bytes); | ||
| tableLocation = SerializationUtil.deserializeFromBytes(bytes); | ||
|
|
||
| innerSplit = new IcebergSplit(); | ||
| innerSplit.readFields(in); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.