flink-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [flink] fhueske commented on a change in pull request #8034: [FLINK-11733] Provide HadoopMapFunction for org.apache.hadoop.mapreduce.Mapper
Date Wed, 29 May 2019 09:14:24 GMT
fhueske commented on a change in pull request #8034: [FLINK-11733] Provide HadoopMapFunction
for org.apache.hadoop.mapreduce.Mapper
URL: https://github.com/apache/flink/pull/8034#discussion_r288468272
 
 

 ##########
 File path: flink-connectors/flink-hadoop-compatibility/src/main/java/org/apache/flink/hadoopcompatibility/mapreduce/wrapper/HadoopProxyMapper.java
 ##########
 @@ -0,0 +1,388 @@
+/*
+ * 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.mapreduce.wrapper;
+
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.util.Collector;
+import org.apache.flink.util.Preconditions;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.io.RawComparator;
+import org.apache.hadoop.mapreduce.Counter;
+import org.apache.hadoop.mapreduce.InputFormat;
+import org.apache.hadoop.mapreduce.InputSplit;
+import org.apache.hadoop.mapreduce.JobID;
+import org.apache.hadoop.mapreduce.Mapper;
+import org.apache.hadoop.mapreduce.OutputCommitter;
+import org.apache.hadoop.mapreduce.OutputFormat;
+import org.apache.hadoop.mapreduce.Partitioner;
+import org.apache.hadoop.mapreduce.Reducer;
+import org.apache.hadoop.mapreduce.TaskAttemptID;
+import org.apache.hadoop.security.Credentials;
+
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.net.URI;
+
+/**
+ * This is a proxy maps a Hadoop Mapper (mapreduce API) which use reflection to get and invoke
some method.
+ */
+public class HadoopProxyMapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT> extends Mapper<KEYIN,
VALUEIN, KEYOUT, VALUEOUT> {
+
+	private final Mapper delegatedMapper;
+	private Method mapMethod;
+	private Method setupMethod;
+	private Method cleanupMethod;
+
+	public HadoopProxyMapper(Mapper delegatedMapper) {
+		this.delegatedMapper = Preconditions.checkNotNull(delegatedMapper);
+		try {
+			mapMethod = delegatedMapper.getClass().getDeclaredMethod("map", Object.class, Object.class,
Mapper.Context.class);
+			mapMethod.setAccessible(true);
+		} catch (NoSuchMethodException e) {
+			mapMethod = null;
+		}
+
+		try {
+			setupMethod = delegatedMapper.getClass().getDeclaredMethod("setup", Context.class);
+			setupMethod.setAccessible(true);
+		} catch (NoSuchMethodException e) {
+			setupMethod = null;
+		}
+
+		try {
+			cleanupMethod = delegatedMapper.getClass().getDeclaredMethod("cleanup", Context.class);
+			cleanupMethod.setAccessible(true);
+		} catch (NoSuchMethodException e) {
+			cleanupMethod = null;
+		}
+	}
+
+	@Override
+	public void setup(Context context) throws IOException, InterruptedException {
+		super.setup(context);
+		if (setupMethod != null) {
+			try {
+				setupMethod.invoke(delegatedMapper, context);
+			} catch (Exception e) {
+				throw new RuntimeException(e);
+			}
+		}
+	}
+
+	@Override
+	public void map(KEYIN key, VALUEIN value, Context context) throws IOException, InterruptedException
{
+		if (mapMethod != null) {
+			try {
+				mapMethod.invoke(delegatedMapper, key, value, context);
+			} catch (Exception e) {
+				throw new RuntimeException(e);
 
 Review comment:
   If we move this to `HadoopMapFunction`, we can remove the `try` completely as `MapFunction.map()`
throws `Exception`.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message