Aerospike增删改查操作
Aerospike底层是基于Linux,所以安装要在Linux系统上安装,故此次我并没有用虚拟机而是采用的连接公司的远程服务器进行测试
以下仅演示部分Api的使用,具体详细的去底层包中查看
依赖
<dependencies>
<dependency>
<groupId>com.aerospike</groupId>
<artifactId>aerospike-client</artifactId>
<version>4.0.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
具体操作
public class AS {
/**
* 新增一条record
*/
@Test
public void putRecord() {
// Establishes a connection to the server
AerospikeClient client = new AerospikeClient("服务器ip", 服务端口);
// Create a new write policy
WritePolicy writePolicy = new WritePolicy();
writePolicy.sendKey = true;
// Create the record key
Key key = new Key("ns1", "rxy1", "k1");
// Create a list of shapes to add to the report map
ArrayList<String> shape = new ArrayList<String>();
shape.add("circle");
shape.add("flash");
shape.add("disc");
// Create the report map
Map reportMap = new HashMap<String, Object>();
reportMap.put("city", "Ann Arbor");
reportMap.put("state", "Michigan");
reportMap.put("shape", shape);
reportMap.put("duration", "5 minutes");
reportMap.put("summary", "Large flying disc flashed in the sky above the student union. Craziest thing I've ever seen!");
// Format coordinates as a GeoJSON string
String geoLoc = "{\"type\":\"Point\", \"coordinates\":[42.2808,83.7430]}";
// Create the bins as Bin("binName", value)
Bin occurred = new Bin("occurred", 20220531);
Bin reported = new Bin("reported", 20220601);
Bin posted = new Bin("posted", 20220601);
// reportMap defined in the section above
Bin report = new Bin("report", reportMap);
// geoLoc defined in the section above
Bin location = new Bin("location", Value.getAsGeoJSON(geoLoc));
// Write the record to Aerospike
client.put(writePolicy, key, occurred, reported, posted, report, location);
// Close the connection to the server
client.close();
}
/**
* 读出一条记录
*/
@Test
public void readRecord(){
AerospikeClient client = new AerospikeClient("服务器ip", 服务端口);
Key key = new Key("ns1", "rxy", "k1");
Policy policy = new Policy();
policy.socketTimeout = 1000;
Record record = client.get(policy, key);
System.out.println(record.bins);
System.out.format("Record: %s",record.bins);
client.close();
}
/**
* 扫描ns1中set为rxy的里面所有的record(根据key)
*/
@Test
public void readAllRecord(){
AerospikeClient client = new AerospikeClient("服务器ip", 服务端口);
ScanPolicy scanPolicy = new ScanPolicy();
client.scanAll(scanPolicy, "ns1", "rxy", new ScanCallback() {
@Override
public void scanCallback(Key key, Record record) throws AerospikeException {
System.out.println(key);
System.out.println(record.bins);
}
});
}
/**
* 更新一条记录
*/
@Test
public void updateRecord(){
// Establishes a connection to the server
AerospikeClient client = new AerospikeClient("服务器ip", 服务端口);
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001
Key key = new Key("ns1", "rxy", "k1");
// Can use a previously defined write policy or create a new one
WritePolicy updatePolicy = new WritePolicy();
updatePolicy.recordExistsAction = RecordExistsAction.UPDATE_ONLY;
// Create bin with new value
Bin newPosted = new Bin("posted", 1112431);
// Update record with new bin value
client.put(updatePolicy, key, newPosted);
// Close the connection to the server
client.close();
}
/**
* 删除一条记录
* @throws AerospikeException
*/
@Test
public void deleteRecord() {
AerospikeClient client = new AerospikeClient("服务器ip", 服务端口);
Key key = new Key("ns1", "rxy", "k1");
// WritePolicy deletePolicy = new WritePolicy();
// deletePolicy.durableDelete = true;
// client.
client.delete(null, key);
client.close();
}
}