Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.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中数据类型分析问题的行_Scala_Apache Spark - Fatal编程技术网

Scala 筛选导致spark中数据类型分析问题的行

Scala 筛选导致spark中数据类型分析问题的行,scala,apache-spark,Scala,Apache Spark,我有一个spark数据框,其中包含列Salary,如下所示: |Salary| |"100"| |"200"| |"abc"| dafault数据类型为字符串。我想通过删除导致解析问题的行,将其转换为整数 所需输出 |Salary| |100| |200| 有人可以让我知道代码过滤的行,这将导致数据类型解析问题。 提前感谢。您可以使用正则表达式筛选所需字段,然后强制转换列: import org.apache.spark.sql.t

我有一个spark数据框,其中包含列Salary,如下所示:

|Salary|
|"100"|
|"200"|
|"abc"|
dafault数据类型为字符串。我想通过删除导致解析问题的行,将其转换为整数

所需输出

|Salary|
|100|
|200|
有人可以让我知道代码过滤的行,这将导致数据类型解析问题。
提前感谢。

您可以使用正则表达式筛选所需字段,然后强制转换列:

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

df.filter(row => row.getAs[String]("Salary").matches("""\d+"""))
  .withColumn("Salary", $"Salary".cast(IntegerType))
如果您不喜欢正则表达式,也可以使用Try:

import scala.util._

df.filter(row => Try(row.getAs[String]("Salary").toInt).isSuccess)
  .withColumn("Salary", $"Salary".cast(IntegerType))

您可以使用正则表达式过滤所需字段,然后强制转换列:

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

df.filter(row => row.getAs[String]("Salary").matches("""\d+"""))
  .withColumn("Salary", $"Salary".cast(IntegerType))
如果您不喜欢正则表达式,也可以使用Try:

import scala.util._

df.filter(row => Try(row.getAs[String]("Salary").toInt).isSuccess)
  .withColumn("Salary", $"Salary".cast(IntegerType))

@如果答案符合你的目的,那么出于礼貌,接受答案answer@ankush如果答案符合你的目的,那么出于礼貌,接受答案