com.aerospike.client.AerospikeException Java Examples

The following examples show how to use com.aerospike.client.AerospikeException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AerospikeInputFormat.java    From aerospike-hadoop with Apache License 2.0 6 votes vote down vote up
private List<Host> getAliases(Host host) {
	InetAddress[] addresses;
	
	try {
		addresses = InetAddress.getAllByName(host.name);
	}
	catch (UnknownHostException uhe) {
		throw new AerospikeException.Connection("Invalid host: " + host);
	}
		
	if (addresses.length == 0) {
		throw new AerospikeException.Connection("Failed to find addresses for " + host);
	}
	
	// Add capacity for current address aliases plus IPV6 address and hostname.
	List<Host> aliases = new ArrayList<Host>(addresses.length + 2);
	
	for (InetAddress address : addresses) {
		aliases.add(new Host(address.getHostAddress(), host.tlsName, host.port));
	}
	
	return aliases;
}
 
Example #2
Source File: AerospikeTransactionalStore.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
@Override
public long getCommittedWindowId(String appId, int operatorId)
{
  try {
    lastWindowFetchCommand.setFilters(Filter.equal(metaTableOperatorIdColumn, operatorId));
    lastWindowFetchCommand.setFilters(Filter.equal(metaTableAppIdColumn, appId));
    long lastWindow = -1;
    RecordSet recordSet = client.query(null, lastWindowFetchCommand);
    while (recordSet.next()) {
      lastWindow = Long.parseLong(recordSet.getRecord().getValue(metaTableWindowColumn).toString());
    }
    return lastWindow;
  } catch (AerospikeException ex) {
    throw new RuntimeException(ex);
  }
}
 
Example #3
Source File: AbstractAerospikeTransactionalPutOperator.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
@Override
public void processBatch(Collection<T> tuples)
{
  Key key;
  Bin[] binsArray;
  try {
    for (T tuple: tuples) {
      key = getUpdatedBins(tuple,bins);
      binsArray = new Bin[bins.size()];
      binsArray = bins.toArray(binsArray);
      store.getClient().put(null, key, binsArray);
      bins.clear();
    }
  } catch (AerospikeException e) {
    throw new RuntimeException(e);
  }
}
 
Example #4
Source File: AerospikeBeanConfig.java    From datacollector with Apache License 2.0 6 votes vote down vote up
/**
 * initialize and validate configuration options
 *
 * @param context
 * @param issues
 */
public void init(Target.Context context, List<Target.ConfigIssue> issues) {
  List<Host> hosts = getAerospikeHosts(issues, connectionString, Groups.AEROSPIKE.getLabel(), "aerospikeBeanConfig.connectionString", context);
  ClientPolicy cp = new ClientPolicy();
  try {
    client = new AerospikeClient(cp, hosts.toArray(new Host[hosts.size()]));
    int retries = 0;
    while (!client.isConnected() && retries <= maxRetries) {
      if (retries > maxRetries) {
        issues.add(context.createConfigIssue(Groups.AEROSPIKE.getLabel(), "aerospikeBeanConfig.connectionString", AerospikeErrors.AEROSPIKE_03, connectionString));
        return;
      }
      retries++;
      try {
        Thread.sleep(100);
      } catch (InterruptedException ignored) {
      }
    }

  } catch (AerospikeException ex) {
    issues.add(context.createConfigIssue(Groups.AEROSPIKE.getLabel(), "aerospikeBeanConfig.connectionString", AerospikeErrors.AEROSPIKE_03, connectionString));
  }
}
 
Example #5
Source File: AbstractAerospikeNonTransactionalPutOperator.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@Override
public void processTuple(T tuple)
{
  Key key;
  Bin[] binsArray;
  try {
    key = getUpdatedBins(tuple,bins);
    binsArray = new Bin[bins.size()];
    binsArray = bins.toArray(binsArray);
    store.getClient().put(null, key, binsArray);
    bins.clear();
  } catch (AerospikeException e) {
    throw new RuntimeException(e);
  }
}
 
Example #6
Source File: AerospikeRecordReader.java    From deep-spark with Apache License 2.0 5 votes vote down vote up
@Override
public void scanCallback(Key key, Record record)
    throws AerospikeException {
    try {
        queue.put(new KeyRecPair(new AerospikeKey(key),
                                 new AerospikeRecord(record)));
    } catch (Exception ex) {
        throw new ScanTerminated(ex);
    }
}
 
Example #7
Source File: AerospikeRecordReader.java    From aerospike-hadoop with Apache License 2.0 5 votes vote down vote up
public void scanCallback(Key key, Record record)
    throws AerospikeException {
    try {
        queue.put(new KeyRecPair(new AerospikeKey(key),
                                 new AerospikeRecord(record)));
    } catch (Exception ex) {
        throw new ScanTerminated(ex);
    }
}
 
Example #8
Source File: AerospikeTargetIT.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test(expected = AerospikeException.class)
public void testNonExistentNamespace() throws Exception {
  AerospikeDTarget config = getDefaultConfig();
  Target target = config.createTarget();
  TargetRunner runner = new TargetRunner.Builder(AerospikeDTarget.class, target).setOnRecordError(OnRecordError.TO_ERROR).build();
  List<Record> records = new LinkedList<Record>();
  records.add(getTestRecord("notExists", "", "notExists", ""));
  runner.runInit();
  runner.runWrite(records);
  assertTrue("Record contains non-valid namespace", runner.getErrorRecords().stream().anyMatch(record -> record.getHeader().getErrorMessage().contains("Invalid namespace")));
  getRecord("notExists", "", "notExists");
}
 
Example #9
Source File: AerospikeTargetIT.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test(expected = AerospikeException.class)
public void testEmptyNamespace() throws Exception {
  AerospikeDTarget config = getDefaultConfig();
  Target target = config.createTarget();
  TargetRunner runner = new TargetRunner.Builder(AerospikeDTarget.class, target).setOnRecordError(OnRecordError.TO_ERROR).build();
  List<Record> records = new LinkedList<Record>();
  records.add(getTestRecord("", "", "emptyNamespace", ""));
  runner.runInit();
  runner.runWrite(records);
  assertTrue("Record contains empty namespace", runner.getErrorRecords().stream().anyMatch(record -> record.getHeader().getErrorMessage().contains("Invalid namespace")));
  getRecord("", "", "emptyNamespace");
}
 
Example #10
Source File: AerospikeTestUtils.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
public Key getKey()
{
  try {
    Key key = new Key(NAMESPACE, SET_NAME, String.valueOf(id));
    return key;
  } catch (AerospikeException e) {
    throw new RuntimeException("getKey failed: ", e);
  }
}
 
Example #11
Source File: AerospikeOperatorTest.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@Override
protected Key getUpdatedBins(TestEvent tuple, List<Bin> bins) throws AerospikeException
{

  Key key = new Key(NAMESPACE,SET_NAME,String.valueOf(tuple.id));
  bins.add(new Bin("ID",tuple.id));
  return key;
}
 
Example #12
Source File: AerospikeAbstractSink.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public void onFailure(AerospikeException e) {
    if (context != null) {
        context.fail();
    }
    try {
        queue.put(this);
    } catch (InterruptedException ex) {
        throw new RuntimeException("Interrupted while being added to the queue", ex);
    }
}
 
Example #13
Source File: AerospikeStore.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
/**
 * Create connection with database.
 */
@Override
public void connect()
{
  try {
    client = new AerospikeClient(node, port);
    logger.debug("Aerospike connection Success");
  } catch (AerospikeException ex) {
    throw new RuntimeException("closing database resource", ex);
  } catch (Throwable t) {
    DTThrowable.rethrow(t);
  }
}
 
Example #14
Source File: AerospikeTransactionalStore.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@Override
public void removeCommittedWindowId(String appId, int operatorId)
{
  try {
    String keyString = appId + String.valueOf(operatorId);
    Key key = new Key(namespace,metaSet,keyString.hashCode());
    client.delete(null, key);
  } catch (AerospikeException e) {
    throw new RuntimeException(e);
  }
}
 
Example #15
Source File: AerospikeTransactionalStore.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@Override
public void storeCommittedWindowId(String appId, int operatorId, long windowId)
{
  try {
    String keyString = appId + String.valueOf(operatorId);
    Key key = new Key(namespace,metaSet,keyString.hashCode());
    Bin bin1 = new Bin(metaTableAppIdColumn,appId);
    Bin bin2 = new Bin(metaTableOperatorIdColumn,operatorId);
    Bin bin3 = new Bin(metaTableWindowColumn,windowId);
    client.put(null, key, bin1,bin2,bin3);
  } catch (AerospikeException e) {
    throw new RuntimeException(e);
  }
}
 
Example #16
Source File: AerospikeTransactionalStore.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
private void createIndexes()
{
  IndexTask task;
  try {
    task = client.createIndex(null, namespace, metaSet,
        "operatorIdIndex", metaTableOperatorIdColumn, IndexType.NUMERIC);
    task.waitTillComplete();
    task = client.createIndex(null, namespace, metaSet,
        "appIdIndex", metaTableAppIdColumn, IndexType.STRING);
    task.waitTillComplete();
  } catch (AerospikeException ex) {
    throw new RuntimeException(ex);
  }

}
 
Example #17
Source File: AerospikeOutputOperator.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@Override
protected Key getUpdatedBins(Integer tuple, List<Bin> bins)
  throws AerospikeException
{

  Key key = new Key(KEYSPACE, SET_NAME, id++);
  bins.add(new Bin("ID", tuple));
  return key;
}
 
Example #18
Source File: AerospikeTarget.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Override
protected void write(Record record) throws StageException {
  ELVars variables = getContext().createELVars();
  RecordEL.setRecordInContext(variables, record);
  TimeEL.setCalendarInContext(variables, Calendar.getInstance());
  TimeNowEL.setTimeNowInContext(variables, new Date());

  List<Bin> bins = new LinkedList<>();
  for (BinConfig binConfig : binConfigList) {
    // retrieve action for record
    String opType = record.getHeader().getAttribute(OperationType.SDC_OPERATION_TYPE);
    int opCode = -1;
    if (!StringUtils.isEmpty(opType)) {
      try {
        opCode = AerospikeOperationType.convertToIntCode(opType);
      } catch (NumberFormatException | UnsupportedOperationException ex) {
        // Operation obtained from header is not supported. Handle accordingly
        switch (this.unsupportedAction) {
          case DISCARD:
            LOG.debug("Discarding record with unsupported operation {}", opType);
            return;
          case SEND_TO_ERROR:
            throw new OnRecordErrorException(AerospikeErrors.AEROSPIKE_08, opType);
          case USE_DEFAULT:
            opCode = this.defaultOperation.code;
            break;
          default: //unknown action
            throw new OnRecordErrorException(AerospikeErrors.AEROSPIKE_08, opType);
        }
      }
    } else {
      // No header attribute set. Use default.
      opCode = this.defaultOperation.code;
    }

    switch (opCode) {
      case OperationType.UPSERT_CODE:
        bins.add(
            new Bin(
                resolveEL(getContext().createELEval("binName"), variables, binConfig.binName, String.class),
                resolveEL(getContext().createELEval("binValue"), variables, binConfig.binValue, binConfig.valueType.getClassName())
            )
        );
        break;
      case OperationType.DELETE_CODE:
        bins.add(
            Bin.asNull(
                resolveEL(getContext().createELEval("binName"), variables, binConfig.binName, String.class)
            )
        );
        break;
      default:
        throw new UnsupportedOperationException(String.format("Unsupported Operation: %s", opCode));
    }
  }
  String namespace = resolveEL(elEvals.namespaceELEval, variables, namespaceEL, String.class);
  String set = resolveEL(elEvals.setELEval, variables, setEL, String.class);
  String key = resolveEL(elEvals.keyELEval, variables, keyEL, String.class);

  Key k = new Key(namespace, set, key);
  int retryCount = 0;
  while (retryCount <= aerospikeBeanConfig.maxRetries) {
    try {
      aerospikeBeanConfig.getAerospikeClient().put(
          defaultWritePolicy,
          k,
          bins.toArray(new Bin[bins.size()])
      );
      return;
    } catch (AerospikeException e) {
      retryCount++;
      if (retryCount > aerospikeBeanConfig.maxRetries) {
        throw new OnRecordErrorException(AerospikeErrors.AEROSPIKE_04, e);
      }
    }
  }

}
 
Example #19
Source File: AerospikeTargetIT.java    From datacollector with Apache License 2.0 4 votes vote down vote up
private static com.aerospike.client.Record getRecord(String namespace, String set, String key) throws InterruptedException, AerospikeException {
  synchronized (aerospikeServer) {
    return client.get(new QueryPolicy(), new Key(namespace, set, key));
  }
}
 
Example #20
Source File: AbstractAerospikeNonTransactionalPutOperator.java    From attic-apex-malhar with Apache License 2.0 2 votes vote down vote up
/**
 * Any concrete class needs to implement this method which using the input tuple, adds the
 * modified bins to bins list and returns the key for that updated record.
 *
 * @param tuple Tuple coming in from input port
 * @param bins list of bins that would be updated for this tuple
 * @return key for the row to be updated in the database
 * @throws AerospikeException
 */
protected abstract Key getUpdatedBins(T tuple, List<Bin> bins) throws AerospikeException;
 
Example #21
Source File: AbstractAerospikeTransactionalPutOperator.java    From attic-apex-malhar with Apache License 2.0 2 votes vote down vote up
/**
 * Any concrete class needs to implement this method which using the input tuple, adds the
 * modified bins to bins list and returns the key for that updated record.
 *
 * @param tuple Tuple coming in from input port
 * @param bins list of bins that would be updated for this tuple
 * @return key for the row to be updated in the database
 * @throws AerospikeException
 */
protected abstract Key getUpdatedBins(T tuple, List<Bin> bins) throws AerospikeException;