com.aerospike.client.Bin Java Examples

The following examples show how to use com.aerospike.client.Bin. 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: SampleData.java    From aerospike-hadoop with Apache License 2.0 6 votes vote down vote up
public static void runTextFile(String[] args, int argi) throws Exception {

        while (argi < args.length) {
            String path = args[argi++];
            log.info("processing " + path + " ...");
            int nrecs = 0;
            BufferedReader br = new BufferedReader(new FileReader(path));
            for (String line; (line = br.readLine()) != null; ) {
                // The key is "path:linenum".
                String keystr = path + ':' + Long.toString(nrecs++);
                Key key = new Key(namespace, setName, keystr);
                Bin bin = new Bin(binName, line);
                client.put(writePolicy, key, bin);
            }
            log.info("inserted " + nrecs + " records");
            br.close();
        }
    }
 
Example #2
Source File: ExternalJoin.java    From aerospike-hadoop with Apache License 2.0 6 votes vote down vote up
@Override
public void writeAerospike(Text sessid,
                           Session session,
                           AerospikeClient client,
                           WritePolicy writePolicy,
                           String namespace,
                           String setName) throws IOException {
    writePolicy.totalTimeout = 10000;
    Key kk = new Key(namespace, setName, sessid.toString());
    Bin bin0 = new Bin("userid", session.userid);
    Bin bin1 = new Bin("start", session.start);
    Bin bin2 = new Bin("end", session.end);
    Bin bin3 = new Bin("nhits", session.nhits);
    Bin bin4 = new Bin("age", session.age);
    Bin bin5 = new Bin("isMale", session.isMale);
    client.put(writePolicy, kk, bin0, bin1, bin2, bin3, bin4, bin5);
}
 
Example #3
Source File: AerospikeJavaRDDFT.java    From deep-spark with Apache License 2.0 6 votes vote down vote up
/**
 * Imports dataset.
 *
 * @throws java.io.IOException
 */
private static void dataSetImport() throws IOException, ParseException {
    URL url = Resources.getResource(DATA_SET_NAME);
    JSONParser parser = new JSONParser();
    Object obj = parser.parse(new FileReader(url.getFile()));
    JSONObject jsonObject = (JSONObject) obj;

    String id = (String) jsonObject.get("id");
    JSONObject metadata = (JSONObject) jsonObject.get("metadata");
    JSONArray cantos = (JSONArray) jsonObject.get("cantos");

    Key key = new Key(NAMESPACE_ENTITY, SET_NAME, id);
    Bin binId = new Bin("id", id);
    Bin binMetadata = new Bin("metadata", metadata);
    Bin binCantos = new Bin("cantos", cantos);
    aerospike.put(null, key, binId, binMetadata, binCantos);
    aerospike.createIndex(null, NAMESPACE_ENTITY, SET_NAME, "id_idx", "id", IndexType.STRING);

    Key key2 = new Key(NAMESPACE_CELL, SET_NAME, 3);
    Bin bin_id = new Bin("_id", "3");
    Bin bin_number = new Bin("number", 3);
    Bin bin_text = new Bin("message", "new message test");
    aerospike.put(null, key2, bin_id, bin_number, bin_text);
    aerospike.createIndex(null, NAMESPACE_CELL, SET_NAME, "num_idx", "number", IndexType.NUMERIC);
    aerospike.createIndex(null, NAMESPACE_CELL, SET_NAME, "_id_idx", "_id", IndexType.STRING);
}
 
Example #4
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 #5
Source File: RedissonClient.java    From XRTB with Apache License 2.0 6 votes vote down vote up
/**
 * Set a key value as string with an expiration (No expiration set on cache2k, it is already set 
 * @param skey String. The key name.
 * @param value String. The value.
 * @param expire int. The number of seconds before expiring.
 * @throws Exception on aerorpike or cache errors.
 */
public void set(String skey, String value, int expire) throws Exception {
	if (ae == null) {
		cache.put(skey, value);
		return;
	}
	
	AerospikeClient client = ae.getClient();
	if (client == null)
		return;
	
	WritePolicy policy = new WritePolicy();
	policy.expiration = expire;
	Key key = new Key("test", "cache", skey);
	Bin bin1 = new Bin("value", value);
	ae.getClient().put(policy, key, bin1);
}
 
Example #6
Source File: SparkSessionRollup.java    From aerospike-hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public void writeAerospike(String sessid,
                           Session session,
                           AerospikeClient client,
                           WritePolicy writePolicy,
                           String namespace,
                           String setName) throws IOException {
    Key kk = new Key(namespace, setName, sessid.toString());
    Bin bin0 = new Bin("userid", session.userid);
    Bin bin1 = new Bin("start", session.start);
    Bin bin2 = new Bin("end", session.end);
    Bin bin3 = new Bin("nhits", session.nhits);
    client.put(writePolicy, kk, bin0, bin1, bin2, bin3);
}
 
Example #7
Source File: GenerateProfiles.java    From aerospike-hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public void writeAerospike(LongWritable userid,
                           Profile profile,
                           AerospikeClient client,
                           WritePolicy writePolicy,
                           String namespace,
                           String setName) throws IOException {
    writePolicy.totalTimeout = 10000;
    Key kk = new Key(namespace, setName, userid.get());
    Bin bin0 = new Bin("userid", profile.userid);
    Bin bin1 = new Bin("age", profile.age);
    Bin bin2 = new Bin("isMale", profile.isMale);
    client.put(writePolicy, kk, bin0, bin1, bin2);
}
 
Example #8
Source File: AerospikeDeepOutputFormat.java    From deep-spark with Apache License 2.0 5 votes vote down vote up
@Override
public void writeAerospike(Object key,
                           AerospikeRecord record,
                           AerospikeClient client,
                           WritePolicy writePolicy,
                           String namespace,
                           String setName) throws IOException {
    Key k = new Key(namespace, setName, key.toString());
    List<Bin> bins = new ArrayList<>();
    for (Map.Entry<String, Object> bin : record.bins.entrySet()) {
        Bin aerospikeBin = new Bin(bin.getKey(), bin.getValue());
        bins.add(aerospikeBin);
    }
    client.put(writePolicy, k, bins.toArray(new Bin[bins.size()]));
}
 
Example #9
Source File: RedissonClient.java    From XRTB with Apache License 2.0 5 votes vote down vote up
/**
 * Add a map (a user map) to the the cache or aerorspike.
 * @param name String. The name of the map.
 * @param map Map. The map of the User object.
 * @throws Exception on cache2k/aerospike errors
 */
public void addMap(String name, Map map) throws Exception {
	if (ae == null) {
		cacheDb.put(name,map);
		return;
	}
	
	Key key = new Key("test", "database", "rtb4free");
	String data = mapper.writer().writeValueAsString(map);
	Bin bin1 = new Bin("map", data);
	ae.getClient().put(null, key, bin1);
}
 
Example #10
Source File: RedissonClient.java    From XRTB with Apache License 2.0 5 votes vote down vote up
/**
 * Ass a set of strings to the cache or aerospike. Used for blacklists.
 * @param name String. The name of the set of strings.
 * @param set Set. The set of strings.
 * @throws Exception
 */
public void addSet(String name, Set set) throws Exception {
	if (ae == null) {
		cacheDb.put(name,set);
		return;
	}
	
	Key key = new Key("test", "database", "rtb4free");
	String data = mapper.writer().writeValueAsString(set);
	Bin bin1 = new Bin("set", data);
	ae.getClient().put(null, key, bin1);
}
 
Example #11
Source File: RedissonClient.java    From XRTB with Apache License 2.0 5 votes vote down vote up
/**
 * Set a key value as string.
 * @param skey String. The key name.
 * @param value String. The value.
 * @throws Exception on aerorpike or cache errors.
 */
public void set(String skey, String value)  {
	if (ae == null) {
		cache.put(skey, value);
		return;
	}
	Key key = new Key("test", "cache", skey);
	Bin bin1 = new Bin("value", value);
	ae.getClient().delete(null, key);
	ae.getClient().put(null, key, bin1);
}
 
Example #12
Source File: RedissonClient.java    From XRTB with Apache License 2.0 5 votes vote down vote up
/**
 * Mimic a REDIS mhset operation.
 * @param id String. The key of the map.
 * @param m Map. The map to set.
 */
public void hmset(String id, Map m) throws Exception {
	if (ae == null) {
		cache.put(id, m);
		return;
	}
	
	Key key = new Key("test", "cache", id);
	Bin bin1 = new Bin("value", m);
	ae.getClient().put(null, key, bin1);
}
 
Example #13
Source File: RedissonClient.java    From XRTB with Apache License 2.0 5 votes vote down vote up
/**
 * Do a mhset with expire (No op on cache2k, expiry already set globally
 * @param id String. The key name.
 * @param m Map. The value to set.
 * @param expire int. The number of seconds before expiry.
 * @throws Exception on Cache2k or aerospike errors.
 */
public void hmset(String id, Map m, int expire) throws Exception  {
	if (ae == null) {
		cache.put(id, m);
		return;
	}
	WritePolicy policy = new WritePolicy();
	policy.expiration = expire;
	Key key = new Key("test", "cache", id);
	Bin bin1 = new Bin("value", m);
	ae.getClient().put(policy, key, bin1);
}
 
Example #14
Source File: RedissonClient.java    From XRTB with Apache License 2.0 5 votes vote down vote up
/**
 * Add a list to the cach2k/Aerorpike
 * @param id String. The name of the value.
 * @param list List. The value to set, a list.
 */
public void addList(String id, List list) throws Exception {
	if (ae == null) {
		cacheDb.put(id, list);
		return;
	}
	
	Key key = new Key("test", "cache", id);
	Bin bin1 = new Bin("list", list);
	ae.getClient().put(null, key, bin1);
}
 
Example #15
Source File: Membership.java    From XRTB with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor for loading a membership list into Aerospike
 * @param name String. The symbol name.
 * @param fileName String. The filename with data.
 * @param client AerospikeClient. Pointer to your aerospike client.
 * @throws Exception on file and aerospike errors.
 */
public Membership(String name, String fileName, AerospikeClient client) throws Exception {
	this.name = name;
	this.client = client;
	readData(fileName);
	key = new Key("test", "database", "rtb4free");
	bin1 = new Bin(name, tree);
	client.put(null, key, bin1);
	tree = null;
}
 
Example #16
Source File: Membership.java    From XRTB with Apache License 2.0 5 votes vote down vote up
/**
 * Read only constructor (does not load the data) for use with Aerospike.
 * @param name String. The name of the symbol.
 * @param client AerospikeClient. The aerospike to use.
 */
public Membership(String name, AerospikeClient client) {
	this.name = name;
	this.client = client;
	key = new Key("test", "database", "rtb4free");
	bin1 = new Bin(name, tree);
	client.put(null, key, bin1);
	tree = null;
}
 
Example #17
Source File: CookieList.java    From XRTB with Apache License 2.0 5 votes vote down vote up
public static void main(String args[]) throws Exception {
	int i = 0;
	String aero = "localhost:3000";
	String setName = null;
	String mapName = null;
	String op = null;
	String name = null;
	String file = null;
	boolean range = false;

	AerospikeClient client = new AerospikeClient("localhost",3000);
	
	Key key = new Key("test", "database", "rtb4free");
	
	ArrayList<String> list = new ArrayList<String>();
	
	TreeSet set = new TreeSet();
	for (i=0; i < 100000; i++) {
		list.add(Integer.toString(i));
		set.add(Integer.toString(i));
	}
	Bin bin1 = new Bin("c1x-cookies", set);
	client.put(null, key, bin1);
	
	System.out.println("Done!");
	
	Record record = client.get(null, key);
	long time = System.currentTimeMillis();
	Set<String> receivedList = (Set<String>) record.getValue("c1x-cookies");
	receivedList.contains("99999");
	System.out.println(System.currentTimeMillis() - time);
	System.out.println("Received List = " + receivedList.size());
}
 
Example #18
Source File: AerospikeAbstractSink.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public void write(Record<byte[]> record) {
    KeyValue<K, V> keyValue = extractKeyValue(record);
    Key key = new Key(aerospikeSinkConfig.getKeyspace(), aerospikeSinkConfig.getKeySet(), keyValue.getKey().toString());
    Bin bin = new Bin(aerospikeSinkConfig.getColumnName(), Value.getAsBlob(keyValue.getValue()));
    AWriteListener listener = null;
    try {
        listener = queue.take();
    } catch (InterruptedException ex) {
        record.fail();
        return;
    }
    listener.setContext(record);
    client.put(eventLoop, listener, writePolicy, key, bin);
}
 
Example #19
Source File: WordCountOutput.java    From aerospike-hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public void writeAerospike(Text key,
                           IntWritable value,
                           AerospikeClient client,
                           WritePolicy writePolicy,
                           String namespace,
                           String setName) throws IOException {
    Key kk = new Key(namespace, setName, key.toString());
    Bin bin1 = new Bin("word", key.toString());
    Bin bin2 = new Bin("count", value.get());
    client.put(writePolicy, kk, bin1, bin2);
}
 
Example #20
Source File: SessionRollup.java    From aerospike-hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public void writeAerospike(Text sessid,
                           Session session,
                           AerospikeClient client,
                           WritePolicy writePolicy,
                           String namespace,
                           String setName) throws IOException {
    Key kk = new Key(namespace, setName, sessid.toString());
    Bin bin0 = new Bin("userid", session.userid);
    Bin bin1 = new Bin("start", session.start);
    Bin bin2 = new Bin("end", session.end);
    Bin bin3 = new Bin("nhits", session.nhits);
    client.put(writePolicy, kk, bin0, bin1, bin2, bin3);
}
 
Example #21
Source File: AerospikeTestUtils.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
public List<Bin> getBins()
{
  List<Bin> list = new ArrayList<Bin>();
  list.add(new Bin(ID, id));
  list.add(new Bin(VAL, value));
  return list;
}
 
Example #22
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 #23
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 #24
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 #25
Source File: AerospikePOJONonTransactionalPutOperator.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@Override
protected Key getUpdatedBins(Object tuple, List<Bin> list)
{
  if (null == keyGetter) {    // first tuple
    Class<?> tupleClass = tuple.getClass();
    keyGetter  = PojoUtils.createGetter(tupleClass, expressions.get(0), Key.class);
    binsGetter = PojoUtils.createGetter(tupleClass, expressions.get(1), List.class);
  }
  Key key = keyGetter.get(tuple);
  List<Bin> binList = binsGetter.get(tuple);
  if (!(null == binList || binList.isEmpty())) {
    list.addAll(binList);
  }
  return key;
}
 
Example #26
Source File: AerospikePOJOTransactionalPutOperator.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@Override
protected Key getUpdatedBins(Object tuple, List<Bin> list)
{
  if (null == keyGetter) {    // first tuple
    Class<?> tupleClass = tuple.getClass();
    keyGetter  = PojoUtils.createGetter(tupleClass, expressions.get(0), Key.class);
    binsGetter = PojoUtils.createGetter(tupleClass, expressions.get(1), List.class);
  }
  Key key = keyGetter.get(tuple);
  List<Bin> binList = binsGetter.get(tuple);
  if ( !(null == binList || binList.isEmpty()) ) {
    list.addAll(binList);
  }
  return key;
}
 
Example #27
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 #28
Source File: AerospikeTargetIT.java    From datacollector with Apache License 2.0 4 votes vote down vote up
private static void putRecord(String namespace, String set, String key, String bin, String value) {
  synchronized (aerospikeServer) {
    client.put(new WritePolicy(), new Key(namespace, set, key), new Bin(bin, value));
  }
}
 
Example #29
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 #30
Source File: SampleData.java    From aerospike-hadoop with Apache License 2.0 3 votes vote down vote up
public static void runSeqInt(String[] args, int argi) throws Exception {

        int offset = Integer.parseInt(args[argi++]);
        int nrecs = Integer.parseInt(args[argi++]);

        String ndxname = binName + "ndx";
        
        IndexTask task =
            client.createIndex(null, namespace, setName,
                               ndxname, binName, IndexType.NUMERIC);

        task.waitTillComplete();
        log.info("created secondary index on " + binName);

        for (long ll = offset; ll < offset + nrecs; ++ll) {

            String keystr = "key-" + ll;

            Key key = new Key(namespace, setName, keystr);
            Bin bin1 = new Bin(binName, ll);
            Bin bin2 = new Bin("bin2", "value2");

            client.put(writePolicy, key, bin1, bin2);
        }

        log.info("inserted " + nrecs + " records");
    }