13 Replies Latest reply on Mar 11, 2016 11:55 AM by Gustavo del Gerbo

    Using CMDBGraphQuery from the CMDB Java API

      Share This:

      Is there an example of how to use CMDBUtil.CMDBGraphQuery(....) ?


      The last parameter is a CMDBGraph[] and all the JavaDoc says is "input graph to query on".  I have no idea what this means, though.


      Does anyone know what is supposed to go in this parameter?



        • 1. Re: Using CMDBGraphQuery from the CMDB Java API
          Carey Walker

          The graph query is very poorly understood and even worse, poorly documented as you have seen.


          We did a POC back in CMDB V1.x days (the graph query was available back then too), where the customer wanted to see how to use this mechanism so that they could generically walk the CI relationship hierarchy from other tools (back then via the C API probably). We didn't have the CI relationship viewer then, so they were looking at ways of creating something similar (but much more basic).


          After struggling with the lack of doco, we managed to get a result using the CMDBDRIVER command line utility, which has a graph query front end as well.


          The required input parameters are obviously similar to the C and Java API, but I can't recall if we were obliged to supply the 'input graph to query on' parameter for that or not (sorry). We did reach a point where we were able to run the graph query from this command line interface, enter approrpiate responses to the prompts, and have it 'walk' the relationship tree from where we told it to start, to the required end point. In simple terms, we created a series of dependency relationships between various CIs (e.g. A -> B -> C-> D) and the requirement from the customer was to enter the Instance ID for A and have the graph query walk the (dependency) tree until it got to the end, and out popped the instance ID for D. Once we figured out what each prompted variable was (it wasn't too heard for most of them) we were able to apply it to any similar scenario (e.g. A -> B -> C -> E -> F) and it still gave the right answer, F in this case.


          The graphquery utlility was asking for things like starting class and instance, relationship class to walk, direction to walk etc etc and whether to stop along the way or only when the end was reached and so on.


          I am sorry I can;t recall any more details than this, but if you haven't already done so, take a look at the CMDB V2.x or V7.x Developer's Reference Guide. It has a whole chapter on the graphquery concepts and usage.


          Good luck.

          • 3. Re: Using CMDBGraphQuery from the CMDB Java API

            Also, there is pretty detailed information on the graph structure in the Developers Reference Guide.It is called 'Using the CMDBGraphQuery function'. There are some diagrams showing you couple examples of what the input graph can look like, etc.

            • 4. Re: Using CMDBGraphQuery from the CMDB Java API



              Thanks for sharing your experiences.  I share your pain.


              I've read all the documentation that BCM provides for the Java API, and I've found it to be pretty worthless when it comes to using CMDBGraphQuery.  It sounds like you have to input the result you expect in the last parameter (graphArray).

              • 5. Re: Using CMDBGraphQuery from the CMDB Java API



                I've read your docs on the CMDB Java API and they're worthless.  I've seen the nice pictures, but they don't give me any idea on how to use CMDBGraphQuery().


                Do you know what the last parameter, graphArray, is?  The Javadocs say "input graph to query on".  What does that mean?


                Answers are badly needed.  RTFM is a mindless response.

                1 of 1 people found this helpful
                • 6. Re: Using CMDBGraphQuery from the CMDB Java API

                  Hi Dean,


                  Perhaps some example code will help. here's a simple example illustrating a use case where you start from a particular instanceId, and walk all the connected base relationship instances. Then, from those connected endpoint instances, each will again walk more base relationship instances till there are no more connections.


                      protected static void graphQueryWork(ARServerUser context,
                                                           CMDBClassNameKey startClassName,
                                                           String startInstId){


                          CMDBClassNameKey baseClassKey = new CMDBClassNameKey("BMC_BaseElement", "BMC.CORE");
                          CMDBClassNameKey relClassKey = new CMDBClassNameKey("BMC_BaseRelationship", "BMC.CORE");
                      // create a query graph with only 1 graph element with 1 adjacency in the graph element
                      // (Walk all the Base Relationship instances from the starting instanceId)


                      // first, create the adjacency list
                          CMDBGraphAdjacency adjacency1 = new CMDBGraphAdjacency(relClassKey, baseClassKey);
                          String[] attNames = {"Name"};
                          adjacency1.setRelationAttribute(attNames); // retrieve Name attribute for all the relationship instances returned
                          adjacency1.setObjectAttribute(attNames); // retrieve Name attribute for all the CI instances returned
                          CMDBGraphAdjacency[] adjacencyList = {adjacency1};
                      // create the query graph, which contains the adjacency list
                          CMDBGraph graph1 = new CMDBGraph(startClassName, "", adjacencyList);
                          CMDBGraph[] graphArray = {graph1};


                          CMDBGraphResult graphResult = null;
                              graphResult = CMDBUtil.CMDBGraphQuery(context, startClassName, "", startInstId,
                                                      2, CMDBUtil.CMDB_RELATIONSHIP_DIRECTION_OUT,
                                                      true, true, graphArray);


                              System.out.println("Successfully performed Graph Query");
                          }catch(ARException ex){
                              System.out.println("Failed to perform Graph Query");

                  • 7. Re: Using CMDBGraphQuery from the CMDB Java API

                    If you are referring to Appendix B, I've seen it.  It has nice pictures, but no actual examples.  It gives only an abstract sense of what a graph query is.  In addition to some abstract understanding I need something that works.

                    • 8. Re: Using CMDBGraphQuery from the CMDB Java API

                      Yes, this is helpful.  It's still a little abstract, though.


                      What would really help would be a domain-specific example like this using CI and Relationship subclasses instead of the BMC base classes.  The example should include CIs and Relationships from some example domain so the code relates to something we know about (invoices to customers, or some other domain we understand.).


                      Concreteness is what is lacking in the documentation.

                      • 9. Re: Using CMDBGraphQuery from the CMDB Java API

                        Your original question is pretty generic, that's why a generic example was provided.


                        I'd highly recommend you to try out the sample provided to get a feel of the type of data it returns you and how the query works. If you wish to walk different relationships or endpoints, you simply replace BaseElement with the class you are interested in and BaseRelationship with the relationship you want to walk. If you wish to walk couple different types of relationships from a common class type, then in the adjacency list, you would provide a list of relationships rather than just one type of relationship as the example does.

                        • 10. Re: Using CMDBGraphQuery from the CMDB Java API

                          Hi Dean Schulze, hi all,


                          Do you have resolved your issue? Is it possible to use the CMDBGraphQuery and work with in a java program?

                          Have you successfully printed the result?


                          Thanks in advance for your reply


                          Best regards


                          • 11. Re: Using CMDBGraphQuery from the CMDB Java API

                            There is a fairly good example of using a graphwalk query int the CMDBGraphWalkQuery Java API documentation. I have included it here:


                            public class CMDBGraphWalkQueryextends java.lang.Objectimplements java.io.Serializable

                            The CMDBGraphWalkQuery class defines the query to be used for a graph walk. You can specify the dataset ID, dataset mask, the query mode (such as how much of the graph to walk), the number of levels to walk, the maximum number of objects to retrieve, the relationships to walk (as defined by a CMDBGraphWalkRelationList object, and the search criteria (as defined by a CMDBGraphWalkSelectorAndFilterList object.
                            Use a graph walk to retrieve CI and relationship instances when you do not know the exact relationship path to follow, for example, to view all of the dependencies for an instance of a computer system.
                            To successfully retrieve instances using a graph walk, you need to first specify the start node using the CMDBGraphWalkBegin() method of the CMDBUtil class, and then use the CMDBGraphWalkNext() method of CMDBUtil to loop until you retrieve all of the rows that match the query.
                            Chunking occurs when the maxRetrieve parameter has a value greater than 0. Otherwise, the query returns all the nodes in the first call. For example, if the query retrieves 500 rows of data and the maxRetrieve parameter is set to 100, the CMDBGraphWalkNext() method will return 100 nodes in the first call, then 100 nodes in the next call, and so on until all data has been returned.
                            The following example starts with instance ID AG0019B926E5AE3vO1SA1xwBAAwVcA of the BMC_ComputerSystem data-model class, and queries for the name of every computer system with a BMC_Dependency relationship to that instance. The related instances can be either source or destination instances, as specified by CMDB_RELATIONSHIP_DIRECTION_BOTH. The query returns records in chunks of 50, as specified by query.setMaxRetrieve(50). The query returns instances one level from the starting instance, as specified by query.setNumLevels(1), so the query returns only computer systems with a direct relationship to the starting instance. Note the looping CMDBUtil.CMDBGraphWalkNext call that returns successive chunks of data.

                             public static void graphWalkTest(ARServerUser context){
                              String startInstanceId = "AG0019B926E5AE3vO1SA1xwBAAwVcA";
                            List attrList = new ArrayList(); attrList.add("Name");
                              CMDBGraphWalkQuery query = new CMDBGraphWalkQuery("BMC.ASSET");
                            CMDBGraphWalkRelation relation = new CMDBGraphWalkRelation(); CMDBClassNameKey relClassNameId = new CMDBClassNameKey("BMC_Dependency","BMC.CORE");
                              relation.setDirection(CMDBGraphWalkRelation.CMDB_RELATIONSHIP_DIRECTION_BOTH);   relation.setRelationshipClassNameId(relClassNameId);   relation.setQualification(new QualifierInfo());   relation.setGetAttributeList(attrList);
                            CMDBGraphWalkRelationList relList = new CMDBGraphWalkRelationList();
                              relList.setAttributeFlag   (CMDBGraphWalkRelationList.CMDB_GRAPH_WALK_ATTRIBUTE_FLAG_DEFAULT_ALL);   relList.add(relation);
                            CMDBClassNameKey classNameId = new CMDBClassNameKey("BMC_ComputerSystem","BMC.CORE"); CMDBGraphWalkSelectorAndFilter sf = new CMDBGraphWalkSelectorAndFilter(classNameId); sf.setAttributes(attrList); CMDBGraphWalkSelectorAndFilterList sfList = new CMDBGraphWalkSelectorAndFilterList(); sfList.add(sf);
                              query.setDatasetMask(0);   query.setMaxRetrieve(50);   query.setNumLevels(1);   query.setObjSelectorAndFilterList(sfList);
                            query.setQueryMode(CMDBGraphWalkQuery.CMDB_GRAPH_WALK_WALK_COMPLETE_GRAPH); query.setWalkRelationList(relList); CMDBGraphWalkState state = null; CMDBGraphWalkOutput output = null;
                              state = CMDBUtil.CMDBGraphWalkBegin(context,classNameId,startInstanceId,query);
                            while(state.hasNext()){ output = CMDBUtil.CMDBGraphWalkNext(context, state); ... state = output.getGraphWalkState(); }}
                              state = CMDBUtil.CMDBGraphWalkEnd(context, state);
                            • 12. Re: Using CMDBGraphQuery from the CMDB Java API

                              I am still looking for a Developers Guide for the Java API.  All I can find is the BMCAtriumCore7604DevelopersReferenceGuide.pdf - which is just the C API.