Compare commits
60 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
db923e8ec1 | 5 days ago |
|
|
9d00a459eb | 5 days ago |
|
|
b80c076ec9 | 5 days ago |
|
|
9bf19be8e9 | 3 weeks ago |
|
|
8381f4cbc0 | 3 weeks ago |
|
|
2054a8b095 | 3 weeks ago |
|
|
7aec155334 | 3 weeks ago |
|
|
3267f93331 | 2 months ago |
|
|
a065ac8f12 | 2 months ago |
|
|
72ab0d1708 | 2 months ago |
|
|
2021abd1da | 2 months ago |
|
|
977aa91045 | 2 months ago |
|
|
d154ac3cd0 | 5 months ago |
|
|
e0e48a3d02 | 6 months ago |
|
|
aba887ec29 | 6 months ago |
|
|
570f2294b9 | 6 months ago |
|
|
e3efc70c01 | 6 months ago |
|
|
6ffc7ed7b5 | 6 months ago |
|
|
3025e6571b | 6 months ago |
|
|
a42ee59129 | 7 months ago |
|
|
b1975d6a42 | 9 months ago |
|
|
c7a5d42f42 | 9 months ago |
|
|
2eee726191 | 9 months ago |
|
|
bf32c54cd4 | 10 months ago |
|
|
4fbf2d495a | 10 months ago |
|
|
73fa9d90a9 | 10 months ago |
|
|
fce888606c | 10 months ago |
|
|
2d359db5fa | 10 months ago |
|
|
cdeebd3ce4 | 10 months ago |
|
|
c1bc167b7f | 10 months ago |
|
|
d8dd50ab47 | 10 months ago |
|
|
a0b0a8a26f | 10 months ago |
|
|
97c5ae6fb3 | 10 months ago |
|
|
a6da232d5f | 10 months ago |
|
|
e7df1e967f | 11 months ago |
|
|
a4b4a453bb | 11 months ago |
|
|
a022bdf92f | 11 months ago |
|
|
93537095a4 | 11 months ago |
|
|
329a575f89 | 11 months ago |
|
|
0dbb0a38e5 | 11 months ago |
|
|
6637405898 | 11 months ago |
|
|
e82145690b | 12 months ago |
|
|
5c423205c5 | 12 months ago |
|
|
b2a2e49858 | 1 year ago |
|
|
0fd29207cf | 1 year ago |
|
|
5ee0186e58 | 1 year ago |
|
|
541ae41786 | 1 year ago |
|
|
dc9329218a | 1 year ago |
|
|
990bb343a4 | 1 year ago |
|
|
bcf25a4e27 | 1 year ago |
|
|
a2ab60660e | 1 year ago |
|
|
92db89daaf | 1 year ago |
|
|
a28848194a | 1 year ago |
|
|
5dbe541025 | 1 year ago |
|
|
ea1cb7b1bb | 1 year ago |
|
|
8904c7184a | 1 year ago |
|
|
4e97b32530 | 1 year ago |
|
|
a1cf78a889 | 1 year ago |
|
|
685aac7d6b | 1 year ago |
|
|
07be81bace | 1 year ago |
@ -0,0 +1,2 @@
|
||||
cd /D "%~dp0"
|
||||
python transport %1 %2 %3 %4 %5 %6
|
||||
@ -0,0 +1,54 @@
|
||||
[build-system]
|
||||
requires = ["setuptools>=61.0", "wheel"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "data-transport"
|
||||
dynamic = ["version"]
|
||||
authors = [
|
||||
{name="Steve L. Nyemba" , email = "info@the-phi.com"},
|
||||
]
|
||||
description = ""
|
||||
readme = "README.md"
|
||||
license = {text = "LICENSE"}
|
||||
keywords = ["mongodb","duckdb","couchdb","rabbitmq","file","read","write","s3","sqlite"]
|
||||
classifiers = [
|
||||
"License :: OSI Approved :: MIT License",
|
||||
"Topic :: Utilities",
|
||||
]
|
||||
dependencies = [
|
||||
"termcolor","sqlalchemy", "aiosqlite","duckdb-engine",
|
||||
"mysql-connector-python","psycopg2-binary","nzpy","pymssql","duckdb-engine","aiosqlite",
|
||||
"typer","pandas","numpy","sqlalchemy","pyarrow","smart-open",
|
||||
"plugin-ix@git+https://github.com/lnyemba/plugins-ix"
|
||||
]
|
||||
[project.optional-dependencies]
|
||||
#sql = ["mysql-connector-python","psycopg2-binary","nzpy","pymssql","duckdb-engine","aiosqlite"]
|
||||
nosql = ["pymongo","cloudant"]
|
||||
cloud = ["boto","boto3","botocore","pyncclient","pandas-gbq","google-cloud-bigquery","google-cloud-bigquery-storage", "databricks-sqlalchemy","pyncclient","boto3","boto","botocore"]
|
||||
warehouse = ["pydrill","pyspark","sqlalchemy_drill"]
|
||||
other = ["pika","flask-session"]
|
||||
all = ["pymongo","cloudant","pandas-gbq","google-cloud-bigquery","google-cloud-bigquery-storage", "databricks-sqlalchemy","pyncclient","boto3","boto","botocore","pydrill","pyspark","sqlalchemy_drill", "pika","aiosqlite","boto3","boto","botocore", "pyncclient"]
|
||||
|
||||
[project.urls]
|
||||
Homepage = "https://healthcareio.the-phi.com/git/code/transport.git"
|
||||
|
||||
#[project.scripts]
|
||||
#transport = "transport:main"
|
||||
|
||||
[tool.setuptools]
|
||||
include-package-data = true
|
||||
zip-safe = false
|
||||
script-files = ["bin/transport","bin/transport.cmd"]
|
||||
|
||||
[tool.setuptools.packages.find]
|
||||
include = [ "transport", "transport.*"]
|
||||
|
||||
[tool.setuptools.dynamic]
|
||||
version = {attr = "transport.info.__version__"}
|
||||
#authors = {attr = "transport.__author__"}
|
||||
|
||||
# If you have a info.py file, you might also want to include the author dynamically:
|
||||
# [tool.setuptools.dynamic]
|
||||
# version = {attr = "info.__version__"}
|
||||
# authors = {attr = "info.__author__"}
|
||||
@ -1,28 +0,0 @@
|
||||
"""
|
||||
This is a build file for the
|
||||
"""
|
||||
from setuptools import setup, find_packages
|
||||
import os
|
||||
import sys
|
||||
# from version import __version__,__author__
|
||||
from info import __version__, __author__,__app_name__,__license__
|
||||
|
||||
|
||||
def read(fname):
|
||||
return open(os.path.join(os.path.dirname(__file__), fname)).read()
|
||||
args = {
|
||||
"name":__app_name__,
|
||||
"version":__version__,
|
||||
"author":__author__,"author_email":"info@the-phi.com",
|
||||
"license":__license__,
|
||||
# "packages":["transport","info","transport/sql"]},
|
||||
|
||||
"packages": find_packages(include=['info','transport', 'transport.*'])}
|
||||
args["keywords"]=['mongodb','duckdb','couchdb','rabbitmq','file','read','write','s3','sqlite']
|
||||
args["install_requires"] = ['pyncclient','duckdb-engine','pymongo','sqlalchemy','pandas','typer','pandas-gbq','numpy','cloudant','pika','nzpy','termcolor','boto3','boto','pyarrow','google-cloud-bigquery','google-cloud-bigquery-storage','flask-session','smart_open','botocore','psycopg2-binary','mysql-connector-python','numpy','pymssql']
|
||||
args["url"] = "https://healthcareio.the-phi.com/git/code/transport.git"
|
||||
args['scripts'] = ['bin/transport']
|
||||
# if sys.version_info[0] == 2 :
|
||||
# args['use_2to3'] = True
|
||||
# args['use_2to3_exclude_fixers']=['lib2to3.fixes.fix_import']
|
||||
setup(**args)
|
||||
@ -1,19 +0,0 @@
|
||||
"""
|
||||
This file will be intended to handle duckdb database
|
||||
"""
|
||||
|
||||
import duckdb
|
||||
from transport.common import Reader,Writer
|
||||
|
||||
class Duck(Reader):
|
||||
def __init__(self,**_args):
|
||||
super().__init__(**_args)
|
||||
self._path = None if 'path' not in _args else _args['path']
|
||||
self._handler = duckdb.connect() if not self._path else duckdb.connect(self._path)
|
||||
|
||||
|
||||
class DuckReader(Duck) :
|
||||
def __init__(self,**_args):
|
||||
super().__init__(**_args)
|
||||
def read(self,**_args) :
|
||||
pass
|
||||
@ -0,0 +1,25 @@
|
||||
__app_name__ = 'data-transport'
|
||||
__author__ = 'Steve L. Nyemba'
|
||||
__version__= '2.4.34'
|
||||
__edition__= 'enterprise'
|
||||
__email__ = "info@the-phi.com"
|
||||
__license__=f"""
|
||||
Copyright 2010 - 2024, Steve L. Nyemba
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
"""
|
||||
|
||||
__whatsnew__=f"""version {__version__},
|
||||
1. Added support for read/write logs as well as plugins (when applied)
|
||||
2. Bug fix with duckdb (adding readonly) for readers because there are issues with threads & processes
|
||||
3. support for streaming data, important to use this with large volumes of data
|
||||
|
||||
|
||||
|
||||
|
||||
"""
|
||||
@ -0,0 +1,7 @@
|
||||
"""
|
||||
This namespace/package is intended to handle read/writes against data warehouse solutions like :
|
||||
- apache iceberg
|
||||
- clickhouse (...)
|
||||
"""
|
||||
|
||||
from . import iceberg, drill
|
||||
@ -0,0 +1,57 @@
|
||||
import sqlalchemy
|
||||
import pandas as pd
|
||||
from .. sql.common import BaseReader , BaseWriter
|
||||
import sqlalchemy as sqa
|
||||
|
||||
def template():
|
||||
return {'host':'localhost','port':8047,'ssl':False,'table':None,'database':None}
|
||||
class Drill :
|
||||
__template = {'host':None,'port':None,'ssl':None,'table':None,'database':None}
|
||||
def __init__(self,**_args):
|
||||
|
||||
self._host = _args['host'] if 'host' in _args else 'localhost'
|
||||
self._port = _args['port'] if 'port' in _args else self.get_default_port()
|
||||
self._ssl = False if 'ssl' not in _args else _args['ssl']
|
||||
|
||||
self._table = _args['table'] if 'table' in _args else None
|
||||
if self._table and '.' in self._table :
|
||||
_seg = self._table.split('.')
|
||||
if len(_seg) > 2 :
|
||||
self._schema,self._database = _seg[:2]
|
||||
else:
|
||||
|
||||
self._database=_args['database']
|
||||
self._schema = self._database.split('.')[0]
|
||||
|
||||
def _get_uri(self,**_args):
|
||||
return f'drill+sadrill://{self._host}:{self._port}/{self._database}?use_ssl={self._ssl}'
|
||||
def get_provider(self):
|
||||
return "drill+sadrill"
|
||||
def get_default_port(self):
|
||||
return "8047"
|
||||
def meta(self,**_args):
|
||||
_table = _args['table'] if 'table' in _args else self._table
|
||||
if '.' in _table :
|
||||
_schema = _table.split('.')[:2]
|
||||
_schema = '.'.join(_schema)
|
||||
_table = _table.split('.')[-1]
|
||||
else:
|
||||
_schema = self._schema
|
||||
|
||||
# _sql = f"select COLUMN_NAME AS name, CASE WHEN DATA_TYPE ='CHARACTER VARYING' THEN 'CHAR ( 125 )' ELSE DATA_TYPE END AS type from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='{_schema}' and TABLE_NAME='{_table}'"
|
||||
_sql = f"select COLUMN_NAME AS name, CASE WHEN DATA_TYPE ='CHARACTER VARYING' THEN 'CHAR ( '||COLUMN_SIZE||' )' ELSE DATA_TYPE END AS type from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='{_schema}' and TABLE_NAME='{_table}'"
|
||||
try:
|
||||
_df = pd.read_sql(_sql,self._engine)
|
||||
return _df.to_dict(orient='records')
|
||||
except Exception as e:
|
||||
print (e)
|
||||
pass
|
||||
return []
|
||||
class Reader (Drill,BaseReader) :
|
||||
def __init__(self,**_args):
|
||||
super().__init__(**_args)
|
||||
self._chunksize = 0 if 'chunksize' not in _args else _args['chunksize']
|
||||
self._engine= sqa.create_engine(self._get_uri(),future=True)
|
||||
class Writer(Drill,BaseWriter):
|
||||
def __init__(self,**_args):
|
||||
super().__init__(self,**_args)
|
||||
@ -0,0 +1,155 @@
|
||||
"""
|
||||
dependency:
|
||||
- spark and SPARK_HOME environment variable must be set
|
||||
NOTE:
|
||||
When using streaming option, insure that it is inline with default (1000 rows) or increase it in spark-defaults.conf
|
||||
|
||||
"""
|
||||
from pyspark.sql import SparkSession
|
||||
from pyspark import SparkContext
|
||||
from pyspark.sql.types import *
|
||||
from pyspark.sql.functions import col, to_date, to_timestamp
|
||||
import copy
|
||||
|
||||
|
||||
def template():
|
||||
return {'catalog':None,'database':None,'table':None}
|
||||
|
||||
class Iceberg :
|
||||
def __init__(self,**_args):
|
||||
"""
|
||||
providing catalog meta information (you must get this from apache iceberg)
|
||||
"""
|
||||
#
|
||||
# Turning off logging (it's annoying & un-professional)
|
||||
#
|
||||
# _spconf = SparkContext()
|
||||
# _spconf.setLogLevel("ERROR")
|
||||
#
|
||||
# @TODO:
|
||||
# Make arrangements for additional configuration elements
|
||||
#
|
||||
self._session = SparkSession.builder.appName("data-transport").getOrCreate()
|
||||
self._session.conf.set("spark.sql.parquet.outputTimestampType", "TIMESTAMP_MICROS")
|
||||
# self._session.sparkContext.setLogLevel("ERROR")
|
||||
self._catalog = self._session.catalog
|
||||
self._table = _args['table'] if 'table' in _args else None
|
||||
|
||||
if 'catalog' in _args :
|
||||
#
|
||||
# Let us set the default catalog
|
||||
self._catalog.setCurrentCatalog(_args['catalog'])
|
||||
|
||||
else:
|
||||
# No current catalog has been set ...
|
||||
pass
|
||||
if 'database' in _args :
|
||||
self._database = _args['database']
|
||||
self._catalog.setCurrentDatabase(self._database)
|
||||
else:
|
||||
#
|
||||
# Should we set the default as the first one if available ?
|
||||
#
|
||||
pass
|
||||
self._catalogName = self._catalog.currentCatalog()
|
||||
self._databaseName = self._catalog.currentDatabase()
|
||||
def meta (self,**_args) :
|
||||
"""
|
||||
This function should return the schema of a table (only)
|
||||
"""
|
||||
_schema = []
|
||||
try:
|
||||
_table = _args['table'] if 'table' in _args else self._table
|
||||
_tableName = self._getPrefix(**_args) + f".{_table}"
|
||||
_tmp = self._session.table(_tableName).schema
|
||||
_schema = _tmp.jsonValue()['fields']
|
||||
for _item in _schema :
|
||||
del _item['nullable'],_item['metadata']
|
||||
except Exception as e:
|
||||
|
||||
pass
|
||||
return _schema
|
||||
def _getPrefix (self,**_args):
|
||||
_catName = self._catalogName if 'catalog' not in _args else _args['catalog']
|
||||
_datName = self._databaseName if 'database' not in _args else _args['database']
|
||||
|
||||
return '.'.join([_catName,_datName])
|
||||
def apply(self,_query):
|
||||
"""
|
||||
sql query/command to run against apache iceberg
|
||||
"""
|
||||
return self._session.sql(_query).toPandas()
|
||||
def has (self,**_args):
|
||||
try:
|
||||
_prefix = self._getPrefix(**_args)
|
||||
if _prefix.endswith('.') :
|
||||
return False
|
||||
return _args['table'] in [_item.name for _item in self._catalog.listTables(_prefix)]
|
||||
except Exception as e:
|
||||
print (e)
|
||||
return False
|
||||
|
||||
def close(self):
|
||||
self._session.stop()
|
||||
class Reader(Iceberg) :
|
||||
def __init__(self,**_args):
|
||||
super().__init__(**_args)
|
||||
def read(self,**_args):
|
||||
_table = self._table
|
||||
_prefix = self._getPrefix(**_args)
|
||||
if 'table' in _args or _table:
|
||||
_table = _args['table'] if 'table' in _args else _table
|
||||
_table = _prefix + f'.{_table}'
|
||||
return self._session.table(_table).toPandas()
|
||||
else:
|
||||
sql = _args['sql']
|
||||
return self._session.sql(sql).toPandas()
|
||||
pass
|
||||
class Writer (Iceberg):
|
||||
"""
|
||||
Writing data to an Apache Iceberg data warehouse (using pyspark)
|
||||
"""
|
||||
def __init__(self,**_args):
|
||||
super().__init__(**_args)
|
||||
self._mode = 'append' if 'mode' not in _args else _args['mode']
|
||||
self._table = None if 'table' not in _args else _args['table']
|
||||
def format (self,_schema) :
|
||||
_iceSchema = StructType([])
|
||||
_map = {'integer':IntegerType(),'float':DoubleType(),'double':DoubleType(),'date':DateType(),
|
||||
'timestamp':TimestampType(),'datetime':TimestampType(),'string':StringType(),'varchar':StringType()}
|
||||
for _item in _schema :
|
||||
_name = _item['name']
|
||||
_type = _item['type'].lower()
|
||||
if _type not in _map :
|
||||
_iceType = StringType()
|
||||
else:
|
||||
_iceType = _map[_type]
|
||||
|
||||
_iceSchema.add (StructField(_name,_iceType,True))
|
||||
return _iceSchema if len(_iceSchema) else []
|
||||
def write(self,_data,**_args):
|
||||
_prefix = self._getPrefix(**_args)
|
||||
if 'table' not in _args and not self._table :
|
||||
raise Exception (f"Table Name should be specified for catalog/database {_prefix}")
|
||||
_schema = self.format(_args['schema']) if 'schema' in _args else []
|
||||
if not _schema :
|
||||
rdd = self._session.createDataFrame(_data,verifySchema=False)
|
||||
else :
|
||||
rdd = self._session.createDataFrame(_data,schema=_schema,verifySchema=True)
|
||||
_mode = self._mode if 'mode' not in _args else _args['mode']
|
||||
_table = self._table if 'table' not in _args else _args['table']
|
||||
|
||||
# print (_data.shape,_mode,_table)
|
||||
|
||||
if not self._session.catalog.tableExists(_table):
|
||||
# # @TODO:
|
||||
# # add partitioning information here
|
||||
rdd.writeTo(_table).using('iceberg').create()
|
||||
|
||||
# # _mode = 'overwrite'
|
||||
# # rdd.write.format('iceberg').mode(_mode).saveAsTable(_table)
|
||||
else:
|
||||
# rdd.writeTo(_table).append()
|
||||
# # _table = f'{_prefix}.{_table}'
|
||||
|
||||
rdd.coalesce(10).write.format('iceberg').mode('append').save(_table)
|
||||
Loading…
Reference in new issue