示例#1
0
  public void runQuery() {

    String binName = "bin1";

    Statement stmt = new Statement();
    stmt.setNamespace("test");
    stmt.setSetName("demo");
    stmt.setBinNames(binName);
    // stmt.setFilters(Filter.range(binName, begin, end));

    final AtomicInteger count = new AtomicInteger();

    aclient.query(
        null,
        new RecordSequenceListener() {
          public void onRecord(Key key, Record record) throws AerospikeException {

            System.out.printf(
                "Record found: ns=%s set=%s bin=%s digest=%s value=%s\n",
                key.namespace, key.setName, binName, Buffer.bytesToHexString(key.digest), record);

            count.incrementAndGet();
          }

          public void onSuccess() {}

          public void onFailure(AerospikeException e) {

            System.out.printf("Query failed: " + Util.getErrorMessage(e));
            // notifyComplete();
          }
        },
        stmt);
  }
    @Override
    public Statement queryToRetrieveData() {

      Statement stmnt = new Statement();
      stmnt.setNamespace(NAMESPACE);
      stmnt.setSetName(SET_NAME);

      return stmnt;
    }
  private List<Person> getPersons(Filter f) {
    Statement statement = new Statement();
    statement.setNamespace("test");
    statement.setSetName("demo");
    statement.setFilters(f);

    RecordSet rs = client.query(null, statement);
    if (rs == null) {
      throw new IllegalArgumentException("Wrong f:" + f);
    }
    List<Person> persons = new ArrayList<Person>();
    try {
      while (rs.next()) {
        Person person = getPerson(rs);
        persons.add(person);
      }
    } finally {
      rs.close();
    }

    return persons;
  }