3

Consider a csv:

Name,Color
Apple,""
 val df = spark.read
.option("header", "true")
.option("inferSchema", "true")
.option("treatEmptyValuesAsNulls","false")
.csv(mycsv)

This still gives:

+--------+----------+
|Name    |Color     |
+--------+----------+
|   Apple|      null|
+--------+----------+

Expected was:

+--------+----------+
|Name    |Color     |
+--------+----------+
|   Apple|          |
+--------+----------+
4
  • then how will we read empty string as empty without having it as null Commented Oct 12, 2020 at 12:39
  • Sorry, I was confused. Commented Oct 12, 2020 at 12:41
  • 2
    Try .option("nullValue", null) instead. Commented Oct 12, 2020 at 12:44
  • Yes, it is getting empty now . Thanks @ernest_k Commented Oct 12, 2020 at 13:04

1 Answer 1

6

AFAIK, the option "treatEmptyValuesAsNulls" does not exist. See the doc for more details. Two other options may be of interest to you though. emptyValue and nullValue. By default, they are both set to "" but since the null value is possible for any type, it is tested before the empty value that is only possible for string type. Therefore, empty strings are interpreted as null values by default. If you set nullValue to anything but "", like "null" or "none", empty strings will be read as empty strings and not as null values anymore.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.