使用 aerospikeTemplate 将数据插入 aerospike java
Inserting data into aerospike using aerospikeTemplate java
我正在尝试将数据插入 aerospike。为了对 AerospikeClient 做同样的事情,我写道:
Key key = new Key("test", "myset", "mykey");
Bin bin = new Bin("shahjahan", "k");
aerospikeClient.put(new WritePolicy(), key, bin);
现在,我想使用 AerospikeTemplate 做同样的事情。但是 AerospikeTemplate 中的插入方法需要对象作为参数,而不是键和容器。
@Override
public <T> T insert(T objectToInsert, WritePolicy policy) {
Assert.notNull(objectToInsert, "Object to insert must not be null!");
try {
AerospikeData data = AerospikeData.forWrite(this.namespace);
converter.write(objectToInsert, data);
Key key = data.getKey();
Bin[] bins = data.getBinsAsArray();
client.put(policy == null ? this.insertPolicy : policy, key, bins);
}
catch (AerospikeException o_O) {
DataAccessException translatedException = exceptionTranslator
.translateExceptionIfPossible(o_O);
throw translatedException == null ? o_O : translatedException;
}
return null;
}
我想知道如何通过键和值来插入数据。
您的对象需要有一个 @Id
注释来指定记录的键。所有其他字段将存储为 bin。这是一个例子:
public class Product {
@Id
private Integer id;
private String productId;
private String description;
private String imageUrl;
private double price;
...
}
然后你只需在对象上调用 save()
:
productRepository.save(product);
我正在尝试将数据插入 aerospike。为了对 AerospikeClient 做同样的事情,我写道:
Key key = new Key("test", "myset", "mykey");
Bin bin = new Bin("shahjahan", "k");
aerospikeClient.put(new WritePolicy(), key, bin);
现在,我想使用 AerospikeTemplate 做同样的事情。但是 AerospikeTemplate 中的插入方法需要对象作为参数,而不是键和容器。
@Override
public <T> T insert(T objectToInsert, WritePolicy policy) {
Assert.notNull(objectToInsert, "Object to insert must not be null!");
try {
AerospikeData data = AerospikeData.forWrite(this.namespace);
converter.write(objectToInsert, data);
Key key = data.getKey();
Bin[] bins = data.getBinsAsArray();
client.put(policy == null ? this.insertPolicy : policy, key, bins);
}
catch (AerospikeException o_O) {
DataAccessException translatedException = exceptionTranslator
.translateExceptionIfPossible(o_O);
throw translatedException == null ? o_O : translatedException;
}
return null;
}
我想知道如何通过键和值来插入数据。
您的对象需要有一个 @Id
注释来指定记录的键。所有其他字段将存储为 bin。这是一个例子:
public class Product {
@Id
private Integer id;
private String productId;
private String description;
private String imageUrl;
private double price;
...
}
然后你只需在对象上调用 save()
:
productRepository.save(product);