SPRKPY1027

pyspark.sql.readwriter.DataFrameReader.json has a workaround

Category

Warning.

Description

This issue appears when the tool detects the usage of pyspark.sql.readwriter.DataFrameReader.json which has a workaround.

Input code:

stringmap = sparkSession.read.json(path, primitiveAsString=True, dateFormat="2023-06-20")

Output code:

#EWI: SPRKPY1027 => pyspark.sql.readwriter.DataFrameReader.json has a workaround, see documentation for more info
stringmap = sparkSession.read.json(path, primitiveAsString=True, dateFormat="2023-06-20")

Scenario:

json(

#Path path,

#Schema schema=None,

#Options primitivesAsString=None, prefersDecimal=None, allowComments=None, allowUnquotedFieldNames=None, allowSingleQuotes=None, allowNumericLeadingZero=None, allowBackslashEscapingAnyCharacter=None, mode=None, columnNameOfCorruptRecord=None, dateFormat=None, timestampFormat=None, multiLine=None, allowUnquotedControlChars=None, lineSep=None, samplingRatio=None, dropFieldIfAllNull=None, encoding=None, locale=None, pathGlobFilter=None, recursiveFileLookup=None, allowNonNumericNumbers=None, modifiedBefore=None, modifiedAfter=None ) Several workarounds are possible in this scenario. Path: The first parameter "path" must be a stage to make an equivalence with Snowpark, so is recommended to implement a temporary stage and add each ".json" path to the stage, using the prefix "file://", as follows. Source:

stringmap = sparkSession.read.json(["./data/file1.json", "./data/file2.json"])

Expected:

stage = f'{sparkSession.get_fully_qualified_current_schema()}.{_generate_prefix("TEMP_STAGE")}'
sparkSession.sql(f'CREATE TEMPORARY STAGE IF NOT EXISTS {stage}').show()
sparkSession.file.put(f"file://./data/file1.json", f"@{stage}")
sparkSession.file.put(f"file://./data/file2.json", f"@{stage}")
stringmap = sparkSession.read.json(stage)

Schema: The second parameter "schema" is not supported for Snowpark as a parameter, so you have to specify it, if it does not have an schema yet, use the schema function, as follows: Source:

stringmap = sparkSession.read.json(path, schema)

Expected:

stringmap = sparkSession.read.schema(schema).json(path)

Options: The additional parameters are also not supported by Snowpark as parameters, but for many of them you can use the "option" function to specify those .json parameters as options, as follows: Source:

stringmap = sparkSession.read.json(path, primitiveAsString=True, dateFormat="2023-06-20")

Expected:

stringmap = sparkSession.read.option("dateFormat", "2023-06-20").json(path)

The following options are not supported for Snowpark: timeZone, primitiveAsString, prefersDecimal, allowComments, allowUnquotedFieldNames, allowSingleQuotes, allowNumericLeadingZero, allowBackslashEscapingAnyCharacter, mode, columnNameOfCorruptRecord, timestampNTZFormat, multiline, allowUnquotedControlChars, encoding, lineSep, samplingRatio, dropFiledIfAllNull, locale, allowNonNumericNumbers, ignoreNullFields.

Recommendation

  • For more support, you can email us at snowconvert-info@snowflake.com. If you have a contract for support with Snowflake, reach out to your sales engineer and they can direct your support needs.

Last updated