2016-10-05 4 views
0

Ich habe eine DynamoDB-Tabelle, die Videos enthält Informationen.DynamoDB erhalten einzelne Spalte Liste der Bereichsschlüssel oder globalen sekundären

Derzeit ist "videoID" der primäre (Hash) Schlüssel und "Category" ist der Bereich (sort) Schlüssel.

Ich möchte eine Liste aller "Kategorien" (Bereichstasten) bekommen, damit ich dem Benutzer erlauben kann, aus einer der verfügbaren Videokategorien auszuwählen.

https://www.quora.com/What-are-some-good-ways-to-extract-one-single-column-from-a-DynamoDB-table

Ich lese, dass, wenn Sie das Attribut „Kategorie“ zu einem globalen Sekundärindex ändern geändert, um die Elemente für dieses GSI zurückkehren kann. Aber ich habe nicht herausgefunden, wie ich das machen soll.

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSIJavaDocumentAPI.html

Also ich denke, dass mir drei Fragen gibt: durch Abfragen die Elemente in Kategorie finden gerade den Bereich Schlüssel

Gibt es eine Möglichkeit, zu tun?

Wenn ich eine Kategorie in eine GSI ändere, kann ich die Artikel auf diese Weise finden?

oder

ist der einzige Weg, sie scannen die gesamte Tabelle zu tun?

Vielen Dank im Voraus für Ihre Hilfe

Antwort

0
Is the only way of doing it scanning the whole table? 

-NO, können Sie implementieren GSI es

Is there a way to do to find the items in Category by querying just the range key? 

zu vermeiden - Ja, wenn Sie nicht wollen, gesamte Tabelle scannen, dann müssen Sie GSI erstellen, die Kategorie als Hash haben. Diese GSI fungiert als eine Tabelle für sich selbst und Sie können sie abfragen, indem Sie Kategorienwerte übergeben.

If change Category to a GSI can I find the items that way? 

-Ja, Sie auf GSI mit Kategorie abfragen können Werte

I was reading that if you modified change the attribute "Category" to a global secondary index you can return the items for that GSI. But I have not been able to find how to do that. 

-Sie brauchen GSI zu erstellen, wenn Sie Tabelle erstellen, beispielsweise in der Verbindung gegeben wird, die Sie angegeben haben, sobald das geschehen ist Sie können abfragen, dass GSI

Referenzen: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html

0

Hier ist der Beispielcode ist Videos Tabelle mit GSI zu erstellen.

"Videos" Create Tabelle mit GSI: -

@Autowired 
    private AmazonDynamoDBClient dynamoDBClient; 

    public Boolean createTableWithGlobalSecondaryIndex(String tableName) { 

     CreateTableRequest createTableRequest = null; 

     DynamoDB dynamoDB = new DynamoDB(dynamoDBClient); 

     try { 

      ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>(); 
      attributeDefinitions.add(new AttributeDefinition().withAttributeName("videoid").withAttributeType("S")); 
      attributeDefinitions.add(new AttributeDefinition().withAttributeName("category").withAttributeType("S")); 

      ArrayList<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>(); 
      keySchema.add(new KeySchemaElement().withAttributeName("videoid").withKeyType(KeyType.HASH)); 
      keySchema.add(new KeySchemaElement().withAttributeName("category").withKeyType(KeyType.RANGE)); 

      // Initial provisioned throughput settings for the indexes 
      ProvisionedThroughput ptIndex = new ProvisionedThroughput().withReadCapacityUnits(150L) 
        .withWriteCapacityUnits(150L); 

      GlobalSecondaryIndex videoCategoryGsi = new GlobalSecondaryIndex().withIndexName("VideoCategoryGsi") 
        .withProvisionedThroughput(ptIndex) 
        .withKeySchema(new KeySchemaElement().withAttributeName("category").withKeyType(KeyType.HASH), 
          new KeySchemaElement().withAttributeName("videoid").withKeyType(KeyType.RANGE)) 
        .withProjection(new Projection().withProjectionType(ProjectionType.ALL)); 

      createTableRequest = new CreateTableRequest().withTableName(tableName).withKeySchema(keySchema) 
        .withAttributeDefinitions(attributeDefinitions) 
        .withProvisionedThroughput(
          new ProvisionedThroughput().withReadCapacityUnits(100L).withWriteCapacityUnits(100L)) 
        .withGlobalSecondaryIndexes(videoCategoryGsi); 

      Table table = dynamoDB.createTable(createTableRequest); 

      table.waitForActive(); 

     } catch (ResourceInUseException re) { 

      if (re.getErrorMessage().equalsIgnoreCase("Cannot create preexisting table")) { 
       LOGGER.info("Table already exists =============>" + tableName); 
      } else if (re.getErrorMessage().contains("Table already exists")) { 
       LOGGER.info("Table already exists =============>" + tableName); 
       LOGGER.info("Message =============>" + re.getErrorCode() + ";" + re.getErrorMessage()); 
      } else { 

       throw new RuntimeException("DynamoDB table cannot be created ...", re); 
      } 

     } catch (Exception db) { 

      throw new RuntimeException("DynamoDB table cannot be created ...", db); 

     } 

     return true; 

    } 

Abfrage GSI nach Kategorie: -

Hier ist der Eingang nur Kategorie ist, und es wird die Abfrage mit GSI. Mit anderen Worten, es wird nicht die gesamte Tabelle gescannt.

public List<String> findVideosByCategoryUsingGlobalSecondaryIndex(String category) { 

     List<String> videoAsJson = new ArrayList<>(); 

     DynamoDB dynamoDB = new DynamoDB(dynamoDBClient); 

     Table table = dynamoDB.getTable("Videos"); 

     Index index = table.getIndex("VideoCategoryGsi"); 

     ItemCollection<QueryOutcome> items = null; 

     QuerySpec querySpec = new QuerySpec(); 

     querySpec.withKeyConditionExpression("category = :val1") 
       .withValueMap(new ValueMap() 
         .withString(":val1", category)); 
     items = index.query(querySpec); 

     Iterator<Item> pageIterator = items.iterator(); 


     while (pageIterator.hasNext()) { 
      String videoJson = pageIterator.next().toJSON(); 
      System.out.println("Video json ==================>" + videoJson); 
      videoAsJson.add(videoJson); 
     } 

     return videoAsJson; 

    } 
Verwandte Themen