如何在Spark中使用Transform Python UDF执行HQL脚本



我是新来的火花和学习。作为此POC的一部分,我正在尝试直接执行HQL文件,该文件具有转换关键字以使用Python UDF。

我已经在CLI" Hive -f filename.hql"中测试了HQL脚本,并且工作正常。我在Spark-SQL中尝试过的同一脚本,但是HDFS路径未发现错误。我试图以不同的方式给予HDFS路径,但所有人都无法正常工作

"/test/scripts/test.hql"
"hdfs://test.net:8020/test/scripts/test.hql"
"hdfs:///test.net:8020/test/scripts/test.hql"

还尝试在下面的Hive Transform Code中提供完整的路径

USING "scl enable python27 'python hdfs://test.net:8020/user/test/scripts/TestPython.py'" 

Hive Code

add file hdfs://test.net:8020/user/test/scripts/TestPython.py;

select * from 
    (select transform (*)
    USING "scl enable python27 'python TestPython.py'" 
    as (Col_1     STRING,
    col_2        STRING,
    ...
    ..
    col_125 STRING
    )
    FROM
    test.transform_inner_temp1 a) b;

testpytython代码:

#!/usr/bin/env python
'''
Created on June 2, 2017
@author: test
'''
import sys
from datetime import datetime
import decimal
import string
D = decimal.Decimal
for line in sys.stdin:
    line = sys.stdin.readline()   
    TempList = line.strip().split('t')
    col_1 = TempList[0]
    ... 
    ....
    col_125 = TempList[34] + TempList[32]
    outList.extend((col_1,....col_125))
    outValue = "t".join(map(str,outList))
    print "%s"%(outValue)

所以我尝试了另一种方法,因为直接在Spark-Submit中执行

spark-submit --master yarn-cluster  hdfs://test.net:8020/user/test/scripts/testspark.py

testspark.py

from pyspark.sql.types import StringType
from pyspark import SparkConf, SparkContext
from pyspark import SQLContext
conf = SparkConf().setAppName("gveeran pyspark test")
sc = SparkContext(conf=conf)
sqlContext = SQLContext(sc)
with open("hdfs://test.net:8020/user/test/scripts/test.hql") as fr:
   query = fr.read()
results = sqlContext.sql(query)
results.show()

,但再次与下面的问题

Traceback (most recent call last):
  File "PySparkTest2.py", line 7, in <module>
    with open("hdfs://test.net:8020/user/test/scripts/test.hql") as fr:
IOError: [Errno 2] No such file or directory: 'hdfs://test.net:8020/user/test/scripts/test.hql'

您可以将文件读取为查询,然后执行为spark sql Job

示例: -

from pyspark.sql import SparkSession
from pyspark.sql import SQLContext
sc =SparkContext.getOrCreate()
sqlCtx = SQLContext(sc)
with open("/home/hadoop/test/abc.hql") as fr:
    query = fr.read()
    print(query)
    results = sqlCtx.sql(query)

相关内容

  • 没有找到相关文章

最新更新