how to add data to the repeatable fields in avro schema? - serialization

I'm trying to test avro serde and deserde without code generation (I completed this task using code generation). Schema is as follows
{
"type": "record",
"name" : "person",
"namespace" : "avro",
"fields": [
{ "name" : "personname", "type": ["null","string"] },
{ "name" : "personId", "type": ["null","string"] },
{ "name" : "Addresses", "type": {
"type": "array",
"items": [ {
"type" : "record",
"name" : "Address",
"fields" : [
{ "name" : "addressLine1", "type": ["null", "string"] },
{ "name" : "addressLine2", "type": ["null", "string"] },
{ "name" : "city", "type": ["null", "string"] },
{ "name" : "state", "type": ["null", "string"] },
{ "name" : "zipcode", "type": ["null", "string"] }
]
}]
}
},
{ "name" : "contact", "type" : ["null", "string"]}
]
}
I understand this is how data is added to the schema.
Schema schema = new Schema.Parser().parse(new File("src/person.avsc.txt"));
GenericRecord person1 = new GenericData.Record(schema);
person1.put("personname", "goud");
But how do I add city, state etc to address and then add it to addresses?
GenericRecord address1 = new GenericData.Record(schema);
address1.put("city", "SanJose");
The above snippet doesn't work. I tried to look into GenericArray, but I couldn't get my head around it.

You need to describe inner complex type ("type" : "record", "name" : "Address") in separate schema, like this:
{
"type" : "record",
"name" : "Address",
"fields" : [
{ "name" : "addressLine1", "type": ["null", "string"] },
{ "name" : "addressLine2", "type": ["null", "string"] },
{ "name" : "city", "type": ["null", "string"] },
{ "name" : "state", "type": ["null", "string"] },
{ "name" : "zipcode", "type": ["null", "string"] }
]
}
Then you may create an inner object:
Schema innerSchema = new Schema.Parser().parse(new File("person_address.avsc"));
GenericRecord address = new GenericData.Record(innerSchema);
address.put("addressLine1", "adr_1");
address.put("addressLine2", "adr_2");
address.put("city", "test_city");
address.put("state", "test_state");
address.put("zipcode", "zipcode_00000");
Then add an inner object you created to ArrayList.
At last, create the main object and add all this staff in it.
Here is full example in java:
Schema innerSchema = new Schema.Parser().parse(new File("person_address.avsc"));
GenericRecord address = new GenericData.Record(innerSchema);
address.put("addressLine1", "adr_1");
address.put("addressLine2", "adr_2");
address.put("city", "test_city");
address.put("state", "test_state");
address.put("zipcode", "zipcode_00000");
ArrayList<GenericRecord> addresses = new ArrayList<>();
addresses.add(address);
Schema mainSchema = new Schema.Parser().parse(new File("person.avsc"));
GenericRecord person1 = new GenericData.Record(mainSchema);
person1.put("personname", "goud");
person1.put("personId", "123_id");
person1.put("Addresses", addresses);
Result:
{
"personname": "goud",
"personId": "123_id",
"Addresses": [
{
"addressLine1": "adr_1",
"addressLine2": "adr_2",
"city": "test_city",
"state": "test_state",
"zipcode": "zipcode_00000"
}
],
"contact": "test_contact"
}

Related

Unable to access data in Postgres query where clause

I am dealing with the following JSON in a Postgres database column called steps in a table called tCampaign :
[
{
"name":"Step 1",
"stepReference":"01e9f7c0-bc79-11eb-ab6f-2fa1cb676e38",
"rewardConditions": [
{
"conditionDefinitions": [
{
"instanceId":"01805260-0818-4e99-e5b1-5820d1b133cd",
"type":"registration",
"properties": null,
"name": "Registration"
},
{
"instanceId":"01e115c3-5e56-437a-5d13-6c04281e9588",
"type":"optIn",
"properties": null,
"name":"Opt In"
}
],
"rewardDefinitions":[
{
"instanceId":"01c82190-1d56-44f9-474a-513732302e28",
"type":"sportsReward",
"properties": {"activation": {"type": "onReward"}, "betFlavour": "SPORTS", "channels": ["__use_campaign_restrictions__"], "expiry": {"offset": {"days": "02", "hours": "00", "minutes": "00", "seconds": "00"}, "type": "relative"}, "inRunning": "-", "maxReward": {"USD": "1"}, "minimumOdds": "", "oddsInput": {"minimumOdds": {"american": "", "european": ""}}, "retail": "offBetBuild", "returnStakeOnPayout": "false"},
"name":"Freebet",
"calculator":{"type":"fixed","value":"100"}
}
]
}
]
},
{
"name" : "Step 2",
"stepReference" : "01daa4a0-bc79-11eb-ab6f-2fa1cb676e38",
"rewardConditions": [
{
"conditionDefinitions": [
{
"instanceId" : "01fb15ae-01d0-49e1-966a-8ff438e9a191",
"type" : "genericSportsBet",
"properties" : {"betFlavour": "SPORTS", "betTrackEventThreshold": "10", "betTypes": [ "SGL" ], "builderBetOption": "ALL", "channels": [ "__use_campaign_restrictions__" ], "currencyThresholdMap": { "USD": "1" }, "eventHierarchySelection": { "categories": [], "classes": [], "events": [], "marketTemplates": [], "markets": [ "5824" ], "retrobetEventIds": [ "1200" ], "selections": [], "selectionsMarket": [], "types": [] }, "eventHierarchySelectionUI": { "markets": [ { "id": 5824, "mapping": [], "name": "Match Result", "parentId": 1200, "parentParentId": 5, "path": [ "Category: |England|", "Class: |England Premier League|", "Type: |GK Team K| |vs| |GK Team L|" ], "selectionMapper": false, "settled": "N", "startTime": "2021-05-31 11:15:00", "status": "A" } ] }, "inRunning": "-", "legTypes": [ "WIN" ], "metOnSettlement": false, "minOdds": "", "oddsInput": { "minOdds": { "american": "", "european": "" } }, "priceTypes": [ "LP" ]},
"name" : "Sports Bet"
}
],
"rewardDefinitions":[
{
"instanceId" : "0110eb70-44f9-4d57-40bb-09ff4169136c",
"type" : "sportsReward",
"properties" : {"activation": {"type": "onReward"}, "betFlavour": "SPORTS", "channels": ["__use_campaign_restrictions__"], "expiry": {"offset": {"days": "02", "hours": "00", "minutes": "00", "seconds": "00"}, "type": "relative"}, "inRunning": "-", "maxReward": {"USD": "2"}, "minimumOdds": "", "oddsInput": {"minimumOdds": {"american": "", "european": ""}}, "retail": "offBetBuild", "returnStakeOnPayout": "false"},
"name" : "Freebet",
"calculator" : {"type":"fixed","value":"100"}
}
]
}
]
}
]
and have written the following query to extract properties from conditionDefinitions :
select conditionDefinitions->'properties' as properties from tcampaign cmp
LEFT JOIN LATERAL json_array_elements(steps) singleStep ON true
LEFT JOIN LATERAL json_array_elements(singleStep->'rewardConditions') rewardConditions on TRUE
LEFT JOIN LATERAL json_array_elements(rewardConditions->'conditionDefinitions') conditionDefinitions on TRUE
where properties is not null ;
but I get the following error :
ERROR: column "properties" does not exist
LINE 5: where properties is null ;
If I remove the where clause the query runs fine. Why do I not have access to properties in the where clause? Because I can see results coming back if I remove the WHERE clause, so the query does have results

Sparql Group Entire Objects into List

I have a list of restaurants and want to return them grouped in lists by two identifiers. The response would look this this in JSON:
[
{
"restaurantChainId": "1",
"restaurantTypeId": "001",
"restaurantList": [
{
"restaurantId": "1-1",
"restaurantChainId": "1",
"restaurantTypeId": "001",
"restaurant name": "Pizza Place London"
"restaurant location": "London"
},
{
"restaurantId": "1-2",
"restaurantChainId": "1",
"restaurantTypeId": "001",
"restaurant name": "Pizza Place Birmingham"
"restaurant location": "Birmingham"
}
]
},
{
"restaurantChainId": "1",
"restaurantTypeId": "002",
"restaurantList": [
{
"restaurantId": "1-3",
"restaurantChainId": "1",
"restaurantTypeId": "002",
"restaurant name": "Pizza Place Manchester"
"restaurant location": "Manchester"
}
]
},
{
"restaurantChainId": "2",
"restaurantTypeId": "002",
"restaurantList": [
{
"restaurantId": "2-1",
"restaurantChainId": "2",
"restaurantTypeId": "002",
"restaurant name": "Taco Town London"
"restaurant location": "London"
},
{
"restaurantId": "2-2",
"restaurantChainId": "2",
"restaurantTypeId": "002",
"restaurant name": "Taco Town Newcastle"
"restaurant location": "Newcastle"
}
]
}
]
So that an object in the array has a list where each in the list shares the restaurantChainId and the restaurantTypeId.
I have tried the following group_concat query:
SELECT ?restaurantChainId (GROUP_CONCAT(?restaurantTypeId; SEPARATOR=", ") AS ?restaurantTypeIdList)
WHERE {
?restaurant <http://example.com/terms#docType> "restaurant" ;
<http://example.com/terms#restaurantTypeId> ?restaurantTypeId;
<http://example.com/terms#institutionFid> ?restaurantChainId .
FILTER (strstarts(str(?restaurantTypeId), '00'))
} GROUP BY ?restaurantChainId
But obviously this is just producing a list restaurantTypeId for each restaurantChainId. So it has grouped things correctly. I have things in the correct groups but I only have the id list for each restaurant where I actually want all related information of that restaurant. I don't really know how to go about this. Is it possible?
Improved Query
SELECT ?restaurantChainId (GROUP_CONCAT(DISTINCT ?restaurantTypeId; SEPARATOR=", ") AS ?restaurantTypeIdList) (GROUP_CONCAT(?restaurant; SEPARATOR=", ") AS ?restaurant)
WHERE {
?restaurant <http://example.co.uk/terms#docType> "restaurant" ;
<http://example.co.uk/terms#swiftbic> ?restaurantTypeId;
<http://example.co.uk/terms#institutionFid> ?restaurantChainId ;
<http://example.co.uk/terms#fid> ?officeId .
FILTER (strstarts(str(?restaurantTypeId), 'ABA'))
} GROUP BY ?restaurantChainId ?restaurantTypeId
And here is the current result:
{
"head" : {
"vars" : [ "restaurantChainId", "restaurantTypeIdList", "restaurant" ]
},
"results" : {
"bindings" : [ {
"restaurantChainId" : {
"type" : "literal",
"value" : "XYZ"
},
"restaurantTypeIdList" : {
"type" : "literal",
"value" : "ABA3"
},
"restaurant" : {
"type" : "literal",
"value" : "http://example.co.uk/restaurant/XYZ-4, http://example.co.uk/restaurant/XYZ-5, http://example.co.uk/restaurant/XYZ-6"
}
}, {
"restaurantChainId" : {
"type" : "literal",
"value" : "ABC"
},
"restaurantTypeIdList" : {
"type" : "literal",
"value" : "ABA1"
},
"restaurant" : {
"type" : "literal",
"value" : "http://example.co.uk/restaurant/ABC-14, http://example.co.uk/restaurant/ABC-15, http://example.co.uk/restaurant/ABC-4"
}
}, {
"restaurantChainId" : {
"type" : "literal",
"value" : "XYZ"
},
"restaurantTypeIdList" : {
"type" : "literal",
"value" : "ABA1"
},
"restaurant" : {
"type" : "literal",
"value" : "http://example.co.uk/restaurant/XYZ-2, http://example.co.uk/restaurant/XYZ-1"
}
}, {
"restaurantChainId" : {
"type" : "literal",
"value" : "XYZ"
},
"restaurantTypeIdList" : {
"type" : "literal",
"value" : "ABA2"
},
"restaurant" : {
"type" : "literal",
"value" : "http://example.co.uk/restaurant/XYZ-3"
}
}, {
"restaurantChainId" : {
"type" : "literal",
"value" : "ABC"
},
"restaurantTypeIdList" : {
"type" : "literal",
"value" : "ABA2"
},
"restaurant" : {
"type" : "literal",
"value" : "http://example.co.uk/restaurant/ABC-9"
}
} ]
}
}
So the actually groupings are correct and I have the uri for each restaurant but I don't know how to get information specifically related to them.

how to create and pass more or less json payload in karate at runtime

Sometimes I have to pass following payload
{
"code": "hhhh",
"name": "NDS coupon Testing",
"description": "NDS couopn testing via postman",
"status": "ACTIVE",
"comments": null,
"rules": {
"AND":[{
"rule":{
"operator" : "EQ",
"status" : "ACTIVE",
"attribute_name" : "HOTEL_ID",
"attribute_value" : "12744",
"attribute_value_type" : "INTEGER"
}
}]
}
}
And sometimes following payload
{
"code": "hhhh",
"name": "NDS coupon Testing",
"description": "NDS couopn testing via postman",
"status": "ACTIVE",
"comments": null,
"rules": {
"AND":[{
"rule":{
"operator" : "EQ",
"status" : "ACTIVE",
"attribute_name" : "HOTEL_ID",
"attribute_value" : "12744",
"attribute_value_type" : "INTEGER"
}
}
{
"AND" : [{"rule" : {
"status" : "ACTIVE",
"operator" : "EQUALS_LT"
"attribute_name" : "checkout",
"attribute_value" : "2018-12-31T00:00:00-05:30",
"attribute_value_type" : "DATE_TIME"
}
},]
}
}
and my framework structure is
payload.json which has default payload. Now in my feature file I load payload.json set values which I want to update , but how to add / remove some part of json at runtime ?
There are multiple ways to do this. For further reading I suggest the following sections:
Embedded Expressions
Remove if Null
Conditional Logic
Set and Remove
And it would have been really nice if you had taken the time to ensure your JSON is well-formed. Anyway.
* def json =
"""
{
"code": "hhhh",
"name": "NDS coupon Testing",
"description": "NDS couopn testing via postman",
"status": "ACTIVE",
"comments": null,
"rules": {
"AND": [
{
"rule": {
"operator": "EQ",
"status": "ACTIVE",
"attribute_name": "HOTEL_ID",
"attribute_value": "12744",
"attribute_value_type": "INTEGER"
}
},
{
"rule": {
"status": "ACTIVE",
"operator": "EQUALS_LT",
"attribute_name": "checkout",
"attribute_value": "2018-12-31T00:00:00-05:30",
"attribute_value_type": "DATE_TIME"
}
}
]
}
}
"""
* eval if (true) karate.remove('json', '$.rules.AND[1]')
* print json

dojo How Tree insert data to children?

hi i want to insert data to children to Tree.but I want to put the data.for example i want to update children[0] information.Rather than creating a new one I'd like to update the existing data.
my Tree.json
{
"name": "SCATTER/BUBBLE CHART",
"id": "SCATTERBUBBLE",
"children": [
{
"name": "Series",
"id": "SERIES",
"children": [
{
"name" : "Data:X",
"id" : "DX"
},
{
"name" : "Data:Y",
"id" : "DY"
}
]
},
{
"name": "XAxis",
"id": "X"
},
{
"name": "YAxis",
"id": "Y"
}
]
}
if i click button,i want to result
{
"name": "SCATTER/BUBBLE CHART",
"id": "SCATTERBUBBLE",
"children": [
{
"name": "Series",
"id": "SERIES",
"children": [
{
"name" : "Data:X",
"id" : "DX"
},
{
"name" : "Data:Y",
"id" : "DY"
},
{
"name" : "Data:Z",
"id" : "DZ"
}
]
},
{
"name": "XAxis",
"id": "X"
},
{
"name": "YAxis",
"id": "Y"
},
{
"name": "ZAxis",
"id": "Z"
}
]
}
i don't know update children tree ask for advice
Use node.item to get the store item object which has created the node. I hope you have the node object. For instance if you want to get the root node of your tree :-
var rootNode = dijit.byId("treeID").attr("rootNode");
After you get the node's item object you may update any of its attributes and your store will be modified. Your store should also extend "dojo/store/Observable", so that your tree gets updated with the changes to store.

Query for missing fields in nested documents

I have a user document which contains many tags
Here is the mapping:
{
"user" : {
"properties" : {
"tags" : {
"type" : "nested",
"properties" : {
"id" : {
"type" : "string",
"index" : "not_analyzed",
"store" : "yes"
},
"current" : {
"type" : "boolean"
},
"type" : {
"type" : "string"
},
"value" : {
"type" : "multi_field",
"fields" : {
"value" : {
"type" : "string",
"analyzer" : "name_analyzer"
},
"value_untouched" : {
"type" : "string",
"index" : "not_analyzed",
"include_in_all" : false
}
}
}
}
}
}
}
}
Here are the sample user documents:
User 1
{
"created_at": 1317484762000,
"updated_at": 1367040856000,
"tags": [
{
"type": "college",
"value": "Dhirubhai Ambani Institute of Information and Communication Technology",
"id": "a6f51ef8b34eb8f24d1c5be5e4ff509e2a361829"
},
{
"type": "company",
"value": "alma connect",
"id": "58ad4afcc8415216ea451339aaecf311ed40e132"
},
{
"type": "company",
"value": "Google",
"id": "93bc8199c5fe7adfd181d59e7182c73fec74eab5",
"current": true
},
{
"type": "discipline",
"value": "B.Tech.",
"id": "a7706af7f1477cbb1ac0ceb0e8531de8da4ef1eb",
"institute_id": "4fb424a5addf32296f00013a"
},
]
}
User 2:
{
"created_at": 1318513355000,
"updated_at": 1364888695000,
"tags": [
{
"type": "college",
"value": "Dhirubhai Ambani Institute of Information and Communication Technology",
"id": "a6f51ef8b34eb8f24d1c5be5e4ff509e2a361829"
},
{
"type": "college",
"value": "Bharatiya Vidya Bhavan's Public School, Jubilee hills, Hyderabad",
"id": "d20730345465a974dc61f2132eb72b04e2f5330c"
},
{
"type": "company",
"value": "Alma Connect",
"id": "93bc8199c5fe7adfd181d59e7182c73fec74eab5"
},
{
"type": "sector",
"value": "Website and Software Development",
"id": "dc387d78fc99ab43e6ae2b83562c85cf3503a8a4"
}
]
}
User 3:
{
"created_at": 1318513355001,
"updated_at": 1364888695010,
"tags": [
{
"type": "college",
"value": "Dhirubhai Ambani Institute of Information and Communication Technology",
"id": "a6f51ef8b34eb8f24d1c5be5e4ff509e2a361821"
},
{
"type": "sector",
"value": "Website and Software Development",
"id": "dc387d78fc99ab43e6ae2b83562c85cf3503a8a1"
}
]
}
Using the above ES documents for search, I want to construct a query where I need to fetch users who have company tags in nested tag documents or the users who do not have any company tags. What will be my search query?
For example in above case, if search for google tag, then the returned documents should be 'user 1' and 'user 3' (as user 1 has company tag google and user 3 has no company tag). User 2 is not returned as it has a company tag other than google too.
Not trivial at all, mainly due to the not have a type:company tag clause. Here's what I came up with:
{
"or" : {
"filters" : [ {
"nested" : {
"filter" : {
"and" : {
"filters" : [ {
"term" : {
"tags.value" : "google"
}
}, {
"term" : {
"tags.type" : "company"
}
} ]
}
},
"path" : "tags"
}
}, {
"not" : {
"filter" : {
"nested" : {
"filter" : {
"term" : {
"tags.type" : "company"
}
},
"path" : "tags"
}
}
}
} ]
}
}
It contains an or filter with two nested clauses: the first one finds the documents that have tags.type:company and tags.value:google, while the second one finds all the documents that don't have any tags.type:company.
This needs to be optimized though since and/or/not filters don't take advantage of caching for filters that work with bitsets, like the term filter does. It would be best to take some more time to find a way to use a bool filter and obtain the same result. Have a lookt this article to know more.