---
title: "Simple put-get"
description: "Write a record with a put call and read it back with a get call in the Java and Python Developer SDKs or the Rust, C#, Go, Node.js, C, Java, or Python client."
---

# Simple put-get

> For the complete documentation index see: [llms.txt](https://aerospike.com/docs/llms.txt)
> 
> All documentation pages available in markdown.

In this tutorial, you use the two most basic Aerospike calls: `put`, which writes a record, and `get`, which reads one. Choose your client in any tab group. The page remembers your choice.

::: note
**A note on terminology:**

-   An **operation** is a discrete action on the contents of a [bin](https://aerospike.com/docs/database/learn/architecture/data-storage/data-model). Create, read, update, and delete actions using the `operate` API method are operations.
    
-   A **command** consists of one or more operations and affects one or more [records](https://aerospike.com/docs/database/learn/architecture/data-storage/data-model).
    
-   A **transaction** is made up of one or more commands, which occur in a guaranteed order and must all complete for the transaction to be successful.
:::

The **Java SDK** and **Python SDK** tabs use the [Aerospike Developer SDKs](https://aerospike.com/docs/develop/client/sdk). The **Java** and **Python** tabs use the legacy clients. In the SDKs, the write is an `upsert` and the read is a `query` by key.

## Prerequisites

-   An Aerospike Database running on your computer and listening on port 3000, with a namespace named `test`. The Java SDK and Python SDK tabs require Aerospike Database 8.2.0 or later. For the other clients, see the [Client Compatibility Matrix](https://aerospike.com/docs/develop/client-matrix). To set up a database, see the [Quickstart](https://aerospike.com/docs/database/quick-start).
-   Your client installed. To install it, follow the [Install the client](https://aerospike.com/docs/develop/tutorials/intro/hello-world/#install-the-client) step of the Hello, World! tutorial.

::: caution
The programs in this tutorial write records to the `test` namespace and use no authentication or TLS. Run them against a local development database that holds no data you need.
:::

## Import the client modules

-   [Java SDK](#tab-panel-6963)
-   [Python SDK](#tab-panel-6964)
-   [Rust](#tab-panel-6965)
-   [C#](#tab-panel-6966)
-   [Go](#tab-panel-6967)
-   [Node.js](#tab-panel-6968)
-   [C](#tab-panel-6969)
-   [Java](#tab-panel-6970)
-   [Python](#tab-panel-6971)

```java
import com.aerospike.client.sdk.Cluster;

import com.aerospike.client.sdk.ClusterDefinition;

import com.aerospike.client.sdk.DataSet;

import com.aerospike.client.sdk.Key;

import com.aerospike.client.sdk.Record;

import com.aerospike.client.sdk.Session;

import com.aerospike.client.sdk.policy.Behavior;
```

```python
from aerospike_sdk import Behavior, DataSet

from aerospike_sdk.sync import ClusterDefinition
```

```rust
use aerospike::{as_bin, as_key, Bins, Client, ClientPolicy, ReadPolicy, WritePolicy};
```

```csharp
using System;

using Aerospike.Client;
```

```go
import (

  "fmt"

  "log"

  "github.com/aerospike/aerospike-client-go/v8"

)
```

```js
import Aerospike from 'aerospike';
```

```c
#include <stdio.h>

#include <aerospike/aerospike.h>

#include <aerospike/aerospike_key.h>

#include <aerospike/as_error.h>

#include <aerospike/as_record.h>
```

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

import com.aerospike.client.Bin;

import com.aerospike.client.Key;

import com.aerospike.client.Record;
```

```python
import aerospike
```

## Connect to the database

Connect to the database on port 3000 of your computer. This is the default port. If your database runs on another host or port, change the address.

The client connects to the seed node you specify, which is the first node it contacts, and then discovers the other nodes in the cluster.

-   [Java SDK](#tab-panel-6972)
-   [Python SDK](#tab-panel-6973)
-   [Rust](#tab-panel-6974)
-   [C#](#tab-panel-6975)
-   [Go](#tab-panel-6976)
-   [Node.js](#tab-panel-6977)
-   [C](#tab-panel-6978)
-   [Java](#tab-panel-6979)
-   [Python](#tab-panel-6980)

```java
Cluster cluster = new ClusterDefinition("127.0.0.1", 3000).connect();

Session session = cluster.createSession(Behavior.DEFAULT);
```

The session applies a `Behavior`, which sets the timeouts and retries for each command. `Behavior.DEFAULT` is a reasonable starting point.

```python
cluster = ClusterDefinition("127.0.0.1", 3000).connect()

session = cluster.create_session(Behavior.DEFAULT)
```

The session applies a `Behavior`, which sets the timeouts and retries for each command. `Behavior.DEFAULT` is a reasonable starting point.

```rust
let client = Client::new(&ClientPolicy::default(), &"127.0.0.1:3000").await?;
```

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

```go
client, err := aerospike.NewClient("127.0.0.1", 3000)

if err != nil {

  log.Fatal(err)

}
```

```js
const client = await Aerospike.connect({ hosts: '127.0.0.1:3000' });
```

```c
as_config config;

as_config_init(&config);

as_config_add_host(&config, "127.0.0.1", 3000);

aerospike as;

aerospike_init(&as, &config);

as_error err;

if (aerospike_connect(&as, &err) != AEROSPIKE_OK) {

    fprintf(stderr, "Error connecting: %s\n", err.message);

    return 1;

}
```

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

```python
config = {"hosts": [("127.0.0.1", 3000)]}

client = aerospike.client(config).connect()
```

## Define a record key

A record key has three parts: a namespace, an optional set, and a user key. Together they identify a record, so you can read or write the record with its key. By default, the database doesn’t store the user key. It stores a 20-byte digest that the client computes from the key. For details, see the [Data model](https://aerospike.com/docs/database/learn/architecture/data-storage/data-model).

-   [Java SDK](#tab-panel-6981)
-   [Python SDK](#tab-panel-6982)
-   [Rust](#tab-panel-6983)
-   [C#](#tab-panel-6984)
-   [Go](#tab-panel-6985)
-   [Node.js](#tab-panel-6986)
-   [C](#tab-panel-6987)
-   [Java](#tab-panel-6988)
-   [Python](#tab-panel-6989)

```java
Key key = DataSet.of("test", "demo").id("putgetkey");
```

```python
key = DataSet.of("test", "demo").id("putgetkey")
```

```rust
let key = as_key!("test", "demo", "putgetkey");
```

```csharp
Key key = new Key("test", "demo", "putgetkey");
```

```go
key, err := aerospike.NewKey("test", "demo", "putgetkey")

if err != nil {

  log.Fatal(err)

}
```

```js
const key = new Aerospike.Key('test', 'demo', 'putgetkey');
```

```c
as_key key;

as_key_init_str(&key, "test", "demo", "putgetkey");
```

```java
Key key = new Key("test", "demo", "putgetkey");
```

```python
key = ("test", "demo", "putgetkey")
```

## Write a record

Aerospike has no schema, so you can write a record without any setup. This step writes the bins `name` and `age` to the record that the key identifies.

-   [Java SDK](#tab-panel-6990)
-   [Python SDK](#tab-panel-6991)
-   [Rust](#tab-panel-6992)
-   [C#](#tab-panel-6993)
-   [Go](#tab-panel-6994)
-   [Node.js](#tab-panel-6995)
-   [C](#tab-panel-6996)
-   [Java](#tab-panel-6997)
-   [Python](#tab-panel-6998)

```java
session.upsert(key)

    .bin("name").setTo("John Doe")

    .bin("age").setTo(32)

    .execute();
```

An `upsert` creates the record, or replaces the listed bins if the record exists.

```python
session.upsert(key).put({"name": "John Doe", "age": 32}).execute()
```

An `upsert` creates the record, or replaces the listed bins if the record exists.

```rust
client.put(&WritePolicy::default(), &key, &[

    as_bin!("name", "John Doe"),

    as_bin!("age", 32),

]).await?;
```

```csharp
client.Put(null, key,

    new Bin("name", "John Doe"),

    new Bin("age", 32));
```

The `null` argument tells the client to use its default write policy.

```go
err = client.PutBins(nil, key,

  aerospike.NewBin("name", "John Doe"),

  aerospike.NewBin("age", 32))

if err != nil {

  log.Fatal(err)

}
```

The `nil` argument tells the client to use its default write policy.

```js
await client.put(key, { name: 'John Doe', age: 32 });
```

```c
as_record rec;

as_record_inita(&rec, 2);

as_record_set_str(&rec, "name", "John Doe");

as_record_set_int64(&rec, "age", 32);

if (aerospike_key_put(&as, &err, NULL, &key, &rec) != AEROSPIKE_OK) {

    fprintf(stderr, "Error writing record: %s\n", err.message);

    as_record_destroy(&rec);

    return 1;

}

as_record_destroy(&rec);
```

The `NULL` argument tells the client to use its default write policy.

```java
client.put(null, key,

    new Bin("name", "John Doe"),

    new Bin("age", 32));
```

The `null` argument tells the client to use its default write policy.

```python
client.put(key, {"name": "John Doe", "age": 32})
```

## Read the record

Read the record with the same key.

-   [Java SDK](#tab-panel-6999)
-   [Python SDK](#tab-panel-7000)
-   [Rust](#tab-panel-7001)
-   [C#](#tab-panel-7002)
-   [Go](#tab-panel-7003)
-   [Node.js](#tab-panel-7004)
-   [C](#tab-panel-7005)
-   [Java](#tab-panel-7006)
-   [Python](#tab-panel-7007)

```java
Record record = session.query(key).execute().getFirstRecord();
```

```python
record = session.query(key).execute().first_or_raise().record_or_raise()
```

```rust
let record = client.get(&ReadPolicy::default(), &key, Bins::All).await?;
```

```csharp
Record record = client.Get(null, key);
```

```go
record, err := client.Get(nil, key)

if err != nil {

  log.Fatal(err)

}
```

```js
const record = await client.get(key);
```

```c
as_record* record = NULL;

if (aerospike_key_get(&as, &err, NULL, &key, &record) != AEROSPIKE_OK) {

    fprintf(stderr, "Error reading record: %s\n", err.message);

    return 1;

}
```

```java
Record record = client.get(null, key);
```

```python
(read_key, meta, bins) = client.get(key)
```

In the legacy Python client, the call returns the record’s key, its metadata, and its bins.

## Display the record

Print the values of the bins and the record’s generation. The generation counts the writes to the record. It is 1 after the first write and increases with each later write.

-   [Java SDK](#tab-panel-7008)
-   [Python SDK](#tab-panel-7009)
-   [Rust](#tab-panel-7010)
-   [C#](#tab-panel-7011)
-   [Go](#tab-panel-7012)
-   [Node.js](#tab-panel-7013)
-   [C](#tab-panel-7014)
-   [Java](#tab-panel-7015)
-   [Python](#tab-panel-7016)

```java
System.out.println("name: " + record.getString("name"));

System.out.println("age: " + record.getInt("age"));

System.out.println("Generation: " + record.generation);
```

```python
print("name:", record.bins["name"])

print("age:", record.bins["age"])

print("Generation:", record.generation)
```

```rust
println!("name: {}", record.bins["name"]);

println!("age: {}", record.bins["age"]);

println!("Generation: {}", record.generation);
```

```csharp
Console.WriteLine("name: " + record.GetString("name"));

Console.WriteLine("age: " + record.GetInt("age"));

Console.WriteLine("Generation: " + record.generation);
```

```go
fmt.Println("name:", record.Bins["name"])

fmt.Println("age:", record.Bins["age"])

fmt.Println("Generation:", record.Generation)
```

```js
console.log('name:', record.bins.name);

console.log('age:', record.bins.age);

console.log('Generation:', record.gen);
```

```c
printf("name: %s\n", as_record_get_str(record, "name"));

printf("age: %lld\n", (long long)as_record_get_int64(record, "age", 0));

printf("Generation: %d\n", record->gen);
```

```java
System.out.println("name: " + record.getString("name"));

System.out.println("age: " + record.getInt("age"));

System.out.println("Generation: " + record.generation);
```

```python
print("name:", bins["name"])

print("age:", bins["age"])

print("Generation:", meta["gen"])
```

```text
name: John Doe

age: 32

Generation: 1
```

Output

## Delete the record

Delete the record so the tutorial leaves no data behind and you can run it again.

-   [Java SDK](#tab-panel-7017)
-   [Python SDK](#tab-panel-7018)
-   [Rust](#tab-panel-7019)
-   [C#](#tab-panel-7020)
-   [Go](#tab-panel-7021)
-   [Node.js](#tab-panel-7022)
-   [C](#tab-panel-7023)
-   [Java](#tab-panel-7024)
-   [Python](#tab-panel-7025)

```java
session.delete(key).execute();
```

```python
session.delete(key).execute()
```

```rust
client.delete(&WritePolicy::default(), &key).await?;
```

```csharp
client.Delete(null, key);
```

```go
_, err = client.Delete(nil, key)

if err != nil {

  log.Fatal(err)

}
```

```js
await client.remove(key);
```

```c
if (aerospike_key_remove(&as, &err, NULL, &key) != AEROSPIKE_OK) {

    fprintf(stderr, "Error deleting record: %s\n", err.message);

    return 1;

}
```

```java
client.delete(null, key);
```

```python
client.remove(key)
```

## Close the connection

When your program is finished with the database, close the client.

-   [Java SDK](#tab-panel-7026)
-   [Python SDK](#tab-panel-7027)
-   [Rust](#tab-panel-7028)
-   [C#](#tab-panel-7029)
-   [Go](#tab-panel-7030)
-   [Node.js](#tab-panel-7031)
-   [C](#tab-panel-7032)
-   [Java](#tab-panel-7033)
-   [Python](#tab-panel-7034)

```java
cluster.close();
```

```python
cluster.close()
```

```rust
client.close().await?;
```

```csharp
client.Close();
```

```go
client.Close()
```

```js
client.close();
```

```c
as_record_destroy(record);

as_key_destroy(&key);

aerospike_close(&as, &err);

aerospike_destroy(&as);
```

```java
client.close();
```

```python
client.close()
```

## Complete program

This program combines the steps.

-   [Java SDK](#tab-panel-7035)
-   [Python SDK](#tab-panel-7036)
-   [Rust](#tab-panel-7037)
-   [C#](#tab-panel-7038)
-   [Go](#tab-panel-7039)
-   [Node.js](#tab-panel-7040)
-   [C](#tab-panel-7041)
-   [Java](#tab-panel-7042)
-   [Python](#tab-panel-7043)

```java
import com.aerospike.client.sdk.Cluster;

import com.aerospike.client.sdk.ClusterDefinition;

import com.aerospike.client.sdk.DataSet;

import com.aerospike.client.sdk.Key;

import com.aerospike.client.sdk.Record;

import com.aerospike.client.sdk.Session;

import com.aerospike.client.sdk.policy.Behavior;

public class SimplePutGet {

    public static void main(String[] args) {

        try (Cluster cluster = new ClusterDefinition("127.0.0.1", 3000).connect()) {

            Session session = cluster.createSession(Behavior.DEFAULT);

            Key key = DataSet.of("test", "demo").id("putgetkey");

            session.upsert(key)

                .bin("name").setTo("John Doe")

                .bin("age").setTo(32)

                .execute();

            Record record = session.query(key).execute().getFirstRecord();

            System.out.println("name: " + record.getString("name"));

            System.out.println("age: " + record.getInt("age"));

            System.out.println("Generation: " + record.generation);

            session.delete(key).execute();

        }

    }

}
```

```python
import sys

from aerospike_sdk import Behavior, DataSet

from aerospike_sdk.exceptions import AerospikeError

from aerospike_sdk.sync import ClusterDefinition

cluster = ClusterDefinition("127.0.0.1", 3000).connect()

try:

    session = cluster.create_session(Behavior.DEFAULT)

    key = DataSet.of("test", "demo").id("putgetkey")

    session.upsert(key).put({"name": "John Doe", "age": 32}).execute()

    record = session.query(key).execute().first_or_raise().record_or_raise()

    print("name:", record.bins["name"])

    print("age:", record.bins["age"])

    print("Generation:", record.generation)

    session.delete(key).execute()

except AerospikeError as e:

    print(f"Error: {e}", file=sys.stderr)

    sys.exit(1)

finally:

    cluster.close()
```

```rust
use aerospike::{as_bin, as_key, Bins, Client, ClientPolicy, ReadPolicy, WritePolicy};

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {

    let client = Client::new(&ClientPolicy::default(), &"127.0.0.1:3000").await?;

    let key = as_key!("test", "demo", "putgetkey");

    client.put(&WritePolicy::default(), &key, &[

        as_bin!("name", "John Doe"),

        as_bin!("age", 32),

    ]).await?;

    let record = client.get(&ReadPolicy::default(), &key, Bins::All).await?;

    println!("name: {}", record.bins["name"]);

    println!("age: {}", record.bins["age"]);

    println!("Generation: {}", record.generation);

    client.delete(&WritePolicy::default(), &key).await?;

    client.close().await?;

    Ok(())

}
```

```csharp
using System;

using Aerospike.Client;

public class SimplePutGet

{

    public static void Main()

    {

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

        try

        {

            Key key = new Key("test", "demo", "putgetkey");

            client.Put(null, key,

                new Bin("name", "John Doe"),

                new Bin("age", 32));

            Record record = client.Get(null, key);

            Console.WriteLine("name: " + record.GetString("name"));

            Console.WriteLine("age: " + record.GetInt("age"));

            Console.WriteLine("Generation: " + record.generation);

            client.Delete(null, key);

        }

        finally

        {

            client.Close();

        }

    }

}
```

```go
package main

import (

  "fmt"

  "log"

  "github.com/aerospike/aerospike-client-go/v8"

)

func main() {

  client, err := aerospike.NewClient("127.0.0.1", 3000)

  if err != nil {

    log.Fatal(err)

  }

  defer client.Close()

  key, err := aerospike.NewKey("test", "demo", "putgetkey")

  if err != nil {

    log.Fatal(err)

  }

  err = client.PutBins(nil, key,

    aerospike.NewBin("name", "John Doe"),

    aerospike.NewBin("age", 32))

  if err != nil {

    log.Fatal(err)

  }

  record, err := client.Get(nil, key)

  if err != nil {

    log.Fatal(err)

  }

  fmt.Println("name:", record.Bins["name"])

  fmt.Println("age:", record.Bins["age"])

  fmt.Println("Generation:", record.Generation)

  _, err = client.Delete(nil, key)

  if err != nil {

    log.Fatal(err)

  }

}
```

```js
import Aerospike from 'aerospike';

const client = await Aerospike.connect({ hosts: '127.0.0.1:3000' });

try {

    const key = new Aerospike.Key('test', 'demo', 'putgetkey');

    await client.put(key, { name: 'John Doe', age: 32 });

    const record = await client.get(key);

    console.log('name:', record.bins.name);

    console.log('age:', record.bins.age);

    console.log('Generation:', record.gen);

    await client.remove(key);

} finally {

    client.close();

}
```

```c
#include <stdio.h>

#include <aerospike/aerospike.h>

#include <aerospike/aerospike_key.h>

#include <aerospike/as_error.h>

#include <aerospike/as_record.h>

int main(void)

{

    as_config config;

    as_config_init(&config);

    as_config_add_host(&config, "127.0.0.1", 3000);

    aerospike as;

    aerospike_init(&as, &config);

    as_error err;

    if (aerospike_connect(&as, &err) != AEROSPIKE_OK) {

        fprintf(stderr, "Error connecting: %s\n", err.message);

        return 1;

    }

    as_key key;

    as_key_init_str(&key, "test", "demo", "putgetkey");

    as_record rec;

    as_record_inita(&rec, 2);

    as_record_set_str(&rec, "name", "John Doe");

    as_record_set_int64(&rec, "age", 32);

    if (aerospike_key_put(&as, &err, NULL, &key, &rec) != AEROSPIKE_OK) {

        fprintf(stderr, "Error writing record: %s\n", err.message);

        as_record_destroy(&rec);

        aerospike_close(&as, &err);

        aerospike_destroy(&as);

        return 1;

    }

    as_record_destroy(&rec);

    as_record* record = NULL;

    if (aerospike_key_get(&as, &err, NULL, &key, &record) != AEROSPIKE_OK) {

        fprintf(stderr, "Error reading record: %s\n", err.message);

        aerospike_close(&as, &err);

        aerospike_destroy(&as);

        return 1;

    }

    printf("name: %s\n", as_record_get_str(record, "name"));

    printf("age: %lld\n", (long long)as_record_get_int64(record, "age", 0));

    printf("Generation: %d\n", record->gen);

    if (aerospike_key_remove(&as, &err, NULL, &key) != AEROSPIKE_OK) {

        fprintf(stderr, "Error deleting record: %s\n", err.message);

    }

    as_record_destroy(record);

    as_key_destroy(&key);

    aerospike_close(&as, &err);

    aerospike_destroy(&as);

    return 0;

}
```

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

import com.aerospike.client.Bin;

import com.aerospike.client.Key;

import com.aerospike.client.Record;

public class SimplePutGet {

    public static void main(String[] args) {

        try (AerospikeClient client = new AerospikeClient("127.0.0.1", 3000)) {

            Key key = new Key("test", "demo", "putgetkey");

            client.put(null, key,

                new Bin("name", "John Doe"),

                new Bin("age", 32));

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

            System.out.println("name: " + record.getString("name"));

            System.out.println("age: " + record.getInt("age"));

            System.out.println("Generation: " + record.generation);

            client.delete(null, key);

        }

    }

}
```

```python
import sys

import aerospike

from aerospike import exception as ex

config = {"hosts": [("127.0.0.1", 3000)]}

try:

    client = aerospike.client(config).connect()

except ex.ClientError as e:

    print(f"Failed to connect to the cluster with {config['hosts']}: {e}", file=sys.stderr)

    sys.exit(1)

try:

    key = ("test", "demo", "putgetkey")

    client.put(key, {"name": "John Doe", "age": 32})

    (read_key, meta, bins) = client.get(key)

    print("name:", bins["name"])

    print("age:", bins["age"])

    print("Generation:", meta["gen"])

    client.remove(key)

except ex.AerospikeError as e:

    print(f"Error: {e}", file=sys.stderr)

    sys.exit(1)

finally:

    client.close()
```

```text
name: John Doe

age: 32

Generation: 1
```

Output

## What you learned

You wrote a record with two bins, read the record back, printed its bins and generation, and deleted the record.

## Next steps

-   Update a record with the [Basic operations](https://aerospike.com/docs/develop/tutorials/intro/basic-operations) tutorial.
-   Learn how each client connects: [Java SDK and Python SDK](https://aerospike.com/docs/develop/client/sdk/connect), [Rust](https://aerospike.com/docs/develop/client/rust/connect), [C#](https://aerospike.com/docs/develop/client/csharp/connect), [Go](https://aerospike.com/docs/develop/client/go/connect), [Node.js](https://aerospike.com/docs/develop/client/node/connect), [C](https://aerospike.com/docs/develop/client/c/connect), [Java](https://aerospike.com/docs/develop/client/java/connect), or [Python](https://aerospike.com/docs/develop/client/python/connect).