athena-udf


Nameathena-udf JSON
Version 0.2 PyPI version JSON
download
home_pagehttps://github.com/dmarkey/python-athena-udf
SummaryAthena User Defined Functions(UDFs) made easy!
upload_time2022-12-22 21:30:47
maintainer
docs_urlNone
authorDavid Markey
requires_python>=3.7
licenseApache License, Version 2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # athena-udf

[![PyPI](https://img.shields.io/pypi/v/athena-udf.svg)](https://pypi.org/project/athena-udf/)
[![Changelog](https://img.shields.io/github/v/release/dmarkey/python-athena-udf?include_prereleases&label=changelog)](https://github.com/dmarkey/athena-udf/releases)
[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/dmarkey/python-athena-udf/blob/main/LICENSE)

Athena User Defined Functions(UDFs) in Python made easy!

This library implements the Athena UDF protocol in Python so you don't have to use Java and you can use any Python library you wish including numpy/pandas!

## Installation

Install this library using `pip`:

    pip install athena-udf

## Usage

Simply install the package, create a lambda handler Python file, subclass `BaseAthenaUDF` and implement the `handle_athena_record` static method with your required functionality like this:

```
import athena_udf


class SimpleVarcharUDF(athena_udf.BaseAthenaUDF):

    @staticmethod
    def handle_athena_record(input_schema, output_schema, arguments):
        varchar = arguments[0]
        return varchar.lower()


lambda_handler = SimpleVarcharUDF().lambda_handler
```

This very basic example takes a `varchar` input, and returns the lowercase version.

`varchar` is converted to a python string on the way in and way out.

`input_schema` contains a `PyArrow` schema representing the schema of the data being passed

`output_schema` contains a `PyArrow` schema representing the schema of what athena expects to be returned.

`arguments` contains a list of arguments given to the function. Can be more than 1 with different types.

If you package the above into a zip, with dependencies and name your lambda function `my-kambda` you can then run it from the athena console like so:

```sql
USING EXTERNAL FUNCTION my_udf(col1 varchar) RETURNS varchar LAMBDA 'athena-test'

SELECT my_udf('FooBar');
```

Which will yield the result `foobar`

See other examples in the [examples](examples) folder of this repo.

## Important information before using.

Each lambda instance will take multiple requests for the same query. Each request can contain multiple rows, `athena-udf` handles this for you and your implementation will receive a single row.

Athena will group your data into around 1MB chunks in a single request. The maximum your function can return in 6MB per chunk. 

This library uses `PyArrow`. This is a large library so the Lambdas will be around 50MB zipped.

Timestamps seem to be truncated into Python `date` objects missing the time. 

Functions can return one value only. To return more complex data structures consider returning a JSON payload and parsing on athena.
## Development

To contribute to this library, first checkout the code. Then create a new virtual environment:

    cd athena-udf
    python -m venv venv
    source venv/bin/activate

Now install the dependencies and test dependencies:

    pip install -e '.[test]'

To run the tests:

    pytest

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/dmarkey/python-athena-udf",
    "name": "athena-udf",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "",
    "author": "David Markey",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/a3/ef/2fcfd7a257075f075c06bd625081bdeb1e64fe217ce17d32568beaf41225/athena-udf-0.2.tar.gz",
    "platform": null,
    "description": "# athena-udf\n\n[![PyPI](https://img.shields.io/pypi/v/athena-udf.svg)](https://pypi.org/project/athena-udf/)\n[![Changelog](https://img.shields.io/github/v/release/dmarkey/python-athena-udf?include_prereleases&label=changelog)](https://github.com/dmarkey/athena-udf/releases)\n[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/dmarkey/python-athena-udf/blob/main/LICENSE)\n\nAthena User Defined Functions(UDFs) in Python made easy!\n\nThis library implements the Athena UDF protocol in Python so you don't have to use Java and you can use any Python library you wish including numpy/pandas!\n\n## Installation\n\nInstall this library using `pip`:\n\n    pip install athena-udf\n\n## Usage\n\nSimply install the package, create a lambda handler Python file, subclass `BaseAthenaUDF` and implement the `handle_athena_record` static method with your required functionality like this:\n\n```\nimport athena_udf\n\n\nclass SimpleVarcharUDF(athena_udf.BaseAthenaUDF):\n\n    @staticmethod\n    def handle_athena_record(input_schema, output_schema, arguments):\n        varchar = arguments[0]\n        return varchar.lower()\n\n\nlambda_handler = SimpleVarcharUDF().lambda_handler\n```\n\nThis very basic example takes a `varchar` input, and returns the lowercase version.\n\n`varchar` is converted to a python string on the way in and way out.\n\n`input_schema` contains a `PyArrow` schema representing the schema of the data being passed\n\n`output_schema` contains a `PyArrow` schema representing the schema of what athena expects to be returned.\n\n`arguments` contains a list of arguments given to the function. Can be more than 1 with different types.\n\nIf you package the above into a zip, with dependencies and name your lambda function `my-kambda` you can then run it from the athena console like so:\n\n```sql\nUSING EXTERNAL FUNCTION my_udf(col1 varchar) RETURNS varchar LAMBDA 'athena-test'\n\nSELECT my_udf('FooBar');\n```\n\nWhich will yield the result `foobar`\n\nSee other examples in the [examples](examples) folder of this repo.\n\n## Important information before using.\n\nEach lambda instance will take multiple requests for the same query. Each request can contain multiple rows, `athena-udf` handles this for you and your implementation will receive a single row.\n\nAthena will group your data into around 1MB chunks in a single request. The maximum your function can return in 6MB per chunk. \n\nThis library uses `PyArrow`. This is a large library so the Lambdas will be around 50MB zipped.\n\nTimestamps seem to be truncated into Python `date` objects missing the time. \n\nFunctions can return one value only. To return more complex data structures consider returning a JSON payload and parsing on athena.\n## Development\n\nTo contribute to this library, first checkout the code. Then create a new virtual environment:\n\n    cd athena-udf\n    python -m venv venv\n    source venv/bin/activate\n\nNow install the dependencies and test dependencies:\n\n    pip install -e '.[test]'\n\nTo run the tests:\n\n    pytest\n",
    "bugtrack_url": null,
    "license": "Apache License, Version 2.0",
    "summary": "Athena User Defined Functions(UDFs) made easy!",
    "version": "0.2",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "24d991595b801d66d0711054acefaabd",
                "sha256": "5e3f6b550b39b38bdea5c512aed8c7e9ca56b3736d88a2d46ab6a15ce2ead1af"
            },
            "downloads": -1,
            "filename": "athena_udf-0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "24d991595b801d66d0711054acefaabd",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 7514,
            "upload_time": "2022-12-22T21:30:46",
            "upload_time_iso_8601": "2022-12-22T21:30:46.030927Z",
            "url": "https://files.pythonhosted.org/packages/8a/94/ade84eb16ef620666e984e435ba8def91978b2f4fe51c774f47b1e6762d2/athena_udf-0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "b5321966d2b8a07407764d37ebbe1ea3",
                "sha256": "acb81600b1c52974121b0b001664e44d687ba9d32e48bcfc4db5a3b18f77f154"
            },
            "downloads": -1,
            "filename": "athena-udf-0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "b5321966d2b8a07407764d37ebbe1ea3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 10469,
            "upload_time": "2022-12-22T21:30:47",
            "upload_time_iso_8601": "2022-12-22T21:30:47.826003Z",
            "url": "https://files.pythonhosted.org/packages/a3/ef/2fcfd7a257075f075c06bd625081bdeb1e64fe217ce17d32568beaf41225/athena-udf-0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-12-22 21:30:47",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "dmarkey",
    "github_project": "python-athena-udf",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "athena-udf"
}
        
Elapsed time: 0.02855s