JSON Files¶
-
class
JSONExtractor(input_path=None, base_path=None, partition=None)[source]¶ Bases:
spooq2.extractor.extractor.ExtractorThe JSONExtractor class provides an API to extract data stored as JSON format, deserializes it into a PySpark dataframe and returns it. Currently only single-line JSON files are supported, stored either as textFile or sequenceFile.
Examples
>>> from spooq2 import extractor as E
>>> extractor = E.JSONExtractor(input_path="tests/data/schema_v1/sequenceFiles") >>> extractor.input_path == "tests/data/schema_v1/sequenceFiles" + "/*" True
>>> extractor = E.JSONExtractor( >>> base_path="tests/data/schema_v1/sequenceFiles", >>> partition="20200201" >>> ) >>> extractor.input_path == "tests/data/schema_v1/sequenceFiles" + "/20/02/01" + "/*" True
Parameters: - input_path (
str) – The path from which the JSON files should be loaded (“/*” will be added if omitted) - base_path (
str) – Spooq tries to infer theinput_pathfrom thebase_pathand thepartitionif theinput_pathis missing. - partition (
strorint) – Spooq tries to infer theinput_pathfrom thebase_pathand thepartitionif theinput_pathis missing. Only daily partitions in the form of “YYYYMMDD” are supported. e.g., “20200201” => <base_path> + “/20/02/01/*”
Returns: The extracted data set as a PySpark DataFrame
Return type: Raises: exceptions.AttributeError– Please define eitherinput_pathorbase_pathandpartitionWarning
Currently only single-line JSON files stored as SequenceFiles or TextFiles are supported!
Note
The init method checks which input parameters are provided and derives the final input_path from them accordingly.
- If
input_pathis notNone: - Cleans
input_pathand returns it as the finalinput_path - Elif
base_pathandpartitionare notNone: - Cleans
base_path, infers the sub path from thepartitionand returns the combined string as the finalinput_path - Else:
- Raises an
exceptions.AttributeError
-
extract()[source]¶ This is the Public API Method to be called for all classes of Extractors
Returns: Complex PySpark DataFrame deserialized from the input JSON Files Return type: pyspark.sql.DataFrame
- input_path (