我一直在尝试将来自 kafka 的大量 json 消息(每个大约 2KB)推送到 cassandra 以触发流式传输。
模拟器---->Kafka---->SparkStreaming--->Cassandra。
它们中的每一个都在单独的 ec2 实例上运行,具有 30GB 的 Ram 和 8 核处理器作为独立的单节点设置。
当我试图从模拟器推送大约 500 万条消息时,在大约 100k 条消息之后,cassandra 停止插入消息,并且 spark 流式作业只是继续创建批处理(如 spark 流式 Web ui 中所示)。我什至检查了日志,但没有发现任何问题。
另外,我不确定我在代码中使用 spark 连接器写入 cassandra 的方式。
请看下面的代码,
/**
* Spark Streaming to cassandra code
*/
package org.sparkexample;
import java.util.HashMap;
import java.util.Map;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.streaming.Duration;
import org.apache.spark.streaming.api.java.JavaDStream;
import org.apache.spark.streaming.api.java.JavaPairReceiverInputDStream;
import org.apache.spark.streaming.api.java.JavaStreamingContext;
import org.apache.spark.streaming.kafka.KafkaUtils;
import com.datastax.spark.connector.japi.CassandraJavaUtil;
import com.datastax.spark.connector.japi.CassandraStreamingJavaUtil;
import scala.Tuple2;
public class SparkStreamingKafkaTest {
private SparkStreamingKafkaTest() {
}
public static void main(String[] args) {
if (args.length < 6) {
System.err.println("Usage: SparkStreamingKafka <zkQuorum> <group> <topics> <numThreads> <conc write> <cassandra ip>");
System.exit(1);
}
SparkConf sparkConf = new SparkConf().setAppName("SparkStreamingKafka");
//specific to cassandra
sparkConf.set("spark.cassandra.output.concurrent.writes", args[4]);
sparkConf.set("spark.cassandra.connection.host",args[5]);
// Create the context with a 2 second batch size
JavaStreamingContext jssc = new JavaStreamingContext(sparkConf, new Duration(2000));
int numThreads = Integer.parseInt(args[3]);
Map<String, Integer> topicMap = new HashMap<String, Integer>();
String[] topics = args[2].split(",");
for (String topic : topics) {
topicMap.put(topic, numThreads);
}
JavaPairReceiverInputDStream<String, String> messages = KafkaUtils.createStream(jssc, args[0], args[1],
topicMap);
JavaDStream<WordCount> wc = messages.map(new Function<Tuple2<String, String>, WordCount>() {
@Override
public WordCount call(Tuple2<String, String> tuple2) {
String key = System.currentTimeMillis()+ "_"+ Math.random();
return new WordCount(key, tuple2._2());
}
});
Map <String, String> map = new HashMap<String, String>();
map.put("word", "word");
map.put("count", "count");
CassandraStreamingJavaUtil.javaFunctions(wc).writerBuilder("mykeyspace", "wordcount",CassandraJavaUtil.mapToRow(WordCount.class, map)).saveToCassandra();
jssc.start();
jssc.awaitTermination();
}
}
WordCount.java
package org.sparkexample;
import java.io.Serializable;
public class WordCount implements Serializable{
private String word;
private String count;
public WordCount(){
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
public WordCount(String key, String count) {
this.word = key;
this.count = count;
}
}
我一直在使用具有以下主要依赖项的默认 cassandra.yml,
- 火花-cassandra-connector_2.10 - 1.4.0-M3
- spark-cassandra-connector-java_2.10 - 1.4.0-M3
- cassandra 驱动程序核心 - 2.1.7.1
- 火花流-kafka_2.10 - 1.4.1
- 火花流_2.10 - 1.4.1
- 火花核心_2.10 - 1.4.1
请提出可能是什么问题。
nodetool info 和 nodetool tpstats 的输出如下。