Read
For the complete documentation index see: llms.txt
All documentation pages available in markdown.
Jump to the Code block for a combined complete example.
Description
Demonstrates single record reads in an Aerospike database.
Setup
Boilerplate
The following examples leverage the following boilerplate to initialize the Aerospike module and connect to a server before reading a record.
const Aerospike = await import("aerospike");
// Set hosts to your server's address and portconst config = { hosts: "YOUR_HOST_ADDRESS:YOUR_PORT" };
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001const key = new Aerospike.Key("sandbox", "ufodata", 5001);
// Establishes a connection to the serverconst client = await Aerospike.connect(config);The record structure:
+------+----------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+| PK | occurred | reported | posted | report | location |+------+----------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+| 5001 | 20220531 | 20220601 | 20220601 | MAP('{"shape":["circle", "flash", "disc"], "summary":"Large flying disc flashed in the sky above the student union. Craziest thing I've ever seen!", "city":"Ann Arbor", "state":"Michigan", "duration":"5 minutes"}') | GeoJSON('{"type":"Point","coordinates":[42.2808,83.743]}') |+------+----------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+Policies
Instead of using the default read policies, we can set them on a per command basis.
The following example creates a new read policy and sets the socket timeout for the command.
// Create a new read policyconst readPolicy = new Aerospike.ReadPolicy({ socketTimeout: 300,});Read a record
Record exists
Checking record existence is faster than getting the record because it only needs to look at the primary index.
// Returns true if exists, false if notconst exists = await client.exists(key, readPolicy);Record metadata only
Read record metadata (generation and expiration information) without reading the bins for a specified key.
The returned record’s gen property holds the generation, and its ttl property holds the remaining time to live in seconds.
If the record doesn’t exist, both properties are null.
// Returns a record with metadata and no binsconst record = await client.existsWithMetadata(key, readPolicy);console.info("Generation: %s, TTL: %s", record.gen, record.ttl);Whole record
Read the record metadata and all bins for a specified key.
// Get whole recordconst record = await client.get(key, readPolicy);Specific bins
Read the record metadata and the report and location bins from the record.
// Get bins 'report' and 'location'const record = await client.select(key, ["report", "location"], readPolicy);Code block
Expand this section for a single code block to read a record
const Aerospike = await import("aerospike");
// Set hosts to your server's address and portconst config = { hosts: "YOUR_HOST_ADDRESS:YOUR_PORT" };
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001const key = new Aerospike.Key("sandbox", "ufodata", 5001);
// Establishes a connection to the serverconst client = await Aerospike.connect(config);
// Create a new read policyconst readPolicy = new Aerospike.ReadPolicy({ socketTimeout: 300,});
// Get bins 'report' and 'location'const record = await client.select(key, ["report", "location"], readPolicy);
console.info("Record: %o", record);
// Close the connection to the serverawait client.close();