Querying and Returning All Documents
The examples in this section show you two ways of retrieving all documents from a document store.
The following example queries a document store and returns all documents by using the DocumentStore.find method.
/** * Copyright (c) 2017 MapR, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.mapr.ojai.examples; import org.ojai.Document; import org.ojai.DocumentStream; import org.ojai.store.Connection; import org.ojai.store.DocumentStore; import org.ojai.store.DriverManager; public class OJAI_004_FindAll { public static void main(String[] args) { System.out.println("==== Start Application ==="); // Create an OJAI connection to MapR cluster final Connection connection = DriverManager.getConnection("ojai:mapr:"); // Get an instance of OJAI DocumentStore final DocumentStore store = connection.getStore("/demo_table"); // fetch all OJAI Documents from this store final DocumentStream stream = store.find(); for (final Document userDocument : stream) { // Print the OJAI Document System.out.println(userDocument.asJsonString()); } // Close this instance of OJAI DocumentStore store.close(); // close the OJAI connection and release any resources held by the connection connection.close(); System.out.println("==== End Application ==="); } }
The following example queries a document store and returns all documents. It creates a Query object and passes that to the DocumentStore.findQuery method.
/** * Copyright (c) 2017 MapR, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ package com.mapr.ojai.examples; import org.ojai.Document; import org.ojai.DocumentStream; import org.ojai.store.Connection; import org.ojai.store.DocumentStore; import org.ojai.store.DriverManager; import org.ojai.store.Query; public class OJAI_005_FindAllQuery { public static void main(final String[] args) { System.out.println("==== Start Application ==="); // Create an OJAI connection to MapR cluster final Connection connection = DriverManager.getConnection("ojai:mapr:"); // Get an instance of OJAI DocumentStore final DocumentStore store = connection.getStore("/demo_table"); // Build an OJAI query final Query query = connection.newQuery().build(); // fetch all OJAI Documents from this store final DocumentStream stream = store.find(query); for (final Document userDocument : stream) { // Print the OJAI Document System.out.println(userDocument.asJsonString()); } // Close this instance of OJAI DocumentStore store.close(); // close the OJAI connection and release any resources held by the connection connection.close(); System.out.println("==== End Application ==="); } }
The following example queries a document store and returns all documents by using the DocumentStore.find method.
/* * Copyright (c) 2018 MapR, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ const { ConnectionManager } = require('node-maprdb'); const connectionString = 'localhost:5678?' + 'auth=basic;' + 'user=mapr;' + 'password=mapr;' + 'ssl=true;' + 'sslCA=/opt/mapr/conf/ssl_truststore.pem;' + 'sslTargetNameOverride=node1.mapr.com'; let connection; // Create a connection to data access server ConnectionManager.getConnection(connectionString) .then((conn) => { connection = conn; // Get a store return connection.getStore('/demo_table'); }) .then((store) => { // fetch all OJAI Documents from table return store.find({}); }) .then((queryResult) => { queryResult.on('data', (document) => console.log(document)); queryResult.on('end', () => { // close the OJAI connection connection.close(); }); });
The following example queries a document store and returns all documents. It creates an empty query and passes that to the DocumentStore.find method.
/* * Copyright (c) 2018 MapR, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ const { ConnectionManager } = require('node-maprdb'); const connectionString = 'localhost:5678?' + 'auth=basic;' + 'user=mapr;' + 'password=mapr;' + 'ssl=true;' + 'sslCA=/opt/mapr/conf/ssl_truststore.pem;' + 'sslTargetNameOverride=node1.mapr.com'; let connection; // Create a connection to data access server ConnectionManager.getConnection(connectionString) .then((conn) => { connection = conn; // Get a store return connection.getStore('/demo_table'); }) .then((store) => { // options for find request const options = { 'ojai.mapr.query.include-query-plan': true, 'ojai.mapr.query.timeout-milliseconds': 10000 } // fetch all OJAI Documents from table return store.find({}, options) }) .then((queryResult) => { // get query plan console.log(queryResult.queryPlan); queryResult.on('data', (document) => { // Print OJAI Documents from document stream console.log(document); }); queryResult.on('end', () => { // close the OJAI connection connection.close(); }); });
The following example queries a document store and returns all documents by using the DocumentStore.find method.
from mapr.ojai.storage.ConnectionFactory import ConnectionFactory # Create a connection to data access server connection_str = "localhost:5678?auth=basic;user=mapr;password=mapr;" \ "ssl=true;" \ "sslCA=/opt/mapr/conf/ssl_truststore.pem;" \ "sslTargetNameOverride=node1.mapr.com" connection = ConnectionFactory.get_connection(connection_str=connection_str) # Get a store and assign it as a DocumentStore object store = connection.get_store('/demo_table') # fetch all OJAI Documents from table query_result = store.find() # Print OJAI Documents from document stream for doc in query_result: print(doc) # close the OJAI connection connection.close()
The following example queries a document store and returns all documents. It creates a Query object and passes that to the DocumentStore.find method.
from mapr.ojai.storage.ConnectionFactory import ConnectionFactory # Create a connection to data access server connection_str = "localhost:5678?auth=basic;user=mapr;password=mapr;" \ "ssl=true;" \ "sslCA=/opt/mapr/conf/ssl_truststore.pem;" \ "sslTargetNameOverride=node1.mapr.com" connection = ConnectionFactory.get_connection(connection_str=connection_str) # Get a store and assign it as a DocumentStore object store = connection.get_store('/demo_table') # Build an OJAI query query = connection.new_query().build() # options for find request options = { 'ojai.mapr.query.include-query-plan': True, 'ojai.mapr.query.result-as-document': True, 'ojai.mapr.query.timeout-milliseconds': 10000 } # fetch all OJAI Documents from table query_result = store.find(query, options=options) # get query plan print(query_result.get_query_plan()) doc_stream = query_result # Print OJAI Documents from document stream for doc in doc_stream: print(doc.as_dictionary()) # close the OJAI connection connection.close()
The following is the equivalent of the code examples using dbshell. See dbshell find or findbyid for more details about the syntax dbshell provides.
# mapr dbshell
maprdb root:> find /demo_table
The following example queries a document store and returns all documents by using the
GetAllDocuments
method.
using System; using MapRDB.Driver; using MapRDB.Driver.Ojai; public class FindAllDocuments { public async void FindAllDocuments() { // Create a connection to data access server 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); // Get a store and assign it as a DocumentStore object var store = connection.GetStore("/demo_table"); // Fetch all OJAI Documents from table var queryResult = store.Find(); var documentStream = await queryResult.GetAllDocuments(); // Print OJAI Documents from document stream foreach (var document in documentStream) { Console.WriteLine(document.ToJsonString()); } // Close the OJAI connection connection.Close(); } }
The following example queries a document store and returns all documents. It creates a
Query
object and passes that to the
DocumentStore.Find
method.
using System; using MapRDB.Driver; using MapRDB.Driver.Ojai; public class FindAllQuery { public async void FindAllQuery() { // Create a connection to data access server 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); // Get a store and assign it as a DocumentStore object var store = connection.GetStore("/demo_table"); // Build an OJAI query var query = connection.NewQuery().Build(); // Options for find request var options = new QueryOptions() { IncludeQueryPlan = true, Timeout = 1000 }; // Fetch all OJAI Documents from table var queryResult = store.Find(query, options); // Get query plan Console.WriteLine(queryResult.GetQueryPlan()); var documentStream = await queryResult.GetDocumentAsyncStream().GetAllDocuments(); // Print OJAI Documents from document stream foreach (var document in documentStream) { Console.WriteLine(document.ToDictionary()); } // Close the OJAI connection connection.Close(); } }
The following example queries a document store and returns all documents by using the DocumentStore.FindAll function.
package main import ( "fmt" client "github.com/mapr/private-maprdb-go-client" ) func main() { // Create connection string connectionString := "192.168.33.11:5678?" + "auth=basic;" + "user=mapr;" + "password=mapr;" + "ssl=true;" + "sslCA=/opt/mapr/conf/ssl_truststore.pem;" + "sslTargetNameOverride=node1.cluster.com" storeName := "/demo_table" // Create a connection to DAG connection, err := client.MakeConnection(connectionString) if err != nil { panic(err) } // Get a store and assign it as a DocumentStore struct store, err := connection.GetStore(storeName) if err != nil { panic(err) } // Fetch all OJAI Documents from table findResult, err := store.FindAll(&client.FindOptions{IncludeQueryPlan: false, ResultAsDocument: true}) // Print OJAI Documents from document stream for _, doc := range findResult.DocumentList() { fmt.Println(doc) } // Close connection connection.Close() }
The following example queries a document store and returns all documents. It creates a Query object and passes that to the DocumentStore.FindQuery function.
package main import ( "fmt" client "github.com/mapr/private-maprdb-go-client" ) func main() { // Create connection string connectionString := "192.168.33.11:5678?" + "auth=basic;" + "user=mapr;" + "password=mapr;" + "ssl=true;" + "sslCA=/opt/mapr/conf/ssl_truststore.pem;" + "sslTargetNameOverride=node1.cluster.com" storeName := "/demo_table" // Create a connection to DAG connection, err := client.MakeConnection(connectionString) if err != nil { panic(err) } // Get a store and assign it as a DocumentStore struct store, err := connection.GetStore(storeName) if err != nil { panic(err) } // Options for find request options := &client.FindOptions{IncludeQueryPlan: true, ResultAsDocument: true} // Build an OJAI query query, err := client.MakeQuery() if err != nil { panic(err) } // Fetch all OJAI Documents from table findResult, err := store.FindQuery(query, options) // Get query plan fmt.Println(findResult.QueryPlan()) // Print OJAI Documents from document stream for _, doc := range findResult.DocumentList() { fmt.Println(doc) } // Close connection connection.Close() }
Paginating Your Result
An alternative to returning all documents from a store is to specify a limit in the query. Another alternative is to paginate the result using offset and limit. Querying with Order By contains an example that shows you how to use offset and limit. Although the example also uses order by, you can use offset and limit independent of order by.