Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
  • Blog
  • Pricing
  • Documentation
Yandex project
© 2025 Yandex.Cloud LLC
Yandex Managed Service for YDB
  • Getting started
  • Access management
      • Setting up AWS tools
      • Working with data through the HTTP interface
        • Overview
        • Creating a table
        • Uploading data to a table
          • Creating a record
          • Reading a record
          • Updating a record
          • Deleting a record
        • Searching and extracting data
        • Deleting a table
    • Common errors when working with the Document API
  • Monitoring metrics
  • Audit Trails events
  • FAQ
  • Public materials
  1. Amazon DynamoDB-compatible Document API
  2. Tools
  3. Working with the AWS SDK
  4. Managing records in a table
  5. Reading a record

Reading a record

Written by
Yandex Cloud
Updated at January 30, 2024

To read a record from the Series table:

Java
Python
PHP
Node.js
Ruby
  1. Create the SeriesItemOps02 project:

    mvn -B archetype:generate \
      -DarchetypeGroupId=org.apache.maven.archetypes \
      -DgroupId=com.mycompany.app \
      -DartifactId=SeriesItemOps02
    

    As a result of running the command, the SeriesItemOps02 project folder is created in the current working folder with a structure of subfolders and the pom.xml project description file.

  2. Go to the project folder:

    cd SeriesItemOps02
    
  3. Edit the project description in the pom.xml file, for example, using the nano editor:

    nano pom.xml
    

    Sample pom.xml file:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.mycompany.app</groupId>
      <artifactId>SeriesItemOps02</artifactId>
      <packaging>jar</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>SeriesItemOps02</name>
      <url>http://maven.apache.org</url>
      <build>
          <plugins>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-jar-plugin</artifactId>
                  <configuration>
                      <archive>
                          <manifest>
                              <addClasspath>true</addClasspath>
                              <classpathPrefix>lib/</classpathPrefix>
                              <mainClass>com.mycompany.app.SeriesItemOps02</mainClass>
                          </manifest>
                          <manifestEntries>
                              <Class-Path>.</Class-Path>
                          </manifestEntries>
                      </archive>
                      <finalName>release/SeriesItemOps02</finalName>
                  </configuration>
              </plugin>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-dependency-plugin</artifactId>
                  <executions>
                      <execution>
                          <id>copy-dependencies</id>
                          <phase>prepare-package</phase>
                          <goals>
                              <goal>copy-dependencies</goal>
                          </goals>
                          <configuration>
                              <outputDirectory>${project.build.directory}/release/lib</outputDirectory>
                              <overWriteReleases>false</overWriteReleases>
                              <overWriteSnapshots>false</overWriteSnapshots>
                              <overWriteIfNewer>true</overWriteIfNewer>
                          </configuration>
                      </execution>
                  </executions>
              </plugin>
          </plugins>
      </build>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.13.1</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>com.amazonaws</groupId>
          <artifactId>aws-java-sdk-dynamodb</artifactId>
          <version>1.11.1012</version>
        </dependency>
      </dependencies>
      <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
      </properties>
    </project>
    

    Check the current versions of junit and aws-java-sdk-dynamodb.

  4. In the folder src/main/java/com/mycompany/app/, create the SeriesItemOps02.java file, for example, using the nano editor:

    nano src/main/java/com/mycompany/app/SeriesItemOps02.java
    

    Copy the following code to the created file:

    Warning

    Instead of <Document_API_endpoint>, specify the prepared value.

    package com.mycompany.app;
    
    import com.amazonaws.client.builder.AwsClientBuilder;
    import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
    import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
    import com.amazonaws.services.dynamodbv2.document.DynamoDB;
    import com.amazonaws.services.dynamodbv2.document.Item;
    import com.amazonaws.services.dynamodbv2.document.Table;
    import com.amazonaws.services.dynamodbv2.document.spec.GetItemSpec;
    
    public class SeriesItemOps02 {
    
        public static void main(String[] args) throws Exception {
    
            AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("<Document_API_endpoint>", "ru-central1"))
                .build();
    
            DynamoDB dynamoDB = new DynamoDB(client);
    
            Table table = dynamoDB.getTable("Series");
    
            int series_id = 3;
            String title = "Supernatural";
    
            GetItemSpec spec = new GetItemSpec().withPrimaryKey("series_id", series_id, "title", title);
    
            try {
                System.out.println("Trying to retrieve a record...");
                Item outcome = table.getItem(spec);
                System.out.println("Record retrieved: " + outcome);
            }
            catch (Exception e) {
                System.err.println("Couldn't retrieve record: " + series_id + " " + title);
                System.err.println(e.getMessage());
            }
    
        }
    }
    

    To read a record by its primary key, the getItem method is used.

  5. Build a project:

    mvn package
    

    As a result of running the command, the SeriesItemOps02.jar file is generated in the folder target/release/.

  6. Run the application:

    java -jar target/release/SeriesItemOps02.jar
    

    Result:

    Attempting to obtain record...
    Record obtained: { Item: {title=Supernatural, series_id=3, info={release_date=2015-09-13, series_info=Supernatural is an American television series created by Eric Kripke}} }
    
  1. Create the SeriesItemOps02.py file, for example, using the nano editor:

    nano SeriesItemOps02.py
    

    Copy the following code to the created file:

    Warning

    Instead of <Document_API_endpoint>, specify the prepared value.

    from pprint import pprint
    import boto3
    from botocore.exceptions import ClientError
    
    def get_serie(title, series_id):
        ydb_docapi_client = boto3.resource('dynamodb', endpoint_url = "<Document_API_endpoint>")
    
        table = ydb_docapi_client.Table('Series')
    
        try:
            response = table.get_item(Key = {'series_id': series_id, 'title': title})
        except ClientError as e:
            print(e.response['Error']['Message'])
        else:
            return response['Item']
    
    if __name__ == '__main__':
        serie = get_serie("Supernatural", 3,)
        if serie:
            print("Record retrieved:")
            pprint(serie, sort_dicts = False)
    

    To read a record from the table, use the get_item method that passes the primary key of the desired item.

  2. Run the program:

    python SeriesItemOps02.py
    

    Result:

    Record retrieved:
    {'series_id': Decimal('3'),
    'title': ' Supernatural,
    'info': {'release_date': 2015-09-13',
              'series_info': 'Supernatural is an American television series '
                            'created by Eric Kripke'}}
    
  1. Create the SeriesItemOps02.php file, for example, using the nano editor:

    nano SeriesItemOps02.php
    

    Copy the following code to the created file:

    Warning

    Instead of <Document_API_endpoint>, specify the prepared value.

    <?php
    
    require 'vendor/autoload.php';
    
    date_default_timezone_set('UTC');
    
    use Aws\DynamoDb\Exception\DynamoDbException;
    use Aws\DynamoDb\Marshaler;
    
    $sdk = new Aws\Sdk([
        'endpoint' => '<Document_API_endpoint>',
        'region'   => 'ru-central1',
        'version'  => 'latest'
    ]);
    
    $dynamodb = $sdk->createDynamoDb();
    $marshaler = new Marshaler();
    
    $tableName = 'Series';
    
    $series_id = 3;
    $title = 'Supernatural';
    
    $key = $marshaler->marshalJson('
        {
            "series_id": ' . $series_id . ',
            "title": "' . $title . '"
        }
    ');
    
    $params = [
        'TableName' => $tableName,
        'Key' => $key
    ];
    
    try {
        $result = $dynamodb->getItem($params);
        echo json_encode($result["Item"], JSON_PRETTY_PRINT);
    
    } catch (DynamoDbException $e) {
        echo "Couldn't retrieve record:\n";
        echo $e->getMessage() . "\n";
    }
    
    ?>
    

    To read a record by its primary key, the getItem method is used.

  2. Run the program:

    php SeriesItemOps02.php
    

    Result:

    {
        "series_id": {
            "N": ".3e1"
        },
        "title": {
            "S": "Supernatural"
        },
        "info": {
            "M": {
                "release_date": {
                    "S": "2015-09-13"
                },
                "series_info": {
                    "S": "Supernatural is an American television series created by Eric Kripke"
                }
            }
        }
    }
    
  1. Create the SeriesItemOps02.js file, for example, using the nano editor:

    nano SeriesItemOps02.js
    

    Copy the following code to the created file:

    Warning

    Instead of <Document_API_endpoint>, specify the prepared value.

    const AWS = require("@aws-sdk/client-dynamodb");
    const { marshall } = require("@aws-sdk/util-dynamodb");
    
    // Credentials should be defined via environment variables AWS_SECRET_ACCESS_KEY and AWS_ACCESS_KEY_ID
    const dynamodb = new AWS.DynamoDBClient({
        region: "ru-central1",
        endpoint: "<Document_API_endpoint>",
    });
    
    const table = "Series";
    const series_id = 3;
    const title = "Supernatural";
    
    dynamodb.send(new AWS.GetItemCommand({
        TableName: table,
        Key: marshall({
            "series_id": series_id,
            "title": title
        })
    }))
        .then(data => {
            console.log("Record read successfully:", JSON.stringify(data, null, 2));
        })
        .catch(err => {
            console.error("Couldn't read record. JSON error:", JSON.stringify(err, null, 2));
        })
    

    To read a record from the table, use the command GetItemCommand. By specifying the primary key values (series_id and title), you can read any record from the Series table.

  2. Run the program:

    node SeriesItemOps02.js
    

    Result:

    Record read: {
      "Item": {
        "series_id": 3,
        "title": "Supernatural",
        "info": {
          "series_info": "Supernatural is an American television series created by Eric Kripke",
          "release_date": "2015-09-13"
        }
      }
    }
    
  1. Create the SeriesItemOps02.rb file, for example, using the nano editor:

    nano SeriesItemOps02.rb
    

    Copy the following code to the created file:

    Warning

    Instead of <Document_API_endpoint>, specify the prepared value.

    require 'aws-sdk-dynamodb'
    
    def get_item_from_table(dynamodb_client, table_item)
      result = dynamodb_client.get_item(table_item)
      puts "#{result.item['title']} (#{result.item['series_id'].to_i}):"
      puts "  Release date: #{result.item['info']['release_date']}"
      puts "  Series info: #{result.item['info']['series_info']}"
    rescue StandardError => e
      puts "Error retrieving series '#{table_item[:key][:title]} " \
            "(#{table_item[:key][:series_id]})': #{e.message}"
    end
    
    def run_me
      region = 'ru-central1'
      table_name = 'Series'
      title = 'Supernatural'
      series_id = 3
    
      Aws.config.update(
        endpoint: '<Document_API_endpoint>',
        region: region
      )
    
      dynamodb_client = Aws::DynamoDB::Client.new
    
      table_item = {
        table_name: table_name,
        key: {
          series_id: series_id,
          title: title
        }
      }
    
      puts "Retrieving data about '#{title} (#{series_id})' " \
        "from the table '#{table_name}'..."
      get_item_from_table(dynamodb_client, table_item)
    end
    
    run_me if $PROGRAM_NAME == __FILE__
    

    To read a record from a table, use the get_item method, in which you can specify the primary key value (series_id and title) to read any record from the Series table.

  2. Run the program:

    ruby SeriesItemOps02.rb
    

    Result:

    Getting information about 'Supernatural (3)' from table 'Series'...
    Supernatural (3):
      Release date: 2015-09-13
      Series info: Supernatural is an American television series created by Eric Kripke
    

Was the article helpful?

Previous
Creating a record
Next
Updating a record
Yandex project
© 2025 Yandex.Cloud LLC