Querying in OJAI Applications

To query MapR 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 MapR Database JSON table you want to query, constructing the query, performing the query, and then processing the results.

Note: The Node.js and Python OJAI clients are supported starting in MEP 6.0.

Description

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

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.

Note: Beginning with MapR version 6.0, the MapR Database Table interface is deprecated and replaced by the OJAI version 2.0 DocumentStore interface.

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:

Basic Application Flow

The following steps describe the basics in developing client applications that query MapR Database JSON tables using the OJAI API.

  1. Create a Connection instance to your MapR 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 MapR 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 MapR:
    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 MapR 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 MapR:
    stream.on('end', () => {
      console.log('end');
      connection.close();
    });
  1. Create a Connection instance to your MapR 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 MapR 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 MapR:
    connection.close()

See Examples: Querying JSON Documents for complete code examples.