Search in sources :

Example 11 with HiveWriter

use of org.apache.storm.hive.common.HiveWriter in project storm by apache.

the class HiveBolt method retireIdleWriters.

/**
 * Locate all writers past idle timeout and retire them.
 * @return number of writers retired
 */
private int retireIdleWriters() {
    LOG.info("Attempting close idle writers");
    int count = 0;
    long now = System.currentTimeMillis();
    // 1) Find retirement candidates
    for (Entry<HiveEndPoint, HiveWriter> entry : allWriters.entrySet()) {
        if (now - entry.getValue().getLastUsed() > options.getIdleTimeout()) {
            ++count;
            retire(entry.getKey());
        }
    }
    return count;
}
Also used : HiveWriter(org.apache.storm.hive.common.HiveWriter) HiveEndPoint(org.apache.hive.hcatalog.streaming.HiveEndPoint) HiveEndPoint(org.apache.hive.hcatalog.streaming.HiveEndPoint)

Example 12 with HiveWriter

use of org.apache.storm.hive.common.HiveWriter in project storm by apache.

the class HiveBolt method prepare.

@Override
public void prepare(Map<String, Object> conf, TopologyContext topologyContext, OutputCollector collector) {
    try {
        tokenAuthEnabled = HiveUtils.isTokenAuthEnabled(conf);
        try {
            ugi = HiveUtils.authenticate(tokenAuthEnabled, options.getKerberosKeytab(), options.getKerberosPrincipal());
        } catch (HiveUtils.AuthenticationFailed ex) {
            LOG.error("Hive kerberos authentication failed " + ex.getMessage(), ex);
            throw new IllegalArgumentException(ex);
        }
        this.collector = collector;
        this.batchHelper = new BatchHelper(options.getBatchSize(), collector);
        allWriters = new ConcurrentHashMap<HiveEndPoint, HiveWriter>();
        String timeoutName = "hive-bolt-%d";
        this.callTimeoutPool = Executors.newFixedThreadPool(1, new ThreadFactoryBuilder().setNameFormat(timeoutName).build());
        sendHeartBeat.set(true);
        heartBeatTimer = new Timer(topologyContext.getThisTaskId() + "-hb-timer", true);
        setupHeartBeatTimer();
    } catch (Exception e) {
        LOG.warn("unable to make connection to hive ", e);
    }
}
Also used : Timer(java.util.Timer) HiveWriter(org.apache.storm.hive.common.HiveWriter) HiveUtils(org.apache.storm.hive.common.HiveUtils) HiveEndPoint(org.apache.hive.hcatalog.streaming.HiveEndPoint) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) StreamingException(org.apache.hive.hcatalog.streaming.StreamingException) IOException(java.io.IOException) BatchHelper(org.apache.storm.utils.BatchHelper)

Example 13 with HiveWriter

use of org.apache.storm.hive.common.HiveWriter in project storm by apache.

the class HiveState method retireEldestWriter.

/**
 * Locate writer that has not been used for longest time and retire it.
 */
private void retireEldestWriter() {
    long oldestTimeStamp = System.currentTimeMillis();
    HiveEndPoint eldest = null;
    for (Entry<HiveEndPoint, HiveWriter> entry : allWriters.entrySet()) {
        if (entry.getValue().getLastUsed() < oldestTimeStamp) {
            eldest = entry.getKey();
            oldestTimeStamp = entry.getValue().getLastUsed();
        }
    }
    try {
        LOG.info("Closing least used Writer to Hive end point : " + eldest);
        allWriters.remove(eldest).flushAndClose();
    } catch (IOException e) {
        LOG.warn("Failed to close writer for end point: " + eldest, e);
    } catch (InterruptedException e) {
        LOG.warn("Interrupted when attempting to close writer for end point: " + eldest, e);
        Thread.currentThread().interrupt();
    } catch (Exception e) {
        LOG.warn("Interrupted when attempting to close writer for end point: " + eldest, e);
    }
}
Also used : HiveWriter(org.apache.storm.hive.common.HiveWriter) HiveEndPoint(org.apache.hive.hcatalog.streaming.HiveEndPoint) IOException(java.io.IOException) FailedException(org.apache.storm.topology.FailedException) StreamingException(org.apache.hive.hcatalog.streaming.StreamingException) IOException(java.io.IOException)

Example 14 with HiveWriter

use of org.apache.storm.hive.common.HiveWriter in project storm by apache.

the class HiveState method writeTuples.

private void writeTuples(List<TridentTuple> tuples) throws Exception {
    for (TridentTuple tuple : tuples) {
        List<String> partitionVals = options.getMapper().mapPartitions(tuple);
        HiveEndPoint endPoint = HiveUtils.makeEndPoint(partitionVals, options);
        HiveWriter writer = getOrCreateWriter(endPoint);
        writer.write(options.getMapper().mapRecord(tuple));
        currentBatchSize++;
        if (currentBatchSize >= options.getBatchSize()) {
            flushAllWriters();
            currentBatchSize = 0;
        }
    }
}
Also used : HiveWriter(org.apache.storm.hive.common.HiveWriter) HiveEndPoint(org.apache.hive.hcatalog.streaming.HiveEndPoint) TridentTuple(org.apache.storm.trident.tuple.TridentTuple)

Example 15 with HiveWriter

use of org.apache.storm.hive.common.HiveWriter in project storm by apache.

the class HiveState method prepare.

public void prepare(Map<String, Object> conf, IMetricsContext metrics, int partitionIndex, int numPartitions) {
    try {
        tokenAuthEnabled = HiveUtils.isTokenAuthEnabled(conf);
        try {
            ugi = HiveUtils.authenticate(tokenAuthEnabled, options.getKerberosKeytab(), options.getKerberosPrincipal());
        } catch (HiveUtils.AuthenticationFailed ex) {
            LOG.error("Hive kerberos authentication failed " + ex.getMessage(), ex);
            throw new IllegalArgumentException(ex);
        }
        allWriters = new ConcurrentHashMap<HiveEndPoint, HiveWriter>();
        String timeoutName = "hive-bolt-%d";
        this.callTimeoutPool = Executors.newFixedThreadPool(1, new ThreadFactoryBuilder().setNameFormat(timeoutName).build());
        heartBeatTimer = new Timer("hive-hb-timer", true);
        setupHeartBeatTimer();
    } catch (Exception e) {
        LOG.warn("unable to make connection to hive ", e);
    }
}
Also used : Timer(java.util.Timer) HiveWriter(org.apache.storm.hive.common.HiveWriter) HiveUtils(org.apache.storm.hive.common.HiveUtils) HiveEndPoint(org.apache.hive.hcatalog.streaming.HiveEndPoint) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) FailedException(org.apache.storm.topology.FailedException) StreamingException(org.apache.hive.hcatalog.streaming.StreamingException) IOException(java.io.IOException)

Aggregations

HiveWriter (org.apache.storm.hive.common.HiveWriter)15 HiveEndPoint (org.apache.hive.hcatalog.streaming.HiveEndPoint)12 IOException (java.io.IOException)11 StreamingException (org.apache.hive.hcatalog.streaming.StreamingException)9 FailedException (org.apache.storm.topology.FailedException)5 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)4 Timer (java.util.Timer)4 HiveUtils (org.apache.storm.hive.common.HiveUtils)4 ExecutorService (java.util.concurrent.ExecutorService)2 BatchHelper (org.apache.storm.utils.BatchHelper)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ArrayList (java.util.ArrayList)1 SerializationError (org.apache.hive.hcatalog.streaming.SerializationError)1 TridentTuple (org.apache.storm.trident.tuple.TridentTuple)1