kina.examples.java.ReadingCellFromMongoDB.java Source code

Java tutorial

Introduction

Here is the source code for kina.examples.java.ReadingCellFromMongoDB.java

Source

/*
 * Copyright 2014, Luca Rosellini.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package kina.examples.java;

import com.mongodb.QueryBuilder;
import kina.config.MongoKinaConfig;
import kina.config.MongoConfigFactory;
import kina.context.MongoKinaContext;
import kina.testutils.ContextProperties;
import org.apache.log4j.Logger;
import org.apache.spark.rdd.RDD;
import org.bson.BSONObject;
import org.bson.BasicBSONObject;

/**
 * Example class to read a collection from mongoDB
 */
public final class ReadingCellFromMongoDB {
    private static final Logger LOG = Logger.getLogger(ReadingCellFromMongoDB.class);

    private ReadingCellFromMongoDB() {
    }

    public static void main(String[] args) {
        doMain(args);
    }

    public static void doMain(String[] args) {
        String job = "java:readingCellFromMongoDB";

        String host = "127.0.0.1:27017";

        String database = "test";
        String inputCollection = "input";

        // Creating the Kina Context where args are Spark Master and Job Name
        ContextProperties p = new ContextProperties(args);
        MongoKinaContext kinaContext = new MongoKinaContext(p.getCluster(), job, p.getSparkHome(), p.getJars());

        QueryBuilder query = QueryBuilder.start();
        query.and("number").greaterThan(27).lessThan(30);

        BSONObject bsonSort = new BasicBSONObject();
        bsonSort.put("number", 1);

        BSONObject bsonFields = new BasicBSONObject();
        bsonFields.put("number", 1);
        bsonFields.put("text", 1);
        bsonFields.put("_id", 0);

        MongoKinaConfig inputConfigEntity = MongoConfigFactory.createMongoDB().host(host).database(database)
                .collection(inputCollection).createInputSplit(false).filterQuery(query).sort(bsonSort)
                .fields(bsonFields).initialize();

        RDD inputRDDEntity = kinaContext.mongoRDD(inputConfigEntity);

        LOG.info("count : " + inputRDDEntity.count());
        LOG.info("prints first cell  : " + inputRDDEntity.first());

        kinaContext.stop();
    }
}