-1

我正在按照Spark 客户接收器提供的 spark 站点中给出的使用客户接收器的 spark 流式传输示例进行操作。

但是,这项工作似乎丢弃了我的大部分数据。无论我流传输的数据量是多少,消费者都会成功接收到它。但是,当我对其进行任何地图/平面地图操作时,我只会看到 10 行数据。无论我传输多少数据,情况总是如此。

我已修改此程序以从ActiveMQ队列中读取。如果我查看 ActiveMQ Web 界面,火花作业成功地使用了我生成的所有数据。但是,每批只处理 10 个数据。我尝试将批量大小更改为各种值,并在本地以及 6 节点 Spark 集群上进行了尝试——到处都是相同的结果。

这真的很令人沮丧,因为我不知道为什么要处理有限数量的数据。我在这里缺少什么吗?

这是我的火花程序。包括自定义接收器。此外,我并没有真正创建任何套接字连接。相反,我为测试目的对消息进行了硬编码。行为与为流创建套接字连接时相同。

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 com.rzt.main;

import com.google.common.collect.Lists;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.storage.StorageLevel;
import org.apache.spark.streaming.Duration;
import org.apache.spark.streaming.api.java.JavaDStream;
import org.apache.spark.streaming.api.java.JavaPairDStream;
import org.apache.spark.streaming.api.java.JavaReceiverInputDStream;
import org.apache.spark.streaming.api.java.JavaStreamingContext;
import org.apache.spark.streaming.receiver.Receiver;
import scala.Tuple2;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.Socket;
import java.util.regex.Pattern;

/**
 * Custom Receiver that receives data over a socket. Received bytes is
 * interpreted as text and \n delimited lines are considered as records. They
 * are then counted and printed.
 *
 * Usage: TestReceiv3 <master> <hostname> <port> <master> is the Spark master
 * URL. In local mode, <master> should be 'local[n]' with n > 1. <hostname> and
 * <port> of the TCP server that Spark Streaming would connect to receive data.
 *
 * To run this on your local machine, you need to first run a Netcat server `$
 * nc -lk 9999` and then run the example `$ bin/run-example
 * org.apache.spark.examples.streaming.TestReceiv3 localhost 9999`
 */

public class TestReceiv3 extends Receiver<String> {
    private static final Pattern SPACE = Pattern.compile(" ");

    public static void main(String[] args) {

        // Create the context with a 1 second batch size
        SparkConf sparkConf = new SparkConf().setAppName("TestReceiv3").setMaster("local[4]");
        JavaStreamingContext ssc = new JavaStreamingContext(sparkConf, new Duration(1000));

        // Create a input stream with the custom receiver on target ip:port and
        // count the
        // words in input stream of \n delimited text (eg. generated by 'nc')
        JavaReceiverInputDStream<String> lines = ssc.receiverStream(new TestReceiv3("TEST", 1));
        JavaDStream<String> words = lines.flatMap(new FlatMapFunction<String, String>() {
            public Iterable<String> call(String x) {
                System.out.println("Message received" + x);
                return Lists.newArrayList(x);
            }
        });

        words.print();
        ssc.start();
        ssc.awaitTermination();
    }

    // ============= Receiver code that receives data over a socket
    // ==============

    String host = null;
    int port = -1;

    public TestReceiv3(String host_, int port_) {
        super(StorageLevel.MEMORY_AND_DISK_2());
        host = host_;
        port = port_;
    }

    public void onStart() {
        // Start the thread that receives data over a connection
        new Thread() {
            @Override
            public void run() {
                receive();
            }
        }.start();
    }

    public void onStop() {
        // There is nothing much to do as the thread calling receive()
        // is designed to stop by itself isStopped() returns false
    }

    /** Create a socket connection and receive data until receiver is stopped */
    private void receive() {
        Socket socket = null;
        String userInput = null;

        try {

            int i = 0;
            // Until stopped or connection broken continue reading
            while (true) {
                i++;
                store("MESSAGE " + i);
                if (i == 1000)
                    break;
            }

            // Restart in an attempt to connect again when server is active
            // again
            restart("Trying to connect again");
        } catch (Throwable t) {
            restart("Error receiving data", t);
        }
    }
}
4

1 回答 1

3

您看到的输出来自words.print(). DStream.print仅打印 DStream 的前 10 个元素。从文档

def print(): 单位

打印此 DStream 中生成的每个 RDD 的前十个元素。这是一个输出操作符,所以这个 DStream 将被注册为一个输出流并在那里实现。

您将需要将流数据存储在某处(例如使用DStream.saveAsTextFiles(...)以全面检查它。

于 2014-12-11T09:27:59.157 回答