Primary index
For the complete documentation index see: llms.txt
All documentation pages available in markdown.
Jump to the Code block for a combined complete example.
Basic PI queries have the following features:
- Filter records by set name.
- Filter records by filter expressions.
- Limit the number of records returned, useful for pagination.
- Return only record digests and metadata (generation and TTL).
- Return specified bins.
Setup
The following examples use the setup and record structure below to illustrate primary index queries in an Aerospike database.
import com.aerospike.client.AerospikeClient;import com.aerospike.client.Key;import com.aerospike.client.Operation;import com.aerospike.client.Record;import com.aerospike.client.Value;import com.aerospike.client.cdt.MapOperation;import com.aerospike.client.cdt.MapReturnType;import com.aerospike.client.exp.Exp;import com.aerospike.client.exp.ListExp;import com.aerospike.client.exp.MapExp;import com.aerospike.client.policy.QueryPolicy;import com.aerospike.client.query.PartitionFilter;import com.aerospike.client.query.RecordSet;import com.aerospike.client.query.Statement;
import java.util.Arrays;
// Establishes a connection to the serverAerospikeClient client = new AerospikeClient("127.0.0.1", 3000);The record structure:
Occurred: IntegerReported: IntegerPosted: IntegerReport: Map{ shape: List, summary: String, city: String, state: String, duration: String}Location: GeoJSONPolicies
See Basic Queries for query policy information.
Query a set
The following example queries the sandbox namespace and ufodata set name, while limiting the record set to only 20 records.
// Create statementStatement stmt = new Statement();
// Set namespace and set namestmt.setNamespace("sandbox");stmt.setSetName("ufodata");
// Set max records to returnstmt.setMaxRecords(20);
// Execute the queryRecordSet recordSet = client.query(null, stmt);
// Get the resultstry{ while(recordSet.next()){ Key key = recordSet.getKey(); Record record = recordSet.getRecord(); // Do something System.out.format("Key: %s | Record: %s\\n", key.userKey, record.bins); }}finally{ recordSet.close();}
// Close the connection to the serverclient.close();Query with projection operations
Use projection operations to project read operations or read expressions in the result, similar to batched operations. The following example returns only city and state from the report map for each matching record:
Basic read operations (Operation.get(String), Operation.get(), Operation.getHeader()) are supported for query projection on server versions prior to 8.1.2. Extended read operations, such as the CDT MapOperation.getByKeyList() used below, require server version 8.1.2 or later.
// Create statementStatement stmt = new Statement();stmt.setNamespace("sandbox");stmt.setSetName("ufodata");stmt.setMaxRecords(20);
// Project map keys from report binstmt.setOperations(Operation.array( MapOperation.getByKeyList("report", Arrays.asList(Value.get("city"), Value.get("state")), MapReturnType.VALUE)));
RecordSet recordSet = client.query(null, stmt);try { while (recordSet.next()) { Record record = recordSet.getRecord(); // record.bins contains the projected map values }} finally { recordSet.close();}Query with a metadata filter
The following example queries the same namespace and set as the example above, but also adds a metadata Filter Expression that will only return records that are greater than 16 KiB.
// Create query policyQueryPolicy queryPolicy = new QueryPolicy();queryPolicy.filterExp = Exp.build( Exp.gt(Exp.deviceSize(), Exp.val(1024 * 16)));
// Create statementStatement stmt = new Statement();
// Set namespace and set namestmt.setNamespace("sandbox");stmt.setSetName("ufodata");
// Execute the queryRecordSet recordSet = client.query(queryPolicy, stmt);
// Get the resultstry{ while(recordSet.next()){ Key key = recordSet.getKey(); Record record = recordSet.getRecord(); // Do something System.out.format("Key: %s | Record: %s\\n", key.userKey, record.bins); }}finally{ recordSet.close();}
// Close the connection to the serverclient.close();Query with a data filter
The following example queries the same namespace and set as the example above, but also adds a data Filter Expression that
will only return records where the occurred bin value is in the inclusive range 20200101 to 20211231.
Take a look at secondary index queries to see how this same query can be run more efficiently with an index.
// Create query policyQueryPolicy queryPolicy = new QueryPolicy();queryPolicy.filterExp = Exp.build( Exp.let( Exp.def("bin", Exp.intBin("occurred")), Exp.and( Exp.ge(Exp.var("bin"), Exp.val(20210101)), Exp.le(Exp.var("bin"), Exp.val(20211231)) ) ));
// Create statementStatement stmt = new Statement();
// Set namespace and set namestmt.setNamespace("sandbox");stmt.setSetName("ufodata");
// Execute the queryRecordSet recordSet = client.query(queryPolicy, stmt);
// Get the resultstry{ while(recordSet.next()){ Key key = recordSet.getKey(); Record record = recordSet.getRecord(); // Do something System.out.format("Key: %s | Record: %s\\n", key.userKey, record.bins); }}finally{ recordSet.close();}
// Close the connection to the serverclient.close();Pagination
Pagination uses a combination of a partition filter and a defined maximum records to return query results. The partition filter maintains a cursor identifying the end of the current page and the beginning of the next. Moving to the next page of results is as simple as executing the query again, with the previously defined partition filter.
The following example executes a query with an Expression Filter identifying records with more than 3 shape items in the report map,
returning 10 records per page. The partition filter is set to query all 4096 partitions in the database.
// Create query policyQueryPolicy queryPolicy = new QueryPolicy();queryPolicy.filterExp = Exp.build( Exp.gt( ListExp.size( MapExp.getByKey(MapReturnType.VALUE, Exp.Type.LIST, Exp.val("shape"), Exp.mapBin("report")) ), Exp.val(3) ));
// Create statementStatement stmt = new Statement();
// Set namespace and set namestmt.setNamespace("sandbox");stmt.setSetName("ufodata");
// Set max records to returnstmt.setMaxRecords(10);
// Create the partition filterPartitionFilter partitionFilter = PartitionFilter.all();
// Execute the queryRecordSet recordSet = client.queryPartitions(queryPolicy, stmt, partitionFilter);
// Get the resultstry{ int page = 0; while(!partitionFilter.isDone()){ int count = 0; while(recordSet.next()){ Key key = recordSet.getKey(); Record record = recordSet.getRecord(); // Do something System.out.format("Key: %s | Record: %s\\n", key.userKey, record.bins); count++; } page++; System.out.format("\\nPage %s | %s records\\n\\n", page, count); // Execute the query reusing the partitionFilter until all records returned recordSet = client.queryPartitions(queryPolicy, stmt, partitionFilter); }}finally{ recordSet.close();}
// Close the connection to the serverclient.close();Code block
Expand this section for a single code block to execute a basic PI query
import com.aerospike.client.AerospikeClient;import com.aerospike.client.Key;import com.aerospike.client.Record;import com.aerospike.client.cdt.MapReturnType;import com.aerospike.client.exp.Exp;import com.aerospike.client.exp.ListExp;import com.aerospike.client.exp.MapExp;import com.aerospike.client.policy.QueryPolicy;import com.aerospike.client.query.PartitionFilter;import com.aerospike.client.query.RecordSet;import com.aerospike.client.query.Statement;
// Establishes a connection to the serverAerospikeClient client = new AerospikeClient("127.0.0.1", 3000);
// Create query policyQueryPolicy queryPolicy = new QueryPolicy();queryPolicy.filterExp = Exp.build( Exp.let( Exp.def("bin", Exp.intBin("occurred")), Exp.and( Exp.ge(Exp.var("bin"), Exp.val(20210101)), Exp.le(Exp.var("bin"), Exp.val(20211231)) ) ));
// Create statementStatement stmt = new Statement();
// Set namespace and set namestmt.setNamespace("sandbox");stmt.setSetName("ufodata");
// Execute the queryRecordSet recordSet = client.query(queryPolicy, stmt);
// Get the resultstry{ while(recordSet.next()){ Key key = recordSet.getKey(); Record record = recordSet.getRecord(); // Do something System.out.format("Key: %s | Record: %s\\n", key.userKey, record.bins); }}finally{ recordSet.close();}
// Close the connection to the serverclient.close();