Downloading Streaming Data

Sample Code

Initialize a DIS client as instructed in Initializing a DIS SDK Client Instance. Use the client instance to obtain data through the DIS stream.

var dic = new DISIngestionClient();
//Specify the stream name.
String streamName = "streamName";     
//Specify the partition ID.
String shardId = "shardId-0000000000";    
//Specify the sequence number.
const string startingSequenceNumber = "0";
//Specify the data download mode.
const string shardIteratorType = "TRIM_HORIZON";
//Obtain the data cursor.
            var request = new GetShardIteratorRequest
            {
//Specify the stream name.
                StreamName = streamName,
//Specify the partition ID.
                ShardId = shardId,
//Specify the sequence number.
                StartingSequenceNumber = startingSequenceNumber,
//Specify the cursor type.
                ShardIteratorType = shardIteratorType
            };

            var recordsRequest = new GetRecordsRequest();
            var response = dic.GetShardIterator(request);
            Console.Out.WriteLine(response);

            var iterator = response.ShardIterator;
//Download data.
            while (true)
            {
//Obtain the data cursor.
                recordsRequest.ShardIterator = iterator;
                var recordResponse = dic.GetRecords(recordsRequest);
                if (recordResponse.Records.Count > 0)
                {
                    foreach (var record in recordResponse.Records)
                    {
                        Console.WriteLine("Record[{0}] = {1}", record.SequenceNumber, DecodeData(record.Data));
                    }
                }
                else
                {
                    break;
                }
                iterator = recordResponse.NextShardIterator;
                
}

Viewing Execution Results

Run Ctrl+F5. Information similar to the following is displayed on the console:

GetShardIteratorResult[shardIterator=eyJnZXRJdGVyYXRvclBhcmFtIjp7InN0cmVhbS1uYW1lIjoiZGlzLXNoYXc1IiwicGFydGl0aW9uLWlkIjoic2hhcmRJZC0wMDAwMDAwMDAwIiwiY3Vyc29yLXR5cGUiOiJUUklNX0hPUklaT04iLCJzdGFydGluZy1zZXF1ZW5jZS1udW1iZXIiOiIwIn0sImdlbmVyYXRlVGltZXN0YW1wIjoxNTMxMTI1ODEwNDE1fQ]
Record[0] = shardId-00000000000
Record[1] = shardId-00000000001
Record[2] = shardId-00000000002
Table 1 Response parameter description

Parameter

Type

Description

records

List<Record>

Information of data records.

next_partition_cursor

String

Next iterator.

NOTE:

The validity period of a cursor is 5 minutes.

Description of parameter Record

partition_key

String

Partition into which data will be written.

sequence_number

String

Sequence number. A sequence number is the unique identifier of each record. DIS automatically allocates a sequence number when the data producer calls the PutRecords operation to add data to the DIS stream. The sequence number of the same partition key usually changes with time. A longer interval between PutRecords requests results in a larger sequence number.

timestamp

Long

Timestamp when the record is written to DIS.

timestamp_type

String

Type of the timestamp.

The value is CreateTime, specifying the creation time.