我想为DynamoDB添加多个值。我想添加所有数据集。
例如,如果有4个值,那么我想在DynamoDB中添加4行。对于' n'接收值的数量,' n'应该添加它们的行。
我循环访问数组但仍然只将最新数据设置为 item ,我只能保存一行。即每个数组的最后一个值。
我怎样才能做到这一点。请帮帮我。
@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===================================================
答案 0 :(得分:2)
DynamoDB评分表上的哈希键(和组合键的范围)是什么?我觉得你可能会一遍又一遍地覆盖同一个项目。
如果表具有哈希键,则每个项在该列中必须具有唯一值。如果表具有哈希/范围键,则多个项在哈希列中可以具有相同的值,但它们的范围值必须不同。更多详情here。
答案 1 :(得分:0)
除了@ readyornot的答案之外,我还可以通过从 foreach循环更改为 for loop 来解决问题。所以这是代码,
//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);