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.io.emulator; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.io.Writable; import org.apache.hadoop.mapreduce.MRJobConfig; /** * A {@link RecordReader}, which writes key-value pairs to the binary files. * * <p> * The behavior of the {@code SequenceWriter} is similar to the behavior of the * Hadoop IO class {@code SequenceFileOutputFormat}. * </p> * * <p> * The record format: * </p> * <ul> * <li>Key type: <b>Writable</b></li> * <li>Value type: <b>Writable</b></li> * </ul> * * @author Mykola Galushka */ public class SequenceWriter extends FileWriter<Writable, Writable> { /** * The key class. */ private final Class<?> keyClass; /** * The value class. */ private final Class<?> valueClass; /** * Constructs a new sequence file writer. * * @param config * the writer configuration. * @throws IOException * if the writer has failed to initialize. */ public SequenceWriter(Configuration config) throws IOException { super(config); try { this.keyClass = Class.forName(config.get(MRJobConfig.OUTPUT_KEY_CLASS)); this.valueClass = Class.forName(config.get(MRJobConfig.OUTPUT_VALUE_CLASS)); super.out.writeUTF(this.keyClass.getCanonicalName()); super.out.writeUTF(this.valueClass.getCanonicalName()); } catch (ClassNotFoundException exc) { throw new IOException(exc); } } /** * Writes a {@link Record record} to the sequence files. */ @Override public void writeRecord(Record<Writable, Writable> record) throws IOException { record.getKey().write(super.out); record.getValue().write(super.out); } }