Skip to content

Commit f3ecca1

Browse files
authored
Merge pull request #40 from marklogic/feature/589-docs
DEVEXP-589 Added docs for rows feature
2 parents 7b3185b + ee0179a commit f3ecca1

File tree

7 files changed

+181
-5
lines changed

7 files changed

+181
-5
lines changed

docs/example-setup.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ Then perform the following steps to create a new user:
1313
1. Click on "Users" in the "Security" box.
1414
2. Click on "Create".
1515
3. In the form, enter "python-user" for "User Name" and "pyth0n" as the password.
16-
4. Scroll down until you see the "Roles" section. Click on the "rest-reader" and "rest-writer" checkboxes.
16+
4. Scroll down until you see the "Roles" section. Click on the "rest-reader", "rest-writer", and "security" checkboxes.
1717
5. Scroll to the top or bottom and click on "OK" to create the user.
1818

19+
(The `security` role is only needed to allow for the user to load documents into the out-of-the-box Schemas database
20+
in MarkLogic; in a production application, an admin or admin-like user would typically be used for this use case.)
21+
1922
You can verify that you correctly created the user by accessing the REST API for the out-of-the-box REST API
2023
server in MarkLogic that listens on port 8000. Go to <http://localhost:8000/v1/search> (changing "localhost" to
2124
the correct name of your MarkLogic host if you are not running it locally) in a web browser and enter the

docs/managing-documents/managing-documents.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ has_children: true
66
permalink: /documents
77
---
88

9-
The [/v1/documents endpoint](https://docs.marklogic.com/REST/client/management) in the MarkLogic REST API supports
9+
The [MarkLogic REST documents service](https://docs.marklogic.com/REST/client/management) supports
1010
operations that involve writing or reading a single document. It also supports operations that involve multiple
1111
documents, though those require use of a potentially complex multipart HTTP request or response. The MarkLogic Python
1212
client simplifies those operations by hiding the details of multipart requests and responses.

docs/rows.md

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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". |

docs/transactions.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
layout: default
33
title: Managing transactions
4-
nav_order: 4
4+
nav_order: 5
55
---
66

7-
The [/v1/transactions endpoint](https://docs.marklogic.com/REST/client/transaction-management)
8-
in the MarkLogic REST API supports managing a transaction that can be referenced in
7+
The [MarkLogic REST transactions service](https://docs.marklogic.com/REST/client/transaction-management)
8+
supports managing a transaction that can be referenced in
99
multiple separate calls to other REST API endpoints, with all calls being committed or
1010
rolled back together. The MarkLogic Python client simplifies usage of these endpoints
1111
via a `Transaction` class that is also a

shell/docs.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Startup script when wanting to hit port 8000 as the user that's setup in the
2+
# documentation.
3+
4+
from marklogic import Client
5+
from marklogic.documents import Document, DefaultMetadata
6+
7+
client = Client("http://localhost:8000", digest=("python-user", "pyth0n"))

shell/test_app.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Startup script when wanting to hit the test-app server on port 8030 in a Python shell.
2+
from marklogic import Client
3+
4+
client = Client("http://localhost:8030", digest=("python-test-user", "password"))
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"user-name": "python-user",
3+
"password": "pyth0n",
4+
"description": "Used by the documentation, not by tests",
5+
"role": [
6+
"rest-reader",
7+
"rest-writer",
8+
"security"
9+
]
10+
}

0 commit comments

Comments
 (0)