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.
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.
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.
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.
Query
functions
are available in the Go OJAI API: Basic Application Flow
The following steps describe the basics in developing client applications that query MapR Database JSON tables using the OJAI API.
- 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. - Obtain a DocumentStore handle to a MapR Database JSON table using the
connection
object:
DocumentStore store = connection.getStore(tablePath);
- Create a Query object
using the connection
object:
Query query = connection.newQuery();
- Perform the query operation on the table:
QueryResult result = store.find(query);
- 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()); } } }
- Close the result stream, the connection to the
document store, and the connection to
MapR:
result.close(); store.close; connection.close();
- Create a
connection:
ConnectionManager.getConnection('localhost:5678?;user=mapr;password=mapr;ssl=false') .then((connection) => { // Process connection ... });
- Obtain a handle to a MapR Database JSON table using the
connection
object:
connection.getStore(tablePath) .then((store) => { // Process store ... });
- Create a query
object:
const query = {};
- Perform the query operation on the
table:
const stream = store.find(query)
- Process the
results:
stream.on('data', (document) => console.log(document));
- Close the connection to
MapR:
stream.on('end', () => { console.log('end'); connection.close(); });
- 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)
- Obtain a DocumentStore handle to a
MapR Database JSON table
using the connection
object:
store = connection.get_store(table_path)
- Create a Query
object using the connection
object:
query = connection.new_query().build()
- Perform the query operation on the
table:
query_result = store.find(query)
- 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)
- Close the connection to
MapR:
connection.close()
- Create a
Connection
instance to your MapR cluster using theConnectionFactory
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);
- Obtain a
DocumentStore
handle to a MapR Database JSON table using the connection object:var store = connection.GetStore(storePath);
- Create a
Query
object using the connection object:var query = connection.NewQuery().Build();
- Perform the query operation on the
table:
var queryResult = store.Find(query);
- 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()); }
- Close the connection to
MapR:
connection.Close();
- Create a
Connection
instance to your MapR cluster:connectionString := "localhost:5678?auth=basic;user=mapr;password=mapr;ssl=false" connection, error := client.MakeConnection(connectionString)
- Obtain a
DocumentStore
handle to a MapR Database JSON table using the connection object:store, error := connection.CreateStore("/store_path")
- Create a Query
object:
query, err := client.MakeQuery() query.Build()
- Perform the query operation on the
table:
queryResult, err := store.FindQuery(query, &client.FindOptions{})
- 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) }
- Close the connection to
MapR:
connection.Close()
See Examples: Querying JSON Documents for complete code examples.