JSONiq - the SQL of NoSQL 1.0 JSONiq Use Cases

Similar documents
Module 1: Getting Started with Databases and Transact-SQL in SQL Server 2008

Writing Queries Using Microsoft SQL Server 2008 Transact-SQL

Writing Queries Using Microsoft SQL Server 2008 Transact-SQL

Lecture 21: NoSQL III. Monday, April 20, 2015

IBM DB2 XML support. How to Configure the IBM DB2 Support in oxygen

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

How to Improve Database Connectivity With the Data Tools Platform. John Graham (Sybase Data Tooling) Brian Payton (IBM Information Management)

How To Use Query Console

WEB DEVELOPMENT COURSE (PHP/ MYSQL)

Teiid - Salesforce Connector Guide 6.2.0

OData Extension for XML Data A Directional White Paper

Creating a TEI-Based Website with the exist XML Database

Get Started with LeadSquared Guide for Marketing Users. Complete lead to revenue platform

What's new in 3.0 (XSLT/XPath/XQuery) (plus XML Schema 1.1) Michael Kay Saxonica

COURSE OUTLINE UCLA EXTENSI ON MGMNT X

Microsoft' Excel & Access Integration

SAP Business Objects XIR3.0/3.1, BI 4.0 & 4.1 Course Content

DataDirect XQuery Technical Overview

Dashboard Admin Guide

InstallShield Tip: Accessing the MSI Database at Run Time

Using SQL Queries in Crystal Reports

Advanced SQL Queries for the EMF

Querying MongoDB without programming using FUNQL

DBMS / Business Intelligence, SQL Server

USER GUIDE Chapter 20 Using Podcasts. Schoolwires Academic Portal Version 4.1

Microsoft Access 3: Understanding and Creating Queries

Instant SQL Programming

NoSQL's biggest secret: SQL went nowhere Matthew Revell Director of Developer Advocacy, Couchbase

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Real World Hadoop Use Cases

Implementing and Maintaining Microsoft SQL Server 2005 Reporting Services COURSE OVERVIEW AUDIENCE OUTLINE OBJECTIVES PREREQUISITES

Web Development using PHP (WD_PHP) Duration 1.5 months

IBM WebSphere ILOG Rules for.net

Databases for text storage

ABAP How To on SQL Trace Analysis

NO SQL! NO INJECTION?

Data XML and XQuery A language that can combine and transform data

BIA and BO integration other performance management options Crystal Reports Basic: Fundamentals of Report Design

LABSHEET 1: creating a table, primary keys and data types

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

Before you may use any database in Limnor, you need to create a database connection for it. Select Project menu, select Databases:

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Analytics March 2015 White paper. Why NoSQL? Your database options in the new non-relational world

Monitoring PostgreSQL database with Verax NMS

Programming Against Hybrid Databases with Java Handling SQL and NoSQL. Brian Hughes IBM

Oracle Database 12c Plug In. Switch On. Get SMART.

BRIO QUERY FUNCTIONALITY IN COMPARISION TO CRYSTAL REPORTS

Education. Relevant Courses

Oracle Database: SQL and PL/SQL Fundamentals

Introduction to SQL C H A P T E R3. Exercises

Equipment Room Database and Web-Based Inventory Management

Duration Vendor Audience 5 Days Oracle Developers, Technical Consultants, Database Administrators and System Analysts

In-Memory Database: Query Optimisation. S S Kausik ( ) Aamod Kore ( ) Mehul Goyal ( ) Nisheeth Lahoti ( )

How To Use X Query For Data Collection

DYNAMIC QUERY FORMS WITH NoSQL

Developing Web Applications for Microsoft SQL Server Databases - What you need to know

LAUREA MAGISTRALE - CURRICULUM IN INTERNATIONAL MANAGEMENT, LEGISLATION AND SOCIETY. 1st TERM (14 SEPT - 27 NOV)

SelectSurvey.NET Basic Training Class 1

Users are Complaining that the System is Slow What Should I Do Now? Part 1

MS Designing and Optimizing Database Solutions with Microsoft SQL Server 2008

The Import & Export of Data from a Database

No SQL! no injection? A talk on the state of NoSQL security

Consulting. Personal Attention, Expert Assistance

MongoDB Aggregation and Data Processing Release 3.0.4

Qlik REST Connector Installation and User Guide

2016 Examina on dates

MongoDB Aggregation and Data Processing

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

Database Master User Manual

PDA DRIVEN WAREHOUSE INVENTORY MANAGEMENT SYSTEM Sebastian Albert Master of Science in Technology

Oracle Database: SQL and PL/SQL Fundamentals NEW

Data Tool Platform SQL Development Tools

I2B2 TRAINING VERSION Informatics for Integrating Biology and the Bedside

ADVANCED DATABASES PROJECT. Juan Manuel Benítez V Gledys Sulbaran

2015 Examination dates

Business Insight Report Authoring Getting Started Guide

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Physical Design. Phases of database design. Physical design: Inputs.

Oracle 11g PL/SQL training

Integration and Virtualization of Relational SQL and NoSQL Systems including MySQL and MongoDB

MarkLogic Server. Application Developer s Guide. MarkLogic 8 February, Last Revised: 8.0-4, November, 2015

Introduction to Business Reporting Using IBM Cognos

SQL Nested & Complex Queries. CS 377: Database Systems

Excel Functions (fx) Click the Paste Function button. In the Function Category select All. Scroll down the Function Name list And select SUM.

TELEPHONE SYSTEM F.A.Q.

Course 6232A: Implementing a Microsoft SQL Server 2008 Database

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Cyber Security Challenge Australia 2014

Desktop, Web and Mobile Testing Tutorials

Database Programming with PL/SQL: Learning Objectives

Using SQL Developer. Copyright 2008, Oracle. All rights reserved.

NUTECH COMPUTER TRAINING INSTITUTE 1682 E. GUDE DRIVE #102, ROCKVILLE, MD WEB: TEL:

Database Systems. Lecture 1: Introduction

Transcription:

JSONiq - the SQL of NoSQL 1.0 JSONiq Use Cases Use cases for the JSONiq core language. Jonathan Robie Ghislain Fourny Matthias Brantner Daniela Florescu Till Westmann Markos Zaharioudakis

JSONiq Use Cases JSONiq - the SQL of NoSQL 1.0 JSONiq Use Cases Use cases for the JSONiq core language. Edition 1.0.9 Author Jonathan Robie jonathan.robie@gmail.com Author Ghislain Fourny ghislain.fourny@28msec.com Author Matthias Brantner matthias.brantner@28msec.com Author Daniela Florescu dana.florescu@oracle.com Author Till Westmann till.westmann@28msec.com Author Markos Zaharioudakis markos.zaharioudakis@oracle.com Editor Ghislain Fourny ghislain.fourny@28msec.com This document introduces some use cases for the JSONiq core language. These use cases were originally use cases for the JSONiq extension to XQuery (this is where it all started). They were converted to the more pleasant JSONiq core syntax. The queries where results are supplied were executed with 28.io.

1. JSONiq Use Cases 1 1.1. Sample Queries... 1 1.1.1. Joins... 1 1.1.2. Grouping Queries for JSON... 2 1.1.3. JSON to JSON Transformations... 4 1.1.4. JSON Updates... 4 1.1.5. Data Transformations... 5 A. Revision History 7 Index 9 iii

iv

Chapter 1. JSONiq Use Cases Queries and Transformations for JSON JSONiq can be used for queries on JSON that return JSON results. These queries have the full power of XQuery, including FLWOR expressions, grouping, windowing, updates, and full-text. But queries or implementations that do not use XML or produce XML can use a subset of XQuery, and need not deal with the additional complexity that XML requires. Used in this way, JSONiq adds significant value to NoSQL databases. Examples of these queries can be found in Section 1.1.1, Joins, Section 1.1.2, Grouping Queries for JSON, and Section 1.1.3, JSON to JSON Transformations. JSON Updates JSONiq provides updates for JSON, in the same way that the XQuery Update Facility provides updates for XML. An example of such a query can be found in Section 1.1.4, JSON Updates. 1.1. Sample Queries 1.1.1. Joins The following queries are based on a social media site that allows users to interact with their friends. collection("users") contains data on users and their friends: "name" : "Sarah", "age" : 13, "gender" : "female", "friends" : [ "Jim", "Mary", "Jennifer"] "name" : "Jim", "age" : 13, "gender" : "male", "friends" : [ "Sarah" ] The following query performs a join on Sarah's friend list to return the Object representing each of her friends: Example 1.1. A join query. for $sarah in collection("users"), $friend in collection("users") where $sarah.name eq "Sarah" and (some $name in $sarah.friends[] satisfies $friend.name eq $name) return $friend 1

Chapter 1. JSONiq Use Cases Result: "name" : "Jim", "age" : 13, "gender" : "male", "friends" : [ "Sarah" ] The query can be simplified using a filter. In the following expression, [$$.name = "Sarah"] is a filter that restricts the set of users to the one named "Sarah". The following query returns the same results as the previous one: Example 1.2. A join query with a predicate. let $sarah := collection("users")[$$.name eq "Sarah"] for $friend in $sarah.friends[] return collection("users")[$$.name eq $friend] Result: "name" : "Jim", "age" : 13, "gender" : "male", "friends" : [ "Sarah" ] 1.1.2. Grouping Queries for JSON 1 collection("sales") is an unordered sequence that contains the following objects: "product" : "broiler", "store number" : 1, "quantity" : 20, "product" : "toaster", "store number" : 2, "quantity" : 100, "product" : "toaster", "store number" : 2, "quantity" : 50, "product" : "toaster", "store number" : 3, "quantity" : 50, "product" : "blender", "store number" : 3, "quantity" : 100, "product" : "blender", "store number" : 3, "quantity" : 150, "product" : "socks", "store number" : 1, "quantity" : 500, "product" : "socks", "store number" : 2, "quantity" : 10, "product" : "shirt", "store number" : 3, "quantity" : 10 20, "product" : "toaster", "store number" : 2, "quantity" : 100, "product" : "toaster", "store number" : 2, "quantity" : 50, "product" : "toaster", "store number" : 3, "quantity" : 50, "product" : "blender", "store number" : 3, "quantity" : 100, "product" : "blender", "store number" : 3, "quantity" : 150, "product" : "socks", "store number" : 1, "quantity" : 500, "product" : "socks", "store number" : 2, "quantity" : 10, "product" : "shirt", "store number" : 3, "quantity" We want to group sales by product, across stores. Example 1.3. A grouping query for $sales in collection("sales") let $pname := $sales.product group by $pname return $pname : sum($sales.quantity) Result: "toaster" : 200, "blender" : 250, "shirt" : 10, "socks" : 510, "broiler" : 20 1 These queries are based on similar queries in the XQuery 3.0 Use Cases. 2

Grouping Queries for JSON Now let's do a more complex grouping query, showing sales by category within each state. We need further data to describe the categories of products and the location of stores. collection("products") contains the following data: "name" : "broiler", "category" : "kitchen", "price" : 100, "cost" : 70, "name" : "toaster", "category" : "kitchen", "price" : 30, "cost" : 10, "name" : "blender", "category" : "kitchen", "price" : 50, "cost" : 25, "name" : "socks", "category" : "clothes", "price" : 5, "cost" : 2, "name" : "shirt", "category" : "clothes", "price" : 10, "cost" : 3 70, "name" : "toaster", "category" : "kitchen", "price" : 30, "cost" : 10, "name" : "blender", "category" : "kitchen", "price" : 50, "cost" : 25, "name" : "socks", "category" : "clothes", "price" : 5, "cost" : 2, "name" : "shirt", "category" : "clothes", "price" : 10, "cost" : collection("stores") contains the following data: "store number" : 1, "state" : "CA", "store number" : 2, "state" : "CA", "store number" : 3, "state" : "MA", "store number" : 4, "state" : "MA", "store number" : 2, "state" : "CA", "store number" : 3, "state" : "MA", "store number" : 4, "state" : "MA" The following query groups by state, then by category, then lists individual products and the sales associated with each. Query: Example 1.4. A nesting grouping query. for $store in collection("stores") let $state := $store.state group by $state return $state : for $product in collection("products") let $category := $product.category group by $category return $category : for $sales in collection("sales") where (some $s in $store satisfies $sales."store number" eq $s."store number") and (some $p in $product satisfies $sales.product eq $p.name) let $pname := $sales.product group by $pname return $pname : sum( $sales.quantity ) Result: "MA" : "clothes" : "shirt" : 10, "kitchen" : "toaster" : 50, "blender" : 250, "CA" : "clothes" : "socks" : 510, "kitchen" : "toaster" : 150, "broiler" : 20 3

Chapter 1. JSONiq Use Cases 1.1.3. JSON to JSON Transformations The following query takes satellite data, and summarizes which satellites are visible. The data for the query is a simplified version of a Stellarium file that contains this information. Collection "satellites": "creator" : "Satellites plugin version 0.6.4", "satellites" : "AAU CUBESAT" : "tle1" : "1 27846U 03031G 10322.04074654.00000056 00000-0 45693-4 0 8768", "visible" : false, "AJISAI (EGS)" : "tle1" : "1 16908U 86061A 10321.84797408 -.00000083 00000-0 10000-3 0 3696", "visible" : true, "AKARI (ASTRO-F)" : "tle1" : "1 28939U 06005A 10321.96319841.00000176 00000-0 48808-4 0 4294", "visible" : true We want to query this data to return a summary. The following is a JSONiq query that returns the desired result. Example 1.5. A summarizing query. let $sats := collection("satellites").satellites return "visible" : [ for $sat in keys($sats) where $sats.$sat.visible return $sat ], "invisible" : [ for $sat in keys($sats) where not $sats.$sat.visible return $sat ] Result: "visible" : [ "AJISAI (EGS)", "AKARI (ASTRO-F)" ], "invisible" : [ "AAU CUBESAT" ] 1.1.4. JSON Updates The XQuery Update Facility allows XML data to be updated. JSONiq provides updating functions to allow JSON to be updated. Suppose an application receives an order that contains a credit card number, and needs to put the user on probation. Data for an order: 4

Data Transformations "user" : "Deadbeat Jim", "credit card" : VISA 4111 1111 1111 1111, "product" : "lottery tickets", "quantity" : 243 collection("users") contains the data for each individual user: "name" : "Deadbeat Jim", "address" : "1 E 161st St, Bronx, NY 10451", "risk tolerance" : "high" The following query adds "status" : "credit card declined" to the user's record. let $dbj := collection("users")[ $$.name = "Deadbeat Jim" ] return insert "status" : "credit card declined" into $dbj After the update is finished, the user's record looks like this: "name" : "Deadbeat Jim", "address" : "1 E 161st St, Bronx, NY 10451", "status" : "credit card declined", "risk tolerance" : "high" 1.1.5. Data Transformations Many applications need to modify data before forwarding it to another source. The XQuery Update Facility provides an expression called a tranform expression that can be used to create modified copies. The transform expression uses updating expressions to perform a transformation. JSONiq defines updating functions for JSON, which can be used in the XQuery transform expression. Suppose an application make videos available using feeds from Youtube. The following data comes from one such feed: "encoding" : "UTF-8", "feed" : "author" : [ "name" : "$t" : "YouTube", "uri" : "$t" : "http://www.youtube.com/" ], "category" : [ "scheme" : "http://schemas.google.com/g/2005#kind", "term" : "http://gdata.youtube.com/schemas/2007#video" ], "entry" : [ 5

Chapter 1. JSONiq Use Cases!!! SNIP!!! "app$control" : "yt$state" : "$t" : "Syndication of this video was restricted by its owner.", "name" : "restricted", "reasoncode" : "limitedsyndication", "author" : [ "name" : "$t" : "beyoncevevo", "uri" : "$t" : "http://gdata.youtube.com/feeds/api/users/beyoncevevo" ] The following query creates a modified copy of the feed by removing all entries that restrict syndication. let $feed := collection("youtube") return copy $out := $feed modify let $feed := $out.feed let $feed-entry := $feed.entry for $entry at $pos in $feed-entry[] where $entry."app$control"."yt$state".name eq "restricted" return delete $feed-entry.pos return $out 6

Appendix A. Revision History Revision 1.0.9 Mon Jul 22 2013 Ghislain Fourny ghislain.fourny@28msec.com Fixed new array unboxing syntax. Revision 1.0.1 fri Apr 5 2013 Ghislain Fourny ghislain.fourny@28msec.com Ran all core queries on 28.io. Revision 1.0.0 Thu Apr 4 2013 Ghislain Fourny ghislain.fourny@28msec.com First publication 7

8

Index 9

10