Skip to content

Commit

Permalink
Add first version of files
Browse files Browse the repository at this point in the history
  • Loading branch information
ConsFontaniliLuca committed Aug 13, 2018
1 parent 1672a0c commit f028a5d
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ venv.bak/

# mypy
.mypy_cache/

#PyCharm
.idea
21 changes: 21 additions & 0 deletions pysqoop/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 lucafon

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.
2 changes: 2 additions & 0 deletions pysqoop/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# pysqoop
A python package that lets you sqoop into HDFS data from RDBMS using sqoop
82 changes: 82 additions & 0 deletions pysqoop/pysqoop/SqoopImport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from subprocess import call, STDOUT


class Sqoop():
_EMPTY_TABLE_AND_QUERY_PARAMETERS_EXCEPTION = '--table or --query is required for import. (Or use sqoop import-all-tables.)\nTry --help for usage instructions.'
_ALL_EMPTY_PARAMETERS_EXCEPTION = 'all parameters are empty'
_WRONG_INCREMENTAL_ATTRIBUTE_EXCEPTION = "--incremental needs either 'append' or 'lastmodified'"
_properties = {}

def __init__(self, create=None, fields_terminated_by=None, input_escaped_by=None, enclosed_by=None, escaped_by=None,
null_string=None, null_non_string=None, table=None, delete_target_dir=None, connect=None,
username=None, password=None, map_colmn_java=None, help=None, query=None, incremental=None,
check_column=None, last_value=None, connection_manager=None, connection_param_file=None, driver=None,
hadoop_home=None, hadoop_mapred_home=None, metadata_transaction_isolation_level=None, password_alias=None,
password_file=None, relaxed_isolation=None, skip_dist_cache=None, temporary_root_dir=None, verbose=None,
num_mappers=None):
self._properties['--create'] = create
self._properties['--fields-terminated-by'] = fields_terminated_by
self._properties['--input-escaped-by'] = input_escaped_by
self._properties['--enclosed-by'] = enclosed_by
self._properties['--escaped-by'] = escaped_by
self._properties['--null-string'] = null_string
self._properties['--null-non-string'] = null_non_string
self._properties['--table'] = table
self._properties['--delete-target-dir'] = delete_target_dir
self._properties['--connect'] = connect
self._properties['--username'] = username
self._properties['--password'] = password
self._properties['--map-column-java'] = map_colmn_java
self._properties['--incremental'] = incremental
self._properties['--check-column'] = check_column
self._properties['--last-value'] = last_value
self._properties['--connection-manager'] = connection_manager
self._properties['--connection-param-file'] = connection_param_file
self._properties['--driver'] = driver
self._properties['--hadoop-home'] = hadoop_home
self._properties['--hadoop-mapred-home'] = hadoop_mapred_home
self._properties['--metadata-transaction-isolation-level'] = metadata_transaction_isolation_level
self._properties['--password-alias'] = password_alias
self._properties['--password-file'] = password_file
self._properties['--relaxed-isolation'] = relaxed_isolation
self._properties['--skip-dist-cache'] = skip_dist_cache
self._properties['--temporary-rootdir'] = temporary_root_dir
self._properties['--verbose'] = verbose
self._properties['--num-mappers'] = num_mappers
if help:
self._properties['--help'] = ''
self._properties['--query'] = query
self._perform_checks()
self._coomand = 'sqoop import {}'.format(
' '.join(['{} {}'.format(key, val) for key, val in self._properties.items() if val is not None]))

def _perform_checks(self):
if all(v is None for v in self._properties.values()):
raise Exception(self._ALL_EMPTY_PARAMETERS_EXCEPTION)
if not self._properties['--table'] and not self._properties['--query'] and '--help' not in self._properties.keys():
raise Exception(self._EMPTY_TABLE_AND_QUERY_PARAMETERS_EXCEPTION)
if self._properties['--incremental'] and self._properties['--incremental'] not in ['lastmodified', 'append']:
raise Exception(self._WRONG_INCREMENTAL_ATTRIBUTE_EXCEPTION)

def properties(self):
return self._properties

def command(self):
return self._coomand

def perform_import(self):
try:
print(self._coomand)
return call(self._coomand, shell=True)
except Exception as e:
print(e)
return 90


if __name__ == '__main__':
# sqoop = Sqoop(fields_terminated_by='\\;', escaped_by='\\\\', enclosed_by='\'\\\"\'', password='RSS_OWNER', null_non_string='\'\'')
# c = sqoop.perform_import()
# print('exit code: {}'.format(c))
sqoop = Sqoop(help=True)
c = sqoop.perform_import()
print('exit code: {}'.format(c))
1 change: 1 addition & 0 deletions pysqoop/pysqoop/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name = "pysqoop"
21 changes: 21 additions & 0 deletions pysqoop/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import setuptools

with open("README.md", "r") as fh:
long_description = fh.read()

setuptools.setup(
name="pysqoop",
version="0.0.1",
author="Luca Fontanili",
author_email="[email protected]",
description="A simple package to let you Sqoop in data in python",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/lucafon/pysqoop",
packages=setuptools.find_packages(),
classifiers=(
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
),
)
Empty file added unittests/__init__.py
Empty file.
25 changes: 25 additions & 0 deletions unittests/unintary_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import unittest
from pysqoop.pysqoop.SqoopImport import Sqoop


class TestStringMethods(unittest.TestCase):

def test_empty_sqoop(self):
try:
Sqoop()
except Exception as e:
self.assertEqual(str(e), 'all parameters are empty')

def test_properties_not_empty(self):
try:
Sqoop(fields_terminated_by='\"')
except Exception as e:
self.assertEqual(str(e), Sqoop._EMPTY_TABLE_AND_QUERY_PARAMETERS_EXCEPTION)

def test_parameters_order(self):
for iteration in range(0, 10000):
sqoop = Sqoop(null_string='\'\'', fields_terminated_by='\"', table='prova')
self.assertEqual(sqoop.command(), 'sqoop import --fields-terminated-by \" --null-string \'\' --table prova')

if __name__ == '__main__':
unittest.main()

0 comments on commit f028a5d

Please sign in to comment.