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;
}
}
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());
}
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());
}
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);
}
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++;
}
}
Aggregations