Search in sources :

Example 1 with Customer

use of org.apache.beam.sdk.extensions.sql.example.model.Customer in project beam by apache.

the class BeamSqlPojoExample method main.

public static void main(String[] args) {
    PipelineOptions options = PipelineOptionsFactory.fromArgs(args).create();
    Pipeline pipeline = Pipeline.create(options);
    // First step is to get PCollections of source objects.
    // In this example we create them directly in memory using Create.of().
    // 
    // In real world such PCollections will likely be obtained from some other source,
    // e.g. a database or a text file. This process is not specific to Beam SQL,
    // please consult Beam programming guide for details.
    PCollection<Customer> customers = loadCustomers(pipeline);
    PCollection<Order> orders = loadOrders(pipeline);
    // Example 1. Run a simple query over java objects:
    PCollection<Row> customersFromWonderland = customers.apply(SqlTransform.query("SELECT id, name " + " FROM PCOLLECTION " + " WHERE countryOfResidence = 'Wonderland'"));
    // Output the results of the query:
    customersFromWonderland.apply(logRecords(": is from Wonderland"));
    // Example 2. Query the results of the first query:
    PCollection<Row> totalInWonderland = customersFromWonderland.apply(SqlTransform.query("SELECT COUNT(id) FROM PCOLLECTION"));
    // Output the results of the query:
    totalInWonderland.apply(logRecords(": total customers in Wonderland"));
    // Example 3. Query multiple PCollections of Java objects:
    PCollection<Row> ordersByGrault = PCollectionTuple.of(new TupleTag<>("customers"), customers).and(new TupleTag<>("orders"), orders).apply(SqlTransform.query("SELECT customers.name, ('order id:' || CAST(orders.id AS VARCHAR))" + " FROM orders " + "   JOIN customers ON orders.customerId = customers.id" + " WHERE customers.name = 'Grault'"));
    // Output the results of the query:
    ordersByGrault.apply(logRecords(": ordered by 'Grault'"));
    pipeline.run().waitUntilFinish();
}
Also used : Order(org.apache.beam.sdk.extensions.sql.example.model.Order) Customer(org.apache.beam.sdk.extensions.sql.example.model.Customer) PipelineOptions(org.apache.beam.sdk.options.PipelineOptions) TupleTag(org.apache.beam.sdk.values.TupleTag) Row(org.apache.beam.sdk.values.Row) Pipeline(org.apache.beam.sdk.Pipeline)

Aggregations

Pipeline (org.apache.beam.sdk.Pipeline)1 Customer (org.apache.beam.sdk.extensions.sql.example.model.Customer)1 Order (org.apache.beam.sdk.extensions.sql.example.model.Order)1 PipelineOptions (org.apache.beam.sdk.options.PipelineOptions)1 Row (org.apache.beam.sdk.values.Row)1 TupleTag (org.apache.beam.sdk.values.TupleTag)1