Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions docs/using_python_client_with_hazelcast_imdg.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1563,7 +1563,7 @@ The following code prints names of the employees whose age is less than 30:

.. code:: python

result = client.sql.execute("SELECT name FROM emp WHERE age < ?", 30)
result = client.sql.execute("SELECT name FROM emp WHERE age < ?", 30).result()

for row in result:
name = row.get_object("name")
Expand Down Expand Up @@ -1658,6 +1658,7 @@ it:
.. code:: python

client = hazelcast.HazelcastClient()
employees = client.get_map("employees").blocking()

mapping_query = """
CREATE MAPPING employees (
Expand All @@ -1671,17 +1672,14 @@ it:
);
"""

with client.sql.execute(mapping_query) as result:
# wait until query completes
result.update_count().result()
client.sql.execute(mapping_query).result()

employees = client.get_map("employees").blocking()
employees.set(1, HazelcastJsonValue({"name": "John Doe", "salary": 60000}))
employees.set(2, HazelcastJsonValue({"name": "Jane Doe", "salary": 80000}))

select_query = "SELECT name FROM employees WHERE salary > 75000"

with client.sql.execute(select_query) as result:
with client.sql.execute(select_query).result() as result:
for row in result:
name = row.get_object("name")
# or, you can use the [] operator
Expand Down
20 changes: 16 additions & 4 deletions examples/sql/sql_async_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,24 @@
for i in range(100):
integers.set(i, i)

# Create mapping for the integers. This needs to be done only once per map.
client.sql.execute(
"""
CREATE MAPPING integers
TYPE IMap
OPTIONS (
'keyFormat' = 'int',
'valueFormat' = 'int'
)
"""
).result()

# Fetch values in between (40, 50)
result = client.sql.execute("SELECT * FROM integers WHERE this > ? AND this < ?", 40, 50)
result_future = client.sql.execute("SELECT * FROM integers WHERE this > ? AND this < ?", 40, 50)


def on_iterator_response(iterator_future):
it = iterator_future.result()
def on_response(sql_result_future):
it = sql_result_future.result().iterator()

def on_next_row(row_future):
try:
Expand All @@ -38,4 +50,4 @@ def on_next_row(row_future):

# Request the iterator over rows and add a callback to
# run, when the response comes
result.iterator().add_done_callback(on_iterator_response)
result_future.add_done_callback(on_response)
29 changes: 24 additions & 5 deletions examples/sql/sql_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,27 @@ def __repr__(self):
customers.set(2, Customer("John", 23, False))
customers.set(3, Customer("Joe", 33, True))

# Create mapping for the customers. This needs to be done only once per map.
client.sql.execute(
"""
CREATE MAPPING customers (
__key INT,
name VARCHAR,
age INT,
is_active BOOLEAN
)
TYPE IMap
OPTIONS (
'keyFormat' = 'int',
'valueFormat' = 'portable',
'valuePortableFactoryId' = '1',
'valuePortableClassId' = '1'
)
"""
).result()

# Project a single column that fits the criterion
result = client.sql.execute("SELECT name FROM customers WHERE age < 35 AND is_active")
result = client.sql.execute("SELECT name FROM customers WHERE age < 35 AND is_active").result()

for row in result:
# Get the object with the given column name in the row
Expand All @@ -52,7 +71,7 @@ def __repr__(self):
# Also, with statement can be used to close the resources on the
# server-side if something goes bad while iterating over rows.

with client.sql.execute("SELECT * FROM customers") as result:
with client.sql.execute("SELECT * FROM customers").result() as result:
for row in result:
# Get the objects with column names
name = row.get_object("name")
Expand Down Expand Up @@ -83,9 +102,9 @@ def __repr__(self):
statement.add_parameter("Jo%")
statement.timeout = 5

with client.sql.execute_statement(statement) as result:
with client.sql.execute_statement(statement).result() as result:
# Row metadata can also be retrieved from the result
row_metadata = result.get_row_metadata().result()
row_metadata = result.get_row_metadata()

for row in result:
key = row.get_object("__key")
Expand All @@ -100,7 +119,7 @@ def __repr__(self):
print(key, age)

# Parameters can be passed directly in the basic execution syntax
result = client.sql.execute("SELECT this FROM customers WHERE age > ? AND age < ?", 30, 40)
result = client.sql.execute("SELECT this FROM customers WHERE age > ? AND age < ?", 30, 40).result()

for row in result:
# Access columns with [] operator
Expand Down
Loading