All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.druid.storage.cassandra.CassandraDataSegmentPusher Maven / Gradle / Ivy

There is a newer version: 0.8.3
Show newest version
/*
 * Druid - a distributed column store.
 * Copyright 2012 - 2015 Metamarkets Group Inc.
 *
 * 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 io.druid.storage.cassandra;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Inject;
import com.metamx.common.CompressionUtils;
import com.metamx.common.logger.Logger;
import com.netflix.astyanax.MutationBatch;
import com.netflix.astyanax.recipes.storage.ChunkedStorage;
import io.druid.segment.SegmentUtils;
import io.druid.segment.loading.DataSegmentPusher;
import io.druid.segment.loading.DataSegmentPusherUtil;
import io.druid.timeline.DataSegment;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Cassandra Segment Pusher
 *
 * @author boneill42
 */
public class CassandraDataSegmentPusher extends CassandraStorage implements DataSegmentPusher
{
	private static final Logger log = new Logger(CassandraDataSegmentPusher.class);
	private static final int CONCURRENCY = 10;
	private static final Joiner JOINER = Joiner.on("/").skipNulls();  
	private final ObjectMapper jsonMapper;

  @Inject
	public CassandraDataSegmentPusher(
	    CassandraDataSegmentConfig config,
	    ObjectMapper jsonMapper)
	{
		super(config);
		this.jsonMapper=jsonMapper;
	}

  @Override
  public String getPathForHadoop(String dataSource)
  {
    throw new UnsupportedOperationException("Cassandra storage does not support indexing via Hadoop");
  }

  @Override
	public DataSegment push(final File indexFilesDir, DataSegment segment) throws IOException
	{
		log.info("Writing [%s] to C*", indexFilesDir);
		String key = JOINER.join(
		    config.getKeyspace().isEmpty() ? null : config.getKeyspace(),
		    DataSegmentPusherUtil.getStorageDir(segment)
		    );

		// Create index
		final File compressedIndexFile = File.createTempFile("druid", "index.zip");
		long indexSize = CompressionUtils.zip(indexFilesDir, compressedIndexFile);
		log.info("Wrote compressed file [%s] to [%s]", compressedIndexFile.getAbsolutePath(), key);

		int version = SegmentUtils.getVersionFromDir(indexFilesDir);

		try
		{
			long start = System.currentTimeMillis();
			ChunkedStorage.newWriter(indexStorage, key, new FileInputStream(compressedIndexFile))
			    .withConcurrencyLevel(CONCURRENCY).call();
			byte[] json = jsonMapper.writeValueAsBytes(segment);
			MutationBatch mutation = this.keyspace.prepareMutationBatch();
      mutation.withRow(descriptorStorage, key)
      	.putColumn("lastmodified", System.currentTimeMillis(), null)
      	.putColumn("descriptor", json, null);      	
      mutation.execute();
			log.info("Wrote index to C* in [%s] ms", System.currentTimeMillis() - start);
		} catch (Exception e)
		{
			throw new IOException(e);
		}

		segment = segment.withSize(indexSize)
		    .withLoadSpec(
		        ImmutableMap. of("type", "c*", "key", key)
		    )
		    .withBinaryVersion(version);

		log.info("Deleting zipped index File[%s]", compressedIndexFile);
		compressedIndexFile.delete();
		return segment;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy