Last active
May 19, 2020 12:59
-
-
Save vepetkov/94e77d7a3836b3d436202b319d40ecd4 to your computer and use it in GitHub Desktop.
PyHive Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pyhive import hive | |
import pandas as pd | |
from vdom import pre | |
# Nteract Data Explorer | |
pd.options.display.html.table_schema = True # Data Explorer On! | |
pd.options.display.max_rows = None # Send all the data! (careful!) | |
def getHiveConn(host, username, port=10000, schema="db_user1"): | |
return hive.connect(host=host, port=port, username=username, database=schema, auth=None) | |
def getHiveData(table, conn = None): | |
import pandas as pd | |
if (conn is None): | |
conn = getHiveConn("localhost", "user1") | |
dfRaw = pd.read_sql(f"SELECT * FROM {table}", conn) | |
return dfRaw | |
def saveHiveData(data, table, conn = None): | |
if (conn is None): | |
conn = getHiveConn("localhost", "user1") | |
query = ("INSERT INTO %s VALUES ('%s', '%s', '%s', %f, %f, %f, '%s')" % | |
(table, data['ts'], data['id'], data['kpi_dt'], | |
data['value'], data['upper'], data['lower'], data['feedback']) | |
) | |
cur = conn.cursor() | |
cur.execute(query) | |
conn.commit() | |
## Save Feedback | |
def saveFeedbackHive(id, date, value, upper, lower, feedback): | |
from datetime import datetime as dt | |
import os | |
data = { | |
'ts' : dt.now(), | |
'id' : id, | |
'kpi_dt' : date, | |
'value' : value, | |
'upper' : upper, | |
'lower' : lower, | |
'feedback': feedback | |
} | |
conn = getHiveConn( | |
host =os.getenv("HIVE_SERVER_2_HOSTNAME", "localhost"), | |
username =os.getenv("HIVE_SERVER_2_USERNAME", "user1"), | |
schema =os.getenv("HIVE_SERVER_2_SCHEMA", "db_user1") | |
) | |
saveHiveData(data, "sample_data", conn) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment