适用于Consumer的Amazon Kinesis KCL客户端无法在.NET中工作

时间:2019-02-05 05:57:21

标签: c# .net amazon-web-services amazon-kinesis

请帮助。我在.NET控制台应用程序中设置Kinesis消费者数据流时遇到问题。

我已经按照文档完成了所有操作,但是无论何时运行使用者,我仍然会看到空白的控制台屏幕。到目前为止,生产者运行良好,并且AWS凭证正在运行。

  1. 我的系统上的JDK都配置正确(对Java开发而言不是新手)
  2. 我已将所有必要的策略附加到我的IAM用户
  3. 我看到生产者可以使用相同的AWS凭证以编程方式创建流,desc流等

创建KclProcess时,我可以在程序中达到断点,但是在下面的KinesisTest类中无法达到任何断点

对于消费者,我创建了一个如下所示的Program.cs类:

class Program  
{

    public static void Main(string[] args)
    {
        //added these lines after trying everything
        Environment.SetEnvironmentVariable("AWS_ACCESS_KEY_ID", "***");
        Environment.SetEnvironmentVariable("AWS_SECRET_ACCESS_KEY", "***");
        Environment.SetEnvironmentVariable("AWS_REGION", "us-east-1");

        try
        {
            KclProcess.Create(new KinesisTest()).Run();
        }
        catch (Exception e)
        {
            Console.Error.WriteLine("ERROR: " + e);
        }
    }

}

和另一个班

public class KinesisTest: IRecordProcessor
{

    private static readonly TimeSpan Backoff = TimeSpan.FromSeconds(3);
    private static readonly TimeSpan CheckpointInterval = TimeSpan.FromMinutes(1);
    private static readonly int NumRetries = 10;

    /// <value>The shard ID on which this record processor is working.</value>
    private string _kinesisShardId;

    private DateTime _nextCheckpointTime = DateTime.UtcNow;


    public void Initialize(InitializationInput input)
    {
        Console.Error.WriteLine("Initializing record processor for shard: " + input.ShardId);
        this._kinesisShardId = input.ShardId;
    }

    public void ProcessRecords(ProcessRecordsInput input)
    {
        Console.Error.WriteLine("Processing " + input.Records.Count + " records from " + _kinesisShardId);
        ProcessRecordsWithRetries(input.Records);

        // Checkpoint once every checkpoint interval.
        if (DateTime.UtcNow >= _nextCheckpointTime)
        {
            Checkpoint(input.Checkpointer);
            _nextCheckpointTime = DateTime.UtcNow + CheckpointInterval;
        }
    }

    public void Shutdown(ShutdownInput input)
    {
        Console.Error.WriteLine("Shutting down record processor for shard: " + _kinesisShardId);
        // Checkpoint after reaching end of shard, so we can start processing data from child shards.
        if (input.Reason == ShutdownReason.TERMINATE)
        {
            Checkpoint(input.Checkpointer);
        }
    }

    private void ProcessRecordsWithRetries(List<Record> records)
    {
        foreach (Record rec in records)
        {
            bool processedSuccessfully = false;
            string data = null;
            for (int i = 0; i < NumRetries; ++i)
            {
                try
                {
                    data = System.Text.Encoding.UTF8.GetString(rec.Data);

                    Console.Error.WriteLine( String.Format("Retrieved record:\n\tpartition key = {0},\n\tsequence number = {1},\n\tdata = {2}", rec.PartitionKey, rec.SequenceNumber, data));

                    // Your own logic to process a record goes here.

                    processedSuccessfully = true;
                    break;
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine("Exception processing record data: " + data, e);
                }

                //Back off before retrying upon an exception.
                Thread.Sleep(Backoff);
            }

            if (!processedSuccessfully)
            {
                Console.Error.WriteLine("Couldn't process record " + rec + ". Skipping the record.");
            }
        }
    }

    private void Checkpoint(Checkpointer checkpointer)
    {
        Console.Error.WriteLine("Checkpointing shard " + _kinesisShardId);

        checkpointer.Checkpoint(RetryingCheckpointErrorHandler.Create(NumRetries, Backoff));
    }
}

最后是kcl.properties文件:

executableName = dotnet KinesisTest.dll

streamName = testStream

applicationName = KinesisTest

AWSCredentialsProvider = DefaultAWSCredentialsProviderChain

processingLanguage = C#

initialPositionInStream = TRIM_HORIZON

regionName = us-east-1

maxRecords = 5000

idleTimeBetweenReadsInMillis = 1000

# failoverTimeMillis = 10000
# workerId =
# shardSyncIntervalMillis = 60000
# callProcessRecordsEvenForEmptyRecordList = false
# parentShardPollIntervalMillis = 10000
# cleanupLeasesUponShardCompletion = true
# taskBackoffTimeMillis = 500
# metricsBufferTimeMillis = 10000
# metricsMaxQueueSize = 10000
# validateSequenceNumberBeforeCheckpointing = true
# maxActiveThreads = 0

如果我做错了事,请让我知道。

我期望看到消费者正在处理流中的数据,但这只是一个空控制台

1 个答案:

答案 0 :(得分:0)

虽然我从未找到这个问题的答案,但我找到了一种使用lambda进行此操作的更好且正确的方法。

我的最终设置包括使用SNS从Kinesis获取消息/事件,然后将消息散发给任何订阅者(在我的情况下,是SQS队列),然后将排队的消息提供给lambda。

类似这样的东西:

                             SQS Queue ----------------> Lambda
                             ^
                            /
                           /
                          /
KINESIS STREAM ------->SNS-------->SQS Queue ------> Lambda
                          \
                           \
                            \
                             >SQS Queue ----------------> Lambda

您可能会问为什么不直接将Kinesis转换为SQS?还是为什么不将SNS直接发送给Lambda?

  

第一个问题的答案是Kinesis不是针对多个版本构建的   读。 SNS允许您阅读一次并将其扇出   所需的订阅者。如果您说有20位正在等待的听众   要对某个事件采取行动,则不能将它们全部插入Kinesis。所以去SNS。

     

第二个问题的答案是SNS有时会丢失消息,并且   不会传递或触发lambda(非常正确)。但是(推测   根据经验),如果您将其连接到   SQS,它几乎总是在队列中。然后队列可以触发   lambda。.他们反正只有一份工作。

所以我希望这对某人有帮助。