Github user fhueske commented on a diff in the pull request:

    https://github.com/apache/incubator-flink/pull/37#discussion_r15342004
  
    --- Diff: 
flink-addons/flink-hadoop-compatibility/src/main/java/org/apache/flink/hadoopcompatibility/mapred/FlinkHadoopJobClient.java
 ---
    @@ -0,0 +1,318 @@
    +/**
    + * 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.flink.hadoopcompatibility.mapred;
    +
    +import org.apache.flink.api.java.DataSet;
    +import org.apache.flink.api.java.ExecutionEnvironment;
    +import org.apache.flink.api.java.operators.FlatMapOperator;
    +import org.apache.flink.api.java.operators.ReduceGroupOperator;
    +import org.apache.flink.api.java.operators.UnsortedGrouping;
    +import org.apache.flink.api.java.typeutils.TypeExtractor;
    +import 
org.apache.flink.hadoopcompatibility.mapred.utils.HadoopIdentityReduce;
    +import 
org.apache.flink.hadoopcompatibility.mapred.wrapper.HadoopGroupingKeySelector;
    +import 
org.apache.flink.hadoopcompatibility.mapred.wrapper.HadoopPartitioner;
    +import org.apache.flink.types.TypeInformation;
    +import org.apache.flink.util.InstantiationUtil;
    +import org.apache.hadoop.conf.Configuration;
    +import org.apache.hadoop.io.RawComparator;
    +import org.apache.hadoop.mapred.Counters;
    +import org.apache.hadoop.mapred.InputFormat;
    +import org.apache.hadoop.mapred.JobClient;
    +import org.apache.hadoop.mapred.JobConf;
    +import org.apache.hadoop.mapred.JobID;
    +import org.apache.hadoop.mapred.JobStatus;
    +import org.apache.hadoop.mapred.Mapper;
    +import org.apache.hadoop.mapred.Partitioner;
    +import org.apache.hadoop.mapred.Reducer;
    +import org.apache.hadoop.mapred.RunningJob;
    +import org.apache.hadoop.mapred.TaskAttemptID;
    +import org.apache.hadoop.mapred.TaskCompletionEvent;
    +
    +import java.io.IOException;
    +
    +/**
    + * The user's view of a Hadoop Job executed on a Flink cluster.
    + */
    +public class FlinkHadoopJobClient extends JobClient {
    +
    +   private final ExecutionEnvironment environment;
    +   private Configuration hadoopConf;
    +
    +   public FlinkHadoopJobClient() throws IOException {
    +           this(new Configuration());
    +   }
    +
    +   public FlinkHadoopJobClient(JobConf jobConf) throws IOException {
    +           this(new Configuration(jobConf));
    +   }
    +
    +   public FlinkHadoopJobClient(Configuration hadoopConf) throws 
IOException{
    +           this(hadoopConf, 
(ExecutionEnvironment.getExecutionEnvironment()));
    +   }
    +
    +   public FlinkHadoopJobClient(Configuration hadoopConf, 
ExecutionEnvironment environment) throws IOException {
    +           super(new JobConf(hadoopConf));
    +           this.hadoopConf = hadoopConf;
    +           this.environment = environment;
    +   }
    +
    +   /**
    +    * Submits a Hadoop job to Flink (as described by the JobConf) and 
returns after the job has been completed.
    +    */
    +   public static RunningJob runJob(JobConf hadoopJobConf) throws 
IOException{
    +           final FlinkHadoopJobClient jobClient = new 
FlinkHadoopJobClient(hadoopJobConf);
    +           final RunningJob job = jobClient.submitJob(hadoopJobConf);
    +           job.waitForCompletion();
    +           return job;
    +   }
    +
    +   /**
    +    * Submits a job to Flink and returns a RunningJob instance which can 
be scheduled and monitored
    +    * without blocking by default. Use waitForCompletion() to block until 
the job is finished.
    +    */
    +   @Override
    +   @SuppressWarnings("unchecked")
    +   public RunningJob submitJob(JobConf hadoopJobConf) throws IOException{
    +
    +           //setting up the inputFormat for the job
    +           final DataSet input = 
environment.createInput(getFlinkInputFormat(hadoopJobConf));
    +
    +           final Mapper mapper = 
InstantiationUtil.instantiate(hadoopJobConf.getMapperClass());
    +           final Class mapOutputKeyClass = 
hadoopJobConf.getMapOutputKeyClass();
    +           final Class mapOutputValueClass = 
hadoopJobConf.getMapOutputValueClass();
    +           final FlatMapOperator mapped = input.flatMap(new 
HadoopMapFunction(mapper, mapOutputKeyClass,
    +                           mapOutputValueClass));
    +           mapped.setParallelism(getMapParallelism(hadoopJobConf));
    +
    +           //Partitioning
    +           final Partitioner partitioner = 
InstantiationUtil.instantiate(hadoopJobConf.getPartitionerClass());
    +           final int noOfReduceTasks = hadoopJobConf.getNumReduceTasks();
    +           final UnsortedGrouping partitions = mapped.groupBy(new 
HadoopPartitioner(partitioner, noOfReduceTasks));
    +
    +           final ReduceGroupOperator identity = partitions.reduceGroup(new 
HadoopIdentityReduce()); //In order to regroup.
    +
    +           //Grouping
    +           final RawComparator comparator = 
hadoopJobConf.getOutputValueGroupingComparator();
    +           final UnsortedGrouping grouping = identity.groupBy(new 
HadoopGroupingKeySelector(comparator, mapOutputKeyClass));
    +
    +           //Sorting. TODO Custom sorting should be implemented. Ascending 
by default.
    +
    +           //Is a combiner specified in the jobConf?
    +           final Class<? extends Reducer> combinerClass = 
hadoopJobConf.getCombinerClass();
    +
    +           //Is a Reducer specified ? No reducer means identity reducer.
    +           final Class<? extends Reducer> reducerClass = 
hadoopJobConf.getReducerClass();
    +           final Reducer reducer = 
InstantiationUtil.instantiate(reducerClass);
    +
    +           //The output types of the reducers.
    +           final Class outputKeyClass = hadoopJobConf.getOutputKeyClass();
    +           final Class outputValueClass = 
hadoopJobConf.getOutputValueClass();
    +
    +           final ReduceGroupOperator reduceOp;
    +           if (combinerClass != null && 
combinerClass.equals(reducerClass)) {
    +                   reduceOp = grouping.reduceGroup(new 
HadoopReduceFunction(reducer, mapOutputKeyClass,
    +                                   mapOutputValueClass));
    +                   reduceOp.setCombinable(true);  //The combiner is the 
same class as the reducer.
    +           }
    +           else if(combinerClass != null) {  //We have a different 
combiner.
    --- End diff --
    
    The case of different combiner and reducer needs to be handled differently.
    You do 
`mapOut.groupBy(hadoopGrouper).reduceGroup(combiner).reduceGroup(reducer)`.
    
    In this case, the combiner is called as it was a reducer, i.e., after the 
data was partitioned and sorted. The purpose of a combiner is to reduce the 
data before shipping it over the network and (completely) sorting it.
    Moreover, the reduce is finally called on the fully reduced data as an 
AllReduce, which puts all data into a single group which will cause wrong 
results.
    
    The solution here is to have a HadoopReduceFunction that can deal with a 
Reduce and Combine function.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to