score:3
Accepted answer
You should use None: Option[String]
instead of None
scala> val maybeString = None: Option[String]
maybeString: Option[String] = None
scala> val sampleData = spark.createDataset(Seq(
| (1, Some("Yes"), maybeString),
| (2, maybeString, maybeString),
| (3, Some("Okay"), maybeString),
| (4, maybeString, maybeString))).toDF("id", "title", "value")
sampleData: org.apache.spark.sql.DataFrame = [id: int, title: string ... 1 more field]
scala> sampleData.show
+---+-----+-----+
| id|title|value|
+---+-----+-----+
| 1| Yes| null|
| 2| null| null|
| 3| Okay| null|
| 4| null| null|
+---+-----+-----+
score:1
Or you can use: null.asInstanceOf[String]
If you're just dealing with Strings
val df1 = sc.parallelize(Seq((1, "Yes", null.asInstanceOf[String]),
| (2, null.asInstanceOf[String], null.asInstanceOf[String]),
| (3, "Okay", null.asInstanceOf[String]),
| (4, null.asInstanceOf[String], null.asInstanceOf[String]))).toDF("id", "title", "value")
df1: org.apache.spark.sql.DataFrame = [id: int, title: string, value: string]
scala> df1.show
+---+-----+-----+
| id|title|value|
+---+-----+-----+
| 1| Yes| null|
| 2| null| null|
| 3| Okay| null|
| 4| null| null|
+---+-----+-----+
Source: stackoverflow.com
Related Query
- How can I print nulls when converting a dataframe to json in Spark
- Spark Structured Streaming left outer joins returns outer nulls for already matched rows
- Spark dataframe filter both nulls and spaces
- Replace Empty values with nulls in Spark Dataframe
- spark dataframe is loading all nulls from csv file
- How do i handle Nulls in pattern matching in spark scala
- How to replace nulls with empty string ("") in Apache spark using scala
- spark sql cast function creates column with NULLS
- Spark SQL - replace nulls with default values
- Spark dataframe replace values of specific columns in a row with Nulls
- How can I optimize the spark function to replace nulls with zeroes?
- Spark GroupBy While Maintaining Schema With Nulls
- Scala Spark - convert DenseVector with nulls to SparseVector
- Spark DataFrame nulls to Dataset
- Casting a column in a spark DataFrame without nulls being created
- Pivot on Spark dataframe returns unexpected nulls on only one of several columns
- Spark performance for Scala vs Python
- Add JAR files to a Spark job - spark-submit
- How can I change column types in Spark SQL's DataFrame?
- How to sort by column in descending order in Spark SQL?
- Spark - load CSV file as DataFrame?
- How to convert rdd object to dataframe in spark
- Spark - Error "A master URL must be set in your configuration" when submitting an app
- java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries. spark Eclipse on windows 7
- Extract column values of Dataframe as List in Apache Spark
- Renaming column names of a DataFrame in Spark Scala
- Is there a way to take the first 1000 rows of a Spark Dataframe?
- How to write unit tests in Spark 2.0+?
- How to pass -D parameter or environment variable to Spark job?
- how to filter out a null value from spark dataframe
More Query from same tag
- scala what does this mean? "{ /* compiled code */ }"
- Scala parser combinators vs ANTLR/Java generated parser?
- Kafka Connect using REST API with Strimzi with kind: KafkaConnector
- Scala determine if global ExecutionContext finished all tasks
- How can I build a Scala Project with Databricks Connect in Visual Studio Code?
- Look up value in an array
- scala.MatchError: in Dataframes
- need to remove first line from a string and then first two word in scala
- How to read a file line by line and close it afterwards in Scala?
- Play Framework 2 - thread safety for view helper functions
- Scala: Filtering List of case class with fields of Option
- How to write pointcut with customized annotation to print the global variable value that used in a function?
- Scala defaulting typed list to be of type String
- Trouble using Threading with Redis (Jedis) in Scala
- Right associative functions with two parameter list
- Scala - Play: How to bind a form field to a Vector?
- What is .toString doing in this parameter- vehicle.toString+"firstImage"
- How Do I Call Snippets From Different Packages In Lift?
- how to pattern match first character in string
- Scala: Is it possible to define a class that extends its class parameter?
- Remove duplicates from Spark SQL joining two dataframes
- Array manipulation in Spark, Scala
- Scala: value :: is not a member of Int
- Error : case class implementing parent's abstract method
- Interceptors/Filters in Spray
- What are the real advantages of immutable collections?
- How to join Dataframe with one-column dataset without using column names in dataset
- Squeryl join with null columns
- Injecting an ActorSystem, with actors created by Guice, in Play 2.4 for Scala
- Reuse json implicit readers in subclasses