我想向DynamoDB添加多個值。我想添加所有的數據集。向dynamoDB添加多個值
例如,如果有4個值,那麼我想在DynamoDB中添加4行。對於'n'個接收值,應該添加'n'行。
我通過數組循環,但仍然只有最新的數據設置爲項目,我只能保存一行。即每個陣列的最後一個值。
我該如何做到這一點。請幫幫我。
@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());
}
}
這裏是我的記錄器:
======================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
但我能救只有一組。
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===================================================
非常感謝。你是對的。我的表的Hashkey是gen_time(時間戳),它不在_for_循環中。因此,所有循環的一個gen_time因此**所有值都一遍又一遍地重寫相同的項目。**而不是_foreach_循環,我使用_for_循環,因此問題得以解決。再一次感謝你。 :+1 – Chandz