Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 检查列数据类型,并仅对Spark SQL中的整数和小数执行SQL_Scala_Apache Spark_Apache Spark Sql_Spark Streaming - Fatal编程技术网

Scala 检查列数据类型,并仅对Spark SQL中的整数和小数执行SQL

Scala 检查列数据类型,并仅对Spark SQL中的整数和小数执行SQL,scala,apache-spark,apache-spark-sql,spark-streaming,Scala,Apache Spark,Apache Spark Sql,Spark Streaming,我试图从输入拼花文件中检查列的数据类型,如果数据类型是整数或十进制,则运行sparksql //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_dataty

我试图从输入拼花文件中检查列的数据类型,如果数据类型是整数或十进制,则运行sparksql

//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循环中,实际上并没有将列限制为整数或小数,而是对所有列类型甚至字符串执行查询。我们如何得到只有小数或整数的字段。我们如何解决这个问题。

请尝试以下方法:

import org.apache.spark.sql.types._

val names = df.schema.fields.collect { 
  case StructField(name, DecimalType(), _, _) => approx_count_distinct(name)
  case StructField(name, IntegerType, _, _)   => approx_count_distinct(name)
}

spark.table("parquetDFTable").select(names: _*)

这就是如何筛选整型和双精度类型的列

// 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)): _*)

我希望这有帮助

谢谢你的回答!但是,由于数据大小将以百万计,因此“近似计数”使此操作有点慢。我们如何添加下面的语句以及您提供的代码。。。val dfs=names.mapfield=>spark.sqls从parquetDFTable val with sum=dfs.reducex,y=>x.uniony.distinct.coalesc1 with sum.showthanker Shankar中选择'DataProfilerStats'作为表名,$field'作为列名!提供的解决方案实际上会过滤整数和小数,但我们如何将其传递给现有的sql,即-val dfs=x.mapfield=>spark.sqls选择“DataProfilerStats”作为表名,从ParquetTable中选择100*approx\u count\u distinct$field/count1作为百分比\u Unique\u作为ParquetTable中的值。此处$field是输入数据框中的列名。有什么想法吗?再次感谢Shankar!我找到了最简单的部分:…val m=z.columns,m是代码中提到的最后一步,后面是val dfs=m.mapfield=>spark.sqlsselect。。它在限制栏上运行..你像往常一样棒!!