Hi guys i have the next problem. I'm using Apache Spark Streaming v1.6.0 with Java to have some messages from IBM MQ. I made my custom receiver for MQ but the problem that i have is that i need to convert the RDD from JavaDStream to DataFrame. For that i iterate the JavaDStream with the foreachRDD and i defined the schema for the DataFrame but when i run the job, the firsts messages tthrow the next exception:

java.lang.ClassCastException: org.apache.spark.rdd.BlockRDDPartition cannot be cast to org.apache.spark.rdd.ParallelCollectionPartition at org.apache.spark.rdd.ParallelCollectionRDD.compute(ParallelCollectionRDD.scala:102) at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:306) at org.apache.spark.rdd.RDD.iterator(RDD.scala:270) at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:66) at org.apache.spark.scheduler.Task.run(Task.scala:89) at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:213) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) 19/03/28 12:53:26 WARN TaskSetManager: Lost task 0.0 in stage 0.0 (TID 0, localhost): java.lang.ClassCastException: org.apache.spark.rdd.BlockRDDPartition cannot be cast to org.apache.spark.rdd.ParallelCollectionPartition at org.apache.spark.rdd.ParallelCollectionRDD.compute(ParallelCollectionRDD.scala:102) at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:306) at org.apache.spark.rdd.RDD.iterator(RDD.scala:270) at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:66) at org.apache.spark.scheduler.Task.run(Task.scala:89) at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:213) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)

Then the code executes very well. Even if i dont have any messages in MQ, is just the first messages when i run de the job.

Here is my CustomMQReceiver

public CustomMQReceiver() {

        super(StorageLevel.MEMORY_ONLY_2());

    }

    @Override
    public void onStart() {

        new Thread() {
            @Override
            public void run() {
                try {
                    initConnection();
                    receive();
                } catch (JMSException ex) {
                    ex.printStackTrace();
                }
            }
        }.start();

    }

    @Override
    public void onStop() {

    }

    private void receive() {

        System.out.print("Started receiving messages from MQ");

        try {

            Message receivedMessage = null;

            while (!isStopped() && (receivedMessage = consumer.receiveNoWait()) != null) {

                String userInput = convertStreamToString(receivedMessage);
                System.out.println("Received data :'" + userInput + "'");
                store(userInput);
            }

            stop("No More Messages To read !");
            qCon.close();
            System.out.println("Queue Connection is Closed");

        } catch (Exception e) {
            e.printStackTrace();
            restart("Trying to connect again");
        } catch (Throwable t) {

            restart("Error receiving data", t);
        }

    }

    public void initConnection() throws JMSException {

        MQQueueConnectionFactory conFactory = new MQQueueConnectionFactory();
        conFactory.setHostName(HOST);
        conFactory.setPort(PORT);
        conFactory.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
        conFactory.setQueueManager(QMGR);
        conFactory.setChannel(CHANNEL);
        conFactory.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, true);
        conFactory.setStringProperty(WMQConstants.USERID, APP_USER);
        conFactory.setStringProperty(WMQConstants.PASSWORD, APP_PASSWORD);

        qCon = (MQQueueConnection) conFactory.createConnection();
        MQQueueSession qSession = (MQQueueSession) qCon.createQueueSession(false, 1);
        MQQueue queue = (MQQueue) qSession.createQueue(QUEUE_NAME);
        consumer = (MQMessageConsumer) qSession.createConsumer(queue);
        qCon.start();

    }

    @Override
    public StorageLevel storageLevel() {
        return StorageLevel.MEMORY_ONLY_2();
    }

    private static String convertStreamToString(final Message jmsMsg) throws Exception {

        String stringMessage = "";
        JMSTextMessage msg = (JMSTextMessage) jmsMsg;
        stringMessage = msg.getText();

        return stringMessage;
    }

And here is my code of spark

SparkConf sparkConf = new SparkConf()
                    .setAppName("MQStreaming")
                    .set("spark.driver.allowMultipleContexts", "true")
                    .setMaster("local[*]");

            JavaSparkContext jsc = new JavaSparkContext(sparkConf);
            final SQLContext sqlContext = new SQLContext(jsc);
            JavaStreamingContext ssc = new JavaStreamingContext(sparkConf, new Duration(Long.parseLong(propertiesConf.getProperty("duration"))));

            JavaDStream<String> customReceiverStream = ssc.receiverStream(new CustomMQReceiver());

            customReceiverStream.foreachRDD(new VoidFunction<JavaRDD<String>>() {

                @Override
                public void call(JavaRDD<String> rdd) throws Exception {

                    JavaRDD<Row> rddRow = rdd.map(new Function<String, Row>() {

                        @Override
                        public Row call(String v1) throws Exception {

                            return RowFactory.create(v1);

                        }

                    });

                    try {

                        StructType schema = new StructType(new StructField[]{
                            new StructField("trama", DataTypes.StringType, true, Metadata.empty())
                        });

                        DataFrame frame = sqlContext.createDataFrame(rddRow, schema);

                        if (frame.count() > 0) {
                            //Here is where the first messages throw the exception
                            frame.show();
                            frame.write().mode(SaveMode.Append).json("file:///C:/tmp/");

                        }

                    } catch (Exception ex) {

                        System.out.println(" INFO " + ex.getMessage());

                    }

                }

            });

            ssc.start();
            ssc.awaitTermination();

I can't change the version of spark because this job will run in a old cloudera cluster with spark 1.6. I don't know if i'm doing something wrong or is a just a bug. Help!!!!

1 Answers

0
Emiliano Lara On Best Solutions

I solved my own problem, this exception is thrown by how I create the SQLContext, the correct way is create the sqlContext with the JavaStreamingContext

//JavaStreamingContext jsc = ...
SQLContext sqlContext = new SQLContext(jsc.sparkContext());