Java连接Aerospike(4.0.3)增删改查

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();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值