lundi 6 juillet 2015

Add multiple values to dynamoDB

I want to add multiple values to DynamoDB.

@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===================================================

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. TIA.




Aucun commentaire:

Enregistrer un commentaire