gpt4 book ai didi

hadoop - 配置单元 SerDe ClassCastException : java. lang.String 无法转换为 java.lang.Long

转载 作者:可可西里 更新时间:2023-11-01 14:58:57 36 4
gpt4 key购买 nike

我正在编写自定义 Hive SerDe 以解析日志(目标是将用户代理解析为配置单元表中的复杂结构,但尚未显示代码)。

但是,当我尝试将数据放入类型不是 STRING 的列中时,会出现 ClassCastException。

我的hive版本是0.9.0

这是我的自定义 Serde:

@Override
public void initialize(Configuration conf, Properties tbl)
throws SerDeException {
String colNamesStr = tbl.getProperty(serdeConstants.LIST_COLUMNS);
colNames = Arrays.asList(colNamesStr.split(","));

String colTypesStr = tbl.getProperty(serdeConstants.LIST_COLUMN_TYPES);
List<TypeInfo> colTypes = TypeInfoUtils.getTypeInfosFromTypeString(colTypesStr);

rowTypeInfo = (StructTypeInfo) TypeInfoFactory.getStructTypeInfo(colNames, colTypes);
rowOI = TypeInfoUtils.getStandardJavaObjectInspectorFromTypeInfo(rowTypeInfo);
}

@Override
public Object deserialize(Writable blob) throws SerDeException {
row.clear();

String[] line = blob.toString().split("\t");

row.add(line[0]);
row.add(Long.parseLong(line[1]));
row.add(line[2]);

return row;
}

这是表的创建:

CREATE EXTERNAL TABLE logs (
token STRING,
tmstmp BIGINT,
user_agent STRING )
ROW FORMAT SERDE 'com.hive.serde.LogsSerDe'
LOCATION '/user/Input/logs';

这里是错误:

java.io.IOException: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long
at org.apache.hadoop.hive.ql.exec.FetchTask.fetch(FetchTask.java:173)
at org.apache.hadoop.hive.ql.Driver.getResults(Driver.java:1382)
at org.apache.hadoop.hive.cli.CliDriver.processLocalCmd(CliDriver.java:270)
at org.apache.hadoop.hive.cli.CliDriver.processCmd(CliDriver.java:216)
at org.apache.hadoop.hive.cli.CliDriver.processLine(CliDriver.java:412)
at org.apache.hadoop.hive.cli.CliDriver.run(CliDriver.java:699)
at org.apache.hadoop.hive.cli.CliDriver.main(CliDriver.java:563)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.hadoop.util.RunJar.main(RunJar.java:156)
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long
at org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaLongObjectInspector.get(JavaLongObjectInspector.java:39)
at org.apache.hadoop.hive.serde2.lazy.LazyUtils.writePrimitiveUTF8(LazyUtils.java:203)
at org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe.serialize(LazySimpleSerDe.java:483)
at org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe.serializeField(LazySimpleSerDe.java:436)
at org.apache.hadoop.hive.serde2.DelimitedJSONSerDe.serializeField(DelimitedJSONSerDe.java:69)
at org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe.serialize(LazySimpleSerDe.java:420)
at org.apache.hadoop.hive.ql.exec.FetchTask.fetch(FetchTask.java:163)
... 11 more

似乎“反序列化”函数返回的所有值都是字符串。

预先感谢您的帮助

最佳答案

DDL 中的 tmstmp 列是一个 BIGINT。您返回的是 Long,而 Hive 期待的是 LongWritable。尝试:

row.add(new LongWritable(Long.valueOf(line[1])));

同样,您可能必须使用以下方法将字符串转换为 Text:new Text(javaStringObject);

关于hadoop - 配置单元 SerDe ClassCastException : java. lang.String 无法转换为 java.lang.Long,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22486392/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com