def __init__(self): self.validator = Validator() self.db_connector = DBConnector(self.validator.server, self.validator.port, self.validator.user, self.validator.passwd) self.query_executor = QueryExecutor(self.db_connector)
def __init__(self, *args, **kwargs): super(TestQueryExecutor, self).__init__(*args, **kwargs) self.host = 'irlinv-dvbacr16' self.db_connector = DBConnector(self.host, 7001, "rasadmin", "rasadmin") self.query_executor = QueryExecutor(self.db_connector) self.db_connector.open()
class Main: """ Connect to rasserver and run query then return the result to client """ def __init__(self): self.validator = Validator() self.db_connector = DBConnector(self.validator.server, self.validator.port, self.validator.user, self.validator.passwd) self.query_executor = QueryExecutor(self.db_connector) def execute(self): """ Execute the query and get the response to client """ # self.query = 'select {33.0, 33.5}' # open connection to rasserver self.db_connector.open() try: tmp = self.validator.query.lower().strip() if tmp.startswith("select") and "into" not in tmp: """ e.g: select c from RAS_COLLECTIONNAMES as c""" res = self.query_executor.execute_read(self.validator.query) elif self.validator.file: # insert or update with file """e.g: rasql -q 'insert into test2 values $1' -f '/home/rasdaman/101.bin' --mdddomain [0:100] --mddtype 'GreyString' --user rasadmin --passwd rasadmin""" res = self.query_executor.execute_update_from_file(self.validator.query, self.validator.file, self.validator.mdddomain, self.validator.mddtype) else: # just normal update query """e.g: rasql -q 'create collection test2 GreySet1' --user rasadmin --passwd rasadmin """ res = self.query_executor.execute_write(self.validator.query) res_arr = res # Depend on the output (string, file) to write the result from rasserver self.__handle_result(res_arr) except Exception as e: if "error message" in str(e): """ e.g: Error executing query 'select stddev_samp(1f)', error message 'rasdaman error 353: Execution error 353 in line 1, column 8, near token stddev_samp: Operand.' """ error_message = re.findall(r"([^']*)'?", str(e), re.M)[-2] # Write error to stderr print_error(error_message) else: raise finally: # Close the connection to rasserver to release rasserver from this client self.db_connector.close() print("rasql done.") def __handle_result(self, res_arr): if isinstance(res_arr, QueryResult): if res_arr.with_error: print_error(res_arr.error_message()) return """ Handle the result of query from rasserver :param list res_arr: list of result which can be MDDArray or scalar values :return: it will print output as string if --out string or write to file if --out file """ if self.validator.out == OUTPUT_STRING: # Output list of results to console self.__handle_result_as_string(res_arr) elif self.validator.out == OUTPUT_FILE: # Output list of results as files self.__handle_result_as_file(res_arr) def __handle_result_as_string(self, res_arr): """ When query outputs to string, then create the output for it. If query returns scalar, it will look like: 'select 1' --out string Query result collection has 1 element(s): Result element 1: 1 If query returns MDD, it will look like: 'select encode(c[0:1, 0:2], "csv") from test_mr as c' Query result collection has 1 element(s): Result object 1: {0,0,0},{0,0,0} :param list res_arr: output from rasserver """ output = "Query result collection has {} element(s): \n".format(res_arr.size) if res_arr.size > 0: for index, res in enumerate(res_arr): msg = res_arr.to_string(res) if res_arr.is_object else res output += " Result {} {}: {}\n".format(res_arr.nature, index + 1, msg) print(output.strip()) def __handle_result_as_file(self, res_arr): """ When query outputs to file, then write MDDArray to files e.g: rasql -q 'select c[0:1] from test2 as c' --out file with test2 has 5 MDDArray, then it writes to: rasql_1.unknown, rasql_2.unknown,... NOTE: if it has --outfile PATH_TO_OUTPUT_FILE parameter, then it will write to PATH_TO_OUTPUT_FILE instead e.g: rasql -q 'select c[0:1] from test2 as c' --out file --outfile /tmp/test1, then it writes to: /tmp/test1_1, /tmp/test1_2,... :param list res_arr: output from rasserver """ cur_data = self.validator.query.lower() if '"png"' in cur_data: file_ext = "png" elif '"jp2"' in cur_data: file_ext = "jp2" elif '"jpg"' in cur_data: file_ext = "jpg" elif '"bmp"' in cur_data: file_ext = "bmp" elif '"netcdf"' in cur_data: file_ext = "nc" elif '"json"' in cur_data: file_ext = "json" elif '"csv"' in cur_data: file_ext = "csv" elif '"tiff"' in cur_data: file_ext = "tif" elif '"gtiff"' in cur_data: file_ext = "tif" else: file_ext = "unknown" # Only has paramter --out file file_name_prefix = "rasql" # It has --outfile PATH_TO_FILE parameter if self.validator.outfile is not None: file_name_prefix = self.validator.outfile # Write output results to files for i, cur_data in enumerate(res_arr): if res_arr.size > 1: file_name = file_name_prefix + "_{}.{}".format(i + 1, file_ext) else: file_name = file_name_prefix + ".{}".format(file_ext) if res_arr.is_object: with open(file_name, "wb") as binary_file: # If it is MDDArray then write the data inside it binary_file.write(res_arr.to_binary(cur_data)) else: with open(file_name, "w") as text_file: output = " Result {} {}: {}\n".format(res_arr.nature, i + 1, cur_data) # If it is scalar value then just write it text_file.write(output)
q_result = query_executor.execute_update_from_file( "insert into mr values decode($1)", png_filename) if not q_result.error(): elts = q_result.get_elements() print(elts) return elts[0] else: print(q_result.error_message()) if __name__ == '__main__': data_dir = os.path.join(os.path.curdir, "../data") print(data_dir) db_connector = DBConnector("localhost", 7001, "rasadmin", "rasadmin") query_executor = QueryExecutor(db_connector) db_connector.open() if array_type_flag: list_type = query_executor.execute_read( "select c from RAS_MARRAY_TYPES as c") print(f"retrieves {list_type.size} types from rasdaman") for t in list_type: print(t) query_executor.execute_write("create collection gs1 GreySet1") if png_flag: query_executor.execute_write("create collection mr GreySet") oid = import_png(query_executor, os.path.join(data_dir, "mr_1.png"))
#!/usr/bin/python # Gter copyleft # Author: Roberto Marzocchi #import librerie from rasdapy.db_connector import DBConnector from rasdapy.query_executor import QueryExecutor #define the connection to sinergico03 (IP: 10.10.0.19) #on sinergico there is a docker container print('definisco la connessione') db_connector = DBConnector("10.10.0.19", 7001, "rasadmin", "rasadmin") #Create the query executor query_executor = QueryExecutor(db_connector) #open the connection to rasdaman print('Provo a connettermi a rasdaman') db_connector.open() print('Mi sono connesso a rasdaman') result = query_executor.execute_read( "select avg_cells(c) from fwi_20190618 as c") print(result) #close the connection to rasdaman db_connector.close() exit()
class TestQueryExecutor(TestCase): def import_png(self, png_filename): q_result = self.query_executor.execute_update_from_file( "insert into mr values decode($1)", png_filename) if not q_result.error(): elts = q_result.get_elements() print(elts) return elts[0] else: print(q_result.error_message()) @staticmethod def create_uint8_array(nx, ny, nz, with_scaling=True): a = np.arange(nx * ny * nz, dtype=np.uint8).reshape((nx, ny, nz)) if with_scaling: a = a * 127 / (nx * ny * nz) print(a.dtype.byteorder) return RasGMArrayBuilder.from_np_array(a.astype(np.uint8)) @staticmethod def create_3d_array(nx, ny, nz): array = np.zeros(shape=(nx, ny, nz), dtype=np.dtype('f4')) print(array.strides) value = 0. increment = 255. / (nx * ny * nz) for i in range(array.shape[0]): for j in range(array.shape[1]): for k in range(array.shape[2]): array[i, j, k] = value value += increment print(array.dtype.byteorder) plt.imshow(array[100, :, :]) plt.show() return RasGMArrayBuilder.from_np_array(array) def __init__(self, *args, **kwargs): super(TestQueryExecutor, self).__init__(*args, **kwargs) self.host = 'irlinv-dvbacr16' self.db_connector = DBConnector(self.host, 7001, "rasadmin", "rasadmin") self.query_executor = QueryExecutor(self.db_connector) self.db_connector.open() def test_png(self): self.query_executor.execute_write("create collection mr GreySet") oid = self.import_png(os.path.join("../data", "mr_1.png")) result = self.query_executor.execute_read( f"select a from mr as a where oid(a)={oid}") array = result.to_array() plt.imshow(array) plt.show() self.query_executor.execute_write("drop collection mr") def test_array_uint8(self): ras_array = self.create_uint8_array(4, 3, 2, False) print(ras_array) print(ras_array.data) self.query_executor.execute_write( "create collection greycube GreySet3") q_result = self.query_executor.execute_query( "insert into greycube values $1", ras_array) if not q_result.error(): elts = q_result.get_elements() print(elts) oid = elts[0] result = self.query_executor.execute_read( f"select a[1,*:*,*:*] from greycube as a where oid(a)={oid}") array = result.to_array() print(array) print(array.shape) self.query_executor.execute_write("drop collection greycube") def test_array_float(self): ras_array = self.create_3d_array(182, 180, 60) self.query_executor.execute_write( "create collection floatcube FloatSet3") q_result = self.query_executor.execute_query( "insert into floatcube values $1", ras_array) if not q_result.error(): elts = q_result.get_elements() print(elts) oid = elts[0] result = self.query_executor.execute_read( f"select a[150,*:*,*:*] from floatcube as a where oid(a)={oid}" ) array = result.to_array() plt.imshow(array[:, :]) plt.show() self.query_executor.execute_write("drop collection floatcube")