public Request<GetRecordsRequest> marshall(GetRecordsRequest getRecordsRequest) {
    if (getRecordsRequest == null) {
      throw new AmazonClientException("Invalid argument passed to marshall(GetRecordsRequest)");
    }

    Request<GetRecordsRequest> request =
        new DefaultRequest<GetRecordsRequest>(getRecordsRequest, "AmazonKinesis");
    String target = "Kinesis_20131202.GetRecords";
    request.addHeader("X-Amz-Target", target);
    request.setHttpMethod(HttpMethodName.POST);

    String uriResourcePath = "/";
    request.setResourcePath(uriResourcePath);
    try {
      StringWriter stringWriter = new StringWriter();
      AwsJsonWriter jsonWriter = JsonUtils.getJsonWriter(stringWriter);
      jsonWriter.beginObject();

      if (getRecordsRequest.getShardIterator() != null) {
        String shardIterator = getRecordsRequest.getShardIterator();
        jsonWriter.name("ShardIterator");
        jsonWriter.value(shardIterator);
      }
      if (getRecordsRequest.getLimit() != null) {
        Integer limit = getRecordsRequest.getLimit();
        jsonWriter.name("Limit");
        jsonWriter.value(limit);
      }

      jsonWriter.endObject();
      jsonWriter.close();
      String snippet = stringWriter.toString();
      byte[] content = snippet.getBytes(UTF8);
      request.setContent(new StringInputStream(snippet));
      request.addHeader("Content-Length", Integer.toString(content.length));
    } catch (Throwable t) {
      throw new AmazonClientException("Unable to marshall request to JSON: " + t.getMessage(), t);
    }
    if (!request.getHeaders().containsKey("Content-Type")) {
      request.addHeader("Content-Type", "application/x-amz-json-1.1");
    }

    return request;
  }
Пример #2
0
  /**
   * Get the records from the particular shard
   *
   * @param streamName Name of the stream from where the records to be accessed
   * @param recordsLimit Number of records to return from shard
   * @param shId Shard Id of the shard
   * @param iteratorType Shard iterator type
   * @param seqNo Record sequence number
   * @return the list of records from the given shard
   * @throws AmazonClientException
   */
  public List<Record> getRecords(
      String streamName,
      Integer recordsLimit,
      String shId,
      ShardIteratorType iteratorType,
      String seqNo)
      throws AmazonClientException {
    assert client != null : "Illegal client";
    try {
      // Create the GetShardIteratorRequest instance and sets streamName, shardId and iteratorType
      // to it
      GetShardIteratorRequest iteratorRequest = new GetShardIteratorRequest();
      iteratorRequest.setStreamName(streamName);
      iteratorRequest.setShardId(shId);
      iteratorRequest.setShardIteratorType(iteratorType);

      // If the iteratorType is AFTER_SEQUENCE_NUMBER, set the sequence No to the iteratorRequest
      if (ShardIteratorType.AFTER_SEQUENCE_NUMBER.equals(iteratorType)
          || ShardIteratorType.AT_SEQUENCE_NUMBER.equals(iteratorType))
        iteratorRequest.setStartingSequenceNumber(seqNo);

      // Get the Response from the getShardIterator service method & get the shardIterator from that
      // response
      GetShardIteratorResult iteratorResponse = client.getShardIterator(iteratorRequest);
      // getShardIterator() specifies the position in the shard
      String iterator = iteratorResponse.getShardIterator();

      // Create the GetRecordsRequest instance and set the recordsLimit and iterator
      GetRecordsRequest getRequest = new GetRecordsRequest();
      getRequest.setLimit(recordsLimit);
      getRequest.setShardIterator(iterator);

      // Get the Response from the getRecords service method and get the data records from that
      // response.
      GetRecordsResult getResponse = client.getRecords(getRequest);
      return getResponse.getRecords();
    } catch (AmazonClientException e) {
      throw new RuntimeException(e);
    }
  }