Rename all keys inside of a specific dictionary in fluentd - fluentd

Does anyone have any idea how I can filter the elements of a specific list of dictionaries to add a prepended name to the keys of all elements? The field name that is the list of dictionaries is static and known ahead of time. However, the elements inside each dictionary are not. I have tried to use fluent-plugin-rewrite and fluent-plugin-record-transformer without success.
I have a record like the following. I know 'g' ahead of time, but the fact there are two entries in the dictionary and that the specific elements are 'h, i, k, and l' are dynamic.
{
"a": "d1",
"b": "d2",
"c": {
"e": "d3",
"f": "d4"
},
"g": [
{
"h": "d5",
"i": "d6",
},
{
"h": "d7",
"k": "d8",
"l": "d9",
}
]
}
}
That I would like to change to:
{
"a": "d1",
"b": "d2",
"c": {
"e": "d3",
"f": "d4"
},
"g": [
{
"prepend_h": "d5",
"prepend_i": "d6",
},
{
"prepend_h": "d7",
"prepend_k": "d8",
"prepend_l": "d9",
}
]
}
}

Related

sorted map in cypher

When creating a map from collections via apoc's apoc.map.fromLists (or plain cypher for all I care), is there any way to respect/keep the sorting? Or is it just "It's a map, sorting doesn't make sense anyway".
specifically, when executing
with [{name: "z", a: 1}, {name: "b", b: 1}, {name: "b2", b: 2}] as qs
return apoc.map.fromLists([q in qs | q.name], qs)
I get
{
"b2": {
"name": "b2",
"b": 2
},
"z": {
"name": "z",
"a": 1
},
"b": {
"name": "b",
"b": 1
}
}
where indeed I would like to have
{
"z": {
"name": "z",
"a": 1
},
"b": {
"name": "b",
"b": 1
},
"b2": {
"name": "b2",
"b": 2
}
}
Sorry, maps in Cypher are unsorted and unsortable structures, with regard to map keys.
There is apoc.maps.sortedProperties(), however this doesn't return a map, but a list of key/value pairs, where those pairs are sorted alphabetically.

How to extract some specific values from json

I am still trying to create an app where the user could transform his name or a word with chemical elements (like breaking bad logo).
The user will enter a word in a text field and when he'll submit it will return him the word with the corresponding chemical symbols if they match, or it will display the "raw" letters if they don't match.
ex: If no symbol matches I am keepking the initial entry so it could be: hello => He ll O (bold char represent the existing chemical symbols)
I know this could be done in js, but the challenge is ROR (btw I don't know any js...)
In an earlier question I had just a hash like:
symbols =
{"cr" => "Cr",
"sb" => "Sb",
"ag" => "Ag",
"ar" => "Ar",
"as" => "As",
"at" => "At",
"n" => "N",
"ba" => "Ba",
"bk" => "Bk"}
and I was using name.downcase.gsub!(Regexp.union(symbols.keys), symbols)to transform the user entry. Actually I need more datas... that's why I chosen the json file.
Like on this picture i will need to use:
"number"
"small"
"molar"
(and the "name" will appear in a caption below)
I have organized a .json file with all the symbols I may need in the app and stored it in my config/periodic_table.json (pasted just a sample cause it's very long).
1°) If a user enter "hello" how do I loop to search for the "he" hash and print the "name","number", "small" and the "molar"
2°) I will use the json as a database (I will use heroku to deploy) so do I have anything to transform for using json and pg together?
[ "symbols"
{
"h": {
"name": "Hydrogen",
"number": 1,
"small": "H",
"molar": 1.00794
},
"he": {
"name": "Helium",
"number": 2,
"small": "He",
"molar": 4.002602
},
"b": {
"name": "Boron",
"number": 5,
"small": "B",
"molar": 10.811
},
"c": {
"name": "Carbon",
"number": 6,
"small": "C",
"molar": 12.0107
},
"n": {
"name": "Nitrogen",
"number": 7,
"small": "N",
"molar": 14.0067
}
}
]
I will need to loop first with the symbols that contain 3 chars, then 2 then 1... shall i change anythin in the json, like an harray for the hashes that contains 3 chars, another for 2 chars , and for 1char?
Correct Json Format
[{
"symbols":{
"h":{
"name": "Hydrogen",
"number": 1,
"small": "H",
"molar": 1.0079
},
"he":{
"name": "Helium",
"number": 2,
"small": "He",
"molar": 4.002602
},
"b": {
"name": "Boron",
"number": 5,
"small": "B",
"molar": 10.811
},
"c": {
"name": "Carbon",
"number": 6,
"small": "C",
"molar": 12.0107
},
"n": {
"name": "Nitrogen",
"number": 7,
"small": "N",
"molar": 14.0067
}
}
}]

NSJSONSerialization JSONObjectWithData will generate JSON object even if the JSON is invalid

NSJSONSerialization JSONObjectWithData will generate JSON object even if the JSON is invalid, also the option that is being used in JSONObjectWithData is NSJSONReadingMutableContainers:
it will create a JSON object with only the valid JSON object/array
If the typo (in the JSON) is at the end of the final parentheses (like a comma), the JSON object will be 100% valid AND the error pointer will be nil.
If the typo is in the middle of value/key (inside the JSON) this value won't be created to the corresponding value/key inside that object AND the error pointer won't be nil.
NSJSONSerialization isValidJSONObject can only validate JSON object (NSArray or NSDictionary) thus, (obviously) NSString representation of the JSON will always return false - in other words, creating a JSON object will always be valid using JSONObjectWithData.
So how can I truly validate a JSON in iOS/Objective-c ?
The invalid JSON - a comma in the end makes the JSON invalid (as mentioned in #2):
{
"A": [
{
"B": 1,
"C": 2,
"D": [
1,
2,
3
]
}
],
"test": [
{
"id": 1,
"id2": 2,
"id3": 3,
"B": [
],
"C": [
],
"D": "test msg",
"E": 2,
"F": "www.google.com",
"G": [
2,
3
],
"test2": [
{
"id": 1,
"id2": 2,
"id3": 3,
"B": [
{
"id": 1,
"id2": "2",
"id3": 3,
"B": 4
}
]
},
]
}
]
}

How to return relationship type with Neo4J's Cypher queries?

I am trying to get the relationship type of a very simple Cypher query, like the following
MATCH (n)-[r]-(m) RETURN n, r, m;
Unfortunately this return an empty object for r. This is troublesome since I can't distinguish between the different types of relationships. I can monkey patch this by adding a property like [r:KNOWS {type:'KNOWS'}] but I am wondering if there isn't a direct way to get the relationship type.
I even followed the official Neo4J tutorial (as described below), demonstrating the problem.
Graph Setup:
create (_0 {`age`:55, `happy`:"Yes!", `name`:"A"})
create (_1 {`name`:"B"})
create _0-[:`KNOWS`]->_1
create _0-[:`BLOCKS`]->_1
Query:
MATCH p=(a { name: "A" })-[r]->(b)
RETURN *
JSON RESPONSE BODY:
{
"results": [
{
"columns": [
"a",
"b",
"p",
"r"
],
"data": [
{
"row": [
{
"name": "A",
"age": 55,
"happy": "Yes!"
},
{
"name": "B"
},
[
{
"name": "A",
"age": 55,
"happy": "Yes!"
},
{},
{
"name": "B"
}
],
{}
]
},
{
"row": [
{
"name": "A",
"age": 55,
"happy": "Yes!"
},
{
"name": "B"
},
[
{
"name": "A",
"age": 55,
"happy": "Yes!"
},
{},
{
"name": "B"
}
],
{}
]
}
]
}
],
"errors": []
}
As you can see, I get an empty object for r, which makes it impossible to distinguish between the relationships.
NOTE: I am running Neo4J v.2.2.2
Use the type() function.
MATCH (n)-[r]-(m) RETURN type(r);
Added distinct.
MATCH (n)-[r]-(m) RETURN distinct type(r);

Mongo query in Object of arrays

Document example
{
"_id": 1,
"test": {
"item_obj": {
"item1": ["a", "b"],
"item2": ["c"],
"item3": ["a", "d"]
}
}
}
I want to fetch documents where "a" exists in test.item_obj. "a" may exist in any array. And we don't know the keys present inside item_obj (No idea item1, item2 or item3 exists).
Need rails-mongo query.
If this is your search case, then whatever way you look at it you need the JavaScript evaluation of the $where clause to resolve your current structure. In the shell example ( since you need to use the JavaScript expression anyway ):
db.collection.find(function() {
var root = this.test.item_obj;
return Object.keys(root).some(function(key) {
return root[key] == "a";
});
})
Or for mongoid that is something like:
func = <<-eof
var root = this.test.item_obj;
return Object.keys(root).some(function(key) {
return root[key] == "a";
});
eof
Model.for_js(func)
However, if you simply change your structure to define "items_objects" as an array as follows:
{
"_id": 1,
"test": {
"item_objects": [
{ "name": "item1", "data": ["a","b"] },
{ "name": "item2", "data": ["c"] },
{ "name": "item3", "data": ["a","d"] }
}
}
}
Then asking for what you want here is as basic as:
db.collection.find({
"test.item_objects.data": "a"
})
Or for mongoid:
Model.where( "test.item_objects.data" => "a" )
Nested arrays are not really a great idea though, so perhaps live with:
{
"_id": 1,
"test": {
"item_objects": [
{ "name": "item1", "data": "a" },
{ "name": "item1", "data": "b" },
{ "name": "item2", "data": "c" },
{ "name": "item3", "data": "a" },
{ "name": "item3", "data": "d" }
}
}
}
Which is basically the same thing, but a but more long winded. But ultimately much more easy to deal with in atomic updates. And of course the query to find the values in the document is exactly the same.

Resources