Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在配置单元中将一列拆分为两列_Python_Hadoop_Split_Hive_User Defined Functions - Fatal编程技术网

Python 如何在配置单元中将一列拆分为两列

Python 如何在配置单元中将一列拆分为两列,python,hadoop,split,hive,user-defined-functions,Python,Hadoop,Split,Hive,User Defined Functions,在我的配置单元表中,有一个名为YearMonth的列,其值类似于'Jul1998'。我需要将每行中的字符串拆分为月份('Jul')和年份('1998),并将它们分别添加到配置单元中的新列中。不幸的是,我的代码没有正确执行此操作,并且每隔一行添加一个空实例,如下所示: 我采取以下步骤获得上述输出: 创建了一个split.py文件,将字符串“Jul1998”拆分为“Jul”和“1998” #!/usr/bin/python import sys for line in sys.stdin:

在我的配置单元表中,有一个名为YearMonth的列,其值类似于'Jul1998'。我需要将每行中的字符串拆分为月份('Jul')和年份('1998),并将它们分别添加到配置单元中的新列中。不幸的是,我的代码没有正确执行此操作,并且每隔一行添加一个空实例,如下所示:

我采取以下步骤获得上述输出:

创建了一个split.py文件,将字符串“Jul1998”拆分为“Jul”和“1998”

#!/usr/bin/python
import sys

for line in sys.stdin:
    Month= line[:3]
    Year = line[3:]
    print '\t'.join([Month, Year])
然后我进入配置单元并将python文件添加到配置单元目录中 最后,我使用transform函数分割列并创建了两个新列

ADD FILE /home/ec2-user/split.py;
INSERT OVERWRITE TABLE data
SELECT TRANSFORM (yearmonth) USING 'split.py' AS (month,year) FROM data;

假设您的数据格式是一致的,您可以在Hive中简单地使用
regexp\u extract
,而不需要UDF

SELECT regexp_extract(yearmonth,([^0-9]+)) as mth,regexp_extract(yearmonth,([0-9]+)) as yr
FROM data;
或者一个简单的
子字符串

select substring(yearmonth,1,3) as mth,substring(yearmonth,4) as yr
from data

假设您的数据格式是一致的,您可以在Hive中简单地使用
regexp\u extract
,而不需要UDF

SELECT regexp_extract(yearmonth,([^0-9]+)) as mth,regexp_extract(yearmonth,([0-9]+)) as yr
FROM data;
或者一个简单的
子字符串

select substring(yearmonth,1,3) as mth,substring(yearmonth,4) as yr
from data
你为什么不使用?例如:
从数据中选择SUBSTR(yearmonth,1,3)作为月份,选择SUBSTR(yearmonth,4,3)作为年份
为什么不使用?例如:
从数据中选择SUBSTR(yearmonth,1,3)作为月份,选择SUBSTR(yearmonth,4,3)作为年份