Search in sources :

Example 1 with BufferedRecord

use of org.apache.gobblin.async.BufferedRecord in project incubator-gobblin by apache.

the class AbstractAsyncDataWriter method write.

/**
 * Asynchronously write the record with a callback
 */
@Override
public final Future<WriteResponse> write(D record, @Nullable WriteCallback callback) {
    FutureWrappedWriteCallback wrappedWriteCallback = new FutureWrappedWriteCallback(callback);
    BufferedRecord<D> bufferedRecord = new BufferedRecord<>(record, wrappedWriteCallback);
    put(bufferedRecord);
    return wrappedWriteCallback;
}
Also used : BufferedRecord(org.apache.gobblin.async.BufferedRecord) FutureWrappedWriteCallback(org.apache.gobblin.writer.FutureWrappedWriteCallback)

Example 2 with BufferedRecord

use of org.apache.gobblin.async.BufferedRecord in project incubator-gobblin by apache.

the class ApacheHttpRequestBuilderTest method testBuildWriteRequest.

/**
 * Build a {@link HttpUriRequest} from a {@link GenericRecord}
 */
public void testBuildWriteRequest() throws IOException {
    String urlTemplate = "http://www.test.com/a/part1:${part1}/a/part2:${part2}";
    String verb = "post";
    ApacheHttpRequestBuilder builder = spy(new ApacheHttpRequestBuilder(urlTemplate, verb, "application/json"));
    ArgumentCaptor<RequestBuilder> requestBuilderArgument = ArgumentCaptor.forClass(RequestBuilder.class);
    Queue<BufferedRecord<GenericRecord>> queue = HttpTestUtils.createQueue(1, false);
    AsyncRequest<GenericRecord, HttpUriRequest> request = builder.buildRequest(queue);
    verify(builder).build(requestBuilderArgument.capture());
    RequestBuilder expected = RequestBuilder.post();
    expected.setUri("http://www.test.com/a/part1:01/a/part2:02?param1=01");
    String payloadStr = "{\"id\":\"id0\"}";
    expected.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.getMimeType()).setEntity(new StringEntity(payloadStr, ContentType.APPLICATION_JSON));
    // Compare HttpUriRequest
    HttpTestUtils.assertEqual(requestBuilderArgument.getValue(), expected);
    Assert.assertEquals(request.getRecordCount(), 1);
    Assert.assertEquals(queue.size(), 0);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) StringEntity(org.apache.http.entity.StringEntity) RequestBuilder(org.apache.http.client.methods.RequestBuilder) BufferedRecord(org.apache.gobblin.async.BufferedRecord) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 3 with BufferedRecord

use of org.apache.gobblin.async.BufferedRecord in project incubator-gobblin by apache.

the class R2RestRequestBuilderTest method testBuildWriteRequest.

/**
 * Build a {@link RestRequest} from a {@link GenericRecord}
 */
public void testBuildWriteRequest() throws URISyntaxException, IOException {
    String urlTemplate = "http://www.test.com/a/part1:${part1}/a/part2:${part2}";
    String verb = "update";
    String protocolVersion = "2.0.0";
    R2RestRequestBuilder builder = spy(new R2RestRequestBuilder(urlTemplate, verb, protocolVersion));
    ArgumentCaptor<RestRequestBuilder> requestBuilderArgument = ArgumentCaptor.forClass(RestRequestBuilder.class);
    Queue<BufferedRecord<GenericRecord>> queue = HttpTestUtils.createQueue(1, false);
    AsyncRequest<GenericRecord, RestRequest> request = builder.buildRequest(queue);
    verify(builder).build(requestBuilderArgument.capture());
    RestRequestBuilder expected = new RestRequestBuilder(new URI("http://www.test.com/a/part1:01/a/part2:02?param1=01"));
    expected.setMethod("PUT");
    expected.setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion);
    expected.setHeader(RestConstants.HEADER_RESTLI_REQUEST_METHOD, verb.toLowerCase());
    expected.setHeader(RestConstants.HEADER_CONTENT_TYPE, RestConstants.HEADER_VALUE_APPLICATION_JSON);
    DataMap data = new DataMap();
    data.put("id", "id0");
    expected.setEntity(JACKSON_DATA_CODEC.mapToBytes(data));
    HttpTestUtils.assertEqual(requestBuilderArgument.getValue(), expected);
    Assert.assertEquals(request.getRecordCount(), 1);
    Assert.assertEquals(queue.size(), 0);
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) R2RestRequestBuilder(org.apache.gobblin.r2.R2RestRequestBuilder) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) R2RestRequestBuilder(org.apache.gobblin.r2.R2RestRequestBuilder) BufferedRecord(org.apache.gobblin.async.BufferedRecord) GenericRecord(org.apache.avro.generic.GenericRecord) URI(java.net.URI) DataMap(com.linkedin.data.DataMap)

Example 4 with BufferedRecord

use of org.apache.gobblin.async.BufferedRecord in project incubator-gobblin by apache.

the class HttpTestUtils method createQueue.

public static Queue<BufferedRecord<GenericRecord>> createQueue(int size, boolean isHttpOperation) {
    Queue<BufferedRecord<GenericRecord>> queue = new ArrayDeque<>(size);
    for (int i = 0; i < size; i++) {
        Map<String, String> keys = new HashMap<>();
        keys.put("part1", i + "1");
        keys.put("part2", i + "2");
        Map<String, String> queryParams = new HashMap<>();
        queryParams.put("param1", i + "1");
        GenericRecord record = isHttpOperation ? new HttpOperation() : new MockGenericRecord();
        record.put("keys", keys);
        record.put("queryParams", queryParams);
        record.put("body", "{\"id\":\"id" + i + "\"}");
        BufferedRecord<GenericRecord> item = new BufferedRecord<>(record, null);
        queue.add(item);
    }
    return queue;
}
Also used : HashMap(java.util.HashMap) HttpOperation(org.apache.gobblin.http.HttpOperation) BufferedRecord(org.apache.gobblin.async.BufferedRecord) ByteString(com.linkedin.data.ByteString) GenericRecord(org.apache.avro.generic.GenericRecord) ArrayDeque(java.util.ArrayDeque)

Example 5 with BufferedRecord

use of org.apache.gobblin.async.BufferedRecord in project incubator-gobblin by apache.

the class HttpJoinConverter method convertRecord.

@Override
public final Iterable<DO> convertRecord(SO outputSchema, DI inputRecord, WorkUnitState workUnitState) throws DataConversionException {
    // Convert DI to HttpOperation
    HttpOperation operation = generateHttpOperation(inputRecord, workUnitState);
    BufferedRecord<GenericRecord> bufferedRecord = new BufferedRecord<>(operation, WriteCallback.EMPTY);
    // Convert HttpOperation to RQ
    Queue<BufferedRecord<GenericRecord>> buffer = new LinkedBlockingDeque<>();
    buffer.add(bufferedRecord);
    AsyncRequest<GenericRecord, RQ> request = this.requestBuilder.buildRequest(buffer);
    RQ rawRequest = request.getRawRequest();
    try {
        RP response = httpClient.sendRequest(rawRequest);
        ResponseStatus status = responseHandler.handleResponse(request, response);
        switch(status.getType()) {
            case OK:
            case CLIENT_ERROR:
                // Convert (DI, RQ, RP etc..) to output DO
                log.debug("{} send with status type {}", rawRequest, status.getType());
                DO output = convertRecordImpl(outputSchema, inputRecord, rawRequest, status);
                return new SingleRecordIterable<>(output);
            case SERVER_ERROR:
                // Server side error. Retry
                throw new DataConversionException(rawRequest + " send failed due to server error");
            default:
                throw new DataConversionException(rawRequest + " Should not reach here");
        }
    } catch (IOException e) {
        throw new DataConversionException(e);
    }
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) BufferedRecord(org.apache.gobblin.async.BufferedRecord) IOException(java.io.IOException) ResponseStatus(org.apache.gobblin.http.ResponseStatus) HttpOperation(org.apache.gobblin.http.HttpOperation) GenericRecord(org.apache.avro.generic.GenericRecord)

Aggregations

BufferedRecord (org.apache.gobblin.async.BufferedRecord)6 GenericRecord (org.apache.avro.generic.GenericRecord)5 HttpOperation (org.apache.gobblin.http.HttpOperation)3 IOException (java.io.IOException)2 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)2 ByteString (com.linkedin.data.ByteString)1 DataMap (com.linkedin.data.DataMap)1 RestRequest (com.linkedin.r2.message.rest.RestRequest)1 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)1 URI (java.net.URI)1 ArrayDeque (java.util.ArrayDeque)1 HashMap (java.util.HashMap)1 ResponseStatus (org.apache.gobblin.http.ResponseStatus)1 R2RestRequestBuilder (org.apache.gobblin.r2.R2RestRequestBuilder)1 FutureWrappedWriteCallback (org.apache.gobblin.writer.FutureWrappedWriteCallback)1 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)1 RequestBuilder (org.apache.http.client.methods.RequestBuilder)1 StringEntity (org.apache.http.entity.StringEntity)1