Java tutorial
/** * Copyright 2014-2015 SHAF-WORK * * Licensed 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.shaf.core.process.handle; import java.util.EventListener; import org.shaf.core.io.RecordReaderFactory; import org.shaf.core.io.RecordWriterFactory; import org.shaf.core.io.emulator.FileReader; import org.shaf.core.io.emulator.RecordReader; import org.shaf.core.io.emulator.RecordWriter; import org.shaf.core.process.Process; import org.shaf.core.process.ProcessException; import org.shaf.core.process.config.ProcessConfiguration; import org.shaf.core.process.type.dist.DistributedProcess; import org.shaf.core.util.DynamicClassLoader; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ListMultimap; import com.google.common.collect.ObjectArrays; /** * Defines an execution algorithm for the {@link DistributedProcess} in * {@code emulated} mode, when the input is the multiple-mapping. * * @author Mykola Galushka */ public class EmulatorMultiMapHandler extends EmulatorHandler { /** * Constructs a new {@link DistributedProcess distributed process} handler * for {@code emulated} mode, when the input is the multiple-mapping. * * @param cls * the process class, which need to be executed. * @param config * the process configuration. * @param listeners * the registering listeners. * @throws Exception * if an error occurs during handler construction. */ public EmulatorMultiMapHandler(Class<? extends Process> cls, ProcessConfiguration config, EventListener[] listeners) throws Exception { super(cls, config, listeners); } @SuppressWarnings("unchecked") @Override public Object run() throws ProcessException { try { Class<?>[] mcls = new Class<?>[0]; for (String input : super.config.getInputAsArray()) { mcls = ObjectArrays.concat(mcls, DynamicClassLoader.getClassByName(input.split("\\;")[1])); } ListMultimap<Object, Object> buffer = ArrayListMultimap.create(); for (int i = 0; i < mcls.length; i++) { FileReader.setPathIndex(super.job.getConfiguration(), i); try (RecordReader<Object, Object> reader = RecordReaderFactory .<Object, Object, Object, Object, Object, Object>createRecordReader( (Class<? extends DistributedProcess<Object, Object, Object, Object, Object, Object>>) mcls[i], super.job.getConfiguration());) { buffer.putAll(super.doMapping(reader, (Class<? extends DistributedProcess<Object, Object, Object, Object, Object, Object>>) mcls[i])); } catch (Exception exc) { throw new Exception("Fialed to emulate the map process.", exc); } } try (RecordWriter<Object, Object> writer = RecordWriterFactory.createRecordWriter( (Class<? extends DistributedProcess<Object, Object, Object, Object, Object, Object>>) super.cls, super.job.getConfiguration());) { super.doReducing(buffer, writer); } catch (Exception exc) { throw new Exception("Fialed to emulate the reduce process.", exc); } return null; } catch (Exception exc) { throw new ProcessExecException("Failed to emulate the map-reduce process in multi-mapping mode.", exc); } } }