# Read

Jump to the [Code block](#code-block) for a combined complete example.

## Setup

The following examples use the setup and record below to illustrate single record data retrieval from an Aerospike database.

```java
import com.aerospike.client.AerospikeClient;

import com.aerospike.client.policy.Policy;

import com.aerospike.client.Key;

import com.aerospike.client.Record;

// Establishes a connection to the server

AerospikeClient client = new AerospikeClient("127.0.0.1", 3000);

// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001

Key key = new Key("sandbox", "ufodata", 5001);
```

The record structure:

```plaintext
+------+----------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+

| 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](https://aerospike.com/docs/database/learn/policies), we can set them on a per command basis.

The following example creates a new read policy and sets the [socket timeout](https://aerospike.com/docs/database/learn/policies#socket-timeout) for the command.

```java
// Create a new read policy

Policy policy = new Policy();

policy.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.

```java
// Returns true if exists, false if not

boolean exists = client.exists(policy, key);

// Do something

System.out.format("Exists: %s", exists);

// Close the connection to the server

client.close();
```

### Record metadata only

Read record metadata (generation and expiration information) without reading the bins for a specified key.

```java
// Get record metadata

Record record = client.getHeader(policy, key);

// Do something

System.out.format("Record: %s", record);

// Close the connection to the server

client.close();
```

### Whole record

Read the record metadata and all bins for a specified key.

```java
// Get whole record

Record record = client.get(policy, key);

// Do something

System.out.format("Record: %s", record.bins);

// Close the connection to the server

client.close();
```

### Specific bins

Read the record metadata and the `report` and `location` bins from the record.

```java
// Get bins 'report' and 'location'

Record record = client.get(policy, key, "report", "location");

// Do something

System.out.format("Record: %s", record.bins);

// Close the connection to the server

client.close();
```

## Code block

Expand this section for a single code block to read a record

```java
import com.aerospike.client.AerospikeClient;

import com.aerospike.client.policy.Policy;

import com.aerospike.client.Key;

import com.aerospike.client.Record;

// Establishes a connection to the server

AerospikeClient client = new AerospikeClient("127.0.0.1", 3000);

// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001

Key key = new Key("sandbox", "ufodata", 5001);

// Create a new read policy

Policy policy = new Policy();

policy.socketTimeout = 300;

// Get whole record

Record record = client.get(policy, key);

// Do something

System.out.format("Record: %s", record.bins);

// Close the connection to the server

client.close();
```