Giter Site home page Giter Site logo

thien-vuong-cgc / snowflake-ingest-python Goto Github PK

View Code? Open in Web Editor NEW

This project forked from snowflakedb/snowflake-ingest-python

0.0 1.0 0.0 77 KB

A Python API for Asynchronously Loading Data into Snowflake DB -

Home Page: http://www.snowflake.net

License: Apache License 2.0

Shell 5.80% Python 89.29% Lua 4.91%

snowflake-ingest-python's Introduction

Snowflake Python Ingest Service SDK

https://travis-ci.org/snowflakedb/snowflake-ingest-python.svg?branch=master

The Snowflake Ingest Service SDK allows users to ingest files into their Snowflake data warehouse in a programmatic fashion via key-pair authentication.

Prerequisites

Python 3.4+

The Snowflake Ingest SDK requires Python 3.4 or above. Backwards compatibility with older versions of Python 3 or any versions of Python 2 is not planned at this time.

A 2048-bit RSA key pair

Snowflake Authentication for the Ingest Service requires creating a 2048 bit RSA key pair and, registering the public key with Snowflake. For detailed instructions, please visit the relevant Snowflake Documentation Page.

Furl, PyJWT, Requests, and Cryptography

Internally, the Snowflake Ingest SDK makes use of Furl, PyJWT, and Requests. In addition, the cryptography is used with PyJWT to sign JWT tokens.

Installation

If you would like to use this sdk, you can install it using python setuptools.

pip install snowflake-ingest

Usage

Here is a simple "hello world" example for using ingest sdk.

from logging import getLogger
from snowflake.ingest import SimpleIngestManager
from snowflake.ingest import StagedFile
import time
from requests import HTTPError

logger = getLogger(__name__)

# assume public key has been registered in Snowflake
# private key in pem format
private_key="""-----BEGIN PRIVATE KEY-----
abc...
-----END PRIVATE KEY-----"""

# list of files in the stage specified in the pipe definition
file_list=['a.csv', 'b.csv']
ingest_manager = SimpleIngestManager(account='testaccount',
                                     host='testaccount.snowflakecomputing.com',
                                     user='ingest_user',
                                     pipe='TESTDB.TESTSCHEMA.TESTPIPE',
                                     private_key=private_key)


# list of files, but wrapped into a class
staged_file_list = []
for file_name in file_list:
    staged_file_list.append(StagedFile(file_name, None))

try:
    resp = ingest_manager.ingest_files(staged_file_list)
except IngestResponseError as e:
    logger.error(e)
    exit(1)

# This means Snowflake has received file and will start loading
assert(resp['responseCode'] == 'SUCCESS')

# Needs to wait for a while to get result in history
while True:
    history_resp = ingest_manager.get_history()

    if len(history_resp['files']) == 2:
        print('Ingest Report:\n')
        print(history_resp)
        break
    else:
        # wait for 20 seconds
        time.sleep(20)

# Valid ISO 8601 format requires Z at the end
hour = timedelta(hours=1)
date = datetime.datetime.utcnow() - hour
history_range_resp = ingest_manager.get_history_range(date.isoformat() + 'Z')

print('\nHistory scan report: \n')
print(history_range_resp)

snowflake-ingest-python's People

Contributors

howryu avatar smtakeda avatar sfc-gh-hyu avatar markpeters-snow avatar sdankel avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.