Querying By ID

The examples in this section show you how to query for a single document ID.

This example retrieves a single document identified by the ID user001.

NOTE To query for a range of document IDs, you must specify an OJAI QueryCondition. See Querying with Conditions for examples of the syntax.
/**
 * 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.store.Connection;
import org.ojai.store.DocumentStore;
import org.ojai.store.DriverManager;

public class OJAI_003_FindById {

  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 the OJAI Document by its '_id' field
    final Document userDocument = store.findById("user0001");

    // 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 ===");
  }

}

This example retrieves a single document identified by the ID user0001.

NOTE To query for a range of document IDs, you must specify an OJAI query condition. See Querying with Conditions for examples of the syntax.
/*
 * 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;

ConnectionManager.getConnection(connectionString)
  .then((conn) => {
    connection = conn;
    // Get a store
    return connection.getStore('/demo_table');
  })
  .then((store) => {
    // fetch the OJAI Document by its '_id' field
    return store.findById('user0001');
  })
  .then((doc) => {
    // Print the OJAI Document
    console.log(doc);
    connection.close();
  });

This example retrieves a single document identified by the ID user0001.

NOTE To query for a range of document IDs, you must specify an OJAI QueryCondition. See Querying with Conditions for examples of the syntax.
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 the OJAI Document by its '_id' field
doc = store.find_by_id("user0001")

# Print the OJAI Document
print(doc)

# 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:> findbyid /demo_table --id user0001        

This example retrieves a single document identified by the ID user0001.

NOTE To query for a range of document IDs, you must specify an OJAI QueryCondition. See Querying with Conditions for examples of the syntax.
using System;
using MapRDB.Driver;

public class FindById
{
    public void FindById()
	{
        // 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 the OJAI Document by its '_id' field
        var document = store.FindById("user0001");

        // Print the OJAI Document
        Console.WriteLine(document);

        // Close the OJAI connection
        connection.Close();
    }
}

This example retrieves a single document identified by the ID user0001.

NOTE To query for a range of document IDs, you must specify an OJAI Condition. See Querying with Conditions for examples of the syntax.
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 the OJAI Document by its '_id' field
	doc, err := store.FindByIdString("id0001")
	if err != nil {
		panic(err)
	}

	// Print the OJAI Document
	fmt.Println(doc.AsJsonString())

	// Close connection
	connection.Close()
}