嗨,我有下一个问题。我正在将Apache Spark Streaming v1.6.0与Java结合使用,以获取来自IBM MQ的消息。我为MQ创建了自定义接收器,但是我遇到的问题是我需要将RDD从JavaDStream转换为DataFrame。为此,我使用foreachRDD迭代了JavaDStream并定义了DataFrame的架构,但是当我运行作业时,第一个消息将抛出下一个异常:
java.lang.ClassCastException:org.apache.spark.rdd.BlockRDDPartition无法转换为org.apache.spark.rdd.ParallelCollectionPartition 在org.apache.spark.rdd.ParallelCollectionRDD.compute(ParallelCollectionRDD.scala:102) 在org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:306) 在org.apache.spark.rdd.RDD.iterator(RDD.scala:270) 在org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:66) 在org.apache.spark.scheduler.Task.run(Task.scala:89) 在org.apache.spark.executor.Executor $ TaskRunner.run(Executor.scala:213) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624) 在java.lang.Thread.run(Thread.java:748) 19/03/28 12:53:26警告TaskSetManager:在阶段0.0(TID 0,本地主机)中丢失了任务0.0:java.lang.ClassCastException:org.apache.spark.rdd.BlockRDDPartition无法转换为org.apache.spark .rdd.ParallelCollectionPartition 在org.apache.spark.rdd.ParallelCollectionRDD.compute(ParallelCollectionRDD.scala:102) 在org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:306) 在org.apache.spark.rdd.RDD.iterator(RDD.scala:270) 在org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:66) 在org.apache.spark.scheduler.Task.run(Task.scala:89) 在org.apache.spark.executor.Executor $ TaskRunner.run(Executor.scala:213) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624) 在java.lang.Thread.run(Thread.java:748)
然后,代码执行得很好。即使我在MQ中没有任何消息,这也是我在执行工作时的第一条消息。
这是我的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;
}
这是我的火花代码
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();
我无法更改spark的版本,因为此作业将在带有spark 1.6的旧cloudera集群中运行。我不知道我做错了什么还是仅仅是一个错误。救命!!!
答案 0 :(得分:0)
我解决了自己的问题,我创建SQLContext的方式抛出了此异常,正确的方法是使用JavaStreamingContext创建sqlContext
//JavaStreamingContext jsc = ...
SQLContext sqlContext = new SQLContext(jsc.sparkContext());