Skip to main content

KEEP CALM and QUERY JSON



You've seen why and how to model data in JSON. Once you have the data in JSON, you can store and retrieve the JSON documents in any simple key-value databases. If your application only has to set and get the documents based on the key, use any key-value store. you're all set.

That's seldom the case.

You don't use a database just to store and retrieve JSON. You're trying to solve a real business problem. You're developing a business application. For a business application, the questions, operations, and reports don't change just because your database doesn't support query. Business applications have to do many things requiring multi-object operation:

  • Process Order Checkout.
  • Search stores data for the shoe customer is looking for.
  • How many new customers did we get last month?
  • Generate the outstanding list of shipments due for today.
  • Retrieve the customer order using case insensitive customer name.
  • Load the new inventory data.
  • Merge customer lists.

For doing each of these tasks, you need to search your database efficiently. Do the select-join-group-project-aggregate-order processing of the data to produce a report. Similarly, you'll have to insert, update, delete, and merge to represent the real-world business processes. 

Now, the question is, how would you go about implementing all these for a NoSQL key-value systems with GET and SET APIs? You do this by writing a program to do each task. Of course, writing the application is expensive. Scaling, debugging, and maintaining it makes it even more expensive.

Imagine a use case: Find high-value customers with orders > $10,000. With a NoSQL API approach, you write a program to retrieve the objects and do the filtering, grouping, aggregation in your program. 

Image title

Now, the question is, how would you go about implementing all these in a NoSQL key-value systems with GET and SET APIs? You do this by writing a program to do each task. Of course, writing the application is expensive, scaling, debugging and maintaining it makes it even more expensive.

Image title

This is the reason RDBMS and SQL were invented. SQL does set processing naturally. On the relational model, each of these tasks can be done via a series of statements. Each SQL statement takes set of tuples, does the processing, and produces another set of tuples.

Image title


SELECT c.CustID, Customers.Name, SUM(o.Amount)

FROM Orders o                                                      INNER JOIN LineItems l ON (o.CustID = l.CustID)   

      INNER JOIN Customers c (o.CustID = c.CustID)

GROUP BY c.CustID, c.NameHAVING SUM(o.Amount) > 10000 

ORDER BY SUM(o.Amount) DESC

As the NoSQL key-value databases evolved, they added the map-reduce frameworks so developers can use and cascade them to do complex work. CassandraMongoDB, and Couchbase introduced SQL-like languages on top distributed NoSQL database to make the query easier.  MongoDB API looks different, but you'll still see something like SQL.

Couchbase N1QL (Non-First Normal Form Query Language) provides all of the select-join-project operations in SQL on the flexible JSON data model. N1QL defines a four-valued boolean logic  for boolean expressions that include the new MISSING value in a flexible schema and the collation sequence for data types. In addition, N1QL extends SQL  to access and manipulate all parts of nested JSON structure. 

N1QL takes sets of JSON documents as input, processes them and gives you a set of JSON documents. Just replace tuples with JSON in SQL definition, you get N1QL.

Image title

Let's see how we can write the previous query in N1QL. Pretty close to SQL, but it has access to nested data and does the UNNESTING of arrays within ORDERS document.

SELECT Customers.ID, Customers.Name, SUM(OrderLine.Amount) 

FROM Orders UNNEST Orders.LineItems AS OrderLine

            INNER JOIN Customers ON KEYS Orders.CustID

GROUP BY Customers.ID, Customers.Name

HAVING SUM(OrderLine.Amount) > 10000

ORDER BY SUM(OrderLine.Amount) DESC

In addition to SELECT, Couchbase N1QL supports the traditional SQL statements: INSERT, UPDATE, DELETE, and MERGE. Here are some examples:

INSERT INTO ORDERS (KEY, VALUE)

VALUES ("1.ABC.X382", {"O_ID":482, "O_D_ID":3, "O_W_ID":4});

UPDATE ORDERS SET O_CARRIER_ID = ”ABC987” WHERE O_ID = 482 AND O_D_ID = 3 AND O_W_ID = 4

DELETE FROM NEW_ORDER WHERE NO_D_ID = 291 AND NO_W_ID = 3482 AND NO_O_ID = 2483

MERGE INTO CUSTOMER USING

      (SELECT CID FROM NEWCUST WHERE STATE = 'CA') as NC

ON KEY NC.CID  

     WHEN MATCHED THEN UPDATE SET CUSTOMER.STATE= 'CA';

Here's the comparison between SQL based RDBMS and N1QL:  

Query Features

SQL on RDBMS

N1QL: SQL on JSON

ObjectsTables, tuples, columnsKeyspaces, JSON Documents, key-value
ReferencesFlat references:Table.column
E.g. customer.name, customer.zip
Flat & Nested: keyspace.keyvalue
E.g. customer.name, customer.address.zip, 
Customer.contact[0].phone.mobile

Statements

SELECT, INSERT, UPDATE, DELETE, MERGE

SELECT, INSERT, UPDATE, DELETE, MERGE

Query Operations

  • Select, Join, Project, Subqueries
  • Strict Schema 
  • Strict Type checking
  • Select, Join, Project, Subqueries
  • Nest & Unnest
  • Look Ma! No Type Mismatch Errors!
  • JSON keys act as columns

Schema

Predetermined Columns

  • Fully addressable JSON
  • Flexible document structure

Data Types

SQL Data types

Conversion Functions

  • JSON Data types: Strings, Number Boolean,  Objects, Arrays, null
  • Conversion Functions

Query Processing

INPUT: Sets of Tuples

OUPUT: Set of Tuples

INPUT: Sets of JSON

OUTPUT: Set of JSON


Summary:

NoSQL databases give you the ability to read and write massive amounts of data at a substantially low latency and substantially low cost compared to RDBMS.Not having a query is like having a great engine without a steering wheel. With query on JSON from Couchbase, you can ask important questions like -- where can I find my favorite beer?.  

SELECT          {"Mybeer": "My Beer is " || beer.name || "."}, 
     Array_agg({"name":brewery.name})  brewery,
     Array_agg({"name":brewery.name, "state":brewery.state,                   "city":brewery.city, "location":brewery.geo})    locations, 
      Array_count(Array_agg(brewery.NAME))  AS brewery_count
FROM            `beer-sample` beer 
LEFT OUTER JOIN `beer-sample` brewery 
ON              keys beer.brewery_id 
WHERE           beer.type = 'beer' 
AND             brewery.type = 'brewery' 
AND             brewery.state = 'California' 
GROUP BY        beer.NAME 
ORDER BY        array_count(array_agg(brewery.NAME)) DESC, 
                                 beer.NAME ASC limit 5 ;

You have your steering wheel back, now!

Image title


Comments

Popular posts from this blog

Swami Vivekananda: The Monk That Nobody Sent to Chicago

  There’s a saying in Chicago: “We don’t want nobody that nobody sent.” This was the cold reception Swami Vivekananda faced when he arrived in the windy city in July 1893, determined to attend the World Parliament of Religions that September. He belonged to no organization, carried no letter of recommendation, his countrymen were nobody, and represented an alien religion to the Western world. As the days passed, his hope of attending the parliament dwindled. With money running out and the odds stacked against him, he left the Windy City and went to Boston, praying for a glimmer of opportunity.  Swamiji came to America to share India’s most profound gift: the wisdom of the Hindu sages, preserved through centuries of oral tradition and embodied by its monks. This was 1893, not 1993—India was under the British grip, its resources drained, and its spirit subdued. Swamiji’s mission was not just a cultural exchange; it was a bold step toward envisioning a future where India could re...

Why Should Databases Go Natural?

From search to CRM, applications are adopting natural language and intuitive interactions. Should databases follow? This article provides a strategic perspective. Amid the many technological evolutions in software and hardware (CISC/RISC, Internet, Cloud, and AI), one technology has endured:  Relational Database Systems   (RDBMS), aka SQL databases. For over 50 years, RDBMS has survived and thrived, overcoming many challenges. It has evolved and adopted beneficial features from emerging technologies like object-relational databases and now competes robustly with   NoSQL databases .  Today, RDBMS dominates the market, with four of the top five databases and seven of the top ten being relational. RDBMS has smartly borrowed ideas, like JSON support, from NoSQL, while NoSQL has also borrowed from RDBMS. NoSQL no longer rejects SQL. From a user perspective, all modern databases have SQL-inspired query language and a set of APIs. All applications manage the respective data...

iQ Interactive: Cool Things for Developers on Couchbase Capella iQ

  The landscape of software development is ever-evolving with the advent of new technologies. As we venture into 2023, natural language processing ( NLP ) is rapidly emerging as a pivotal aspect of programming. Unlike previous generations of tools that primarily aimed at enhancing coding productivity and code quality, the new generation of Artificial Intelligence ( GenAI ) tools, like iQ, is set to revolutionize every facet of a developer's workflow. This encompasses a wide range of activities: Reading, writing, and rewriting specifications Designing, prototyping, and coding Reviewing, refactoring, and verifying software Going through the iterative cycle of deploying, debugging, and improving the software Create a draft schema and sample data for any use case Natural language queries. Generate sample queries on a given dataset Fix the syntax error for a query Don't stop here. Let your imagination fly. Although the insights garnered from iQ are preliminary and should be treated ...