Is there any Java API for DynamoDB to convert from Item to Map<String, AttributeValue> without implementing it on my own?
EDIT
item.asMap() will return Map<String, Object>, not Map<String, AttributeValue>. Just wondering is there any direct API for this?
Yeah, but I've managed to find it:
// Item item
InternalUtils.toAttributeValues(item)
However, above API is deprecated in newer DynamoDB library which basically delegates the call to ItemUtils which is not deprecated fortunately. So I ended up using this:
ItemUtils.toAttributeValues(item)
Hope this will help others in future!
You can use the method asMap:
Returns all attributes of the current item as a map.
Updated answer:
To get a Map<String, AttributeValue> you can use ItemUtils.toAttributeValue:
Converts an Item into the low-level representation; or null if the input is null.
as follow
Map<String, AttributeValue> map = ItemUtils.toAttributeValue(item);
Related
I have rest server with spring.
There is a lot of requests where one of the params is fields fields is the set of fields that server should return in response. like: /?fields=[id,name] and server should return JSON object with both fields
I would like to know what is the best practice for generating such response.
We do it like this:
private Map<String, Object> processBook(BookEntity book, Set<String> fields, String locale){
Map<String, Object> map = new HashMap<String, Object>();
//..
if(fields.contains(ID)){
map.put(ID, book.getId());
}
if(fields.contains(ISBN)){
map.put(ISBN, book.getIsbn());
}
if(fields.contains(DESCRIPTION)){
if(locale.equals(UserLocale.UK)) map.put(DESCRIPTION, book.getDescriptionUa());
else if(locale.equals(UserLocale.RU)) map.put(DESCRIPTION, book.getDescriptionRu());
else map.put(DESCRIPTION, book.getDescriptionEn());
}
//..
return map;
}
Maybe there is much better alternative?
Note that in your case you obtain all data from DB - fully filled BookEntity object, and then show only requested fields.
In my opinion it'd be "much better alternative" to delegate field list to appropriate downstream integration call and get BookEntity object only with necessary fields. Then mentioned above method will reduce to just one line, your DB responses will be more lightweight, so it will bring simplicity and optimization gain to your system.
Any adequate DB provides such functionality: SQL or NoSQL, etc.
P.S. Plus standard approach of Object to JSON mapping such as Jackson or GSON at top level.
Instead of having a Map, you could have and object with the attributes you need and set them, instead of adding to map.Then you can use Google's Gson to transform your object into a Json object.Take a look at this quick tutorial.
One approach is to have an asMap function.
Map<String, Object> map = book.asMap();
map.keySet().retainAll(fields);
I am currently programming a bukkit plugin that stores a bunch of information about the player in a YAML configuration file. Now I want the plugin to read the YAML file when the server starts up and then add on the that information. I have my loader, but I cant use it because my plugin uses a custom map. Here is the code for the map:
Map<Integer, Map<String, Object>>
And here is the code to get the information from the file:
info = (Map<Integer, Map<String, Object>>) ticket.getConfigurationSection("tickets");
But when I try to run the plugin with that line of code i get this error:
Caused by: java.lang.ClassCastException: org.bukkit.configuration.MemorySection cannot be cast to java.util.Map
Full code is posted here: http://pastebin.com/Xgu8hwM0
The solution to this is not using a custom map. You already get a MemorySection from your configuration.
Work with that. Instead of casting you should use the method: getValues(boolean) which returns a Map<String, Object> containing all the relevant information and is specified by the Interface ConfigurationSection.
ticket.getConfigurationSection("tickets").getValues();
See also the relevant excerpt at bukkit's Configuration API Reference:
The getValues method will return the values in the
ConfigurationSection as a map, it takes a boolean which controls if
the nested maps will be returned in the map.
Yes I solved this. I HAD to use the Map<String, Object> but it worked because the way I had it(Map<Integer, Map<String, Object>>) that is the second part!
I have a spring MVC application(RestFul), The controller has a method/API which returns
Map<Long, List<Long>>.
I need to call the above API in another web application. To do this I have written a client program which will internally call
the API and return the data.
But instead of sending
Map<Long, List<Long>>
it always sends data in
Map<String, List<String>>.
Can't I send directly
Map<Long, List<Long>>
If I create a BO/TO(Java Bean) and which has a property of type Map>
then I am able to get the data in proper format
Below is the code snippet.
public Map<Long, List<Long>> get(Long sourceId){
Map<Long, List<Long>> map = null;
// codes to perform operation and putting data into map.
return map;
}
Can you please suggest what is the issue ?
Anything sent over the wire is a String ... but your code picking up the response should convert it to long. Well it would if it were a spring-mvc controller with the correct method signature.
Are you using Javascript? Try to use the javascript parseFloat method on your JSON data
I am using restassured framework, and inside it, it has JsonPath class.
JsonPath has a method signature of getList(String path, Class T);
I have attempt to do something like this:
List<JsonPath> myList = myJsonPathObject.getList("mypath", JsonPath.class);
And I get a runtime casting exception. So what would be the correct format in calling this.
I also attempted:
List<JsonPath> myList = myJsonPathObject.getList("mypath", new ArrayList<JsonPath>().getClass());
And that also failed. Actually that failed compilation.
JsonPath is used to extract values out of a JSON document. You cannot get a list of "JsonPath" out of JSON document.
I know this is an old threat, but someone commented and reminded me of this. I actually found an answer to this question.
What I had to do was cast it into a Map<String, Object> and while iterating through the objects, I have to cast the object into the appropriate class.
Map<String, Object> myMap = (Map<String, Object>) myJsonPathObject.get("mypath");
for (String key : myMap.getKeySet()) {
Map<String, Object> subMyMap = (Map<String, Object>) myMap.get(key);
}
So now I can continue to get additional mockup, json within a json.
I am currently using the Jackson library to unmarshal json to pojos, using annotations.
The json tree that I would like to unmarshal is as follows:
{
"key1":"value1",
"key2":"value2",
"key3":{
"key31":{
"key311":"value311",
"key312":"value312",
"key313":"value313"
},
"key32":"value32"
},
"key4":"value4",
"key5":"value5",
"key6":{
"key61":"value61"
}
}
I don't know the json structure in advance and would like to completely flatten it to a Map which content would be equivalent to:
Map<String, Object> outputMap = new HashMap<String, Object>();
outputMap.put("key1", "value1");
outputMap.put("key2", "value2");
outputMap.put("key311", "value311");
outputMap.put("key312", "value312");
outputMap.put("key313", "value313");
outputMap.put("key32", "value32");
outputMap.put("key4", "value4");
outputMap.put("key5", "value5");
outputMap.put("key61", "value61");
(note that keys "key3", "key31" and "key6" should be ignored)
Using the annotation #JsonAnySetter, I can create a function to populate the map with all top level atoms, but the method will also catch the node having children (when the value is a Map).
From that point, I can of course write myself the simple recursion over the children, but I would like this part to be handled automatically (in an elegant way) through the use of a facility from the library (annotation, configuration, etc.), and not have to write the recursion by myself.
Note: we assume there is not name clashing in the different level keys.
There is no annotation-based mechanism to do this that I know of, since #JsonUnwrapped which might be applicable is only usable with POJOs, not Maps.
I guess you could file an enhancement request to ask #JsonUnwrapped to be extended to also handle Map case, although it seems only appicable for serialization (not sure how one could deserialize back).