andromache.hadoop.CassandraOutputFormat.java Source code

Java tutorial

Introduction

Here is the source code for andromache.hadoop.CassandraOutputFormat.java

Source

/*
 * Copyright 2013 Illarion Kovalchuk
 * <p/>
 * 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
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * 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 andromache.hadoop;

import andromache.config.CassandraConfigHelper;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapreduce.*;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.List;

/**
 * The <code>ColumnFamilyOutputFormat</code> acts as a Hadoop-specific
 * OutputFormat that allows reduce tasks to store keys (and corresponding
    
+ values) as Cassandra rows (and respective columns).
 * <p/>
 * For the sake of performance, this class employs a lazy write-back caching
 * mechanism, where its record writer batches mutations created based on the
 * reduce's inputs (in a task-specific map), and periodically makes the changes
 * official by sending a batch mutate request to Cassandra.
 * </p>
 */
public class CassandraOutputFormat extends OutputFormat<ByteBuffer, List<WritableMutation>>

{

    /**
    * Check for validity of the output-specification for the job.
    *
    * @param context information about the job
    * @throws java.io.IOException when output should not be attempted
    */
    @Override
    public void checkOutputSpecs(JobContext context) {
        Configuration conf = context.getConfiguration();
        if (CassandraConfigHelper.getOutputPartitioner(conf) == null) {
            throw new UnsupportedOperationException(
                    "You must set the output partitioner to the one used by your Cassandra cluster");
        }
        if (CassandraConfigHelper.getOutputInitialAddress(conf) == null) {
            throw new UnsupportedOperationException("You must set the initial output address to a Cassandra node");
        }
    }

    /**
    * The OutputCommitter for this format does not write any data to the DFS.
    *
    * @param context the task context
    * @return an output committer
    * @throws java.io.IOException
    * @throws InterruptedException
    */
    @Override
    public OutputCommitter getOutputCommitter(TaskAttemptContext context) throws IOException, InterruptedException {
        return new NullOutputCommitter();
    }

    /**
     * Get the {@link RecordWriter} for the given task.
     *
     * @param context the information about the current task.
     * @return a {@link RecordWriter} to write the output for the job.
     * @throws java.io.IOException
     */
    @Override
    public RecordWriter<ByteBuffer, List<WritableMutation>> getRecordWriter(final TaskAttemptContext context)
            throws IOException, InterruptedException {
        return new CassandraRecordWriter(context);
    }

    /**
     * An {@link OutputCommitter} that does nothing.
     */
    public static class NullOutputCommitter extends OutputCommitter {
        public void abortTask(TaskAttemptContext taskContext) {
        }

        public void commitTask(TaskAttemptContext taskContext) {
        }

        public boolean needsTaskCommit(TaskAttemptContext taskContext) {
            return false;
        }

        public void setupJob(JobContext jobContext) {
        }

        public void setupTask(TaskAttemptContext taskContext) {
        }
    }
}