Manipulating Tree Structures for API endpoints - data-structures

I am working with an RDBMS that contains a list of hierarchical objects stored like this:
Id Name ParentId
====================================
1 Food NULL
2 Drink NULL
3 Vegetables 1
4 Fruit 1
5 Liquor 2
6 Carrots 3
7 Onions 3
8 Strawberries 4
...
999 Celery 3
I do not know the specific reason why this was chosen, but it is fixed in so far as the rest of the system relies on fetching the structure in this form.
I want to expose this data via JSON using a RESTful API, and I wish to output this in the following format (array of arrays):
item:
{
id: 1, Description: "Food",
items: [
{
id: 3, Description: "Vegetables",
items: [ ... ]
},
{
id: 4, Description: "Fruit",
items: [ ... ]
}
]
},
item:
{
id: 2, Description: "Drink",
items: [ ... ]
}
What would be a sensible way of looping through the data and producing the desired output? I'm developing in C# but if there are libraries or examples for other languages I would be happy to re-implement where possible.
Thanks :)

Related

Grouping Grouped results from elasticsearch

I'm a super duper newb with elasticsearch
I have a bunch of products on my elasticsearch. Each elasticsearch record has title, pid, product_group, color, size, qty... etc, many more fields
Now when I'm doing my request, what I want to happen is for it to group the results by pid, and then inside the _group part of the response, I also want those grouped as well, by product_group.
So in other words, if I have
pid: 1, product_group: 1, size: 1
pid: 1, product_group: 1, size: 2
pid: 1, product_group: 2, size: 1
pid: 1, product_group: 2, size: 2
pid: 2, product_group: 3, size: 1
pid: 2, product_group: 3, size: 2
pid: 2, product_group: 4, size: 1
pid: 2, product_group: 4, size: 2
I would want my top level search array to have 2 results: 1 for pid1 and 1 for pid2, and then inside of each of those results, inside the _group part of the json, I would expect 2 results each: pid1 would have a result for product_group 1 and product_group 2, and pid2 would have a _group result for product_group 3 and product_group 4.
Is this possible?
At the moment, this is how i'm modifying my query to group it based on pid:
group: {field: "pid", collapse: true}
I don't really know if I want collapse to be true or false, and I do'nt know how, or if it's even possible, to do a second layer of grouping like i'm asking for. Would appreciate any help.
The most straightforward way is to go with child terms aggs:
{
"size": 0,
"aggs": {
"by_pid": {
"terms": {
"field": "pid"
},
"aggs": {
"by_group": {
"terms": {
"field": "product_group"
},
"aggs": {
"underlying_docs": {
"top_hits": {}
}
}
}
}
}
}
}
Note that the last aggs group is optional -- I've put it there in case you'd like to know which docs have been bucketized to which particular band.

convert output of a query to json in oracle 12c

I have a query, the output is like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
- - - - - - - - - - - - -
40 20 22 10 0 0 0 0 0 0 0 0 0
I want to convert the output to one column and the column is like below:
output
-----------
{"1":40,"2":20,"3":22,"4":10,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0,"13":0}
You can use the JSON formatting 'trick' in SQL Developer.
Full scenario:
CREATE TABLE JSON_SO (
"1" INTEGER,
"2" INTEGER,
"3" INTEGER,
"4" INTEGER,
"5" INTEGER,
"6" INTEGER
);
INSERT INTO JSON_SO VALUES (
40,
20,
22,
10,
0,
0
);
select /*json*/ * from json_so;
And the output when executing with F5 (Execute as Script):
{
"results":[
{
"columns":[
{
"name":"1",
"type":"NUMBER"
},
{
"name":"2",
"type":"NUMBER"
},
{
"name":"3",
"type":"NUMBER"
},
{
"name":"4",
"type":"NUMBER"
},
{
"name":"5",
"type":"NUMBER"
},
{
"name":"6",
"type":"NUMBER"
}
],
"items":[
{
"1":40,
"2":20,
"3":22,
"4":10,
"5":0,
"6":0
}
]
}
]
}
Note that the JSON output happens client-side via SQL Developer (this also works in SQLcl) and I formatted the JSON output for display here using https://jsonformatter.curiousconcept.com/
This will work with any version of Oracle Database that SQL Developer supports, while the JSON_OBJECT() function was introduced in Oracle Database 12cR2 - if you want to have the DB format the result set to JSON for you.
If you want Oracle DB server to return the result as JSON, you can do query as below -
SELECT JSON_OBJECT ('1' VALUE col1, '2' VALUE col2, '3' VALUE col3) FROM table

How do I save the csv data into the mongodb?

I want to write a service method for saving bulk amount of csv data into the Mongo database keeping things in mind that it will not give some memory overflow error.
How can I do this if anyone already done or know please help.
You can use GridFS to save document larger than 16 MB, here is the link: https://docs.mongodb.com/v3.2/core/gridfs/, come back again when you have further questions.
Try to model a document based on your CSV that you're trying to store. It's hard to comment directly on your CSV format take the following for example:
Id,Name,Student 1 Id,Student 1 Name,Student 2 Id,Student 2 Name,Student 3 Id,Student 3 Name
48,Bill,1001,Kev,1002,Sakis,1005,Lee
78,Fred,1005,Lee,1073,Karen,1021,Jay
We could break this down in to a teacher collection:
{
_id: 48,
name: "Bill",
students: [
{_id: 1001, name: "Kev"},
{_id: 1002, name: "Sakis"},
{_id: 1005, name: "Lee"}
]
}
{
_id: 78,
name: "Fred",
students: [
{_id: 1005, name: "Lee"},
{_id: 1073, name: "Karen"},
{_id: 1021, name: "Jay"},
]
}

YouTube Analytics not returning channel metrics

API call:
https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel==UCs4uXj0TcstDHqhHDUWlINg&start-date=2016-10-02&end-date=2016-10-02&metrics=views%2ccomments%2clikes%2cdislikes%2cshares%2cestimatedMinutesWatched%2caverageViewDuration%2caverageViewPercentage%2cannotationClickThroughRate%2cannotationCloseRate%2csubscribersGained%2csubscribersLost
token:
ya29
.OAJc4bDrDoA6XVEmCI9KZK6rfIz68aXjibhZFQowWZxHJx7tt0qyvpxUryxtPZtN8IrN
Observe the parameters I used in reports.query Try-it:
The correct format in the ids textfield is 'channel=={YOUR_CHANNEL_ID}'
The response will correspond to the order you enumerated your metrics. For example, the response to the parameters above was:
"rows": [
[
201,
0,
9
]
]
meaning:
201 - views
0 - likes
9 - comments
because my metrics textfield was in this order -> views,likes,comments.

find overlapping times in an array of hashes

I've got an array of classes, and I want to find where there may be a schedule overlap.
My array is something like this
[
{
id:2,
start: "3:30",
length: 40,
break: 30,
num_attendees: 14
},
{
id: 3,
start: "3: 40",
length: 60,
break: 40,
num_attendees: 4
},
{
id: 4,
start: "4: 40",
length: 30,
break: 10,
num_attendees: 40
}
]
pretty simple.
Now, I want to get an array where I add the start and the length, and then get the classes that overlap to notify the user that they have a conflict.
I know I can do a large for loop and compare that way, but I'm thinking there must be a nicer way to do this in Ruby, something like (ignore that we're not working in absolute minutes here, I've got that, I just want to keep the example simple).
overlap = class_list.select{|a,b| if a.start+a.length>b.start return a,b end}
any suggestions?
You can use Array#combination like this:
class_list.combination(2).select{|c1, c2|
# here check if c1 and c2 overlap
}

Resources