UCMDB and UD Practitioners Forum (Previously CMS)
cancel

UCMDB API CIs to Record Set

Highlighted
rp83
Regular Contributor.

UCMDB API CIs to Record Set

Hi,

I am trying to pull the information from UCMDB using the api(Version 8). This extract needs to be stored in a database. I am able to connect to the server and then iterate thru each of the topology. But all topology CI's have 0 properties.

Please let me know how to read the Topology to convert it to a recorset. I wanted somthing like the way we export it to CSV, so we can store it as a database base.

 

 

P.S. This thread has been moevd from Application Perf Mgmt (BAC / BSM) Support and News Forum to CMS and Discovery Support and News Forum. - Hp Forum Moderator

18 REPLIES
Amit Erental
Acclaimed Contributor.

Re: UCMDB API CIs to Record Set

Hi,

I guess you meant you want to get the properties values for the returned CIs

See the example in the doc (Topology Query Example) and also in the QueryDefinition and QueryElement interfaces doc.

In this post you can find where the java doc is located : http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=1441023

Amit
rp83
Regular Contributor.

Re: UCMDB API CIs to Record Set

Hi Amit,

As you said I am looking for the properties of the CI's.

I am able to loop thru the CI's but when I print the properties its all null within the CI's, the document you mentioned I went thru seems like that works for the 7.5 version.

But for the 8.02 version, its not compiling with the API jar given.

Pls let me know if I am missing anything,
Amit Erental
Acclaimed Contributor.

Re: UCMDB API CIs to Record Set

Hi,

Between 7.5 and 8.02 the new Java API was introtuced, see the Solutions and integrations help on 8.02.

Which API you are try to use , the Java or the Web API ? Try to attach the code/errors you have.

Amit
rp83
Regular Contributor.

Re: UCMDB API CIs to Record Set

I went thru the TQL Samples given, The sample seems to reffer to the 7.5 version I guess., as when I tried that didn't comple with the api-jar given.

I am working on the Java api.

I have attached the sample code., I dont see any error, but its not printing the value as I see in the TQL result set when I export it to CSV. I wanted to execute the query and store the result set in a different database.

The no of lines after the export excluding the headers and the count of collections are same. but when I print the properties on each of the collection I dont see any value its null.

Thanks for helping me., Pls suggest your ideas, or if you have any sample code.
Amit Erental
Acclaimed Contributor.

Re: UCMDB API CIs to Record Set

Hi,

The examples (you should use the one from page 406 on the pdf) are for 8.0 and up, and shuold complie and run with no issues with the correct jar.

make sure you follow this step:

Retrieve the API Jar File
Get the ucmdb-api.jar from a BAC or UCMDB server installation. Extract the
jar file from ucmdb-api.war, found in the AppServer\webapps directory on
the server. Inside the war archive, ucmdb-api.jar is located in
theWEB-INF\lib directory.
Compile and run your application with ucmdb-api.jar in the classpath.



rp83
Regular Contributor.

Re: UCMDB API CIs to Record Set

Hi Amit,

As you suggested I saw the examples,and was able to see the ClassDefinition.

The problem I am facing is the executeNamedQuery returns Collection of type Topology, the type displays as "software" or "nt" or "unix" for different topology.

how do I read the attributes on these??

I downloaded some PDF and and trying it on my own. I donnot have UCMDB installed on my mac, its from a different team, so I am not able to get the files from the server as requested.
Amit Erental
Acclaimed Contributor.

Re: UCMDB API CIs to Record Set

Hi

Use getCIsByName and getPropertyValue methods, as seen below:

TopologyQueryService queryService =
ucmdbService.getTopologyQueryService();
TopologyQueryFactory queryFactory =
queryService.getFactory();
QueryDefinition queryDefinition =
queryFactory.createQueryDefinition
("Get hosts with more than one network interface");
String hostNodeName = "Host";
QueryNode hostNode =
queryDefinition.addNode(hostNodeName).ofType("host").queryProperty("display_l
abel");
QueryNode ipNode =
queryDefinition.addNode("IP").ofType("ip").queryProperty("ip_address");
hostNode.linkedTo(ipNode).withLinkOfType("contained").atLeast(2);
Topology topology = queryService.executeQuery(queryDefinition);
Collection hosts = topology.getCIsByName(hostNodeName);
for (TopologyCI host : hosts) {
System.out.println("Host " + host.getPropertyValue("display_label"));
for (TopologyRelation relation : host.getOutgoingRelations()) {
System.out.println
(" has IP " + relation.getEnd2CI().getPropertyValue("ip_address"));
}
}


this is from Topology javadoc:

Result of a topology query execution.
You can retrieve CIs and relations from this by query node name or as a flat collection. If each CI matches exactly one node, the two techniques give the same result. If a CI matches more than one node, the topology structure holds n+1 instances of the CI where n is the number of nodes that contain the CI. When a CI is retrieved from the collection returned by getCIsByName(java.lang.String), its properties and relations are defined by the node with the specified name. When a CI is retrieved from the collection returned by getAllCIs(), the CI has all of the of the queried properties and connected relations of all instances of that CI.

Similarly, when a Relations relation is matched by more than one query link node, the relation appears in this structure more than once.

When traversing the resulting graph with TopologyCI.getOutgoingRelations() and TopologyCI.getIncomingRelations(), the relations are consistent with the connected CIs. If the CI is taken from a node, the relation will be the one connected to that node. When the CI is taken from the collection of all CIs, it is associated with all the relations to which the CI is connected from all nodes.

Examples:

List all hosts in uCMDB:

QueryDefinition queryDefinition = factory.createQueryDefinition("Get all hosts");
queryDefinition.addNode("Hosts").ofType("host").queryProperty("host_dnsname");
Topology topology = topologyQueryService.executeQuery(queryDefinition);
for (TopologyCI host : topology.getAllCIs()) {
System.out.println("Host key:" + host.getPropertyValue("host_dnsname");
}
List all hosts with their contained resources
QueryDefinition queryDefinition = factory.createQueryDefinition("Get hosts' resources");
String hostsNodeName = "Hosts";
QueryNode hostsNode = queryDefinition.addNode(cisNodeName).ofType("host");
QueryNode resourcesNode = queryDefinition.addNode("Resources").ofType("hostresource").queryKeyProperties();
hostsNode.linkedTo(resourcesNode).withLinkOfType("container_f");
Topology topology = topologyQueryService.executeQuery(queryDefinition);
for (TopologyCI host : topology.getCIsByName(hostsNodeName)) {
System.out.println("Host:");
printElement(host);
for (Relation relation : host.getOutgoingRelations()) {
System.out.println("\tResource:");
printElement(relation.getEnd2CI(), "\t");
}
}
rp83
Regular Contributor.

Re: UCMDB API CIs to Record Set

Thanks for the excellent reply!!!

I did work on this example and these examples work great., and prints the result as expected.

For my request, the UCMDB team had created a TQL query, which I am able to access from the UCMDB Reports(front-end).

I wanted to execute that perticular query and retrive the result. I am able to execute the query and iterate thru the loop, but I am not able to print the different columns what I see in the report via front-end.

Topology returned prints only 2 values, UCMDBID and the type, rest all other methos either returns a null or blank string("").

In your reply you mentioned about the Flat collection, not sure if this is what is happening to my queries. Please let me know if you have any suggestions for reading the flat collections.

Thanks,
Amit Erental
Acclaimed Contributor.

Re: UCMDB API CIs to Record Set

Hi,

Make sure you specified the properties go get with the query definition, for example:

Interface QueryDefinition

--------------------------------------------------------------------------------

public interface QueryDefinitionDefinition of a uCMDB TQL query.

Examples of simple queries:

Query CIs by list of IDs
QueryDefinition queryDefinition = topologyQueryFactory.createQueryDefinition("Get Hosts by IDs");
QueryNode queryNode = queryDefinition.addNode("Hosts").ofType("host").withIds(ids);
queryNode.queryPropertiesWithQualifier("COMPARABLE").queryProperties("host_key", "host_iscomplete");
Query CIs by their properties
QueryDefinition queryDefinition = factory.createQueryDefinition("Get Hosts");
QueryNode queryNode = queryDefinition.addNode("Hosts").ofType("host");
queryNode.property("host_key").like("T%").caseInsensitive().and(queryNode.property("host_iscomplete").isNull());
queryNode.queryKeyProperties();
Query CIs' neighbors
QueryDefinition queryDefinition = factory.createQueryDefinition("Get hosts' neighbors");
QueryNode cisNode = queryDefinition.addNode("CIs").ofType("host").withIdsOf(hosts);
QueryNode neighborsNode = queryDefinition.addNode("Neighbors").ofAnyType();
neighborsNode.queryKeyProperties();
cisNode.linkedTo(neighborsNode).withLinkOfAnyType();


Once you got the Topology, you need to get TopologyCI collection from it, and use getPropertyValue method on the TopologyCI

rp83
Regular Contributor.

Re: UCMDB API CIs to Record Set

Was wonderful, but I am not suppose to define the query in java, our team has to just pull data from already created TQLs from the respective team.

uCMDB team will create many such TQLs and we will just have to read the info, we are not suppose to change or create our own queries.

All I need is to do an export of the result set to a different table.,

Hope you undersatnd my limitation.
Amit Erental
Acclaimed Contributor.

Re: UCMDB API CIs to Record Set

Hi,

so you are using the executeNamedQuery method ?

How do you search for the properties values ? please make sure you are using the correct spelling and case sensitive. use the CI type manager (UCMDB admin) to check the attribute names (and not the display name)

Amit
Dmitry Shevchenko
HPE Expert

Re: UCMDB API CIs to Record Set

Navigate to uCMDB Query Manager and open a TQL you work with.

1. Select node whose attributes you are trying to get.
2. Right-click, select Node Properties.
3. Click "Advanced layout settings" button (upper left corner).
4. Select attributes you want and click OK.
rp83
Regular Contributor.

Re: UCMDB API CIs to Record Set

So that means, the way the UCMDB Report(front-end) fetches the result and the executeNamedQuery are different??

Using API will I be not able to produce the same result what we see in the report or when we do a export as CSV??

Sorry if this is very basic., I am very new to UCMDB
Amit Erental
Acclaimed Contributor.

Re: UCMDB API CIs to Record Set

Hi,

you need to specify which properties to fetch.

this can be done by 2 ways: as dmitry wrote above, by the query manager, or by adding the properties to the named query in the java code,but you will need to know the node name(s) for each. something like:

//get ExecutableQuery according to name
ExecutableQuery query = topologyQueryFactory.createNamedQuery(queryName).toExecutable());
// add properties to get
PropertiesToQuery props =query.nodePropertiesToQuery(nodename);
props.one of PropertiesToQuery class methods to add properties to get
// execute
....

rp83
Regular Contributor.

Re: UCMDB API CIs to Record Set

Excellent, I think now I understand., just to make sure, let me know my understanding.

In our example to for query to Get hosts with more than one network interface, lets say we created a TQL for this with Name as "Host_with_more_interface". with the properties display_label and ip_address. So these two columns appear in the result set from front-end.

now I can directly iterate running this query
"Host_with_more_interface" from java using API, and now since the properties are also set I can see them just as the way it is displayed in the reports?

Now I am not seeing them buz the query though had those columns didn't add the properties added.

Please let mek now if my understanding is correct?
Amit Erental
Acclaimed Contributor.

Re: UCMDB API CIs to Record Set

sound correct to me...
rp83
Regular Contributor.

Re: UCMDB API CIs to Record Set

Great, thanks a lot for your Help :)

let me work with that team to add these properties, as I dont have access to do., and will try once again.

manojfmbang
Super Contributor.

Re: UCMDB API CIs to Record Set

Hi All,

We are working on retrieving a CI ID using CI Name from RTSM.

Does anyone have a sample code which perfors this task?

Also, what is the property name to retrieve the CI ID from BSM's RTSM?

 

Thanks!