apache_beam.transforms.sql module

Package for SqlTransform and related classes.

class apache_beam.transforms.sql.SqlTransform(query, dialect=None)[source]

Bases: apache_beam.transforms.external.ExternalTransform

A transform that can translate a SQL query into PTransforms.

Input PCollections must have a schema. Currently, this means the PCollection must have a NamedTuple output type, and that type must be registered to use RowCoder. For example:

Purchase = typing.NamedTuple('Purchase',
                             [('item_name', unicode), ('price', float)])
coders.registry.register_coder(Purchase, coders.RowCoder)

Similarly, the output of SqlTransform is a PCollection with a generated NamedTuple type, and columns can be accessed as fields. For example:

purchases | SqlTransform("""
              SELECT item_name, COUNT(*) AS `count`
              FROM PCOLLECTION GROUP BY item_name""")
          | beam.Map(lambda row: "We've sold %d %ss!" % (row.count,
                                                         row.item_name))

Additional examples can be found in apache_beam.examples.wordcount_xlang_sql, and apache_beam.transforms.sql_test.

For more details about Beam SQL in general see the Java transform, and the documentation.

URN = 'beam:external:java:sql:v1'