I want to get the data from table using this query:
select clientId, Count(countryID) from table groupBy countryID;
which gives me the count of country corresponding to clientId. Normally we fetch into the List<Object>.
How i can fecth these details into a Map<clientId, countryCount> with clientId as key and countryCount as value?
Related
I have a dynamoDB table in my aws account. I can create client like this.
AmazonDynamoDB amazonDynamoDB =
AmazonDynamoDBClient.builder().withRegion("eu-west-1").withCredentials(creds).build();
DynamoDB dynamoDB = new DynamoDB(amazonDynamoDB);
Table table = dynamoDB.getTable("table name");
Suppose there is column name "content". I want to get a list or set of all values in "content" column.
I'm currently developing an Instagram clone using dynamoDB and appSync as back-end.
Im currently working on the user's feed page. I need to query all posts by the user and the users he follows ordered by timestamp.
I created a GSI with userId as partition key and timestamp as sort key.
I tried:
"Query" on GSI, but it allow only one partition key
"Scan" on the GSI then filter on userId, but it doesn't order posts by timestamp
"BatchGet", but it doesn't order posts by timestamp, and the result is not flatten
Post Attributes:
{
type:"POST",
id:"post1",
timeStamp:"213213123",
img_url: "https://img.com/img.jpg",
userComment:"nice pic",
userId:"user1",
userName:"kevin",
numberOfComment:"0",
numberOfLike:"0
}
User attribute:
{
type:"USER",
id:"user1",
userName:"Kevin",
FollowerCount:0,
FollowCount:0,
PostCount:1,
bio:"bio"
}
Is there another solution?
edit: my dynamodbTable
I've recently started learning DynamoDB and created a table 'Communication' with the following attributes (along with the DynamoDB type):
Primary Key Communication ID (randomly generated seq # or UUID): String
Sort Key User ID: String
Attributes/Columns:
Communication_Mode: String
Communication_Channel: String
Communication_Preference: String (possible values Y/N)
DateTime: Number
Use case: User can choose not to be communicated (Communication_Preference: N) and after a month user may opt for it (Communication_Preference: Y); meaning for the same User ID there can be more than 1 record as PartitionKey is randomly generated number
If I have to query above table and retrieve last inserted record for a specific userid do I need to create Global Secondary Index on DateTime.
Can someone correct me if my understanding is wrong or propose me the best option to meet above requirement. Thanks!
I am using the mobile application to query Dynamo DB tables. I have used the below query to fetch an item from the Dynamo DB Test table:
Test t = mapper.load(Test.class, DynamoDBHashKey, DynamoDBRangeKey);
My question is how to query an item from a Global Secondary Index? I have defined the annotations and parameters correctly in the Java class of the Test table.
Is there any other method to query Global Secondary Indexes and the Local Secondary Indexes.
The load api cannot be used to query GSI. The Query API can be used to query the GSI with key attributes.
Sample code:-
Map<String, AttributeValue> vals = new HashMap<>();
vals.put(":val1", new AttributeValue().withS("somevalue"));
DynamoDBQueryExpression<modelclass> queryExp = new DynamoDBQueryExpression<modelclass>()
.withKeyConditionExpression("category = :val1").withIndexName("indexname")
.withExpressionAttributeValues(vals);
dynamoDBMapper.query(modelclass.class, queryExp);
DynamodbQueryExpression class
Suppose I had a users and images tables in DynamoDB.
users table
userId (hash) name email
images table
imagesId (hash) userId filename
How should I set up the range or GSI if I wanted to get all images for a single userId? Should images table be a composite key with imageId (hash) and userId (range) and search by range (if possible)? or should userId (be a GSI) and query just be userId?
It looks like you could add a GSI on your Images table with the reverse key schema of your table:
hash key - userId
range key - imagesId
filename - additional property
You can then query this GSI with a userId to get all of its associated imagesId.
You cannot query using just a range key in DynamoDB.
Unless you want your query results from the images table to be sorted by userId, there's no point to using it as a range key. If you want to retrieve all of the images with a certain userId from the images table using a query, you need to use a GSI on userId.