Example: Using a paged query to retrieve a list of all objects in a namespace

Content Platform Tenant Management Help

Version
9.7.x
File Size
4269 KB
Audience
anonymous
Part Number
MK-95HCPH002-19

The Java® example below implements a paged query that uses multiple requests to retrieve all objects in a namespace. The example returns metadata for fifty objects per request and also returns information about the size and ingest time of each object in the result set.

This example uses the com.hds.hcp.apihelpers.query Java class infrastructure, which uses the Jackson JSON processor to produce a JSON query request body and consume a JSON query response. To limit the example size, the example does not include the source code for this infrastructure. To view the full source code, see http://community.hitachivantara.com/groups/developer-network-for-hitachi-content-platform and reference the sample code section.

The Jackson JSON processor serializes and deserializes JSON formatted content with Java Objects. For more information about the Jackson JSON processor, see http://jackson.codehaus.org.

package com.hds.hcp.examples;


import java.util.List;
import java.io.BufferedReader;
import java.io.InputStreamReader;


import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;


/* General purpose helper routines for samples */
import com.hds.hcp.apihelpers.HCPUtils;

 

/* Provide for helper routines to encapsulate the queryRequest and queryResults. */
import com.hds.hcp.apihelpers.query.request.Object;
import com.hds.hcp.apihelpers.query.request.QueryRequest;
import com.hds.hcp.apihelpers.query.result.Status;
import com.hds.hcp.apihelpers.query.result.QueryResult;
import com.hds.hcp.apihelpers.query.result.ResultSetRecord;

 

public class PagedObjectQuery {

    // Local member variables
    private Boolean bIsInitialized = false;
    private String sQueryTenant;
    private String sQueryNamespace;
    private String sEncodedUserName, sEncodedPassword;
    private String sHTTPQueryURL;

    /**
    * Initialize the object by setting up internal data and establishing the HTTP client.

    * connection.
    *
    * This routine is called by the ReadFromHCP and WriteToHCP routines, so calling it
    * by the consumer of this class is unnecessary.
    */
    void initialize(String inNamespace, String inUsername, String inPassword) throws
    Exception {

 

        if (! bIsInitialized) // Initialize only if we haven't already
        {
         // Break up the namespace specification to get the namespace and tenant parts.
         String parts[] = inNamespace.split("\\.");

 

        sQueryNamespace = parts[0];
         sQueryTenant = parts[1];

 

         // Now extract just the tenant part of the URL and use it to create the
         // HTTPQueryURL.
         parts = inNamespace.split(sQueryNamespace + "\\.");

        sHTTPQueryURL = "https://" + parts[1] + "/query";

 

        // Encode both the username and password for the authentication string.
        sEncodedUserName = HCPUtils.toBase64Encoding(inUsername);
        sEncodedPassword = HCPUtils.toMD5Digest(inPassword);

         // Set up an HTTP client for sample usage.
mHttpClient = HCPUtils.initHttpClient();

 

         bIsInitialized = true;
     }
  }

 

 /**
   * This method performs an orderly shutdown of the HTTP connection manager.
   */
  void shutdown() throws Exception {
    // Clean up open connections by shutting down the connection manager.
    mHttpClient.getConnectionManager().shutdown();
  }

 

  /**
    * This routine issues a query to an HCP namespace requesting information about
    * objects in it. The query requests 1,000 results at a time. If there are more,
    * the routine performs paged queries to retrieve all the results.
    *
    * While processing the query results, the routine displays the name of the first
    * and last object of the result set to system output.
    */
  protected void runQuery() {

 

   // Statistics counters
    Long TotalRecordsProcessed = 0L;
    Integer HTTPCalls = 0;

 

 try {
      /*
       * Set up the query request.
       */

     // Set up for an object query by calling the
      // com.hds.hcp.apihelpers.query.request.Object constructor.
      Object mObjQuery = new Object();

 

     // Get only 50 objects at a time.
      mObjQuery.setCount(50);

 

      // Retrieve only those that reside in the namespace specified in the command.
      mObjQuery.setQuery("+namespace:" + sQueryNamespace + "." + sQueryTenant);

     

     // Retrieve the "size" and "ingestTimeString" properties for the object.
      mObjQuery.setObjectProperties("size,ingestTimeString");

 

      // Set up the query request.
      QueryRequest mQuery = new QueryRequest(mObjQuery);

     /*
       * Loop through and process all the objects one response at a time or until
       * an error occurs.
       */
      QueryResult mQueryResult = null;
      do {
        System.out.println("Issuing query: \n" + mQuery.toString(true));

       /*
         * Execute the query using the HTTP POST method.
         */
        HttpPost httpRequest = new HttpPost(sHTTPQueryURL);

 

       // Add the body of the POST request.
        httpRequest.setEntity(new StringEntity(mQuery.toString()));

 

       // Set the Authorization header.
        httpRequest.setHeader("Authorization: HCP " + sEncodedUserName + ":"
          + sEncodedPassword);

 

       // Execute the query.
        HttpResponse httpResponse = mHttpClient.execute(httpRequest);

 

       // For debugging purposes, dump out the HTTP response.
       HCPUtils.dumpHttpResponse(httpResponse);

 

        // If the status code is anything BUT in the 200 range indicating success,
       // throw an exception.
       if (2 != (int)(httpResponse.getStatusLine().getStatusCode() / 100))
       {
           // Clean up after ourselves and release the HTTP connection to the
           // connection manager.
           EntityUtils.consume(httpResponse.getEntity());

 

          throw new HttpResponseException(httpResponse.getStatusLine()
          .getStatusCode(),
            "Unexpected status returned from " + httpRequest.getMethod() + " ("
             + httpResponse.getStatusLine().getStatusCode() + ": "
             + httpResponse.getStatusLine().getReasonPhrase() + ")");
        }

 

       /*
         *  Process the response from the query request.
        */

       // Put the response in a buffered reader.
       BufferedReader bodyReader = new BufferedReader(newInputStreamReader
         (httpResponse.getEntity().getContent()));
       HTTPCalls += 1;

 

       // Parse the response into the QueryResult object.
        mQueryResult = QueryResult.parse(bodyReader);

 

       // Get a copy of the query status from the query result.
       Status mStatus = mQueryResult.getStatus();

 

       // Display the status of what we just accomplished.
       System.out.println();
       System.out.println("Batch " + HTTPCalls + " Status: " + mStatus.getCode()
         + " Record Count:" + mStatus.getResults());

 

       // Display the first and last object of the result set.
       List<ResultSetRecord> mResultSet = mQueryResult.getResultSet();
        ResultSetRecord mFirstRecord = mResultSet.get(0);

 

       System.out.println(" First Record (" + (TotalRecordsProcessed+1) + ") "
          + mFirstRecord.getUrlName());
        System.out.println(" Size: " + mFirstRecord.getSize());

 

       TotalRecordsProcessed += mStatus.getResults();

 

        ResultSetRecord mLastRecord = mResultSet.get(mResultSet.size()-1);
       System.out.println(" Last Record (" + TotalRecordsProcessed
         + ") "+ mLastRecord.getUrlName());
       System.out.println(" Size: " + mLastRecord.getSize());
       System.out.println();

 

       // Now we need to see whether the query is complete or whether there are more
        // objects. If INCOMPLETE, it is a successful paged query.
        if (Status.Code.INCOMPLETE == mStatus.getCode())
        {

 

        // We have more, so update the offset for the next query to be the previous
        // offset plus the number we just read.
        mObjQuery.setOffset(
          (null == mObjQuery.getOffset() ? 0 : mObjQuery.getOffset())
          + mStatus.getResults()
          );
        }

 

        // Clean up after ourselves and release the HTTP connection to the connection
        // manager.
        EntityUtils.consume(httpResponse.getEntity());

 

    } // Keep doing this while we have more results.

 

    while (Status.Code.INCOMPLETE == mQueryResult.getStatus().getCode());

    /*
     * Print out the final statistics.
     */
    System.out.println("Total Records Processed: " + TotalRecordsProcessed);
    System.out.println("HTTP Calls: " + HTTPCalls);
     } catch(Exception e) {
       e.printStackTrace();
     }
}

 

/*
 * @param args
 */

public static void main(String[] args) {

 

   PagedObjectQuery myClass = new PagedObjectQuery();

  if (args.length != 3) {
  System.out.println();
   System.out.println("Usage: " + myClass.getClass().getSimpleName()
     + " <DNS-Namespace> <Username> <Password>\n");
   System.out.println(" where ");
   System.out.println(" <DNS-Namespace> is the fully qualified domain name"
     + " of the HCP Namespace.");
   System.out.println(" For example: \"ns1.ten1.myhcp.example.com\"");
   System.out.println(" <Username> and <Password> are the credentials of the"
     + " HCP user with data access permissions for the namespace");
   System.out.println();

 

   System.exit(-1);
}

 

try {
   // Initialize the class with the input parameters
   myClass.initialize(args[0], args[1], args[2]);

  // Issue the query and process the results
   myClass.runQuery();

  // Clean up before object destruction
  myClass.shutdown();

 } catch (Exception e) {
   e.printStackTrace();
   }
 }
}