scala – 检查列数据类型并仅在Spark SQL中的Integer和Decimal上
发布时间:2020-12-16 10:06:18 所属栏目:安全 来源:网络整理
导读:我正在尝试从输入Parquet文件中检查列的数据类型,如果数据类型是Integer或Decimal,则运行Spark SQL. //get Array of structfields val datatypes = parquetRDD_subset.schema.fields//Check datatype of column for (val_datatype - datatypes) if (val_data
我正在尝试从输入Parquet文件中检查列的数据类型,如果数据类型是Integer或Decimal,则运行Spark SQL.
//get Array of structfields val datatypes = parquetRDD_subset.schema.fields //Check datatype of column for (val_datatype <- datatypes) if (val_datatype.dataType.typeName == "integer" || val_datatype.dataType.typeName.contains("decimal")) { //get the field name val x = parquetRDD_subset.schema.fieldNames val dfs = x.map(field => spark.sql(s"select 'DataProfilerStats' as Table_Name,(SELECT 100 * approx_count_distinct($field)/count(1) from parquetDFTable) as Percentage_Unique_Value from parquetDFTable")) } 问题是,尽管数据类型验证是成功的,但在获取字段名称之后的for循环中,实际上并不是将列限制为整数或小数,而是对所有列类型甚至字符串执行查询.我们如何得到只有十进制或整数的字段.我们如何解决这个问题. 解决方法
这是您使用整数和双精度类型过滤列的方法
// fiter the columns val columns = df.schema.fields.filter(x => x.dataType == IntegerType || x.dataType == DoubleType) //use these filtered with select df.select(columns.map(x => col(x.name)): _*) 我希望这有帮助! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |