Beispiel #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");
    }
  }
  public void createTweet() throws AerospikeException, InterruptedException {

    console.printf("\n********** Create Tweet **********\n");

    Record userRecord = null;
    Key userKey = null;
    Key tweetKey = null;

    // Get username
    String username;
    console.printf("\nEnter username:"******"test", "users", username);
      userRecord = client.get(null, userKey);
      if (userRecord != null) {
        int nextTweetCount = Integer.parseInt(userRecord.getValue("tweetcount").toString()) + 1;

        // Get tweet
        String tweet;
        console.printf("Enter tweet for " + username + ":");
        tweet = console.readLine();

        // Write record
        WritePolicy wPolicy = new WritePolicy();
        wPolicy.recordExistsAction = RecordExistsAction.UPDATE;

        // Create timestamp to store along with the tweet so we can
        // query, index and report on it
        long ts = getTimeStamp();

        tweetKey = new Key("test", "tweets", username + ":" + nextTweetCount);
        Bin bin1 = new Bin("tweet", tweet);
        Bin bin2 = new Bin("ts", ts);
        Bin bin3 = new Bin("username", username);

        client.put(wPolicy, tweetKey, bin1, bin2, bin3);
        console.printf("\nINFO: Tweet record created!\n");

        // Update tweet count and last tweet'd timestamp in the user
        // record
        updateUser(client, userKey, wPolicy, ts, nextTweetCount);
      } else {
        console.printf("ERROR: User record not found!\n");
      }
    }
  } // createTweet
    @Override
    public TestEvent getTuple(Record record) {

      return new TestEvent(record.getInt("ID"));
    }