Search in sources :

Example 6 with StreamId

use of co.cask.cdap.proto.id.StreamId in project cdap by caskdata.

the class DefaultStreamWriter method createBatchWriter.

@Override
public StreamBatchWriter createBatchWriter(final String stream, String contentType) throws IOException {
    URL url = Retries.callWithRetries(new Retries.Callable<URL, IOException>() {

        @Override
        public URL call() throws IOException {
            return remoteClient.resolve(stream + "/batch");
        }
    }, retryStrategy);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod(HttpMethod.POST.name());
    connection.setReadTimeout(15000);
    connection.setConnectTimeout(15000);
    connection.setRequestProperty(HttpHeaders.CONTENT_TYPE, contentType);
    if (authorizationEnabled) {
        connection.setRequestProperty(Constants.Security.Headers.USER_ID, authenticationContext.getPrincipal().getName());
    }
    connection.setDoOutput(true);
    connection.setChunkedStreamingMode(0);
    connection.connect();
    try {
        StreamId streamId = namespace.stream(stream);
        registerStream(streamId);
        return new DefaultStreamBatchWriter(connection, streamId);
    } catch (IOException e) {
        connection.disconnect();
        throw e;
    }
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) HttpURLConnection(java.net.HttpURLConnection) Retries(co.cask.cdap.common.service.Retries) IOException(java.io.IOException) URL(java.net.URL)

Example 7 with StreamId

use of co.cask.cdap.proto.id.StreamId in project cdap by caskdata.

the class StreamClientTestRun method testDescription.

@Test
public void testDescription() throws Exception {
    String description = "Good Stream";
    Long ttl = 10L;
    Integer notifMB = 300;
    StreamId streamId = namespaceId.stream("testDesc");
    StreamProperties properties = new StreamProperties(ttl, null, notifMB, description);
    streamClient.create(streamId, properties);
    StreamProperties actual = streamClient.getConfig(streamId);
    Assert.assertEquals(description, actual.getDescription());
    Assert.assertEquals(ttl, actual.getTTL());
    Assert.assertEquals(notifMB, actual.getNotificationThresholdMB());
    description = "New Description";
    streamClient.setDescription(streamId, description);
    actual = streamClient.getConfig(streamId);
    Assert.assertEquals(description, actual.getDescription());
    Assert.assertEquals(ttl, actual.getTTL());
    Assert.assertEquals(notifMB, actual.getNotificationThresholdMB());
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) StreamProperties(co.cask.cdap.proto.StreamProperties) Test(org.junit.Test)

Example 8 with StreamId

use of co.cask.cdap.proto.id.StreamId in project cdap by caskdata.

the class StreamClientTestRun method testDelete.

@Test
public void testDelete() throws Exception {
    StreamId streamId = namespaceId.stream("testDelete");
    streamClient.create(streamId);
    // Send an event and get it back
    String msg = "Test Delete";
    streamClient.sendEvent(streamId, msg);
    List<StreamEvent> events = Lists.newArrayList();
    streamClient.getEvents(streamId, 0, Long.MAX_VALUE, Integer.MAX_VALUE, events);
    Assert.assertEquals(1, events.size());
    Assert.assertEquals(msg, Charsets.UTF_8.decode(events.get(0).getBody()).toString());
    // Delete the stream
    streamClient.delete(streamId);
    // Try to get info, it should throw a StreamNotFoundException
    try {
        streamClient.getConfig(streamId);
        Assert.fail();
    } catch (StreamNotFoundException e) {
    // Expected
    }
    // Try to get events, it should throw a StreamNotFoundException
    try {
        streamClient.getEvents(streamId, 0, Long.MAX_VALUE, Integer.MAX_VALUE, events);
        Assert.fail();
    } catch (StreamNotFoundException e) {
    // Expected
    }
    // Create the stream again, it should returns empty events
    streamClient.create(streamId);
    events.clear();
    streamClient.getEvents(streamId, 0, Long.MAX_VALUE, Integer.MAX_VALUE, events);
    Assert.assertTrue(events.isEmpty());
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) StreamEvent(co.cask.cdap.api.flow.flowlet.StreamEvent) StreamNotFoundException(co.cask.cdap.common.StreamNotFoundException) Test(org.junit.Test)

Example 9 with StreamId

use of co.cask.cdap.proto.id.StreamId in project cdap by caskdata.

the class StreamClientTestRun method testAll.

@Test
public void testAll() throws Exception {
    StreamId streamId = namespaceId.stream("testAll");
    LOG.info("Getting stream list");
    int baseStreamCount = streamClient.list(namespaceId).size();
    Assert.assertEquals(baseStreamCount, streamClient.list(namespaceId).size());
    LOG.info("Creating stream");
    streamClient.create(streamId);
    LOG.info("Checking stream list");
    Assert.assertEquals(baseStreamCount + 1, streamClient.list(namespaceId).size());
    StreamProperties config = streamClient.getConfig(streamId);
    Assert.assertNotNull(config);
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) StreamProperties(co.cask.cdap.proto.StreamProperties) Test(org.junit.Test)

Example 10 with StreamId

use of co.cask.cdap.proto.id.StreamId in project cdap by caskdata.

the class StreamClientTestRun method testStreamEvents.

/**
   * Tests for the get events call
   */
@Test
public void testStreamEvents() throws IOException, BadRequestException, StreamNotFoundException, UnauthenticatedException, UnauthorizedException {
    StreamId streamId = namespaceId.stream("testEvents");
    streamClient.create(streamId);
    // Send 5000 events
    int eventCount = 5000;
    for (int i = 0; i < eventCount; i++) {
        streamClient.sendEvent(streamId, "Testing " + i);
    }
    // Read all events
    List<StreamEvent> events = streamClient.getEvents(streamId, 0, Long.MAX_VALUE, Integer.MAX_VALUE, Lists.<StreamEvent>newArrayList());
    Assert.assertEquals(eventCount, events.size());
    for (int i = 0; i < eventCount; i++) {
        Assert.assertEquals("Testing " + i, Bytes.toString(events.get(i).getBody()));
    }
    // Read first 5 only
    events.clear();
    streamClient.getEvents(streamId, 0, Long.MAX_VALUE, 5, events);
    Assert.assertEquals(5, events.size());
    // Use the 2nd and the 3rd events time as start and end time respectively
    long startTime = events.get(1).getTimestamp();
    long endTime = events.get(2).getTimestamp() + 1;
    events.clear();
    streamClient.getEvents(streamId, startTime, endTime, Integer.MAX_VALUE, events);
    // At least read the 2nd and the 3rd event. It might read more than 2 events
    // if events are written within the same timestamp.
    Assert.assertTrue(events.size() >= 2);
    int i = 1;
    for (StreamEvent event : events) {
        Assert.assertEquals("Testing " + i, Charsets.UTF_8.decode(events.get(i - 1).getBody()).toString());
        Assert.assertTrue(event.getTimestamp() >= startTime);
        Assert.assertTrue(event.getTimestamp() < endTime);
        i++;
    }
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) StreamEvent(co.cask.cdap.api.flow.flowlet.StreamEvent) Test(org.junit.Test)

Aggregations

StreamId (co.cask.cdap.proto.id.StreamId)166 Test (org.junit.Test)88 DatasetId (co.cask.cdap.proto.id.DatasetId)33 ProgramId (co.cask.cdap.proto.id.ProgramId)30 NamespaceId (co.cask.cdap.proto.id.NamespaceId)27 Path (javax.ws.rs.Path)27 StreamEvent (co.cask.cdap.api.flow.flowlet.StreamEvent)24 ApplicationId (co.cask.cdap.proto.id.ApplicationId)22 IOException (java.io.IOException)20 StreamProperties (co.cask.cdap.proto.StreamProperties)17 FormatSpecification (co.cask.cdap.api.data.format.FormatSpecification)16 StreamViewId (co.cask.cdap.proto.id.StreamViewId)16 Location (org.apache.twill.filesystem.Location)15 StreamConfig (co.cask.cdap.data2.transaction.stream.StreamConfig)12 NamespaceMeta (co.cask.cdap.proto.NamespaceMeta)12 StreamAdmin (co.cask.cdap.data2.transaction.stream.StreamAdmin)11 ViewSpecification (co.cask.cdap.proto.ViewSpecification)10 MetadataSearchResultRecord (co.cask.cdap.proto.metadata.MetadataSearchResultRecord)10 Action (co.cask.cdap.proto.security.Action)10 GET (javax.ws.rs.GET)10