如何解决如何检查架构字段的类型为字符串数组
当前代码为:
schema.fields.foreach(f => {
// check if schema field is type of array
// if yes then throw array not supported
if (f.dataType.typeName == "array") {
throw ArrayDataTypeNotSupportedException(s"${f.name} column is ArrayType," +
"writing arrays to CSV isn't supported. Please convert this column to a different data type.")
}
})
但是现在我想要这种情况,如果schema.field.typeName是字符串数组或字符串数组,则应该将String array转换为逗号分隔的String。如果不是字符串数组,则抛出数组不被接受的异常。简而言之,我想仅支持字符串数组,如果字符串数组之外没有其他支持,那么我想在异常下面传递
throw ArrayDataTypeNotSupportedException(s"${f.name} column is ArrayType," +
"writing arrays to CSV isn't supported. Please convert this column to a different data type.")
解决方法
您可以做的是进一步深入研究数组类型并检查元素类型。以下代码可以做到这一点:
if (f.dataType == ArrayType) {
val arrayType = f.dataType.asInstanceOf[ArrayType]
if (arrayType.elementType == StringType) {
throw ArrayDataTypeNotSupportedException(s"${f.name} column is ArrayType," +
"writing arrays to CSV isn't supported. Please convert this column to a different data type.")
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。