简体   繁体   中英

How to load data to Dynamo DB Table with Global and Local Secondary Indexes ??

I have created some tables in Dynamo DB using AWS console and defined some global and secondary indexes.

Now the problem is how to load data in those tables using AWS SDK of java. I went through the Developer guide ( http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html ) of Dynamo DB but I could not find how to load data when your table has global secondary indexes.

Code to load table with data is :

  Table table = dynamoDB.getTable(tableName);
  Item item = new Item().withPrimaryKey("Name", "Amazon DynamoDB")
                        .withString("Category", "Amazon Web Services")
                        .withNumber("Threads", 2)
                        .withNumber("Messages", 4)
                        .withNumber("Views", 1000);
  table.putItem(item);

Now suppose in my table if I have defined Views as Global Secondar Index. So the same code will work or there are some different method to deal with this kind of use cases ??

You cannot write directly to GSIs, even if they are defined for a table.

Instead, DynamoDB will automatically propagate item insertions, updates, and deletions to GSIs, depending on the ProjectionType set in each GSI. As long as you have defined the GSIs in the CreateTable operation, or added them in an UpdateTable operation, the GSIs will reflect items you put into the base table, depending on the ProjectionType .

If your table has secondary indexes (being Local or Global) they will be automatically created/maintained as you insert data in the table itself. DynamoDB will manage your indexes automatically making sure they are consistent with whatever you have in the table.

However, another completely different issue (that you should be careful about) is how to properly load data into DynamoDB tables:

There are times when you load data from other data sources into DynamoDB. Typically, DynamoDB partitions your table data on multiple servers. When uploading data to a table, you get better performance if you upload data to all the allocated servers simultaneously. For example, suppose you want to upload user messages to a DynamoDB table. You might design a table that uses a hash and range type primary key in which UserID is the hash attribute and the MessageID is the range attribute.

More details on Distribute Write Activity During Data Upload

Here you can find some additional information about Secondary Indexes to complement what you already know:

For efficient access to data in a table, Amazon DynamoDB creates and maintains indexes for the primary key attributes. This allows applications to quickly retrieve data by specifying primary key values. However, many applications might benefit from having one or more secondary (or alternate) keys available, to allow efficient access to data with attributes other than the primary key. To address this, you can create one or more secondary indexes on a table, and issue Query or Scan requests against these indexes.

A secondary index is a data structure that contains a subset of attributes from a table, along with an alternate key to support Query operations. With a secondary index, queries are no longer restricted to the table primary key; you can also retrieve the data using the alternate key defined by the secondary index. A table can have multiple secondary indexes, which gives your applications access to many different query patterns.

The data in a secondary index consists of attributes that are projected, or copied, from the table into the index. When you create a secondary index, you define the alternate key for the index, along with any other attributes that you want to be projected in the index. DynamoDB copies these attributes into the index, along with the primary key attributes from the table. You can then query or scan the index just as you would query or scan a table.

The section Improving Data Access with Secondary Indexes in DynamoDB should provide helpful details on how to properly define GSI's (including concepts like projections and etc).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM