com.aerospike.examples.pk.StorePrimaryKey.java Source code

Java tutorial

Introduction

Here is the source code for com.aerospike.examples.pk.StorePrimaryKey.java

Source

/* 
 * Copyright 2012-2015 Aerospike, Inc.
 *
 * Portions may be licensed to Aerospike, Inc. under one or more contributor
 * license agreements.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.aerospike.examples.pk;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import java.util.Random;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.PosixParser;
import org.apache.log4j.Logger;

import com.aerospike.client.AerospikeClient;
import com.aerospike.client.AerospikeException;
import com.aerospike.client.Bin;
import com.aerospike.client.Key;
import com.aerospike.client.Record;
import com.aerospike.client.ScanCallback;
import com.aerospike.client.policy.Policy;
import com.aerospike.client.policy.WritePolicy;

/**
@author Peter Milne
*/
public class StorePrimaryKey {
    private static final int MAX_RECORDS = 500;
    private AerospikeClient client;
    private String seedHost;
    private int port;
    private String namespace;
    private String set;

    private static Logger log = Logger.getLogger(StorePrimaryKey.class);

    public StorePrimaryKey(String host, int port, String namespace, String set) throws AerospikeException {
        this.client = new AerospikeClient(host, port);
        this.seedHost = host;
        this.port = port;
        this.namespace = namespace;
        this.set = set;
    }

    public StorePrimaryKey(AerospikeClient client, String namespace, String set) throws AerospikeException {
        this.client = client;
        this.namespace = namespace;
        this.set = set;
    }

    public static void main(String[] args) throws AerospikeException {
        try {
            Options options = new Options();
            options.addOption("h", "host", true, "Server hostname (default: 172.28.128.6)");
            options.addOption("p", "port", true, "Server port (default: 3000)");
            options.addOption("n", "namespace", true, "Namespace (default: test)");
            options.addOption("s", "set", true, "Set (default: demo)");
            options.addOption("u", "usage", false, "Print usage.");

            CommandLineParser parser = new PosixParser();
            CommandLine cl = parser.parse(options, args, false);

            String host = cl.getOptionValue("h", "172.28.128.6");
            String portString = cl.getOptionValue("p", "3000");
            int port = Integer.parseInt(portString);
            String namespace = cl.getOptionValue("n", "test");
            String set = cl.getOptionValue("s", "demo");
            log.debug("Host: " + host);
            log.debug("Port: " + port);
            log.debug("Namespace: " + namespace);
            log.debug("Set: " + set);

            @SuppressWarnings("unchecked")
            List<String> cmds = cl.getArgList();
            if (cmds.size() == 0 && cl.hasOption("u")) {
                logUsage(options);
                return;
            }

            StorePrimaryKey as = new StorePrimaryKey(host, port, namespace, set);

            as.work();

        } catch (Exception e) {
            log.error("Critical error", e);
        }
    }

    /**
     * Write usage to console.
     */
    private static void logUsage(Options options) {
        HelpFormatter formatter = new HelpFormatter();
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        String syntax = StorePrimaryKey.class.getName() + " [<options>]";
        formatter.printHelp(pw, 100, syntax, "options:", options, 0, 2, null);
        log.info(sw.toString());
    }

    public void work() throws Exception {
        /*
         * sset up policies
         */
        this.client.writePolicyDefault.sendKey = true;
        this.client.readPolicyDefault.sendKey = true;
        this.client.scanPolicyDefault.sendKey = true;
        this.client.scanPolicyDefault.includeBinData = false;
        /*
         * write some records
         */
        Random rand = new Random();
        for (int x = 0; x < MAX_RECORDS; x++) {
            Key key = new Key(this.namespace, this.set, "a-meeaningless-key-" + x);
            Bin bin = new Bin("age", (rand.nextInt(15) + 25));
            this.client.put(null, key, bin);
        }
        /*
         * scan them and see the keys
         */
        this.client.scanAll(null, namespace, set, new ScanCallback() {

            @Override
            public void scanCallback(Key key, Record record) throws AerospikeException {
                log.info(key.toString());
            }
        }, null);
    }

}