Skip to content

Commit 27e52bc

Browse files
paviuspavius-poc
andauthored
Add "create_schema" (#14)
Co-authored-by: Eran Duchan <erand@iguazio.com>
1 parent 8b7badc commit 27e52bc

File tree

2 files changed

+129
-1
lines changed

2 files changed

+129
-1
lines changed

tests/test_client.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,64 @@ def _object_contents(idx):
220220
self.assertEqual(_object_contents(response_idx), response.body.decode('utf-8'))
221221

222222

223+
class TestSchema(Test):
224+
225+
def setUp(self):
226+
super(TestSchema, self).setUp()
227+
228+
self._schema_dir = '/v3io-py-test-schema'
229+
self._schema_path = os.path.join(self._schema_dir, '.%23schema')
230+
231+
# clean up
232+
self._delete_dir(self._schema_dir)
233+
234+
def test_create_schema(self):
235+
# write schema
236+
self._client.create_schema(container=self._container,
237+
path=self._schema_dir,
238+
key='key_field',
239+
fields=[
240+
{
241+
'name': 'key_field',
242+
'type': 'string',
243+
'nullable': False
244+
},
245+
{
246+
'name': 'data_field_0',
247+
'type': 'long',
248+
'nullable': True
249+
},
250+
{
251+
'name': 'data_field_1',
252+
'type': 'double',
253+
'nullable': True
254+
},
255+
])
256+
257+
# write to test the values in the UI (requires breaking afterwards)
258+
items = {
259+
'a': {'data_field_0': 30, 'data_field_1': 100},
260+
'b': {'data_field_0': 300, 'data_field_1': 1000},
261+
'c': {'data_field_0': 3000, 'data_field_1': 10000},
262+
}
263+
264+
for item_key, item_attributes in future.utils.viewitems(items):
265+
self._client.put_item(container=self._container,
266+
path=v3io.common.helpers.url_join(self._schema_dir, item_key),
267+
attributes=item_attributes)
268+
269+
# verify the scehma
270+
response = self._client.get_object(container=self._container,
271+
path=self._schema_path,
272+
raise_for_status=v3io.dataplane.RaiseForStatus.never)
273+
274+
self.assertEqual(
275+
'{"hashingBucketNum":0,"key":"key_field","fields":[{"name":"key_field","type":"string","nullable":false},'
276+
'{"name":"data_field_0","type":"long","nullable":true},{"name":"data_field_1","type":"double"'
277+
',"nullable":true}]}',
278+
response.body.decode('utf-8'))
279+
280+
223281
class TestEmd(Test):
224282

225283
def setUp(self):

v3io/dataplane/client.py

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import ujson
23

34
import future.utils
45

@@ -307,7 +308,7 @@ def put_item(self,
307308
locals())
308309

309310
def put_items(self, container, path, items, access_key=None, raise_for_status=None, condition=None):
310-
"""A helper to put several items, calling put_item for each.
311+
"""[OBSOLETED] A helper to put several items, calling put_item for each.
311312
312313
Parameters
313314
----------
@@ -786,9 +787,78 @@ def get_records(self,
786787
locals(),
787788
v3io.dataplane.output.GetRecordsOutput)
788789

790+
#
791+
# Helpers
792+
#
793+
794+
def create_schema(self,
795+
container,
796+
path,
797+
access_key=None,
798+
raise_for_status=None,
799+
transport_actions=None,
800+
key=None,
801+
fields=None):
802+
"""Creates a KV schema file
803+
804+
Parameters
805+
----------
806+
container (Required) : str
807+
The container on which to operate.
808+
path (Required) : str
809+
The path of the object
810+
access_key (Optional) : str
811+
The access key with which to authenticate. Defaults to the V3IO_ACCESS_KEY env.
812+
key (Required) : str
813+
The key field name
814+
fields (Required) : list of dicts
815+
A dictionary of fields, where each item has:
816+
- name (string)
817+
- type (string - one of string, double, long)
818+
- nullable (boolean)
819+
820+
Example: [
821+
{
822+
'name': 'my_field',
823+
'type': 'string',
824+
'nullable': False
825+
},
826+
{
827+
'name': 'who',
828+
'type': 'string',
829+
"nullable": True
830+
}
831+
]
832+
833+
Return Value
834+
----------
835+
A `Response` object
836+
"""
837+
put_object_args = locals()
838+
put_object_args['path'] = os.path.join(put_object_args['path'], '.%23schema')
839+
put_object_args['offset'] = 0
840+
put_object_args['body'] = self._get_schema_contents(key, fields)
841+
del(put_object_args['key'])
842+
del (put_object_args['fields'])
843+
844+
return self._transport.request(container,
845+
access_key or self._access_key,
846+
raise_for_status,
847+
transport_actions,
848+
v3io.dataplane.request.encode_put_object,
849+
put_object_args)
850+
789851
@staticmethod
790852
def _normalize_stream_path(path):
791853
if not path.endswith('/'):
792854
return path + '/'
793855

794856
return path
857+
858+
@staticmethod
859+
def _get_schema_contents(key, fields):
860+
return ujson.dumps({
861+
'hashingBucketNum': 0,
862+
'key': key,
863+
'fields': fields
864+
})

0 commit comments

Comments
 (0)