我正在构建一个应用程序,它从redis a的列表中读取json元素,并使用spark对它们进行流式处理。
这是我写的:
public void readTheStream() throws UnknownHostException, IOException {
SparkConf sparkConf = new SparkConf().setMaster("local[*]").setAppName("Merge").set("redis.host", "localhost")
.set("redis.port", "6379");;
JavaSparkContext ctx = JavaSparkContext.fromSparkContext(SparkContext.getOrCreate(sparkConf));
JavaStreamingContext context = new JavaStreamingContext(ctx, Durations.seconds(1));
}
如何使用jssc对象访问redis。提前谢谢。
最佳答案
下面是一个从myList
读取并将列表项打印到控制台的示例:
SparkConf sparkConf = new SparkConf().setAppName("MyApp").setMaster("local[*]")
.set("redis.host", "localhost")
.set("redis.port", "6379");
JavaStreamingContext jssc = new JavaStreamingContext(sparkConf, Durations.milliseconds(1000));
RedisConfig redisConfig = new RedisConfig(new RedisEndpoint(sparkConf));
RedisStreamingContext redisStreamingContext = new RedisStreamingContext(jssc.ssc());
String[] keys = new String[]{"myList"};
RedisInputDStream<Tuple2<String, String>> redisStream =
redisStreamingContext.createRedisStream(keys, StorageLevel.MEMORY_ONLY(), redisConfig);
redisStream.print();
jssc.start();
jssc.awaitTermination();