Skip to content

Commit 8a65250

Browse files
committed
PYCBC-1676: Update couchbase query operations example to use blocking API
Change-Id: I610210a98475c1967859e4766d062621c17dc8ec Reviewed-on: https://review.couchbase.org/c/couchbase-python-client/+/227602 Tested-by: Build Bot <build@couchbase.com> Reviewed-by: Dimitris Christodoulou <dimitris.christodoulou@couchbase.com>
1 parent dd81975 commit 8a65250

1 file changed

Lines changed: 21 additions & 21 deletions

File tree

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import uuid
22

3-
from acouchbase.cluster import Cluster, get_event_loop
43
from couchbase.auth import PasswordAuthenticator
54

65
# **DEPRECATED**, import ALL options from `couchbase.options`
7-
from couchbase.cluster import (ClusterOptions,
6+
from couchbase.cluster import (Cluster,
7+
ClusterOptions,
88
QueryOptions,
99
QueryScanConsistency)
1010
from couchbase.exceptions import ParsingFailedException
@@ -15,9 +15,9 @@
1515
# from couchbase.n1ql import QueryScanConsistency
1616

1717

18-
async def main():
19-
cluster = await Cluster.connect('couchbase://localhost',
20-
ClusterOptions(PasswordAuthenticator('Administrator', 'password')))
18+
def main():
19+
cluster = Cluster.connect('couchbase://localhost',
20+
ClusterOptions(PasswordAuthenticator('Administrator', 'password')))
2121
bucket = cluster.bucket("travel-sample")
2222
collection = bucket.default_collection()
2323

@@ -26,7 +26,7 @@ async def main():
2626
result = cluster.query(
2727
"SELECT * FROM `travel-sample` LIMIT 10;", QueryOptions(metrics=True))
2828

29-
async for row in result.rows():
29+
for row in result.rows():
3030
print(f'Found row: {row}')
3131

3232
metrics = result.metadata().metrics()
@@ -39,35 +39,36 @@ async def main():
3939
# positional params
4040
q_str = "SELECT ts.* FROM `travel-sample` ts WHERE ts.`type`=$1 LIMIT 10"
4141
result = cluster.query(q_str, "hotel")
42-
rows = [r async for r in result]
42+
rows = [r for r in result]
4343

4444
# positional params via QueryOptions
4545
result = cluster.query(q_str, QueryOptions(positional_parameters=["hotel"]))
46-
rows = [r async for r in result]
46+
rows = [r for r in result]
4747

4848
# named params
4949
q_str = "SELECT ts.* FROM `travel-sample` ts WHERE ts.`type`=$doc_type LIMIT 10"
5050
result = cluster.query(q_str, doc_type='hotel')
51-
rows = [r async for r in result]
51+
rows = [r for r in result]
5252

5353
# name params via QueryOptions
5454
result = cluster.query(q_str, QueryOptions(named_parameters={'doc_type': 'hotel'}))
55-
rows = [r async for r in result]
55+
rows = [r for r in result]
5656

5757
# iterate over result/rows
5858
q_str = "SELECT ts.* FROM `travel-sample` ts WHERE ts.`type`='airline' LIMIT 10"
5959
result = cluster.query(q_str)
6060

6161
# iterate over rows
62-
async for row in result:
62+
for row in result:
6363
# each row is an serialized JSON object
6464
name = row["name"]
6565
callsign = row["callsign"]
6666
print(f'Airline name: {name}, callsign: {callsign}')
6767

6868
# query metrics
6969
result = cluster.query("SELECT 1=1", QueryOptions(metrics=True))
70-
await result.execute()
70+
# ignore results
71+
result.execute()
7172

7273
print("Execution time: {}".format(
7374
result.metadata().metrics().execution_time()))
@@ -76,7 +77,7 @@ async def main():
7677
result = cluster.query(
7778
"SELECT ts.* FROM `travel-sample` ts WHERE ts.`type`='airline' LIMIT 10",
7879
QueryOptions(scan_consistency=QueryScanConsistency.REQUEST_PLUS))
79-
rows = [r async for r in result]
80+
rows = [r for r in result]
8081

8182
# Read your own writes
8283
new_airline = {
@@ -89,35 +90,34 @@ async def main():
8990
"type": "airline"
9091
}
9192

92-
res = await collection.upsert(
93-
"airline_{}".format(new_airline["id"]), new_airline)
93+
res = collection.upsert("airline_{}".format(new_airline["id"]), new_airline)
9494

9595
ms = MutationState(res)
9696

9797
result = cluster.query(
9898
"SELECT ts.* FROM `travel-sample` ts WHERE ts.`type`='airline' LIMIT 10",
9999
QueryOptions(consistent_with=ms))
100-
rows = [r async for r in result]
100+
rows = [r for r in result]
101101

102102
# client context id
103103
result = cluster.query(
104104
"SELECT ts.* FROM `travel-sample` ts WHERE ts.`type`='hotel' LIMIT 10",
105105
QueryOptions(client_context_id="user-44{}".format(uuid.uuid4())))
106-
rows = [r async for r in result]
106+
rows = [r for r in result]
107107

108108
# read only
109109
result = cluster.query(
110110
"SELECT ts.* FROM `travel-sample` ts WHERE ts.`type`='hotel' LIMIT 10",
111111
QueryOptions(read_only=True))
112-
rows = [r async for r in result]
112+
rows = [r for r in result]
113113

114114
agent_scope = bucket.scope("inventory")
115115

116116
result = agent_scope.query(
117117
"SELECT a.* FROM `airline` a WHERE a.country=$country LIMIT 10",
118118
country='France')
119-
rows = [r async for r in result]
119+
rows = [r for r in result]
120+
120121

121122
if __name__ == "__main__":
122-
loop = get_event_loop()
123-
loop.run_until_complete(main())
123+
main()

0 commit comments

Comments
 (0)