diff --git a/python/sdk/client/Abstract.py b/python/sdk/client/Abstract.py index 94b1c1710dfbe85ccb915cb162654af0c7957133..3344242bb6704f0902f62e6e638b489bf6f27421 100644 --- a/python/sdk/client/Abstract.py +++ b/python/sdk/client/Abstract.py @@ -58,12 +58,13 @@ class VectorColumn(Column): """ def __init__(self, name, dimension=0, - index_type=AbstactIndexType.RAW, - store_raw_vector=False): + index_type=None, + store_raw_vector=False, + type=None): self.dimension = dimension self.index_type = index_type self.store_raw_vector = store_raw_vector - super(VectorColumn, self).__init__(name, type=AbstractColumnType.VECTOR) + super(VectorColumn, self).__init__(name, type=type) class TableSchema(object): diff --git a/python/sdk/client/Client.py b/python/sdk/client/Client.py index 5426176444a2a393225aa3852dc208c63ba7eec6..57fd3b135a2d91e658fac800681147bc266ff06c 100644 --- a/python/sdk/client/Client.py +++ b/python/sdk/client/Client.py @@ -2,6 +2,7 @@ import logging, logging.config from thrift.transport import TSocket from thrift.transport import TTransport +from thrift.transport.TTransport import TTransportException from thrift.protocol import TBinaryProtocol, TCompactProtocol, TJSONProtocol from thrift.Thrift import TException, TApplicationException, TType @@ -38,15 +39,10 @@ class IndexType(AbstactIndexType): class ColumnType(AbstractColumnType): - # INVALID = 1 - # INT8 = 2 - # INT16 = 3 - # INT32 = 4 - # INT64 = 5 + FLOAT32 = 6 FLOAT64 = 7 DATE = 8 - # VECTOR = 9 INVALID = TType.STOP INT8 = TType.I08 @@ -62,13 +58,12 @@ class Prepare(object): def column(cls, name, type): """ Table column param - + # todo type :param type: ColumnType, type of the column :param name: str, name of the column :return Column """ - # TODO type in Thrift, may have error temp_column = Column(name=name, type=type) return ttypes.Column(name=temp_column.name, type=temp_column.type) @@ -81,7 +76,7 @@ class Prepare(object): :param dimension: int64, vector dimension :param index_type: IndexType - :param store_raw_vector: Bool, Is vector self stored in the table + :param store_raw_vector: Bool `Column`: :param name: Name of the column @@ -124,8 +119,8 @@ class Prepare(object): Name of the column - type: ColumnType, default=ColumnType.VECTOR, can't change - :param attribute_columns: List of Columns. Attribute - columns are Columns whose type aren't ColumnType.VECTOR + :param attribute_columns: List of Columns. Attribute columns are Columns, + whose types aren't ColumnType.VECTOR `Column`: - name: str @@ -266,7 +261,7 @@ class MegaSearch(ConnectIntf): transport = TSocket.TSocket(host=host, port=port) self.transport = TTransport.TBufferedTransport(transport) - protocol = TJSONProtocol.TJSONProtocol(transport) + protocol = TBinaryProtocol.TBinaryProtocol(transport) self.client = MegasearchService.Client(protocol) try: @@ -312,8 +307,9 @@ class MegaSearch(ConnectIntf): raise NotConnectError('Please Connect to the server first!') try: + LOGGER.error(param) self.client.CreateTable(param) - except (TApplicationException, TException) as e: + except (TApplicationException, ) as e: LOGGER.error('Unable to create table') return Status(Status.INVALID, str(e)) return Status(message='Table {} created!'.format(param.table_name)) @@ -463,11 +459,10 @@ class MegaSearch(ConnectIntf): # TODO How to get server version pass - def server_status(self, cmd): + def server_status(self, cmd=None): """ Provide server status :return: Server status """ - self.client.Ping(cmd) - pass + return self.client.Ping(cmd) diff --git a/python/sdk/examples/connection_exp.py b/python/sdk/examples/connection_exp.py index dbe5949274b1103a02df73b308b538269bc5a01e..cf761d129a8cd3d0d21c2086896f3e56ccf3cde5 100644 --- a/python/sdk/examples/connection_exp.py +++ b/python/sdk/examples/connection_exp.py @@ -1,5 +1,8 @@ from client.Client import MegaSearch, Prepare, IndexType, ColumnType from client.Status import Status +import time + +from megasearch.thrift import MegasearchService, ttypes def main(): @@ -13,31 +16,65 @@ def main(): is_connected = mega.connected print('Connect status: {}'.format(is_connected)) - # # Create table with 1 vector column, 1 attribute column and 1 partition column - # # 1. prepare table_schema - # vector_column = { - # 'name': 'fake_vec_name01', - # 'store_raw_vector': True, - # 'dimension': 10 - # } - # attribute_column = { - # 'name': 'fake_attri_name01', - # 'type': ColumnType.DATE, - # } + # Create table with 1 vector column, 1 attribute column and 1 partition column + # 1. prepare table_schema + + # table_schema = Prepare.table_schema( + # table_name='fake_table_name' + time.strftime('%H%M%S'), # - # table = { - # 'table_name': 'fake_table_name01', - # 'vector_columns': [Prepare.vector_column(**vector_column)], - # 'attribute_columns': [Prepare.column(**attribute_column)], - # 'partition_column_names': ['fake_attri_name01'] - # } - # table_schema = Prepare.table_schema(**table) + # vector_columns=[Prepare.vector_column( + # name='fake_vector_name' + time.strftime('%H%M%S'), + # store_raw_vector=False, + # dimension=256)], # - # # 2. Create Table - # create_status = mega.create_table(table_schema) - # print('Create table status: {}'.format(create_status)) + # attribute_columns=[], + # + # partition_column_names=[] + # ) + + # get server version + print(mega.server_status('version')) + + # show tables and their description + statu, tables = mega.show_tables() + print(tables) + + for table in tables: + s,t = mega.describe_table(table) + print('table: {}'.format(t)) + + # Create table + # 1. create table schema + table_schema_full = MegasearchService.TableSchema( + table_name='fake' + time.strftime('%H%M%S'), + + vector_column_array=[MegasearchService.VectorColumn( + base=MegasearchService.Column( + name='111', + type=ttypes.TType.I32 + ), + dimension=256, + )], + + attribute_column_array=[], + + partition_column_name_array=None + ) + + table_schema_empty = MegasearchService.TableSchema( + table_name='fake' + time.strftime('%H%M%S'), + + vector_column_array=[MegasearchService.VectorColumn()], + + attribute_column_array=[], + + partition_column_name_array=None + ) + # 2. Create Table + create_status = mega.create_table(table_schema_full) + print('Create table status: {}'.format(create_status)) - mega.server_status('ok!') + # add_vector # Disconnect discnn_status = mega.disconnect() diff --git a/python/sdk/tests/TestClient.py b/python/sdk/tests/TestClient.py index 51ce1d62a98cba33f856f0003a819e88740ebe0f..3ff6bf9e41a5829b1054fdbf06a5a9396cb6bdc0 100644 --- a/python/sdk/tests/TestClient.py +++ b/python/sdk/tests/TestClient.py @@ -14,6 +14,7 @@ from client.Exceptions import ( from thrift.transport.TSocket import TSocket from megasearch.thrift import ttypes, MegasearchService +from thrift.transport.TTransport import TTransportException LOGGER = logging.getLogger(__name__) @@ -38,7 +39,6 @@ def vector_column_factory(): return { 'name': fake.name(), 'dimension': fake.dim(), - 'index_type': IndexType.IVFFLAT, 'store_raw_vector': True } @@ -46,7 +46,7 @@ def vector_column_factory(): def column_factory(): return { 'name': fake.table_name(), - 'type': IndexType.RAW + 'type': ColumnType.INT32 } @@ -146,9 +146,10 @@ class TestTable: def test_false_create_table(self, client): param = table_schema_factory() - res = client.create_table(param) - LOGGER.error('{}'.format(res)) - assert res != Status.OK + with pytest.raises(TTransportException): + res = client.create_table(param) + LOGGER.error('{}'.format(res)) + assert res != Status.OK @mock.patch.object(MegasearchService.Client, 'DeleteTable') def test_delete_table(self, DeleteTable, client):