0

I want to add multiple values to DynamoDB. I want to add all the sets of data.

For example, If 4 values, then 4 rows I want to add in DynamoDB. For 'n' number of receiving values, 'n' rows of them should be added.

I looped through arrays but still only latest data is set into item and I am able to save only one row. i.e., the last value of every arrays.

How can I achieve this. Please help me.

@RequestMapping(value="/receiveRequests", method=RequestMethod.POST,produces={"application/json"})
public Map<String, Object> receiveRequests(@RequestParam(value="user_id") String user_id,
                                            @RequestParam(value="device_model") String device_model, 
                                            @RequestParam(value="api_key") String api_key,
                                            @RequestParam(value="score") int[] score,
                                            @RequestParam(value="registrationId") String registrationId,
                                            @RequestParam(value="userLocalTime") String[] userLocalTime,
                                            @RequestParam(value="lat") double[] lat,
                                            @RequestParam(value="lon") double[] lon,
                                            @RequestParam(value="accuracy") float[] accuracy,
                                            @RequestParam(value="userTimezone") String userTimezone,
                                            Model model)
{
            try
            {
                AmazonDynamoDBClient client = DynamoDBConfig.getAmazonDBClient();
                DynamoDBMapper mapper = new DynamoDBMapper(client);

                slf4jLogger.info("======================Begin Saving values to DynamoDB===================================================");   
                ScoreTable item = new ScoreTable();
                for(int ScoreValue : score)
                {
                    item.setScore(ScoreValue);
                    slf4jLogger.info("Saving Score: "+ScoreValue);
                }
                item.setDevice_model(device_model);
                slf4jLogger.info("Saving device_model: "+device_model);
                item.setApi_key(api_key);
                slf4jLogger.info("Saving api_key: "+api_key);
                item.setUser_id(user_id);
                slf4jLogger.info("Saving user_id: "+user_id);
                item.setRegistrationId(registrationId);
                slf4jLogger.info("Saving registrationId: "+registrationId);
                for(double latitude : lat)
                {
                    item.setLat(latitude);
                    slf4jLogger.info("Saving latitude: "+latitude);
                }
                for(double longitude : lon)
                {
                    item.setLon(longitude);
                    slf4jLogger.info("Saving longitude: "+longitude);
                }
                for(float accuracyValue : accuracy)
                {
                    item.setAccuracy(accuracyValue);
                    slf4jLogger.info("Saving accuracyValue: "+accuracyValue);
                }
                for(String userlocaltym:userLocalTime)
                {
                    item.setUserLocalTime(userlocaltym);
                    slf4jLogger.info("Saving userlocaltym: "+userlocaltym);
                }
                item.setUserTimezone(userTimezone);
                slf4jLogger.info("Saving userTimezone"+userTimezone);
                mapper.save(item);
                slf4jLogger.info("StressScore Table items: "+item);
            }           
            catch(AmazonServiceException ase)
            {
                //ase.printStackTrace();
                slf4jLogger.error(ase);
                slf4jLogger.error(ase.getMessage());
                slf4jLogger.error(ase.getStackTrace());
            }
            catch (Exception e)
            {
                //e.printStackTrace();
                slf4jLogger.error(e);
                slf4jLogger.error(e.getMessage());
                slf4jLogger.error(e.getStackTrace());
            }
 }

Here is my Logger:

 ======================Begin Saving values to DynamoDB===================================================
Saving Score: 86
Saving Score: 92
Saving Score: 32
Saving Score: 65

Saving device_model: Nexus S
Saving api_key: testApp
Saving user_id: 10f353a0-c6da-44ee-a9cb
Saving registrationId: APA91bFOz1lCFr1dT_s-HH-TatpF1XOIQ6GF846EMfmTjd_x-wARKy1zyEwr86UXfL9K1Xm8_PHpOKVuxmUXLKXHVKurXwe75EPmnBJolvS0

Saving latitude: 12.9563
Saving latitude: 12.9565
Saving latitude: 12.9567
Saving latitude: 12.9511

Saving longitude: 12.9563
Saving longitude: 12.9562
Saving longitude: 12.9563
Saving longitude: 12.912

Saving accuracyValue: 694.0
Saving accuracyValue: 611.0
Saving accuracyValue: 612.0
Saving accuracyValue: 613.0

Saving userlocaltym: 11-02-2015 11:20:25
Saving userlocaltym: 11-02-2015 11:21:26
Saving userlocaltym: 11-02-2015 11:22:25
Saving userlocaltym: 11-02-2015 11:25:25

Saving userTimezoneAsia/Calcutta

But I am able to save only one set.

Score Table items: Score [user_id=10f353a0-c6da-44ee-a9cb, device_model=Nexus S, api_key=testApp, gen_t=1436175254608, score=98, registrationId=APA91bFOz1lCFr1dT_s-HH-TatpF1XOIQ6GF846EMfmTjd_x-wARKy1zyEwr86UXfL9K1Xm8_PHpOKVuxmUXLKXHVKurXwe75EPmnBJolvS0, lat=12.9511, lon=12.912, accuracy=613.0, userLocalTime=11-02-2015 11:25:25, userTimezone=Asia/Calcutta]
======================End Saving values to DynamoDB===================================================

2 Answers 2

2

What is the Hash key (and Range, for a composite key) on your DynamoDB Score Table? I have a feeling that you might be overwriting the same item over and over again.

If a table has a Hash key, each item must have a unique value in that column. If the table has a Hash/Range key, multiple items can have the same value in the Hash column, but their Range values must be different. More details here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. Yes you are right. My table's Hashkey was gen_time (timestamp) and it was not inside the for loop. So one gen_time for all the loops and hence all the values were overwriting the same item over and over again. Also instead of foreach loop, i used for loop and hence problem is solved. Thank you once again. :+1
0

In addition to @readyornot's answer, I was able to solve by changing from foreach loop to for loop. So here is the code,

//Table name: Score
//Since array size of all parameters will be same as they are dependent on each other, 
//for eg: 5 scores has 5 latitudes & 5 longitudes etc, 
//I got the size of array and looped that many no.of times.

                Score item = null;  
                for(int i=0; i<score.length; i++)
                {
                    long gen_timestamp = Calendar.getInstance().getTimeInMillis();
                    client = DynamoDBConfig.getAmazonDBClient();
                    DynamoDBMapper mapper = new DynamoDBMapper(client);
                    item = new Score();
                    item.setGen_t(gen_timestamp);
                    slf4jLogger.info(i+" Saving gen_timestamp: "+gen_timestamp);
                    item.setDevice_model(device_model);
                    slf4jLogger.info(i+" Saving device_model: "+device_model);
                    item.setApi_key(api_key);
                    slf4jLogger.info(i+" Saving api_key: "+api_key);
                    item.setUser_id(user_id);
                    slf4jLogger.info(i+" Saving user_id: "+user_id);
                    item.setRegistrationId(registrationId);
                    slf4jLogger.info(i+" Saving registrationId: "+registrationId);
                    item.setScore(score[i]);
                    slf4jLogger.info(i+" Saving Score in /receiveUpdateStressScore: "+score[i]);
                    item.setLat(lat[i]);
                    slf4jLogger.info(i+" Saving lat in /receiveUpdateStressScore: "+lat[i]);
                    item.setLon(lon[i]);
                    slf4jLogger.info(i+" Saving lon in /receiveUpdateStressScore: "+lon[i]);
                    item.setAccuracy(accuracy[i]);
                    slf4jLogger.info(i+" Saving accuracy in /receiveUpdateStressScore: "+accuracy[i]);
                    item.setUserLocalTime(userLocalTime[i]);
                    slf4jLogger.info(i+" Saving userLocalTime in /receiveUpdateStressScore: "+userLocalTime[i]);
                    item.setUserTimezone(userTimezone);
                    slf4jLogger.info(i+" Saving userTimezone: "+userTimezone);
                    slf4jLogger.info(i+"Items Saving: "+item);
                    mapper.save(item);  
                }
                slf4jLogger.info("Score Table items: "+item);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.