massdosage commented on a change in pull request #933: URL: https://github.com/apache/incubator-iceberg/pull/933#discussion_r414479378
########## File path: mr/src/main/java/org/apache/iceberg/mr/mapred/IcebergInputFormat.java ########## @@ -0,0 +1,259 @@ +/* + * 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.mapred; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Iterator; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; +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.FileSplit; +import org.apache.hadoop.mapred.InputFormat; +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.CombinedScanTask; +import org.apache.iceberg.DataFile; +import org.apache.iceberg.FileScanTask; +import org.apache.iceberg.Schema; +import org.apache.iceberg.Table; +import org.apache.iceberg.data.Record; +import org.apache.iceberg.hadoop.HadoopInputFile; +import org.apache.iceberg.hadoop.HadoopTables; +import org.apache.iceberg.io.CloseableIterable; +import org.apache.iceberg.io.InputFile; +import org.apache.iceberg.mr.SerializationUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * CombineHiveInputFormat.AvoidSplitCombination is implemented to correctly delegate InputSplit + * creation to this class. See: https://stackoverflow.com/questions/29133275/ + * custom-inputformat-getsplits-never-called-in-hive + */ +public class IcebergInputFormat<T> implements InputFormat<Void, T>, CombineHiveInputFormat.AvoidSplitCombination { + private static final Logger LOG = LoggerFactory.getLogger(IcebergInputFormat.class); + + static final String REUSE_CONTAINERS = "iceberg.mr.reuse.containers"; + + private Table table; + + @Override + public InputSplit[] getSplits(JobConf conf, int numSplits) throws IOException { + table = findTable(conf); + CloseableIterable taskIterable = table.newScan().planTasks(); + List<CombinedScanTask> tasks = (List<CombinedScanTask>) StreamSupport + .stream(taskIterable.spliterator(), false) + .collect(Collectors.toList()); + return createSplits(tasks, table.location()); + } + + private Table findTable(JobConf conf) throws IOException { Review comment: I think extending the other InputFormat would get really confusing. In this class you would then effectively end up with methods like `public InputSplit[] getSplits(JobConf conf, int numSplits` (from the mapred API returning a mapred InputSplit) and then also `public List<InputSplit> getSplits(JobContext context)` (from the mapreduce API returning a mapreduce InputSplit but only the former actually being used. I also worry that there will be some bizarre code in Hadoop or elsewhere that does `instanceof` checks on these classes to determine certain code paths and that could go very wrong if the mapred inputform extends the mapreduce one. I would favour moving the duplicated code out more along the lines of what @rdsr suggests and solve this via composition rather than inheritance. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org