TryDaxHelper.java - Amazon DynamoDB

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

TryDaxHelper.java

Le fichier TryDaxHelper.java contient des méthodes d'utilitaire.

Les méthodes getDynamoDBClient et getDaxClient fournissent des clients Amazon DynamoDB et DynamoDB Accelerator (DAX). Pour les opérations de plan de contrôle (CreateTable, DeleteTable) et les opérations d'écriture, le programme utilise le client DynamoDB. Si vous spécifiez un point de terminaison de cluster DAX, le programme principal crée un client DAX pour effectuer les opérations de lecture (GetItem, Query, Scan).

Les autres méthodes TryDaxHelper (createTable, writeData, deleteTable) sont destinées à la configuration et à la destruction de la table DynamoDB et de ses données.

Vous pouvez modifier le programme de plusieurs façons :

  • Utilisez d'autres paramètres de débit alloué pour la table.

  • Modifiez la taille de chaque élément écrit (voir la variable stringSize de la méthode writeData).

  • Modifiez le nombre de tests GetItem, Query et Scan, ainsi que leurs paramètres.

  • Mettez en commentaire les lignes contenant helper.CreateTable et helper.DeleteTable (si vous ne voulez pas créer et supprimer la table chaque fois que vous exécutez le programme).

Note

Pour exécuter ce programme, vous pouvez configurer Maven afin d'utiliser le kit SDK DAX pour Java et l'AWS SDK for Java en tant que dépendances. Pour plus d’informations, consultez Utilisation du client en tant que dépendance Apache Maven.

Sinon, vous pouvez télécharger et inclure le client Java DAX et l'AWS SDK for Java dans votre chemin de classe. Consultez Java et DAX pour obtenir un exemple de configuration de votre variable CLASSPATH.

import com.amazon.dax.client.dynamodbv2.AmazonDaxClientBuilder; 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.model.AttributeDefinition; import com.amazonaws.services.dynamodbv2.model.KeySchemaElement; import com.amazonaws.services.dynamodbv2.model.KeyType; import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput; import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType; import com.amazonaws.util.EC2MetadataUtils; public class TryDaxHelper { private static final String region = EC2MetadataUtils.getEC2InstanceRegion(); DynamoDB getDynamoDBClient() { System.out.println("Creating a DynamoDB client"); AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard() .withRegion(region) .build(); return new DynamoDB(client); } DynamoDB getDaxClient(String daxEndpoint) { System.out.println("Creating a DAX client with cluster endpoint " + daxEndpoint); AmazonDaxClientBuilder daxClientBuilder = AmazonDaxClientBuilder.standard(); daxClientBuilder.withRegion(region).withEndpointConfiguration(daxEndpoint); AmazonDynamoDB client = daxClientBuilder.build(); return new DynamoDB(client); } void createTable(String tableName, DynamoDB client) { Table table = client.getTable(tableName); try { System.out.println("Attempting to create table; please wait..."); table = client.createTable(tableName, Arrays.asList( new KeySchemaElement("pk", KeyType.HASH), // Partition key new KeySchemaElement("sk", KeyType.RANGE)), // Sort key Arrays.asList( new AttributeDefinition("pk", ScalarAttributeType.N), new AttributeDefinition("sk", ScalarAttributeType.N)), new ProvisionedThroughput(10L, 10L)); table.waitForActive(); System.out.println("Successfully created table. Table status: " + table.getDescription().getTableStatus()); } catch (Exception e) { System.err.println("Unable to create table: "); e.printStackTrace(); } } void writeData(String tableName, DynamoDB client, int pkmax, int skmax) { Table table = client.getTable(tableName); System.out.println("Writing data to the table..."); int stringSize = 1000; StringBuilder sb = new StringBuilder(stringSize); for (int i = 0; i < stringSize; i++) { sb.append('X'); } String someData = sb.toString(); try { for (Integer ipk = 1; ipk <= pkmax; ipk++) { System.out.println(("Writing " + skmax + " items for partition key: " + ipk)); for (Integer isk = 1; isk <= skmax; isk++) { table.putItem(new Item() .withPrimaryKey("pk", ipk, "sk", isk) .withString("someData", someData)); } } } catch (Exception e) { System.err.println("Unable to write item:"); e.printStackTrace(); } } void deleteTable(String tableName, DynamoDB client) { Table table = client.getTable(tableName); try { System.out.println("\nAttempting to delete table; please wait..."); table.delete(); table.waitForDelete(); System.out.println("Successfully deleted table."); } catch (Exception e) { System.err.println("Unable to delete table: "); e.printStackTrace(); } } }