Package org.apache.spark.api.java

Examples of org.apache.spark.api.java.JavaSparkContext


    sparkConf.setIfMissing("spark.logConf", "true");
    sparkConf.setMaster(streamingMaster);
    sparkConf.setAppName("OryxBatchLayer");
    final long batchDurationMS =
       TimeUnit.MILLISECONDS.convert(generationIntervalSec, TimeUnit.SECONDS);
    final JavaSparkContext sparkContext = new JavaSparkContext(sparkConf);

    JavaStreamingContextFactory streamingContextFactory = new JavaStreamingContextFactory() {
      @Override
      public JavaStreamingContext create() {
        return new JavaStreamingContext(sparkContext, new Duration(batchDurationMS));
      }
    };

    if (checkpointDirString == null) {
      log.info("Not using a streaming checkpoint dir");
      streamingContext = streamingContextFactory.create();
    } else {
      log.info("Using streaming checkpoint dir {}", checkpointDirString);
      streamingContext = JavaStreamingContext.getOrCreate(
          checkpointDirString, sparkContext.hadoopConfiguration(), streamingContextFactory, false);
      streamingContext.checkpoint(checkpointDirString);
    }

    log.info("Creating message queue stream");
View Full Code Here


public class JavaApiDemo implements Serializable {

    public JavaApiDemo() {
        // just an initialisation of Spark Context
        DemoApp demoApp = DemoApp$.MODULE$.apply();
        JavaSparkContext sc = new JavaSparkContext(demoApp.sc());

        // here we are going to save some data to Cassandra...
        List<Person> people = Arrays.asList(
                Person.newInstance(1, "John", new Date()),
                Person.newInstance(2, "Anna", new Date()),
                Person.newInstance(3, "Andrew", new Date())
        );
        JavaRDD<Person> rdd = sc.parallelize(people);
        javaFunctions(rdd, Person.class).saveToCassandra("test", "people");

        // use case: we want to read that data as an RDD of CassandraRows and convert them to strings...
        JavaRDD<String> cassandraRowsRDD = javaFunctions(sc).cassandraTable("test", "people")
                .map(new Function<CassandraRow, String>() {
                    @Override
                    public String call(CassandraRow cassandraRow) throws Exception {
                        return cassandraRow.toString();
                    }
                });
        System.out.println("Data as CassandraRows: \n" + StringUtils.join("\n", cassandraRowsRDD.toArray()));

        // use case: we want to read that data as an RDD of Person beans and also convert them to strings...
        JavaRDD<String> rdd2 = javaFunctions(sc).cassandraTable("test", "people", Person.class)
                .map(new Function<Person, String>() {
                    @Override
                    public String call(Person person) throws Exception {
                        return person.toString();
                    }
                });
        System.out.println("Data as Person beans: \n" + StringUtils.join("\n", rdd2.toArray()));

        // use case: we want to filter rows on the database side with use of the where clause
        JavaRDD<String> rdd3 = javaFunctions(sc).cassandraTable("test", "people", Person.class)
                .where("name=?", "Anna").map(new Function<Person, String>() {
                    @Override
                    public String call(Person person) throws Exception {
                        return person.toString();
                    }
                });
        System.out.println("Data filtered by the where clause (name='Anna'): \n" + StringUtils.join("\n", rdd3.toArray()));

        // use case: we want to explicitly set a projection on the column set
        JavaRDD<String> rdd4 = javaFunctions(sc).cassandraTable("test", "people")
                .select("id").map(new Function<CassandraRow, String>() {
                    @Override
                    public String call(CassandraRow cassandraRow) throws Exception {
                        return cassandraRow.toString();
                    }
                });
        System.out.println("Data with only 'id' column fetched: \n" + StringUtils.join("\n", rdd4.toArray()));

        sc.stop();
    }
View Full Code Here

public class JavaApiDemo implements Serializable {

    public JavaApiDemo() {
        // just an initialisation of Spark Context
        DemoApp demoApp = DemoApp$.MODULE$.apply();
        JavaSparkContext sc = new JavaSparkContext(demoApp.sc());

        // here we are going to save some data to Cassandra...
        List<Person> people = Arrays.asList(
                Person.newInstance(1, "John", new Date()),
                Person.newInstance(2, "Anna", new Date()),
                Person.newInstance(3, "Andrew", new Date())
        );
        JavaRDD<Person> rdd = sc.parallelize(people);
        javaFunctions(rdd)
                .writerBuilder("test", "people", mapToRows(Person.class))
                .saveToCassandra();

        // use case: we want to read that data as an RDD of CassandraRows and convert them to strings...
        JavaRDD<String> cassandraRowsRDD = javaFunctions(sc).cassandraTable("test", "people")
                .map(new Function<CassandraRow, String>() {
                    @Override
                    public String call(CassandraRow cassandraRow) throws Exception {
                        return cassandraRow.toString();
                    }
                });
        System.out.println("Data as CassandraRows: \n" + StringUtils.join("\n", cassandraRowsRDD.collect()));

        // use case: we want to read that data as an RDD of Person beans and also convert them to strings...
        JavaRDD<String> rdd2 = javaFunctions(sc).cassandraTable("test", "people", mapRowTo(Person.class))
                .map(new Function<Person, String>() {
                    @Override
                    public String call(Person person) throws Exception {
                        return person.toString();
                    }
                });
        System.out.println("Data as Person beans: \n" + StringUtils.join("\n", rdd2.collect()));

        // use case: we want to filter rows on the database side with use of the where clause
        JavaRDD<String> rdd3 = javaFunctions(sc).cassandraTable("test", "people", mapRowTo(Person.class))
                .where("name=?", "Anna").map(new Function<Person, String>() {
                    @Override
                    public String call(Person person) throws Exception {
                        return person.toString();
                    }
                });
        System.out.println("Data filtered by the where clause (name='Anna'): \n" + StringUtils.join("\n", rdd3.collect()));

        // use case: we want to explicitly set a projection on the column set
        JavaRDD<String> rdd4 = javaFunctions(sc).cassandraTable("test", "people")
                .select("id").map(new Function<CassandraRow, String>() {
                    @Override
                    public String call(CassandraRow cassandraRow) throws Exception {
                        return cassandraRow.toString();
                    }
                });
        System.out.println("Data with only 'id' column fetched: \n" + StringUtils.join("\n", rdd4.collect()));

        sc.stop();
    }
View Full Code Here

    }

    @Test
    public void testJavaFunctions1() throws Exception {
        SparkContext sc = mock(SparkContext.class);
        JavaSparkContext jsc = mock(JavaSparkContext.class);
        when(jsc.sc()).thenReturn(sc);
        SparkContextJavaFunctions scjf = javaFunctions(jsc);
        assertThat(scjf.sparkContext, is(jsc.sc()));
    }
View Full Code Here

TOP

Related Classes of org.apache.spark.api.java.JavaSparkContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.