How to do a jolt default operation with value fetched from a json path - jolt

{
"a": 1
}
Lets say this is my json. Since field b is not present, i want my jolt output to be like
{
"a":1,
"b":1
}
b's value should be fetched from a

You can easily achieve this using the modify-default-beta operator:
{
"operation": "modify-default-beta",
"spec": {
"b": "#(1,a)"
}
}
Basically you are defaulting b's value with a's content. If b has a value it wont overwrite it.

Related

How can I parse nested GenericRecord containing Union, Map, Array etc.?

I am having the genericRecord as well as the schema. I want to parse all the fields and append some text only to those values whose type is String.
Assume that schema is like this:
{
"type":"record",
"name":"MyRecord",
"fields":[
{
"name":"a",
"type":[
"null",
"int",
{
"type":"map",
"values":"string"
}
]
}
]
}
I don't want to write nested if else statements that I have already done and recursively called them. Is there any other efficient way ?

does it possible contains multi format document in one lift-mongo-recorder model?

I attempt build a model able to insert different field(only one field is different).
also, the field should be custom format and I will be a embed object.
document form Assembla use case class define embed field, so I can't use base class(trait/abstract class) to define under my recorder model.
hope insert different format, such as
{
_id: ...,
same_field: "s1",
specify_field: {
format1: "..."
}
}
{
_id: ...,
same_field: "s1",
specify_field: [
formatX: "..",
formatY: ".."
]
}
How to construct my model?
thanks

MongoDB - Document with different type of value

I'm very new to MongoDB, i tell you sorry for this question but i have a problem to understand how to create a document that can contain a value with different "type:
My document can contain data like this:
// Example ONE
{
"customer" : "aCustomer",
"type": "TYPE_ONE",
"value": "Value here"
}
// Example TWO
{
"customer": "aCustomer",
"type": "TYPE_TWO",
"value": {
"parameter1": "value for parameter one",
"parameter2": "value for parameter two"
}
}
// Example THREE
{
"customer": "aCustomer",
"type": "TYPE_THREE",
"value": {
"anotherParameter": "another value",
{
"someParameter": "value for some parameter",
...
}
}
}
Customer field will be even present, the type can be different (TYPE_ONE, TYPE_TWO and so on), based on the TYPE the value can be a string, an object, an array etc.
Looking this example, i should create three kind of collections (one for type) or the same collection (for example, a collection named "measurements") can contain differend kind of value on the field "value" ?
Trying some insert in my DB instance i dont get any error (i'm able to insert object, string and array on property value), but i would like to know if is the correct way...
I come from RDBMS, i'm a bit confused right now.. thanks a lot for your support.
You can find the answer here https://docs.mongodb.com/drivers/use-cases/product-catalog
MongoDB's dynamic schema means that each need not conform to the same schema.

How put index with mapping template to elastic search with elastic4s?

I want create index with dynamic template and turn analyzing off for string fields. I have created query for elastic search, but how to translate it into elastic4s statments? (version elastic4s 1.3.x is preffered)
The statement is:
PUT /myIndex
{
"mappings": {
"myType": {
"dynamic_templates": [
{
"templateName": {
"match": "*",
"match_mapping_type": "string",
"mapping": {
"type": "string",
"index" : "not_analyzed",
"omit_norms" : true
}
}
}
]
}}}
P.S.
May be it is possible to create this index by executing this "raw" request, but I did not find how to do that with elastic4s 1.3.4 :(
Elastic4s (as of 1.5.4) supports dynamic templates when creating indexes. So you can do something like:
val req = create.index("my_index").mappings(
"my_type" templates (
template name "es" matching "*_es" matchMappingType "string" mapping {
field withType StringType analyzer SpanishLanguageAnalyzer
},
template name "en" matching "*" matchMappingType "string" mapping {
field withType StringType analyzer EnglishLanguageAnalyzer
}
)
)
So the equivalent of the example you posted would be:
create.index("my_index").mappings(
"my_type" templates (
template name "templateName" matching "*" matchMappingType "string" mapping {
field typed StringType index NotAnalyzed omitNorms true
}
)
Sometimes it's easier to manage your mapping in raw JSON. You can put the raw JSON on a file to make it possible to be updated without need to rebuild your application. If you want to use this raw JSON to create the index you can do something like this:
client.execute {
create index "myIndex" source rawMapping
}
where rawMapping is the string with your raw JSON content.

Counting field names (not values) in mongo database

Is there a way to count field names in mongodb? I have a mongo database of documents with other embedded documents within them. Here is an example of what the data might look like.
{
"incident": "osint181",
"summary":"Something happened",
"actor": {
"internal": {
"motive": [
"Financial"
],
"notes": "",
"role": [
"Malicious"
],
"variety": [
"Cashier"
]
}
}
}
Another document might look like this:
{
"incident": "osint182",
"summary":"Something happened",
"actor": {
"external": {
"motive": [
"Financial"
],
"notes": "",
"role": [
"Malicious"
],
"variety": [
"Hacker"
]
}
}
}
As you can see, the actor has changed from internal to external in the second document. What I would like to be able to do is count the number of incidents for each type of actor. My first attempt looked like this:
db.public.aggregate( { $group : { _id : "$actor", count : { $sum : 1 }}} );
But that gave me the entire subdocument and the count reflected how many documents were exactly the same. Rather I was hoping to get a count for internal and a count for external, etc. Is there an elegant way to do that? If not elegant, can someone give me a dirty way of doing that?
Best option for this kind of problem is using map-reduce of mongoDB , it will allow you to iterate through all the keys of the mongoDB document and easily you can add your complex logic . Check out map reduce examples here : http://docs.mongodb.org/manual/applications/map-reduce/
This was the answer I came up with based on the hint from Devesh. I create a map function that looks at the value of actor and checks if the document is an empty JSON object using the isEmptyObject function that I defined. Then I used mapReduce to go through the collection and check if the action field is empty. If the object is not empty then rather than returning the value of the key, I return the key itself which will be named internal, or external, or whatever.
The magic here was the scope call in mapReduce which makes it so that my isEmptyObject is in scope for mapReduce. The results are written to a collection which I named temp. After gathering the information I want from the temp collection, I drop it.
var isEmptyObject = function(obj) {
for (var name in obj) {
return false;
}
return true;
};
var mapFunction = function() {
if (isEmptyObject(this.action)) {
emit("Unknown",1); }
else {
for (var key in this.actor) { emit(key,1); } } };
var reduceFunction = function(inKeys,counter) {
return Array.sum(counter); };
db.public.mapReduce(mapFunction, reduceFunction, {out:"temp", scope:{isEmptyObject:isEmptyObject}} );
foo = db.temp.aggregate(
{ $sort : { value : -1 }});
db.temp.drop();
printjson(foo)