Search in sources :

Example 61 with FormatSpecification

use of co.cask.cdap.api.data.format.FormatSpecification in project cdap by caskdata.

the class StreamFormatSpecSpark method run.

@Override
public void run(JavaSparkExecutionContext sec) throws Exception {
    JavaSparkContext jsc = new JavaSparkContext();
    SQLContext sqlContext = new SQLContext(jsc);
    // Read from CSV stream and turn it into a DataFrame
    String streamName = sec.getRuntimeArguments().get("stream.name");
    Schema schema = Schema.recordOf("record", ImmutableList.of(Schema.Field.of("name", Schema.of(Schema.Type.STRING)), Schema.Field.of("age", Schema.of(Schema.Type.INT))));
    FormatSpecification formatSpec = new FormatSpecification("csv", schema);
    JavaPairRDD<Long, GenericStreamEventData<StructuredRecord>> rdd = sec.fromStream(streamName, formatSpec, StructuredRecord.class);
    JavaRDD<Person> personRDD = rdd.values().map(new Function<GenericStreamEventData<StructuredRecord>, Person>() {

        @Override
        public Person call(GenericStreamEventData<StructuredRecord> data) throws Exception {
            StructuredRecord record = data.getBody();
            return new Person(record.<String>get("name"), record.<Integer>get("age"));
        }
    });
    sqlContext.createDataFrame(personRDD, Person.class).registerTempTable("people");
    // Execute a SQL on the table and save the result
    JavaPairRDD<String, Integer> resultRDD = sqlContext.sql(sec.getRuntimeArguments().get("sql.statement")).toJavaRDD().mapToPair(new PairFunction<Row, String, Integer>() {

        @Override
        public Tuple2<String, Integer> call(Row row) throws Exception {
            return new Tuple2<>(row.getString(0), row.getInt(1));
        }
    });
    sec.saveAsDataset(resultRDD, sec.getRuntimeArguments().get("output.dataset"));
}
Also used : Schema(co.cask.cdap.api.data.schema.Schema) FormatSpecification(co.cask.cdap.api.data.format.FormatSpecification) GenericStreamEventData(co.cask.cdap.api.stream.GenericStreamEventData) StructuredRecord(co.cask.cdap.api.data.format.StructuredRecord) Tuple2(scala.Tuple2) JavaSparkContext(org.apache.spark.api.java.JavaSparkContext) Row(org.apache.spark.sql.Row) SQLContext(org.apache.spark.sql.SQLContext)

Aggregations

FormatSpecification (co.cask.cdap.api.data.format.FormatSpecification)61 Test (org.junit.Test)43 Schema (co.cask.cdap.api.data.schema.Schema)32 StructuredRecord (co.cask.cdap.api.data.format.StructuredRecord)19 StreamEvent (co.cask.cdap.api.flow.flowlet.StreamEvent)17 StreamId (co.cask.cdap.proto.id.StreamId)16 ViewSpecification (co.cask.cdap.proto.ViewSpecification)14 StreamProperties (co.cask.cdap.proto.StreamProperties)11 StreamViewId (co.cask.cdap.proto.id.StreamViewId)11 DatasetId (co.cask.cdap.proto.id.DatasetId)6 NamespaceMeta (co.cask.cdap.proto.NamespaceMeta)5 NamespaceId (co.cask.cdap.proto.id.NamespaceId)5 MetadataSearchResultRecord (co.cask.cdap.proto.metadata.MetadataSearchResultRecord)5 IOException (java.io.IOException)5 UnsupportedTypeException (co.cask.cdap.api.data.schema.UnsupportedTypeException)4 NotFoundException (co.cask.cdap.common.NotFoundException)3 ApplicationId (co.cask.cdap.proto.id.ApplicationId)3 ArtifactId (co.cask.cdap.proto.id.ArtifactId)3 ProgramId (co.cask.cdap.proto.id.ProgramId)3 HttpURLConnection (java.net.HttpURLConnection)3