How to use eventing service to replicate subset of document to analytic dataset

Hi,

I want to use a eventing function to extract subset of document and replicate to analytic node.

document in data node:
{
“type”: “user”,
“first_name”: “Scott”,
“last_name”: “Tiger”,
“id”: 80927079070,
“address”: [
“home”: {
“street”: “123 first street”,
“city”: “Boston”,
“zip”: “02102”
},
“billing”: {
“street”: “541 third Street”,
“city”: “Boston”,
“zip”: “02102”
}
],
“phone”: [
“home”: “800-555-9201”,
“work”: “877-123-8811”,
“cell”: “878-234-8171”
]
}

I only want to extract subset of this document and replicate to analytic dataset like below.

{
“type”: “user”,
“first_name”: “Scott”,
“last_name”: “Tiger”,
“id”: 80927079070,
“address”: [
“home”: {
“street”: “123 first street”,
“city”: “Boston”,
“zip”: “02102”
}
],
“phone”: “877-123-8811”
}

How to use eventing service to achieve this?

Thanks,

Matthew

First you don’t appear to have legal JSON the address and phone arrays are bad.

I recommend something like

{
    "type": "user",
    "first_name": "Scott",
    "last_name": "Tiger",
    "id": 80927079070,
    "address": {
            "home": {
                "street": "123 first street",
                "city": "Boston",
                "zip": "02102"
            },

            "billing": {
                "street": "541 third Street",
                "city": "Boston",
                "zip": "02102"
            }

    },
    "phone": {
        "home": "800 - 555 - 9201",
        "work": "877 - 123 - 8811",
        "cell": "878 - 234 - 8171"
    }
}

with a desired output like the follwoing:

{
    "type": "user",
    "first_name": "Scott",
    "last_name": "Tiger",
    "id": 80927079070,
    "home": {
        "street": "123 first street",
        "city": "Boston",
        "zip": "02102"
    },
    "phone": "877 - 123 - 8811"
}

Now the Eventing Function can be pretty simple just create a new object copy what you want and write it back to Couchbase.

function OnUpdate(doc,meta) {
    if (doc.type !== "user") return;
    var newdoc = {};
    // copy the feilds you want
    newdoc.type = doc.type;
    newdoc.first_name = doc.first_name;
    newdoc.last_name = doc.last_name;
    newdoc.id = doc.id;
     // are you sure you want the home address and the work phone number
    newdoc.home = doc.address.home; // just select one
    newdoc.phone = doc.phone.work;  // just select one
    // use a r/w bucket alias named dst_bkt and write 
    // to another bucket with the same ID
    dst_bkt[meta.id] = newdoc;
}

Best

Jon Strabala
Principal Product Manager - Server‌

Hi Jon,

Thanks for your response. Is the “dst_bkt” in the function a new bucket or can it be a dataset of analytic service?

Thanks,

Matthew

If your version is recent 6.5 or above you can right back to the same bucket and in 7.X to a different or same collection in the same bucket. You might want to either overwrite the document in this case add a processed flag or perhaps change its type.

“dst_bk” is an alias set up in the function’s settings (a bucket binding to an existing bucket or collections) to use as in call an analytics query you would need to use curl() call and it’s REST API we don’t have direct integration like we do for SQK++ (aka N1QL)

Now I’m not sure (I don’t do analytics) but if you just write to a different bucket or collection (the analytics listens too or watches) via the bucket alias I think analytics will just pick up the mutation and then you just do those queries against the data outside of Eventing in Analytics.