Skip to main content

sparkql: Apache Spark SQL DataFrame schema management for sensible humans

Project description

sparkql ✨

PyPI version License: MIT CircleCI

Python Spark SQL DataFrame schema management for sensible humans.

Why use sparkql

sparkql takes the pain out of working with DataFrame schemas in PySpark. It's particularly useful when you have structured data.

In plain old PySpark, you might find that you write schemas like this:

CITY_SCHEMA = StructType()
CITY_NAME_FIELD = "name"
CITY_SCHEMA.add(StructField(CITY_NAME_FIELD, StringType(), False))
CITY_LAT_FIELD = "latitude"
CITY_SCHEMA.add(StructField(CITY_LAT_FIELD, FloatType()))
CITY_LONG_FIELD = "longitude"
CITY_SCHEMA.add(StructField(CITY_LONG_FIELD, FloatType()))

CONFERENCE_SCHEMA = StructType()
CONF_NAME_FIELD = "name"
CONFERENCE_SCHEMA.add(StructField(CONF_NAME_FIELD, StringType(), False))
CONF_CITY_FIELD = "city"
CONFERENCE_SCHEMA.add(StructField(CONF_CITY_FIELD, CITY_SCHEMA))

And then refer to fields like this:

dframe("city_name", df[CONF_CITY_FIELD][CITY_NAME_FIELD])

With sparkql, schemas become a lot more literate:

class City(StructObject):
    name = StringField(nullable=False)
    latitude = FloatField()
    longitude = FloatField()

class Conference(StructObject):
    name = StringField(nullable=False)
    city = City()

# ...create a DataFrame...

dframe = dframe.withColumn("city_name", path_col(Conference.city.name))

Features

Prettified Spark schema strings

Spark's stringified schema representation isn't very user friendly, particularly for large schemas:

StructType(List(StructField(name,StringType,false),StructField(city,StructType(List(StructField(name,StringType,false),StructField(latitude,FloatType,true),StructField(longitude,FloatType,true))),true)))

The function pretty_schema will return something more useful:

StructType(List(
    StructField(name,StringType,false),
    StructField(city,
        StructType(List(
            StructField(name,StringType,false),
            StructField(latitude,FloatType,true),
            StructField(longitude,FloatType,true))),
        true)))

Contributing

Developers who'd like to contribute to this project should refer to CONTRIBUTING.md.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for sparkql, version 0.1.2
Filename, size File type Python version Upload date Hashes
Filename, size sparkql-0.1.2-py3-none-any.whl (11.8 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size sparkql-0.1.2.tar.gz (10.0 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page