-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[API-965] Implement SQL JSON support (#529)
* Implement SQL JSON support This PR adds implementation, tests, code samples, and documentation for the SQL JSON column type. * adress review comments * update limitations section of the sql
- Loading branch information
Showing
7 changed files
with
204 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import hazelcast | ||
|
||
from hazelcast.core import HazelcastJsonValue | ||
|
||
client = hazelcast.HazelcastClient() | ||
employees = client.get_map("employees").blocking() | ||
|
||
# Populate some data | ||
employees.put(0, HazelcastJsonValue('{"name": "Alice", "age": 32}')) | ||
employees.put(1, HazelcastJsonValue('{"name": "John", "age": 42}')) | ||
employees.put(2, HazelcastJsonValue('{"name": "Jake", "age": 18}')) | ||
|
||
# Create mapping for the employees map. This needs to be done only once per map. | ||
client.sql.execute( | ||
""" | ||
CREATE OR REPLACE MAPPING employees | ||
TYPE IMap | ||
OPTIONS ( | ||
'keyFormat' = 'int', | ||
'valueFormat' = 'json' | ||
) | ||
""" | ||
).result() | ||
|
||
# Select the names of employees older than 25 | ||
result = client.sql.execute( | ||
""" | ||
SELECT JSON_VALUE(this, '$.name') AS name | ||
FROM employees | ||
WHERE JSON_VALUE(this, '$.age' RETURNING INT) > 25 | ||
""" | ||
).result() | ||
|
||
for row in result: | ||
print(f"Name: {row['name']}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
hazelcast/protocol/codec/custom/hazelcast_json_value_codec.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from hazelcast.protocol.builtin import CodecUtil | ||
from hazelcast.protocol.client_message import END_FRAME_BUF, END_FINAL_FRAME_BUF, BEGIN_FRAME_BUF | ||
from hazelcast.protocol.builtin import StringCodec | ||
from hazelcast.core import HazelcastJsonValue | ||
|
||
|
||
class HazelcastJsonValueCodec: | ||
@staticmethod | ||
def encode(buf, hazelcast_json_value, is_final=False): | ||
buf.extend(BEGIN_FRAME_BUF) | ||
StringCodec.encode(buf, hazelcast_json_value.value) | ||
if is_final: | ||
buf.extend(END_FINAL_FRAME_BUF) | ||
else: | ||
buf.extend(END_FRAME_BUF) | ||
|
||
@staticmethod | ||
def decode(msg): | ||
msg.next_frame() | ||
value = StringCodec.decode(msg) | ||
CodecUtil.fast_forward_to_end_frame(msg) | ||
return HazelcastJsonValue(value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters