📜  DynamoDB-本地二级索引

📅  最后修改于: 2020-11-28 14:04:23             🧑  作者: Mango


某些应用程序仅使用主键执行查询,但某些情况下会受益于备用排序键。通过创建单个或多个本地二级索引来允许您的应用程序选择。

复杂的数据访问要求(例如,合并数百万个项目)使执行更有效的查询/扫描成为必要。本地二级索引为分区键值提供备用排序键。它们还保存所有或某些表属性的副本。他们通过表分区键组织数据,但使用其他排序键。

使用本地二级索引消除了对整个表进行扫描的需要,并允许使用排序键进行简单而快速的查询。

所有本地二级索引必须满足某些条件-

  • 相同的分区键和源表分区键。
  • 仅一个标量属性的排序键。
  • 源表排序键的投影充当非键属性。

所有本地二级索引都会自动保存父表中的分区键和排序键。在查询中,这意味着有效收集计划的属性,并检索未计划的属性。

每个分区键值的本地二级索引的存储限制仍然为10GB,其中包括所有表项以及共享分区键值的索引项。

投射属性

由于复杂性,某些操作需要过多的读取/获取。这些操作会消耗大量的吞吐量。通过投影,您可以通过隔离这些属性来避免进行昂贵的获取并执行丰富的查询。请记住,投影由复制到二级索引中的属性组成。

进行二级索引时,您可以指定投影的属性。回顾DynamoDB提供的三个选项: KEYS_ONLY,INCLUDE和ALL

在选择投影中的某些属性时,请考虑相关的成本权衡-

  • 如果仅投影少量必要属性,则可以大大降低存储成本。

  • 如果您投影经常访问的非关键属性,则可以用存储成本来抵消扫描成本。

  • 如果您投影大多数或所有非关键属性,则可以最大程度地提高灵活性并降低吞吐量(无需检索)。但是,存储成本上升。

  • 如果您将KEYS_ONLY计划用于频繁的写入/更新和不频繁的查询,则它可以最大程度地减小大小,但可以保持查询准备。

本地二级索引创建

使用CreateTable的LocalSecondaryIndex参数可以创建一个或多个本地二级索引。您必须为排序键指定一个非键属性。在创建表时,您将创建本地二级索引。删除后,您将删除这些索引。

具有本地二级索引的表每个分区键值的大小上限为10GB,但可以存储任意数量的项目。

本地二级索引查询和扫描

当索引中的多个项目共享排序键值时,对本地二级索引的查询操作将返回所有具有匹配分区键值的项目。匹配项不会按特定顺序返回。本地二级索引的查询使用最终一致性或强一致性,强一致性读取可提供最新值。

扫描操作将返回所有本地二级索引数据。扫描要求您提供表和索引名称,并允许使用过滤器表达式来丢弃数据。

项目写作

创建本地二级索引时,可以指定排序键属性及其数据类型。当您编写一个项目时,如果该项目定义了索引键的属性,则其类型必须与键模式的数据类型匹配。

DynamoDB对表项和本地二级索引项没有任何一对一的关系要求。具有多个本地二级索引的表比具有较少的表的写入成本更高。

本地二级索引中的吞吐量注意事项

查询的读取容量消耗取决于数据访问的性质。查询使用最终一致性或强一致性,强一致性读取使用一个单位,而最终一致性读取则使用一半。

结果限制包括最大1MB的大小。结果大小来自匹配索引项大小的总和,四舍五入到最接近的4KB,而匹配表项大小也四舍五入到最接近的4KB。

写容量消耗保持在预配单位内。通过查找表写入中消耗的单位和更新索引中消耗的单位的总和来计算总的置备成本。

您还可以考虑影响成本的关键因素,其中一些因素可能是-

  • 当您编写定义索引属性的项目或更新项目以定义未定义索引属性时,将发生一次写入操作。

  • 当表更新更改索引键属性值时,将发生两次写入操作以删除,然后–添加一个项目。

  • 当写操作导致删除索引属性时,将执行一次写操作以删除旧项目投影。

  • 如果更新之前或之后索引中不存在任何项目,则不会发生任何写操作。

本地二级索引存储

在写入表项目时,DynamoDB自动将正确的属性集复制到所需的本地二级索引。这会向您的帐户收费。使用的空间由表主键字节大小,索引键属性字节大小,任何现有的预计属性字节大小以及每个索引项的开销100字节之和得出。

通过估计平均索引项目大小并乘以表项目数量来获得估计存储量。

使用Java处理本地二级索引

首先创建DynamoDB类实例,以创建本地二级索引。然后,使用必要的请求信息创建CreateTableRequest类实例。最后,使用createTable方法。

DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient( 
   new ProfileCredentialsProvider()));
String tableName = "Tools";  
CreateTableRequest createTableRequest = new 
   CreateTableRequest().withTableName(tableName);
   
//Provisioned Throughput
createTableRequest.setProvisionedThroughput (
   new ProvisionedThroughput()
   .withReadCapacityUnits((long)5)
   .withWriteCapacityUnits(( long)5));
   
//Attributes 
ArrayList attributeDefinitions = 
   new ArrayList();
   attributeDefinitions.add(new AttributeDefinition()
   .withAttributeName("Make")
   .withAttributeType("S"));
   
attributeDefinitions.add(new AttributeDefinition()
   .withAttributeName("Model")
   .withAttributeType("S"));
   
attributeDefinitions.add(new AttributeDefinition()
   .withAttributeName("Line")
   .withAttributeType("S"));
   
createTableRequest.setAttributeDefinitions(attributeDefinitions);

//Key Schema 
ArrayList tableKeySchema = new 
   ArrayList();
   
tableKeySchema.add(new KeySchemaElement()
   .withAttributeName("Make")
   .withKeyType(KeyType.HASH));                    //Partition key
   
tableKeySchema.add(new KeySchemaElement()
   .withAttributeName("Model")
   .withKeyType(KeyType.RANGE));                   //Sort key
   
createTableRequest.setKeySchema(tableKeySchema);
ArrayList indexKeySchema = new 
   ArrayList();
   
indexKeySchema.add(new KeySchemaElement()
   .withAttributeName("Make")
   .withKeyType(KeyType.HASH));                   //Partition key
   
indexKeySchema.add(new KeySchemaElement()
   .withAttributeName("Line")
   .withKeyType(KeyType.RANGE));                   //Sort key
   
Projection projection = new Projection()
   .withProjectionType(ProjectionType.INCLUDE);

ArrayList nonKeyAttributes = new ArrayList(); 
nonKeyAttributes.add("Type"); 
nonKeyAttributes.add("Year"); 
projection.setNonKeyAttributes(nonKeyAttributes);  

LocalSecondaryIndex localSecondaryIndex = new LocalSecondaryIndex() 
   .withIndexName("ModelIndex")
   .withKeySchema(indexKeySchema)
   .withProjection(p rojection);  

ArrayList localSecondaryIndexes = new 
   ArrayList(); 

localSecondaryIndexes.add(localSecondaryIndex); 
createTableRequest.setLocalSecondaryIndexes(localSecondaryIndexes);  
Table table = dynamoDB.createTable(createTableRequest); 
System.out.println(table.getDescription());

使用describe方法检索有关本地二级索引的信息。只需创建一个DynamoDB类实例,创建一个Table类实例,然后将该表传递给describe方法。

DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient( 
   new ProfileCredentialsProvider()));
   
String tableName = "Tools";
Table table = dynamoDB.getTable(tableName);
TableDescription tableDescription = table.describe();

List localSecondaryIndexes = 
   tableDescription.getLocalSecondaryIndexes();
   
Iterator lsiIter = 
   localSecondaryIndexes.iterator();
   
while (lsiIter.hasNext()) {  
   LocalSecondaryIndexDescription lsiDescription = lsiIter.next(); 
   System.out.println("Index info " + lsiDescription.getIndexName() + ":"); 
   Iterator kseIter = lsiDescription.getKeySchema().iterator(); 
   
   while (kseIter.hasNext()) { 
      KeySchemaElement kse = kseIter.next(); 
      System.out.printf("\t%s: %s\n", kse.getAttributeName(), kse.getKeyType()); 
   }
   
   Projection projection = lsiDescription.getProjection(); 
   System.out.println("\tProjection type: " + projection.getProjectionType()); 
   
   if (projection.getProjectionType().toString().equals("INCLUDE")) { 
      System.out.println("\t\tNon-key projected attributes: " + 
         projection.getNonKeyAttributes()); 
   } 
}

通过使用与表查询相同的步骤来执行查询。仅创建一个DynamoDB类实例,一个Table类实例,一个Index类实例,一个查询对象,并利用该查询方法。

DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient( 
   new ProfileCredentialsProvider()));
   
String tableName = "Tools";  
Table table = dynamoDB.getTable(tableName); 
Index index = table.getIndex("LineIndex");  
QuerySpec spec = new QuerySpec() 
   .withKeyConditionExpression("Make = :v_make and Line = :v_line") 
   .withValueMap(new ValueMap() 
   .withString(":v_make", "Depault") 
   .withString(":v_line", "SuperSawz"));
      
ItemCollection items = index.query(spec);
Iterator itemsIter = items.iterator();

while (itemsIter.hasNext()) { 
   Item item = itemsIter.next(); 
   System.out.println(item.toJSONPretty()); 
}

您也可以查看以下示例。

–以下示例可能假定以前创建的数据源。在尝试执行之前,请获取支持库并创建必要的数据源(具有所需特征的表或其他引用的源)。

以下示例还使用Eclipse IDE,AWS凭证文件和Eclipse AWS Java Project中的AWS Toolkit。

import java.util.ArrayList;
import java.util.Iterator;

import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;

import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Index;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.ItemCollection;
import com.amazonaws.services.dynamodbv2.document.PutItemOutcome;
import com.amazonaws.services.dynamodbv2.document.QueryOutcome;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.document.spec.QuerySpec;
import com.amazonaws.services.dynamodbv2.document.utils.ValueMap;

import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import com.amazonaws.services.dynamodbv2.model.KeyType;
import com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndex;
import com.amazonaws.services.dynamodbv2.model.Projection;
import com.amazonaws.services.dynamodbv2.model.ProjectionType;
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import com.amazonaws.services.dynamodbv2.model.ReturnConsumedCapacity;
import com.amazonaws.services.dynamodbv2.model.Select;

public class LocalSecondaryIndexSample {  
   static DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient( 
      new ProfileCredentialsProvider()));  
   public static String tableName = "ProductOrders";  
   
   public static void main(String[] args) throws Exception {  
      createTable();
      query(null); 
      query("IsOpenIndex"); 
      query("OrderCreationDateIndex"); 
   }
   public static void createTable() { 
      CreateTableRequest createTableRequest = new CreateTableRequest() 
         .withTableName(tableName) 
         .withProvisionedThroughput(new ProvisionedThroughput() 
         .withReadCapacityUnits((long) 1) 
         .withWriteCapacityUnits((long) 1));
         
      // Table partition and sort keys attributes 
      ArrayList attributeDefinitions = new 
         ArrayList(); 
      
      attributeDefinitions.add(new AttributeDefinition() 
         .withAttributeName("CustomerID") 
         .withAttributeType("S"));
         
      attributeDefinitions.add(new AttributeDefinition() 
         .withAttributeName("OrderID") 
         .withAttributeType("N"));
         
      // Index primary key attributes 
      attributeDefinitions.add(new AttributeDefinition() 
         .withAttributeName("OrderDate") 
         .withAttributeType("N"));
         
      attributeDefinitions.add(new AttributeDefinition() 
         .withAttributeName("OpenStatus") 
         .withAttributeType("N"));  
      createTableRequest.setAttributeDefinitions(attributeDefinitions);
      
      // Table key schema 
      ArrayList tableKeySchema = new
         ArrayList(); 
      tableKeySchema.add(new KeySchemaElement()  
         .withAttributeName("CustomerID") 
         .withKeyType(KeyType.HASH));                    //Partition key
         
      tableKeySchema.add(new KeySchemaElement() 
         .withAttributeName("OrderID") 
         .withKeyType(KeyType.RANGE));                   //Sort key
         
      createTableRequest.setKeySchema(tableKeySchema);  
      ArrayList localSecondaryIndexes = new 
         ArrayList();  
      
      // OrderDateIndex 
      LocalSecondaryIndex orderDateIndex = new LocalSecondaryIndex() 
         .withIndexName("OrderDateIndex");
         
      // OrderDateIndex key schema 
      ArrayList indexKeySchema = new 
         ArrayList(); 
      indexKeySchema.add(new KeySchemaElement() 
         .withAttributeName("CustomerID") 
         .withKeyType(KeyType.HASH));                   //Partition key
         
      indexKeySchema.add(new KeySchemaElement() 
         .withAttributeName("OrderDate") 
         .withKeyType(KeyType.RANGE));                   //Sort key
      orderDateIndex.setKeySchema(indexKeySchema);
      
      // OrderCreationDateIndex projection w/attributes list 
      Projection projection = new Projection() 
         .withProjectionType(ProjectionType.INCLUDE); 
      
      ArrayList nonKeyAttributes = new ArrayList(); 
      nonKeyAttributes.add("ProdCat"); 
      nonKeyAttributes.add("ProdNomenclature"); 
      projection.setNonKeyAttributes(nonKeyAttributes);
      orderCreationDateIndex.setProjection(projection);  
      localSecondaryIndexes.add(orderDateIndex);  
      
      // IsOpenIndex 
      LocalSecondaryIndex isOpenIndex = new LocalSecondaryIndex() 
         .withIndexName("IsOpenIndex");  
      
      // OpenStatusIndex key schema 
      indexKeySchema = new ArrayList(); 
      indexKeySchema.add(new KeySchemaElement() 
         .withAttributeName("CustomerID") 
         .withKeyType(KeyType.HASH));                   //Partition key
         
      indexKeySchema.add(new KeySchemaElement() 
         .withAttributeName("OpenStatus") 
         .withKeyType(KeyType.RANGE));                   //Sort key
         
      // OpenStatusIndex projection 
      projection = new Projection() .withProjectionType(ProjectionType.ALL);  
      OpenStatusIndex.setKeySchema(indexKeySchema); 
      OpenStatusIndex.setProjection(projection);  
      localSecondaryIndexes.add(OpenStatusIndex);  
      
      // Put definitions in CreateTable request 
      createTableRequest.setLocalSecondaryIndexes(localSecondaryIndexes);  
      System.out.println("Spawning table " + tableName + "..."); 
      System.out.println(dynamoDB.createTable(createTableRequest));  
      
      // Pause for ACTIVE status 
      System.out.println("Waiting for ACTIVE table:" + tableName); 
      try { 
         Table table = dynamoDB.getTable(tableName);
         table.waitForActive(); 
      } catch (InterruptedException e) { 
         e.printStackTrace(); 
      } 
   }
   public static void query(String indexName) {  
      Table table = dynamoDB.getTable(tableName);  
      System.out.println("\n*************************************************\n"); 
      System.out.println("Executing query on" + tableName);  
      QuerySpec querySpec = new QuerySpec() 
         .withConsistentRead(true) 
         .withScanIndexForward(true) 
         .withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);
      
      if (indexName == "OpenStatusIndex") {  
         System.out.println("\nEmploying index: '" + indexName 
            + "' open orders for this customer.");
            
         System.out.println( 
            "Returns only user-specified attribute list\n"); 
         Index index = table.getIndex(indexName); 
             
         querySpec.withKeyConditionExpression("CustomerID = :v_custmid and 
            OpenStatus = :v_openstat") 
            .withValueMap(new ValueMap() 
            .withString(":v_custmid", "jane@sample.com") 
            .withNumber(":v_openstat", 1));  
         
         querySpec.withProjectionExpression( 
            "OrderDate, ProdCat, ProdNomenclature, OrderStatus"); 
            ItemCollection items = index.query(querySpec); 
            Iterator iterator = items.iterator();  
            System.out.println("Printing query results...");  
            
         while (iterator.hasNext()) { 
            System.out.println(iterator.next().toJSONPretty()); 
         }  
      } else if (indexName == "OrderDateIndex") { 
         System.out.println("\nUsing index: '" + indexName 
            + "': this customer's orders placed after 05/22/2016."); 
         System.out.println("Projected attributes are returned\n"); 
         Index index = table.getIndex(indexName); 
             
         querySpec.withKeyConditionExpression("CustomerID = :v_custmid and OrderDate 
            >= :v_ordrdate") 
            .withValueMap(new ValueMap() 
            .withString(":v_custmid", "jane@sample.com") 
            .withNumber(":v_ordrdate", 20160522));
               
         querySpec.withSelect(Select.ALL_PROJECTED_ATTRIBUTES);  
         ItemCollection items = index.query(querySpec); 
         Iterator iterator = items.iterator();  
         System.out.println("Printing query results...");  
            
         while (iterator.hasNext()) { 
            System.out.println(iterator.next().toJSONPretty()); 
         }  
      } else { 
         System.out.println("\nNo index: All Jane's orders by OrderID:\n"); 
         querySpec.withKeyConditionExpression("CustomerID = :v_custmid") 
            .withValueMap(new ValueMap()
            .withString(":v_custmid", "jane@example.com"));  
         
         ItemCollection items = table.query(querySpec); 
         Iterator iterator = items.iterator();  
         System.out.println("Printing query results...");  
         
         while (iterator.hasNext()) { 
            System.out.println(iterator.next().toJSONPretty()); 
         } 
      } 
   } 
}