Search in sources :

Example 1 with Bytes

use of io.cdap.cdap.api.common.Bytes in project cdap by caskdata.

the class MessagingHttpServiceTest method testLargePublish.

@Test
public void testLargePublish() throws IOException, TopicAlreadyExistsException, TopicNotFoundException, UnauthorizedException {
    // A 5MB message, which is larger than the 1MB buffer.
    String message = Strings.repeat("01234", 1024 * 1024);
    TopicId topicId = new NamespaceId("ns1").topic("testLargePublish");
    client.createTopic(new TopicMetadata(topicId));
    StoreRequest request = StoreRequestBuilder.of(topicId).addPayload(message).build();
    client.publish(request);
    // Read it back
    List<RawMessage> messages = new ArrayList<>();
    try (CloseableIterator<RawMessage> iterator = client.prepareFetch(topicId).setLimit(10).fetch()) {
        Iterators.addAll(messages, iterator);
    }
    Assert.assertEquals(1, messages.size());
    Assert.assertEquals(Collections.singletonList(message), messages.stream().map(RawMessage::getPayload).map(Bytes::toString).collect(Collectors.toList()));
}
Also used : Bytes(io.cdap.cdap.api.common.Bytes) StoreRequest(io.cdap.cdap.messaging.StoreRequest) ArrayList(java.util.ArrayList) TopicId(io.cdap.cdap.proto.id.TopicId) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) RawMessage(io.cdap.cdap.messaging.data.RawMessage) TopicMetadata(io.cdap.cdap.messaging.TopicMetadata) Test(org.junit.Test)

Example 2 with Bytes

use of io.cdap.cdap.api.common.Bytes in project cdap by caskdata.

the class MessagingHttpServiceTest method testReuseRequest.

@Test
public void testReuseRequest() throws IOException, TopicAlreadyExistsException, TopicNotFoundException, UnauthorizedException {
    // This test a StoreRequest object can be reused.
    // This test is to verify storing transaction messages to the payload table
    TopicId topicId = new NamespaceId("ns1").topic("testReuseRequest");
    client.createTopic(new TopicMetadata(topicId));
    StoreRequest request = StoreRequestBuilder.of(topicId).addPayload("m1").addPayload("m2").build();
    // Publish the request twice
    client.publish(request);
    client.publish(request);
    // Expects four messages
    List<RawMessage> messages = new ArrayList<>();
    try (CloseableIterator<RawMessage> iterator = client.prepareFetch(topicId).setLimit(10).fetch()) {
        Iterators.addAll(messages, iterator);
    }
    Assert.assertEquals(4, messages.size());
    List<String> expected = Arrays.asList("m1", "m2", "m1", "m2");
    Assert.assertEquals(expected, messages.stream().map(RawMessage::getPayload).map(Bytes::toString).collect(Collectors.toList()));
}
Also used : Bytes(io.cdap.cdap.api.common.Bytes) StoreRequest(io.cdap.cdap.messaging.StoreRequest) ArrayList(java.util.ArrayList) TopicId(io.cdap.cdap.proto.id.TopicId) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) RawMessage(io.cdap.cdap.messaging.data.RawMessage) TopicMetadata(io.cdap.cdap.messaging.TopicMetadata) Test(org.junit.Test)

Example 3 with Bytes

use of io.cdap.cdap.api.common.Bytes in project cdap by caskdata.

the class DraftStore method doSort.

/**
 * Helper method to apply the sorting onto a list of rows. Sorting needs to take place at the store-level so it can
 * leverage the StructuredRow to enable sorting on any field.
 *
 * @param rows list of {@link StructuredRow} to be sorted
 * @param sortRequest {@link SortRequest} describing the sort to be performed
 * @return a sorted list of {@link StructuredRow}
 */
private List<StructuredRow> doSort(List<StructuredRow> rows, @Nullable SortRequest sortRequest) {
    if (sortRequest == null) {
        return rows;
    }
    String sortField = sortRequest.getFieldName();
    FieldType field = TABLE_SPEC.getFieldTypes().stream().filter(f -> f.getName().equals(sortField)).findFirst().orElse(null);
    if (field == null) {
        throw new IllegalArgumentException(String.format("Invalid value '%s' for sortBy. This field does not exist in the Drafts table.", sortField));
    }
    FieldType.Type fieldType = field.getType();
    Comparator<StructuredRow> comparator;
    switch(fieldType) {
        case STRING:
            comparator = Comparator.<StructuredRow, String>comparing(o -> o.getString(sortField));
            break;
        case INTEGER:
            comparator = Comparator.<StructuredRow, Integer>comparing(o -> o.getInteger(sortField));
            break;
        case LONG:
            comparator = Comparator.<StructuredRow, Long>comparing(o -> o.getLong(sortField));
            break;
        case FLOAT:
            comparator = Comparator.<StructuredRow, Float>comparing(o -> o.getFloat(sortField));
            break;
        case DOUBLE:
            comparator = Comparator.<StructuredRow, Double>comparing(o -> o.getDouble(sortField));
            break;
        case BYTES:
            comparator = Comparator.comparing(o -> o.getBytes(sortField), Bytes.BYTES_COMPARATOR);
            break;
        default:
            throw new UnsupportedOperationException(String.format("Cannot sort field '%s' because type '%s' is not supported.", sortField, fieldType.toString()));
    }
    if (sortRequest.getOrder() != SortRequest.SortOrder.ASC) {
        comparator = comparator.reversed();
    }
    rows.sort(comparator);
    return rows;
}
Also used : TransactionRunners(io.cdap.cdap.spi.data.transaction.TransactionRunners) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) Bytes(io.cdap.cdap.api.common.Bytes) Fields(io.cdap.cdap.spi.data.table.field.Fields) ArrayList(java.util.ArrayList) StructuredTableId(io.cdap.cdap.spi.data.table.StructuredTableId) Gson(com.google.gson.Gson) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) ETLConfig(io.cdap.cdap.etl.proto.v2.ETLConfig) Field(io.cdap.cdap.spi.data.table.field.Field) StructuredTableSpecification(io.cdap.cdap.spi.data.table.StructuredTableSpecification) Nullable(javax.annotation.Nullable) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException) StudioUtil(io.cdap.cdap.datapipeline.service.StudioUtil) ETLBatchConfig(io.cdap.cdap.etl.proto.v2.ETLBatchConfig) NamespaceSummary(io.cdap.cdap.api.NamespaceSummary) FieldType(io.cdap.cdap.spi.data.table.field.FieldType) CloseableIterator(io.cdap.cdap.api.dataset.lib.CloseableIterator) Collectors(java.util.stream.Collectors) DataStreamsConfig(io.cdap.cdap.etl.proto.v2.DataStreamsConfig) TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException) List(java.util.List) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) Range(io.cdap.cdap.spi.data.table.field.Range) TransactionRunner(io.cdap.cdap.spi.data.transaction.TransactionRunner) Optional(java.util.Optional) Comparator(java.util.Comparator) Collections(java.util.Collections) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) FieldType(io.cdap.cdap.spi.data.table.field.FieldType)

Aggregations

Bytes (io.cdap.cdap.api.common.Bytes)3 ArrayList (java.util.ArrayList)3 StoreRequest (io.cdap.cdap.messaging.StoreRequest)2 TopicMetadata (io.cdap.cdap.messaging.TopicMetadata)2 RawMessage (io.cdap.cdap.messaging.data.RawMessage)2 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)2 TopicId (io.cdap.cdap.proto.id.TopicId)2 Test (org.junit.Test)2 Gson (com.google.gson.Gson)1 NamespaceSummary (io.cdap.cdap.api.NamespaceSummary)1 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)1 CloseableIterator (io.cdap.cdap.api.dataset.lib.CloseableIterator)1 StudioUtil (io.cdap.cdap.datapipeline.service.StudioUtil)1 DataStreamsConfig (io.cdap.cdap.etl.proto.v2.DataStreamsConfig)1 ETLBatchConfig (io.cdap.cdap.etl.proto.v2.ETLBatchConfig)1 ETLConfig (io.cdap.cdap.etl.proto.v2.ETLConfig)1 InvalidFieldException (io.cdap.cdap.spi.data.InvalidFieldException)1 StructuredRow (io.cdap.cdap.spi.data.StructuredRow)1 StructuredTable (io.cdap.cdap.spi.data.StructuredTable)1 TableNotFoundException (io.cdap.cdap.spi.data.TableNotFoundException)1