sparksql异常总结

一、Caused by: org.apache.hadoop.hive.serde2.SerDeException: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe: columns has 4 elements while columns.types has 3 elements!

SQL:

insert overwrite directory 'xxx'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\001'
select 
id ,
contract_id , 
date_format(update_time,'yyyy-MM-dd hh:mm:ss') 
from (

异常解决:

date_format(update_time,'yyyy-MM-dd hh:mm:ss')  这种字符串作为的列需要起一个别名

insert overwrite directory 'xxx'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\001'
select 
id ,
contract_id , 
date_format(update_time,'yyyy-MM-dd hh:mm:ss')  as update_time
from (

改成这种之后错误异常解决!!!

--------------------------------------------------------------------------------------------------------------------------------

spark-version:2.3 会出现这个问题

出现问题部分的源码

 public void extractColumnInfo() throws SerDeException {
        String columnNameProperty = this.tableProperties.getProperty("columns");
        String columnTypeProperty = this.tableProperties.getProperty("columns.types");
        if (columnNameProperty != null && columnNameProperty.length() > 0) {
            //因为这边根据“,"进行分割,所以如果表达式中包含“,”,且没有别名的话,size就会多一个
            this.columnNames = Arrays.asList(columnNameProperty.split(","));
        } else {
            this.columnNames = new ArrayList();
        }

        if (columnTypeProperty == null) {
            StringBuilder sb = new StringBuilder();

            for(int i = 0; i < this.columnNames.size(); ++i) {
                if (i > 0) {
                    sb.append(":");
                }

                sb.append("string");
            }

            columnTypeProperty = sb.toString();
        }

        this.columnTypes = TypeInfoUtils.getTypeInfosFromTypeString(columnTypeProperty);
        if (this.columnNames.size() != this.columnTypes.size()) {
            //error部分
            throw new SerDeException(this.serdeName + ": columns has " + this.columnNames.size() + " elements while columns.types has " + this.columnTypes.size() + " elements!");
        }
    }

进一步验证,判断是否是“,“分割引起的,将sql调整为如下

insert overwrite directory 'xxx'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\001'
select 
id ,
contract_id , 
--两个,号
substr(update_time,1,10) 
from (

结果报错:org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe: columns has 5 elements while columns.types has 3 elements!

 验证是这个原因导致,起别名会将columeName改成别名,就不会存在分割size问题!!!

spark-version:3.1 不出现这个问题

  /**
   * Extracts and set column names and column types from the table properties
   * @throws SerDeException
   */
  public void extractColumnInfo() throws SerDeException {
    // Read the configuration parameters
    String columnNameProperty = tableProperties.getProperty(serdeConstants.LIST_COLUMNS);
    // NOTE: if "columns.types" is missing, all columns will be of String type
    String columnTypeProperty = tableProperties.getProperty(serdeConstants.LIST_COLUMN_TYPES);

    // Parse the configuration parameters
    String columnNameDelimiter = tableProperties.containsKey(serdeConstants.COLUMN_NAME_DELIMITER) ? tableProperties
        .getProperty(serdeConstants.COLUMN_NAME_DELIMITER) : String.valueOf(SerDeUtils.COMMA);
    if (columnNameProperty != null && columnNameProperty.length() > 0) {
      columnNames = Arrays.asList(columnNameProperty.split(columnNameDelimiter));
    } else {
      columnNames = new ArrayList<String>();
    }
    if (columnTypeProperty == null) {
      // Default type: all string
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < columnNames.size(); i++) {
        if (i > 0) {
          sb.append(":");
        }
        sb.append(serdeConstants.STRING_TYPE_NAME);
      }
      columnTypeProperty = sb.toString();
    }

    columnTypes = TypeInfoUtils.getTypeInfosFromTypeString(columnTypeProperty);

    if (columnNames.size() != columnTypes.size()) {
      throw new SerDeException(serdeName + ": columns has " + columnNames.size()
          + " elements while columns.types has " + columnTypes.size() + " elements!");
    }
  }

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值