Querying in OJAI Applications

To query HPE Ezmeral Data Fabric Database JSON tables in your OJAI applications, you use the OJAI Query interface. The typical flow of your application involves creating a connection, obtaining a handle to the HPE Ezmeral Data Fabric Database JSON table you want to query, constructing the query, performing the query, and then processing the results.

NOTE
The Node.js, Python, C#, and Go OJAI clients are supported starting in EEP 6.0.

Description

The DocumentStore interface includes a Query interface. The Query interface allows you to build a query programmatically.

To construct an OJAI query, call the following methods in the Query interface:

To run the query, pass the Query object to the DocumentStore.find method.

To construct an OJAI query, create a Node.js JSON object using OJAI Query Syntax.

To run the query, pass the Query object to the DocumentStore.find method.

To construct an OJAI query, create a Python dictionary object using OJAI Query Syntax.

To run the query, pass the Query object to the DocumentStore.find method.

NOTE
The following Query methods are available in the Python OJAI API, but creating a Python dictionary is the preferred approach:

To construct an OJAI query, create a C# object.

To run the query, pass the Query object to the DocumentStore.Find method.

NOTE
The following Query methods are available in the C# OJAI API:

To construct an OJAI query, create a Go object.

To run the query, pass the Query object to the DocumentStore.FindQuery function.

NOTE
The following Query functions are available in the Go OJAI API:

Basic Application Flow

The following steps describe the basics in developing client applications that query HPE Ezmeral Data Fabric Database JSON tables using the OJAI API.

  1. Create a Connection instance to your Data Fabric cluster using the DriverManager class:
    Connection connection = DriverManager.getConnection("ojai:mapr:");
    NOTE
    Do not omit the ending colon in the connection string.
  2. Obtain a DocumentStore handle to a HPE Ezmeral Data Fabric Database JSON table using the connection object:
    DocumentStore store = connection.getStore(tablePath);
  3. Create a Query object using the connection object:
    Query query = connection.newQuery();
  4. Perform the query operation on the table:
    QueryResult result = store.find(query);
  5. Process the results.

    The following code snippet iterates through the QueryResult and prints each document as a JSON string:

    for (final Document userDocument : result) {
        // Print the OJAI Document
        System.out.println(userDocument.asJsonString());
    }
    To process individual fields within a document, use the DocumentReader interface. The following code snippet iterates through the fields in a document and prints the fields that are strings:
    Iterable it = result.documentReaders();
    for (DocumentReader reader : it) {
        EventType et = null;
        while ((et = reader.next()) != null) {
            if (et == EventType.STRING) {
                System.out.println("Value of field " + reader.getFieldName() + ": " + reader.getString());
            }
        }
    }
  6. Close the result stream, the connection to the document store, and the connection to Data Fabric:
    result.close();
    store.close;
    connection.close();
  1. Create a connection:
    ConnectionManager.getConnection('localhost:5678?;user=mapr;password=mapr;ssl=false')
        .then((connection) => {
            // Process connection
            ...
        });
  2. Obtain a handle to a HPE Ezmeral Data Fabric Database JSON table using the connection object:
    connection.getStore(tablePath)
        .then((store) => {
            // Process store  
            ...
        });
  3. Create a query object:
    const query = {};
  4. Perform the query operation on the table:
    const stream = store.find(query)
  5. Process the results:
    stream.on('data', (document) => console.log(document));
  6. Close the connection to Data Fabric:
    stream.on('end', () => {
      console.log('end');
      connection.close();
    });
  1. Create a Connection instance to your Data Fabric cluster using the ConnectionFactory class:
    connection_str = 'localhost:5678?;user=mapr;password=mapr;ssl=false'
    connection = ConnectionFactory.get_connection(connection_str=connection_str)
  2. Obtain a DocumentStore handle to a HPE Ezmeral Data Fabric Database JSON table using the connection object:
    store = connection.get_store(table_path)
  3. Create a Query object using the connection object:
    query = connection.new_query().build()
  4. Perform the query operation on the table:
    query_result = store.find(query)
  5. Process the results.

    The following code snippet iterates through the QueryResult and prints each document as a Python dictionary:

    for doc in query_result:
        print(doc)
  6. Close the connection to Data Fabric:
    connection.close()
  1. Create a Connection instance to your Data Fabric cluster using the ConnectionFactory class:
    var connectionStr = $"localhost:5678?auth=basic;" +
            	$"user=mapr;" +
            	$"password=mapr;" +
            	$"ssl=true;" +
            	$"sslCA=/opt/mapr/conf/ssl_truststore.pem;" +
            	$"sslTargetNameOverride=node1.mapr.com";
    var connection = ConnectionFactory.CreateConnection(connectionStr);
    
  2. Obtain a DocumentStore handle to a HPE Ezmeral Data Fabric Database JSON table using the connection object:
    var store = connection.GetStore(storePath);
  3. Create a Query object using the connection object:
    var query = connection.NewQuery().Build();
  4. Perform the query operation on the table:
    var queryResult = store.Find(query);
  5. Process the results.

    The following code snippet iterates through the QueryResult and prints each document as a JSON:

    var documentStream = await queryResult.GetDocumentAsyncStream().GetAllDocuments();
    foreach (var document in documentStream)
    {
            Console.WriteLine(document.ToJsonString());
    }
    
  6. Close the connection to Data Fabric:
    connection.Close();
  1. Create a Connection instance to your Data Fabric cluster:
    connectionString := "localhost:5678?auth=basic;user=mapr;password=mapr;ssl=false"
    connection, error := client.MakeConnection(connectionString)
  2. Obtain a DocumentStore handle to a HPE Ezmeral Data Fabric Database JSON table using the connection object:
    store, error := connection.CreateStore("/store_path")
  3. Create a Query object:
    query, err := client.MakeQuery()
    query.Build()
  4. Perform the query operation on the table:
    queryResult, err := store.FindQuery(query, &client.FindOptions{})
  5. Process the results.

    The following code snippet iterates through the QueryResult and prints each document as a JSON:

    	for _, doc := range queryResult.DocumentList() {
    		fmt.Println(doc)
    	}
  6. Close the connection to Data Fabric:
    connection.Close()

See Examples: Querying JSON Documents for complete code examples.