如何在python中寫hive腳本
這篇文章主要介紹了如何在python中寫hive腳本,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
1、直接執(zhí)行.sql腳本
import numpy as np import pandas as pd import lightgbm as lgb from pandas import DataFrame from sklearn.model_selection import train_test_split from io import StringIO import gc import sys import os hive_cmd = "hive -f ./sql/sql.sql" output = os.popen(hive_cmd) data_cart_prop = pd.read_csv(StringIO(unicode(output.read(),'utf-8')), sep="\t",header=0)
2、Hive語句執(zhí)行
假如有如下hive sql:
hive_cmd = 'hive -e "select count(*) from hbase.routermac_sort_10;"'
一般在python中按照如下方式執(zhí)行該hive sql:
os.system(hive_cmd)
---------------------
hive_cmd1 = "hive -f ./user.sql" output1 = os.popen(hive_cmd1) test_user = pd.read_csv(StringIO(unicode(output1.read(),'utf-8')), sep="\t",header=0) hive_cmd2 = "hive -f ./action.sql" output2 = os.popen(hive_cmd2) test_action = pd.read_csv(StringIO(unicode(output2.read(),'utf-8')), sep="\t",header=0) hive_cmd3 = "hive -f ./click.sql" output3 = os.popen(hive_cmd3) test_click = pd.read_csv(StringIO(unicode(output3.read(),'utf-8')), sep="\t",header=0)
為了顯示表頭,在腳本中加上一句:set hive.cli.print.header=true;
或者,使用如下語句:
hive_cmd = 'hive -e "set hive.cli.print.header=true;SELECT * FROM dev.temp_dev_jypt_decor_user_label_phase_one_view_feature WHERE(dt = "2018-09-17");"' output = os.popen(hive_cmd) data_cart_prop = pd.read_csv(StringIO(unicode(output.read(),'utf-8')), sep="\t",header=0)
3、tf 顯存占用
import tensorflow as tf tf.enable_eager_execution() x = tf.get_variable('x', shape=[1], initializer=tf.constant_initializer(3.)) with tf.GradientTape() as tape: y = tf.square(x) y_grad = tape.gradient(y, x) print([y.numpy(), y_grad.numpy()])
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
解決Opencv+Python cv2.imshow閃退問題
這篇文章主要介紹了解決Opencv+Python cv2.imshow閃退問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04封裝Detours用于Python中x64函數(shù)hook詳解
Detours是微軟發(fā)布的一個API hook框架,同時支持x86和x64,看文檔說也支持ARM和ARM64的Windows,這篇文章主要介紹了封裝Detours用于Python中x64函數(shù)hook,需要的朋友可以參考下2023-12-12對Python中class和instance以及self的用法詳解
今天小編就為大家分享一篇對Python中class和instance以及self的用法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06python 定時器每天就執(zhí)行一次的實現(xiàn)代碼
這篇文章主要介紹了python 定時器每天就執(zhí)行一次的實現(xiàn)代碼,代碼簡單易懂非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08