|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Querying for rows |
| 4 | +nav_order: 4 |
| 5 | +--- |
| 6 | + |
| 7 | + |
| 8 | +The [MarkLogic REST rows service](https://docs.marklogic.com/REST/client/row-management) supports |
| 9 | +operations for querying for rows via a variety of languages. The MarkLogic Python client simplifies submitting queries |
| 10 | +for rows and converting the response into a useful data structure. |
| 11 | + |
| 12 | +## Setup |
| 13 | + |
| 14 | +The examples below require documents to be loaded along with a |
| 15 | +[TDE view](https://docs.marklogic.com/guide/app-dev/TDE) that projects rows from the documents. You must also have |
| 16 | +performed the instructions in the [setup guide](example-setup.md). |
| 17 | + |
| 18 | +Run the following in a Python shell to load 4 documents, each capturing details about a musician: |
| 19 | + |
| 20 | +``` |
| 21 | +from marklogic import Client |
| 22 | +from marklogic.documents import Document, DefaultMetadata |
| 23 | +client = Client('http://localhost:8000', digest=('python-user', 'pyth0n')) |
| 24 | +
|
| 25 | +client.documents.write([ |
| 26 | + DefaultMetadata(permissions={"rest-reader": ["read", "update"]}, collections=["musician"]), |
| 27 | + Document("/musician1.json", {"lastName": "Armstrong", "firstName": "Louis", "dob": "1901-08-04"}), |
| 28 | + Document("/musician2.json", {"lastName": "Byron", "firstName": "Don", "dob": "1958-11-08"}), |
| 29 | + Document("/musician3.json", {"lastName": "Coltrane", "firstName": "John", "dob": "1926-09-23"}), |
| 30 | + Document("/musician4.json", {"lastName": "Davis", "firstName": "Miles", "dob": "1926-05-26"}) |
| 31 | +]) |
| 32 | +``` |
| 33 | + |
| 34 | +Now load a TDE view via the following: |
| 35 | + |
| 36 | +``` |
| 37 | +tde_view = { |
| 38 | + "template": { |
| 39 | + "context": "/", |
| 40 | + "collections": ["musician"], |
| 41 | + "rows": [{ |
| 42 | + "schemaName": "example", |
| 43 | + "viewName": "musician", |
| 44 | + "columns": [ |
| 45 | + {"name": "lastName", "scalarType": "string", "val": "lastName"}, |
| 46 | + {"name": "firstName", "scalarType": "string", "val": "firstName"}, |
| 47 | + {"name": "dob", "scalarType": "date", "val": "dob"} |
| 48 | + ] |
| 49 | + }] |
| 50 | + } |
| 51 | +} |
| 52 | +
|
| 53 | +client.documents.write( |
| 54 | + Document( |
| 55 | + "/tde/musicians.json", tde_view, |
| 56 | + permissions={"rest-reader": ["read", "update"]}, |
| 57 | + collections=["http://marklogic.com/xdmp/tde"] |
| 58 | + ), |
| 59 | + params={"database": "Schemas"} |
| 60 | +) |
| 61 | +``` |
| 62 | + |
| 63 | + |
| 64 | +## Optic queries |
| 65 | + |
| 66 | +The [MarkLogic Optic API](https://docs.marklogic.com/guide/app-dev/OpticAPI) allows for rows to be queried from |
| 67 | +documents via a SQL-like syntax. The [MarkLogic REST API rows service](https://docs.marklogic.com/REST/POST/v1/rows) |
| 68 | +accepts Optic queries either as an [Optic Query DSL statement](https://docs.marklogic.com/guide/app-dev/OpticAPI#id_46710) |
| 69 | +or as [a serialized plan](https://docs.marklogic.com/guide/app-dev/OpticAPI#id_11208). |
| 70 | + |
| 71 | +Since using an Optic DSL query is often the easiest approach, a DSL query can be submitted as the first argument without |
| 72 | +any name: |
| 73 | + |
| 74 | +``` |
| 75 | +client.rows.query("op.fromView('example', 'musician')") |
| 76 | +``` |
| 77 | + |
| 78 | +The above will return a JSON object that captures each of the matching rows along with definitions for each column. See |
| 79 | +the section below on choosing an output format for controlling how data is returned. |
| 80 | + |
| 81 | + |
| 82 | +You can use a named argument as well: |
| 83 | + |
| 84 | +``` |
| 85 | +client.rows.query(dsl="op.fromView('example', 'musician')") |
| 86 | +``` |
| 87 | + |
| 88 | +For some use cases, it may be helpful to capture an Optic query in its serialized form. Such a query can then be |
| 89 | +submitted via the `plan` argument: |
| 90 | + |
| 91 | +``` |
| 92 | +plan = '{"$optic":{"ns":"op", "fn":"operators", "args":[{"ns":"op", "fn":"from-view", "args":["example", "musician"]}]}}' |
| 93 | +client.rows.query(plan=plan) |
| 94 | +``` |
| 95 | + |
| 96 | +Optic supports many different types of queries and operations; please |
| 97 | +[see the documentation]((https://docs.marklogic.com/guide/app-dev/OpticAPI#id_35559)) for further information on |
| 98 | +much more powerful and flexible queries than shown in these examples, which are intended solely for demonstration of |
| 99 | +how to submit an Optic query. |
| 100 | + |
| 101 | + |
| 102 | +## SQL queries |
| 103 | + |
| 104 | +MarkLogic supports [SQL queries against views](https://docs.marklogic.com/guide/sql). SQL queries can be submitted |
| 105 | +via the `sql` argument: |
| 106 | + |
| 107 | +``` |
| 108 | +client.rows.query(sql="select * from example.musician order by lastName") |
| 109 | +``` |
| 110 | + |
| 111 | +This will return a JSON object that captures each of the matching rows along with definitions |
| 112 | +for each column. See the section below on choosing an output format for controlling how data is returned. |
| 113 | + |
| 114 | +## SPARQL queries |
| 115 | + |
| 116 | +MarkLogic supports the [SPARQL query language](https://www.w3.org/TR/sparql11-query/), allowing for |
| 117 | +[SPARQL queries to be run against views](https://docs.marklogic.com/guide/semantics/semantic-searches#id_94155), |
| 118 | +similar to Optic and SQL. SPARQL queries can be submitted via the `sparql` argument: |
| 119 | + |
| 120 | +``` |
| 121 | +sparql = "PREFIX musician: <http://marklogic.com/column/example/musician/> SELECT * WHERE {?s musician:lastName ?lastName} ORDER BY ?lastName" |
| 122 | +client.rows.query(sparql=sparql) |
| 123 | +``` |
| 124 | + |
| 125 | +This will return a JSON object that captures each of the matching rows along with definitions |
| 126 | +for each column. See the section below on choosing an output format for controlling how data is returned. |
| 127 | + |
| 128 | +## GraphQL queries |
| 129 | + |
| 130 | +MarkLogic supports [GraphQL queries](https://docs.marklogic.com/REST/POST/v1/rows/graphql) to be run against views. |
| 131 | +A GraphQL query can be submitted via the `graphql` argument: |
| 132 | + |
| 133 | +``` |
| 134 | +client.rows.query(graphql="query myQuery { example_musician { lastName firstName dob } }") |
| 135 | +``` |
| 136 | + |
| 137 | +This will return a JSON object containing the matching rows. MarkLogic will only return a JSON object for GraphQL |
| 138 | +queries; the `format` argument described below will not have any impact when submitting a GraphQL query. |
| 139 | + |
| 140 | +## Choosing an output format |
| 141 | + |
| 142 | +The [MarkLogic REST endpoint for querying rows](https://docs.marklogic.com/REST/POST/v1/rows) supports several options |
| 143 | +for how data is returned via the `format` parameter. The `client.rows.query` function allows for an output format to be |
| 144 | +selected via a `format` argument. The following table defined the possible values: |
| 145 | + |
| 146 | +| `format` value | Output format | |
| 147 | +| --- | --- | |
| 148 | +| `json` | JSON object with keys of 'columns' and 'rows'. | |
| 149 | +| `xml` | XML document defining the columns and rows. | |
| 150 | +| `csv` | CSV text with the first row defining the columns. | |
| 151 | +| `json-seq` | A [line-delimited JSON sequence](https://datatracker.ietf.org/doc/html/rfc7464) with the first row defining the columns. | |
| 152 | +| `mixed` | TODO Seems like we should remove this as it does the same thing as "return_response". | |
0 commit comments