谁能告诉:可以通过固定偏移量创建配置单元表并分隔值。例如,文件:col1 col2 col3 在这种情况下,1-4 个字符将在表中的第一列,5-8 个字符第二列,8-12 个字符第三。
非常感谢!
我用这种方式解决了类似的问题:
给定换行分隔行、固定偏移列的输入,
首先将数据输入到一个一列的表中,一个字符串
然后通过流式 hadoop 传递一个 Java 类或一个 Python 模块,它接受一行,并返回多个字段:
import sys
for line in sys.stdin:
# line will have a newline on the end you don't want
line = line.strip()
output = []
output.append(line[:4])
output.append(line[4:8])
output.append(line[8:12])
print '\t'.join(output)
您的配置单元脚本将如下所示:
CREATE TABLE IF NOT EXISTS input_raw(line STRING);
LOAD DATA LOCAL INPATH '${hiveconf:input}' OVERWRITE INTO TABLE input_raw;
CREATE TABLE IF NOT EXISTS processed_data(
field1 STRING,
field2 STRING
field3 STRING);
delete FILE processing.py;
add FILE processing.py;
INSERT INTO TABLE processed_data
SELECT
TRANSFORM (line)
USING 'python processing.py'
AS(field1, field2, field3)
FROM input_raw;
DROP TABLE input_raw;
您也可以按照您的方法避免流式传输和 python 以及所有这些,但使用 hive substr()
CREATE TABLE IF NOT EXISTS input_raw(line STRING);
LOAD DATA LOCAL INPATH '${hiveconf:input}' OVERWRITE INTO TABLE input_raw;
CREATE TABLE IF NOT EXISTS processed_data(
field1 STRING,
field2 STRING
field3 STRING);
INSERT INTO TABLE processed_data
SELECT
substr(line,1,4) as field1,
substr(line,5,4) as field2,
substr(line,9,4) as field3
FROM input_raw;
DROP TABLE input_raw;