You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Luis Miguel Silva <lu...@gmail.com> on 2011/04/04 00:55:46 UTC

Views question? (entity relationship)

Dear all,

I'm having some trouble defining a view with entity relationships.

Here is an example on what i need to do:
{
   "_id": "node01",
   "_rev": "2-eec9e84f8146d05f8116a5e064cf87e9",
   "type": "node",
   "amemory": 14543,
   "cmemory": 49118,
   "cproc": 16,
   "cpuload": 2.379,
   "netaddress": "10.40.130.146",
   "power": "on",
   "primary_state": "UP",
   "secondary_state": "Unknown:sshd",
   "os": "esxi4-stateless",
   "alias": "node01",
   "vlans": "[vlan611]",
   "variables": [
       {
           "provision_status": 2
       },
       {
           "another_variable": "something"
       }
   ],
   "aproc": 16,
   "vmcount": 6,
   "cpupercent": 14.8677794448612,
   "acores": 8,
   "ccores": 8,
   "threads": 16,
   "cpuclock": 2666,
   "numvms": 13,
   "hvtype": "esx",
   "htenabled": true
}

And then...
{
   "_id": "esxi4-stateless",
   "_rev": "2-fe0aeb05ac93d5f5a08d4befc68f10a6",
   "type": "os",
   "oslist": [
       "SOE-RHEL-5_5-2H10_0-Statelite-pm",
       "esxi4-stateless",
       "rhel53-statelite-pm",
       "rhel54-statelite-pm"
   ],
   "vmoslist": [
       "rhel48-stateful",
       "rhel53-stateful",
       "rhel53-stateful-cluster",
       "rhel54-stateful",
       "SOE-RHEL-5_5-2H10_0-Stateful",
       "rhel54-stateful-cluster",
       "rhel53-statelite-vm",
       "rhel54-statelite-vm",
       "SOE-RHEL-5_5-2H10_0-Statelite-vm",
       "win2k3-stateful-32-std",
       "win2k3-stateful-32-ent",
       "win2k3-stateful-32-ent-cluster",
       "win2k3-stateful-64-std",
       "win2k3-stateful-64-ent",
       "win2k3-stateful-64-ent-cluster",
       "win2k8-stateful-64-std",
       "win2k8-stateful-64-ent",
       "win2k8-stateful-64-ent-cluster",
       "win2k8R2-stateful-64-std",
       "win2k8R2-stateful-64-ent",
       "win2k8R2-stateful-64-ent-cluster",
       "P2V-WIN,P2V-RHEL"
   ]
}

And my view looks like:
function(doc) {
	if (doc.type == "node")
	{
		emit(doc._id,
			{
				node: doc._id,
				STATE: doc.secondary_state,
				OS: doc.oslist,
				ALIAS: doc.alias,
				FEATURE: doc.vlans,
				"GMETRIC[numvms]": doc.numvms,
				NETADDR: doc.netaddress,
				VARATTR: { "HVTYPE":doc.hvtype},
				VARIABLE: doc.variables,
			}
		);
	}
}

So my question is, how do i get the oslist and vmoslist from the
document whose _id = my register's doc.os key ?

I basically need to understand how i can query the entire database,
getting all the nodes and, while i'm querying all the nodes, get the
appropriate oslist and vmoslist (so i do not have to store these
fields more than once since they only vary based on "os" key).

I've been reading the EntityRelationship wiki page
(http://wiki.apache.org/couchdb/EntityRelationship) but i really do
not understand how can i map without specifying a key.
In the "phone directory example", i would have to specify the
individual key i want to get in order to map relations between
documents and that's not what i want to do.

Any thoughts?

Thank you,
Luis

Re: Views question? (entity relationship)

Posted by Luis Miguel Silva <lu...@gmail.com>.
Dave,

That is an interesting example and the include_docs=true flag is cool
but i really need the ability to specify what my field names are
(unfortunately, i can't just return doc.something, i have to return it
as, for instance, GMETRIC[my_something]=doc.something).

Also, i couldn't get it to work even without remapping the document
fields :o\...

function(doc) {
	if (doc.type == "node")
	{
		emit(doc._id,
			{
				node: doc._id,
				STATE: doc.secondary_state,
				OS: doc.oslist,
				ALIAS: doc.alias,
				FEATURE: doc.vlans,
				"GMETRIC[numvms]": doc.numvms,
				NETADDR: doc.netaddress,
				VARATTR: { "HVTYPE":doc.hvtype},
				VARIABLE: doc.variables,
				OSLIST: doc.oslist,
				VMOSLIST: doc.vmoslist,
			}
		);
		if(doc.os) {
			doc.os.forEach(
				function(member) {
					emit(member, {_id: member});
				}
			);
		}
	}
}

It just doesn't return anything when i add that function() code :o|||.

Any thoughts?

Thank you,
Luis

On Sun, Apr 3, 2011 at 5:13 PM, Dave Cottlehuber <da...@muse.net.nz> wrote:
> On 4 April 2011 10:55, Luis Miguel Silva
> <lu...@gmail.com> wrote:
>> Dear all,
>>
>> I'm having some trouble defining a view with entity relationships.
>>
>> Here is an example on what i need to do:
>> {
>>   "_id": "node01",
>>   "_rev": "2-eec9e84f8146d05f8116a5e064cf87e9",
>>   "type": "node",
>>   "amemory": 14543,
>>   "cmemory": 49118,
>>   "cproc": 16,
>>   "cpuload": 2.379,
>>   "netaddress": "10.40.130.146",
>>   "power": "on",
>>   "primary_state": "UP",
>>   "secondary_state": "Unknown:sshd",
>>   "os": "esxi4-stateless",
>>   "alias": "node01",
>>   "vlans": "[vlan611]",
>>   "variables": [
>>       {
>>           "provision_status": 2
>>       },
>>       {
>>           "another_variable": "something"
>>       }
>>   ],
>>   "aproc": 16,
>>   "vmcount": 6,
>>   "cpupercent": 14.8677794448612,
>>   "acores": 8,
>>   "ccores": 8,
>>   "threads": 16,
>>   "cpuclock": 2666,
>>   "numvms": 13,
>>   "hvtype": "esx",
>>   "htenabled": true
>> }
>>
>> And then...
>> {
>>   "_id": "esxi4-stateless",
>>   "_rev": "2-fe0aeb05ac93d5f5a08d4befc68f10a6",
>>   "type": "os",
>>   "oslist": [
>>       "SOE-RHEL-5_5-2H10_0-Statelite-pm",
>>       "esxi4-stateless",
>>       "rhel53-statelite-pm",
>>       "rhel54-statelite-pm"
>>   ],
>>   "vmoslist": [
>>       "rhel48-stateful",
>>       "rhel53-stateful",
>>       "rhel53-stateful-cluster",
>>       "rhel54-stateful",
>>       "SOE-RHEL-5_5-2H10_0-Stateful",
>>       "rhel54-stateful-cluster",
>>       "rhel53-statelite-vm",
>>       "rhel54-statelite-vm",
>>       "SOE-RHEL-5_5-2H10_0-Statelite-vm",
>>       "win2k3-stateful-32-std",
>>       "win2k3-stateful-32-ent",
>>       "win2k3-stateful-32-ent-cluster",
>>       "win2k3-stateful-64-std",
>>       "win2k3-stateful-64-ent",
>>       "win2k3-stateful-64-ent-cluster",
>>       "win2k8-stateful-64-std",
>>       "win2k8-stateful-64-ent",
>>       "win2k8-stateful-64-ent-cluster",
>>       "win2k8R2-stateful-64-std",
>>       "win2k8R2-stateful-64-ent",
>>       "win2k8R2-stateful-64-ent-cluster",
>>       "P2V-WIN,P2V-RHEL"
>>   ]
>> }
>>
>> And my view looks like:
>> function(doc) {
>>        if (doc.type == "node")
>>        {
>>                emit(doc._id,
>>                        {
>>                                node: doc._id,
>>                                STATE: doc.secondary_state,
>>                                OS: doc.oslist,
>>                                ALIAS: doc.alias,
>>                                FEATURE: doc.vlans,
>>                                "GMETRIC[numvms]": doc.numvms,
>>                                NETADDR: doc.netaddress,
>>                                VARATTR: { "HVTYPE":doc.hvtype},
>>                                VARIABLE: doc.variables,
>>                        }
>>                );
>>        }
>> }
>>
>> So my question is, how do i get the oslist and vmoslist from the
>> document whose _id = my register's doc.os key ?
>>
>> I basically need to understand how i can query the entire database,
>> getting all the nodes and, while i'm querying all the nodes, get the
>> appropriate oslist and vmoslist (so i do not have to store these
>> fields more than once since they only vary based on "os" key).
>>
>> I've been reading the EntityRelationship wiki page
>> (http://wiki.apache.org/couchdb/EntityRelationship) but i really do
>> not understand how can i map without specifying a key.
>> In the "phone directory example", i would have to specify the
>> individual key i want to get in order to map relations between
>> documents and that's not what i want to do.
>>
>> Any thoughts?
>>
>> Thank you,
>> Luis
>>
>
> Hi Luis,
>
> Jan's post should sort you out here
> http://blog.couchbase.com/whats-new-in-apache-couchdb-0-11-part-two-views
>
> A+
> Dave
>

Re: Views question? (entity relationship)

Posted by Dave Cottlehuber <da...@muse.net.nz>.
On 4 April 2011 10:55, Luis Miguel Silva
<lu...@gmail.com> wrote:
> Dear all,
>
> I'm having some trouble defining a view with entity relationships.
>
> Here is an example on what i need to do:
> {
>   "_id": "node01",
>   "_rev": "2-eec9e84f8146d05f8116a5e064cf87e9",
>   "type": "node",
>   "amemory": 14543,
>   "cmemory": 49118,
>   "cproc": 16,
>   "cpuload": 2.379,
>   "netaddress": "10.40.130.146",
>   "power": "on",
>   "primary_state": "UP",
>   "secondary_state": "Unknown:sshd",
>   "os": "esxi4-stateless",
>   "alias": "node01",
>   "vlans": "[vlan611]",
>   "variables": [
>       {
>           "provision_status": 2
>       },
>       {
>           "another_variable": "something"
>       }
>   ],
>   "aproc": 16,
>   "vmcount": 6,
>   "cpupercent": 14.8677794448612,
>   "acores": 8,
>   "ccores": 8,
>   "threads": 16,
>   "cpuclock": 2666,
>   "numvms": 13,
>   "hvtype": "esx",
>   "htenabled": true
> }
>
> And then...
> {
>   "_id": "esxi4-stateless",
>   "_rev": "2-fe0aeb05ac93d5f5a08d4befc68f10a6",
>   "type": "os",
>   "oslist": [
>       "SOE-RHEL-5_5-2H10_0-Statelite-pm",
>       "esxi4-stateless",
>       "rhel53-statelite-pm",
>       "rhel54-statelite-pm"
>   ],
>   "vmoslist": [
>       "rhel48-stateful",
>       "rhel53-stateful",
>       "rhel53-stateful-cluster",
>       "rhel54-stateful",
>       "SOE-RHEL-5_5-2H10_0-Stateful",
>       "rhel54-stateful-cluster",
>       "rhel53-statelite-vm",
>       "rhel54-statelite-vm",
>       "SOE-RHEL-5_5-2H10_0-Statelite-vm",
>       "win2k3-stateful-32-std",
>       "win2k3-stateful-32-ent",
>       "win2k3-stateful-32-ent-cluster",
>       "win2k3-stateful-64-std",
>       "win2k3-stateful-64-ent",
>       "win2k3-stateful-64-ent-cluster",
>       "win2k8-stateful-64-std",
>       "win2k8-stateful-64-ent",
>       "win2k8-stateful-64-ent-cluster",
>       "win2k8R2-stateful-64-std",
>       "win2k8R2-stateful-64-ent",
>       "win2k8R2-stateful-64-ent-cluster",
>       "P2V-WIN,P2V-RHEL"
>   ]
> }
>
> And my view looks like:
> function(doc) {
>        if (doc.type == "node")
>        {
>                emit(doc._id,
>                        {
>                                node: doc._id,
>                                STATE: doc.secondary_state,
>                                OS: doc.oslist,
>                                ALIAS: doc.alias,
>                                FEATURE: doc.vlans,
>                                "GMETRIC[numvms]": doc.numvms,
>                                NETADDR: doc.netaddress,
>                                VARATTR: { "HVTYPE":doc.hvtype},
>                                VARIABLE: doc.variables,
>                        }
>                );
>        }
> }
>
> So my question is, how do i get the oslist and vmoslist from the
> document whose _id = my register's doc.os key ?
>
> I basically need to understand how i can query the entire database,
> getting all the nodes and, while i'm querying all the nodes, get the
> appropriate oslist and vmoslist (so i do not have to store these
> fields more than once since they only vary based on "os" key).
>
> I've been reading the EntityRelationship wiki page
> (http://wiki.apache.org/couchdb/EntityRelationship) but i really do
> not understand how can i map without specifying a key.
> In the "phone directory example", i would have to specify the
> individual key i want to get in order to map relations between
> documents and that's not what i want to do.
>
> Any thoughts?
>
> Thank you,
> Luis
>

Hi Luis,

Jan's post should sort you out here
http://blog.couchbase.com/whats-new-in-apache-couchdb-0-11-part-two-views

A+
Dave