Example usage for com.amazonaws.services.kinesis.model InvalidArgumentException InvalidArgumentException

List of usage examples for com.amazonaws.services.kinesis.model InvalidArgumentException InvalidArgumentException

Introduction

In this page you can find the example usage for com.amazonaws.services.kinesis.model InvalidArgumentException InvalidArgumentException.

Prototype

public InvalidArgumentException(String message) 

Source Link

Document

Constructs a new InvalidArgumentException with the specified error message.

Usage

From source file:StreamUtils.java

License:Open Source License

/**
 * Split a shard by dividing the hash key space in half.
 *
 * @param streamName Name of the stream that contains the shard to split.
 * @param shardId The id of the shard to split.
 *
 * @throws IllegalArgumentException When either streamName or shardId are null or empty.
 * @throws LimitExceededException Shard limit for the account has been reached.
 * @throws ResourceNotFoundException The stream or shard cannot be found.
 * @throws InvalidArgumentException If the shard is closed and no eligible for splitting.
 * @throws AmazonClientException Error communicating with Amazon Kinesis.
 *
 *///from  ww  w  . j  a  v  a  2s  .  c o m
public void splitShardEvenly(String streamName, String shardId) throws LimitExceededException,
        ResourceNotFoundException, AmazonClientException, InvalidArgumentException, IllegalArgumentException {
    if (streamName == null || streamName.isEmpty()) {
        throw new IllegalArgumentException("stream name is required");
    }
    if (shardId == null || shardId.isEmpty()) {
        throw new IllegalArgumentException("shard id is required");
    }

    DescribeStreamResult result = kinesis.describeStream(streamName);
    StreamDescription description = result.getStreamDescription();

    // Find the shard we want to split
    Shard shardToSplit = null;
    for (Shard shard : description.getShards()) {
        if (shardId.equals(shard.getShardId())) {
            shardToSplit = shard;
            break;
        }
    }

    if (shardToSplit == null) {
        throw new ResourceNotFoundException(
                "Could not find shard with id '" + shardId + "' in stream '" + streamName + "'");
    }

    // Check if the shard is still open. Open shards do not have an ending sequence number.
    if (shardToSplit.getSequenceNumberRange().getEndingSequenceNumber() != null) {
        throw new InvalidArgumentException("Shard is CLOSED and is not eligible for splitting");
    }

    // Calculate the median hash key to use as the new starting hash key for the shard.
    BigInteger startingHashKey = new BigInteger(shardToSplit.getHashKeyRange().getStartingHashKey());
    BigInteger endingHashKey = new BigInteger(shardToSplit.getHashKeyRange().getEndingHashKey());
    BigInteger[] medianHashKey = startingHashKey.add(endingHashKey).divideAndRemainder(new BigInteger("2"));
    BigInteger newStartingHashKey = medianHashKey[0];
    if (!BigInteger.ZERO.equals(medianHashKey[1])) {
        // In order to more evenly distributed the new hash key ranges across the new shards we will "round up" to
        // the next integer when our current hash key range is not evenly divisible by 2.
        newStartingHashKey = newStartingHashKey.add(BigInteger.ONE);
    }

    // Submit the split shard request
    kinesis.splitShard(streamName, shardId, newStartingHashKey.toString());
}

From source file:com.kinesis.datavis.utils.StreamUtils.java

License:Open Source License

/**
 * Split a shard by dividing the hash key space in half.
 *
 * @param streamName Name of the stream that contains the shard to split.
 * @param shardId The id of the shard to split.
 *
 * @throws IllegalArgumentException When either streamName or shardId are null or empty.
 * @throws LimitExceededException Shard limit for the account has been reached.
 * @throws ResourceNotFoundException The stream or shard cannot be found.
 * @throws InvalidArgumentException If the shard is closed and no eligible for splitting.
 * @throws AmazonClientException Error communicating with Amazon Kinesis.
 *
 *//*from www .ja va2  s  . c o  m*/
public void splitShardEvenly(String streamName, String shardId) throws LimitExceededException,
        ResourceNotFoundException, AmazonClientException, InvalidArgumentException, IllegalArgumentException {
    if (streamName == null || streamName.isEmpty()) {
        throw new IllegalArgumentException("stream name is required");
    }
    if (shardId == null || shardId.isEmpty()) {
        throw new IllegalArgumentException("shard id is required");
    }

    DescribeStreamResult result = kinesis.describeStream(streamName);
    StreamDescription description = result.getStreamDescription();

    // Find the shard we want to split
    Shard shardToSplit = null;
    for (Shard shard : description.getShards()) {
        if (shardId.equals(shard.getShardId())) {
            shardToSplit = shard;
            break;
        }
    }

    if (shardToSplit == null) {
        throw new ResourceNotFoundException(
                "Could not find shard with id '" + shardId + "' in stream '" + streamName + "'");
    }

    // Check if the shard is still open. Open shards do not have an ending sequence number.
    if (shardToSplit.getSequenceNumberRange().getEndingSequenceNumber() != null) {
        throw new InvalidArgumentException("Shard is CLOSED and is not eligible for splitting");
    }

    // Calculate the median hash key to use as the new starting hash key for the shard.
    BigInteger startingHashKey = new BigInteger(shardToSplit.getHashKeyRange().getStartingHashKey());
    BigInteger endingHashKey = new BigInteger(shardToSplit.getHashKeyRange().getEndingHashKey());
    BigInteger[] medianHashKey = startingHashKey.add(endingHashKey).divideAndRemainder(new BigInteger("2"));
    BigInteger newStartingHashKey = medianHashKey[0];

    if (!BigInteger.ZERO.equals(medianHashKey[1])) {
        // In order to more evenly distributed the new hash key ranges across the new shards we will "round up" to
        // the next integer when our current hash key range is not evenly divisible by 2.
        newStartingHashKey = newStartingHashKey.add(BigInteger.ONE);
    }

    // Submit the split shard request
    kinesis.splitShard(streamName, shardId, newStartingHashKey.toString());
}

From source file:com.srotya.flume.kinesis.sink.KinesisSink.java

License:Apache License

@Override
public void configure(Context ctx) {
    ImmutableMap<String, String> props = ctx.getSubProperties(Constants.SETTINGS);
    if (!props.containsKey(Constants.ACCESS_KEY) || !props.containsKey(Constants.ACCESS_SECRET)) {
        Throwables.propagate(//  ww w  .  j a va2  s .c  o  m
                new InvalidArgumentException("Must provide AWS credentials i.e. accessKey and accessSecret"));
    }
    awsCredentials = new BasicAWSCredentials(props.get(Constants.ACCESS_KEY),
            props.get(Constants.ACCESS_SECRET));
    clientConfig = new ClientConfiguration();
    if (props.containsKey(Constants.PROXY_HOST)) {
        clientConfig.setProxyHost(props.get(Constants.PROXY_HOST));
        clientConfig.setProxyPort(Integer.parseInt(props.getOrDefault(Constants.PROXY_PORT, "80")));
        clientConfig.setProtocol(Protocol.valueOf(props.getOrDefault(Constants.PROTOCOL, "HTTPS")));
    }
    if (!props.containsKey(Constants.STREAM_NAME)) {
        Throwables.propagate(new InvalidArgumentException("Must provide Kinesis stream name"));
    }
    streamName = props.get(Constants.STREAM_NAME);
    putSize = Integer.parseInt(props.getOrDefault(Constants.PUT_SIZE, "100"));
    if (putSize > 500) {
        Throwables.propagate(
                new InvalidArgumentException("AWS Kinesis doesn't allow more than 500 put requests"));
    }
    endpoint = props.getOrDefault(Constants.ENDPOINT, Constants.DEFAULT_ENDPOINT);
    String serializerClass = props.getOrDefault(Constants.SERIALIZER, GsonSerializer.class.getName());
    try {
        serializer = (KinesisSerializer) Class.forName(serializerClass).newInstance();
    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
        Throwables.propagate(e);
    }
    serializer.configure(props);
}

From source file:com.srotya.flume.kinesis.source.KinesisSource.java

License:Apache License

@Override
protected void doConfigure(Context ctx) throws FlumeException {
    ImmutableMap<String, String> props = ctx.getSubProperties(Constants.SETTINGS);
    if (!props.containsKey(Constants.ACCESS_KEY) || !props.containsKey(Constants.ACCESS_SECRET)) {
        Throwables.propagate(//from  w w  w . jav a 2  s  .  c  o  m
                new InvalidArgumentException("Must provide AWS credentials i.e. accessKey and accessSecret"));
    }
    awsCredentials = new BasicAWSCredentials(props.get(Constants.ACCESS_KEY),
            props.get(Constants.ACCESS_SECRET));
    clientConfig = new ClientConfiguration();
    if (props.containsKey(Constants.PROXY_HOST)) {
        clientConfig.setProxyHost(props.get(Constants.PROXY_HOST));
        clientConfig.setProxyPort(Integer.parseInt(props.getOrDefault(Constants.PROXY_PORT, "80")));
        clientConfig.setProtocol(Protocol.valueOf(props.getOrDefault(Constants.PROTOCOL, "HTTPS")));
    }
    if (!props.containsKey(Constants.STREAM_NAME)) {
        Throwables.propagate(new InvalidArgumentException("Must provide Kinesis stream name"));
    }
    streamName = props.get(Constants.STREAM_NAME);
    putSize = Integer.parseInt(props.getOrDefault(Constants.PUT_SIZE, "100"));
    if (putSize > 500) {
        Throwables.propagate(
                new InvalidArgumentException("AWS Kinesis doesn't allow more than 500 put requests"));
    }
    endpoint = props.getOrDefault(Constants.ENDPOINT, Constants.DEFAULT_ENDPOINT);
    String serializerClass = props.getOrDefault(Constants.SERIALIZER, GsonSerializer.class.getName());
    try {
        serializer = (KinesisSerializer) Class.forName(serializerClass).newInstance();
    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
        Throwables.propagate(e);
    }
    serializer.configure(props);
    shardId = Integer.parseInt(props.getOrDefault(SHARD_INDEX, "0"));
    shardIteratorType = props.getOrDefault(ITERATOR_TYPE, DEFAULT_ITERATOR_TYPE);
}