Right data structure for getting data for objects inside MongoDB - java

I have the following JSON data for one of sample mongo record:
{
"_id" : ObjectId("598b53a1e4b0d3153fe5375a"),
"_class" : "com.Request",
"administratorComments" : "",
"statusLog" : [
{
"status" : "Submitted",
"startDate" : ISODate("2017-08-09T18:25:37.870Z"),
"endDate" : ISODate("2017-08-10T15:40:08.495Z")
},
{
"status" : "Backlogged",
"startDate" : ISODate("2017-08-10T15:40:08.495Z"),
"endDate" : ISODate("2017-09-15T18:18:14.241Z")
},
{
"status" : "Deleted",
"startDate" : ISODate("2017-09-15T18:18:14.241Z"),
"endDate" : ISODate("2017-09-15T18:18:24.764Z")
}
],
"lastChangeDate" : ISODate("2017-09-15T18:29:35.886Z"),
"createdDate" : ISODate("2017-08-09T18:25:37.870Z")
}
I am fetching the different status inside statusLog for different _id using a Hashmap:
HashMap<String, List<String>> hm_mongo = new HashMap<String, List<String>>();
Where I am storing _id as Key and different status in an Array<List>.
Now, I am supposed to capture the endDate for all the status.
Could you point any data structure that will be helpful to capture the status and its endDate for different _ids for MongoDB?

You can simply expand your current HashMap's value structure. In order to do this, you can create a class that will store the "status", "startDate", "endDate" fields.
class statusLog{
public string status;
public Date startDate;
public Date endDate;
}
HashMap<String, List<statusLog>> hm_mongo = new HashMap<String, List<statusLog>>();

Related

dynamodb update list of map

I am trying to save List<Map<String,AttributeValue>> into dynamodb
The data looks like
"includeData" : [
{
"data" : "[VALUE]",
"work" : "[VALUE]",
"product" : "[VALUE]",
"catagory" : "[VALUE]",
"type" : "[VALUE]"
},
{
"data" : "[VALUE]",
"work" : "[VALUE]",
"product" : "[VALUE]",
"catagory" : "[VALUE]",
"type" : "[VALUE]"
}
]
In the code, I am creating the update request as
Map<String, AttributeValue> annExpressionAttributeValues = new HashMap<>();
annExpressionAttributeValues.put(V_INCLUDE_DATA, new AttributeValue().withL(Arrays.asList([LIST])));
new UpdateItemRequest().withTableName([TABLE_NAME])
.withKey(keys).withUpdateExpression("SET #includeData = :v_includeData").withExpressionAttributeNames(annExpressionAttributeNames)
.withExpressionAttributeValues(annExpressionAttributeValues).withReturnValues(ReturnValue.ALL_NEW);
I am facing at line AttributeValue().withL(Arrays.asList(annExpressionAttributeValues)));
withL is not suitable for this requirement. I cannot find any other. How can I update this data structure in dynamodb

How to extract item from nested list - Rest Assured

I've got this json object
{
"type" : "employee",
"columns" :
[
{
"id" : 1,
"human" :
[
{
"name" : "ANA",
"age" : "23"
},
{
"name" : "IULIA",
"age" : "22"
}
]
},
{
"id" : 2,
"human" :
[
{
"name" : "ADI",
"age" : "21"
},
{
"name" : "GELU",
"age" : "18"
}
]
}
]
}
and I need to extract the first name from each human list.
I've tried .body("columns.human.name[0]", everyItem(containsString("A"))) but it doesn't work. Any ideas?
Using JsonPath you can get all columns and all humans.
Each of JSON Object is represented as HashMap<>. If it contains only fields it's HashMap<String, String> but if contains arrays or nested JSON Objects then it is HashMap<String, Object> where Object is either another JSON Object or array.
Given the above you can use following code to get all columns and name of first human in each column:
JsonPath path = response.jsonPath();
List<HashMap<String, Object>> columns = path.getList("columns");
for (HashMap<String, Object> singleColumn : columns) {
List<HashMap<String, Object>> humans = (List<HashMap<String, Object>>) singleColumn.get("human");
System.out.println(humans.get(0).get("name"));
}
The above code will print ANA and ADI in the console.
You can store the results in List<String> for further processing
you can get all "name" from humans with jsonPath : $.columns[*].human[*].name it will give below result :
[
"ANA",
"IULIA",
"ADI",
"GELU"
]
And if you want only first "name" then you need to use josn : $.columns[*].human[0].name this will give you below result:
[
"ANA",
"ADI"
]

Compare Map<String,Object> and print the diff attribute value

I want to compare Request object and DB object in jersey PUT/PATCH request, so that requested object i have converted in Map{String,Object} reqObject, and DB object also i converted in Map{String,Object} db object.
Now I want to compare both map and print the diff value in log.
Please help me how to do in generic way.
if(!properties1.equals(properties2)){
for(Map.Entry<String, Object> empNew:properties1.entrySet()){
for(Map.Entry<String, Object> empOld:properties2.entrySet()){
if((empNew.getKey().equals(empOld.getKey())&& !(empNew.getValue().equals(empOld.getValue())))){
System.out.println("Update "+" "+empNew.getKey()+" "+"value"+" "+"from"+" "+empOld.getValue()+" "+"to"+" "+empNew.getValue());
}
}
}
}
My object Structure is like this:
{"id" : "5349b4ddd2781d08c09890f3",
"name" : "Abed",
"address" : [{ "number" : "xyz",
"street" : "asdfgh",
"town" : "AMB6 Ind2",
"postcode" : "600039" }
]
}

How to use Jackson to Marshall a list of JSON objects that contain unknown number of KV paiirs

Lets say that you have a json array that looks as follows :
[
{
"id" : 1,
"first_name" : "Jane",
"last_name" : "Doe"
},
{
"id" : 2,
"first_name" : "John",
"middle_name" : "Q",
"last_name" : "Public",
"birth_year" : 1971
},
{
"id" : 3,
"anonymous_user" : true,
"crm_id" : "abc123"
},
{
"id" : 4,
"first_name" : "Albert",
"last_name" : "Einstein",
"profession" : "Scientist",
"birth_year" : 1879,
"e_equals_mc_squared" : true
}
]
The goal is to use Jackson to marshal to POJO. My thinking is that I could have a class to contain each K,V pair .. something like :
public class myDataObject {
private String key;
private T value;
...
}
And maybe a container class for that :
public class myDataContainer {
private ArrayList<myDataObject> dataList;
...
}
My question becomes what does marshaling that look like using jackson? There is no schema for the json, each json object can have an unspecified number of K,V pairs and the list of keys is also unspecified.
Does something like this work? Is this even the right approach?
ArrayList<myDataContainer> dataList = mapper.readValue(jsonFile, new TypeReference<ArrayList<myDataContainer>() {});
What your JSON really is, is a list of maps - with Strings as keys and Objects as values.
So, using Jackson, you should be able to do:
ObjectMapper mapper = new ObjectMapper();
List<Map<String, Object>> data = mapper.readValue(json, new TypeReference<List<Map<String, Object>>>(){});

how to create mongoDB objectid in java

Refering to post How to add an array to a MongoDB document using Java?
I have created a mongo schema using java
it has sub elements, I am getting _id for main document
I would like to get _id in sub elements also here output looks (I have marked the portion where I need _id) b.party.find().pretty();
{
"_id" : ObjectId("5399aba6e4b0ae375bfdca88"),
"addressDetails" : [
{
// _id here
"locationName" : "Office",
"phones" : [
{ // _id here
"name" : "Tel1",
"value" : "95253-"
},
{ // _id here
"name" : "Tel2",
"value" : "95253-"
},
{ // _id here
"name" : "Tel3",
"value" : "95253-"
},
{ // _id here
"name" : "Fax1",
"value" : "0253-"
}
],
"address" : "A-3,MIDCA-3,MIDC",
"defaultBillAddrerss" : "",
"pincode" : "422 010",
"city" : null,
"state" : "1",
"country" : ""
},
{ // _id here
"locationName" : "Factory",
"phones" : [
{ // _id here
"name" : "Tel1",
"value" : "0253-"
},
{ // _id here
"name" : "Tel2",
"value" : "0253-"
},
{ // _id here
"name" : "Tel3",
"value" : "0253-"
},
{ // _id here
"name" : "Fax1",
"value" : "0253-"
}
],
"address" : "A-3 INDUSTRIAL AREA,",
"defaultBillAddrerss" : "",
"pincode" : "422 010",
"city" : null,
"state" : "1",
"country" : ""
}
],
"crLimit" : "0.0",
"crPeriod" : "",
"name" : "CROMPTON GREAVES "
}
Java code to create is similar to How to add an array to a MongoDB document using Java?
Is there any code to create ObjectId("") programmatically in java?
To create objectId programmatically, use the following syntax
import org.bson.types.ObjectId;
ObjectId id1 = new ObjectId();
ObjectId id2 = ObjectId.get();
// In case you want to mention the parent ID itself,
ObjectId id3 = new ObjectId("5399aba6e4b0ae375bfdca88");
To create objectId programmatically, use the following syntax
Map<String,String> objectId = new HashMap<String,String>();
objectId.put("$oid","5399aba6e4b0ae375bfdca88");
Then insert into mongodb.

Categories