Exemplo n.º 1
0
  /** Drop a bin from a record. */
  @Override
  public void runExample(AerospikeClient client, Parameters params) throws Exception {
    if (params.singleBin) {
      console.info("Delete bin is not applicable to single bin servers.");
      return;
    }

    console.info("Write multi-bin record.");
    Key key = new Key(params.namespace, params.set, "delbinkey");
    String binName1 = params.getBinName("bin1");
    String binName2 = params.getBinName("bin2");
    Bin bin1 = new Bin(binName1, "value1");
    Bin bin2 = new Bin(binName2, "value2");
    client.put(params.writePolicy, key, bin1, bin2);

    console.info("Delete one bin in the record.");
    bin1 = Bin.asNull(binName1); // Set bin value to null to drop bin.
    client.put(params.writePolicy, key, bin1);

    console.info("Read record.");
    Record record = client.get(params.policy, key, bin1.name, bin2.name, "bin3");

    if (record == null) {
      throw new Exception(
          String.format(
              "Failed to get: namespace=%s set=%s key=%s",
              key.namespace, key.setName, key.userKey));
    }

    for (Map.Entry<String, Object> entry : record.bins.entrySet()) {
      console.info(
          "Received: namespace=%s set=%s key=%s bin=%s value=%s",
          key.namespace, key.setName, key.userKey, entry.getKey(), entry.getValue());
    }

    boolean valid = true;

    if (record.getValue("bin1") != null) {
      console.error("bin1 still exists.");
      valid = false;
    }

    Object v2 = record.getValue("bin2");

    if (v2 == null || !v2.equals("value2")) {
      console.error("bin2 value mismatch.");
      valid = false;
    }

    if (valid) {
      console.info("Bin delete successful");
    }
  }
  private void writeUsingUdfAsync(final AsyncClient client, final Parameters params) {
    final Key key = new Key(params.namespace, params.set, "audfkey1");
    final Bin bin = new Bin(params.getBinName("audfbin1"), "string value");

    console.info(
        "Write with udf: namespace=%s set=%s key=%s value=%s",
        key.namespace, key.setName, key.userKey, bin.value);

    client.execute(
        params.writePolicy,
        new ExecuteListener() {

          public void onSuccess(final Key key, final Object obj) {
            try {
              // Write succeeded.  Now call read using udf.
              console.info(
                  "Get: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey);

              client.execute(
                  params.writePolicy,
                  new ExecuteListener() {

                    public void onSuccess(final Key key, final Object received) {
                      Object expected = bin.value.getObject();

                      if (received != null && received.equals(expected)) {
                        console.info(
                            "Data matched: namespace=%s set=%s key=%s bin=%s value=%s",
                            key.namespace, key.setName, key.userKey, bin.name, received);
                      } else {
                        console.error(
                            "Data mismatch: Expected %s. Received %s.", expected, received);
                      }
                      notifyCompleted();
                    }

                    public void onFailure(AerospikeException e) {
                      console.error(
                          "Failed to get: namespace=%s set=%s key=%s exception=%s",
                          key.namespace, key.setName, key.userKey, e.getMessage());
                      notifyCompleted();
                    }
                  },
                  key,
                  "record_example",
                  "readBin",
                  Value.get(bin.name));
            } catch (Exception e) {
              console.error(
                  "Failed to read: namespace=%s set=%s key=%s exception=%s",
                  key.namespace, key.setName, key.userKey, e.getMessage());
            }
          }

          public void onFailure(AerospikeException e) {
            console.error(
                "Failed to write: namespace=%s set=%s key=%s exception=%s",
                key.namespace, key.setName, key.userKey, e.getMessage());
            notifyCompleted();
          }
        },
        key,
        "record_example",
        "writeBin",
        Value.get(bin.name),
        bin.value);
  }