use of co.cask.cdap.data.file.FileWriter in project cdap by caskdata.
the class MultiLiveStreamFileReaderTestBase method testMultiFileReader.
@Test
public void testMultiFileReader() throws Exception {
String streamName = "multiReader";
StreamId streamId = NamespaceId.DEFAULT.stream(streamName);
Location location = getLocationFactory().create(streamName);
location.mkdirs();
// Create a stream with 1 partition.
StreamConfig config = new StreamConfig(streamId, Long.MAX_VALUE, 10000, Long.MAX_VALUE, location, null, 1000);
// Write out 200 events in 5 files, with interleaving timestamps
List<FileWriter<StreamEvent>> writers = Lists.newArrayList();
for (int i = 0; i < 5; i++) {
FileWriter<StreamEvent> writer = createWriter(config, "bucket" + i);
writers.add(writer);
for (int j = 0; j < 200; j++) {
long timestamp = j * 5 + i;
writer.append(StreamFileTestUtils.createEvent(timestamp, "Testing " + timestamp));
}
}
// Flush all writers.
for (FileWriter<StreamEvent> writer : writers) {
writer.flush();
}
// Create a multi stream file reader
List<StreamFileOffset> sources = Lists.newArrayList();
Location partitionLocation = StreamUtils.createPartitionLocation(config.getLocation(), 0, Long.MAX_VALUE);
for (int i = 0; i < 5; i++) {
Location eventFile = StreamUtils.createStreamLocation(partitionLocation, "bucket" + i, 0, StreamFileType.EVENT);
sources.add(new StreamFileOffset(eventFile, 0L, 0));
}
// Reads all events written so far.
MultiLiveStreamFileReader reader = new MultiLiveStreamFileReader(config, sources);
List<StreamEvent> events = Lists.newArrayList();
long expectedTimestamp = 0L;
for (int i = 0; i < 10; i++) {
Assert.assertEquals(100, reader.read(events, 100, 0, TimeUnit.SECONDS));
Assert.assertEquals(100, events.size());
for (StreamEvent event : events) {
Assert.assertEquals(expectedTimestamp, event.getTimestamp());
Assert.assertEquals("Testing " + expectedTimestamp, Charsets.UTF_8.decode(event.getBody()).toString());
expectedTimestamp++;
}
events.clear();
}
Assert.assertEquals(0, reader.read(events, 1, 1, TimeUnit.SECONDS));
// Writes some more events to the first three writers.
for (int i = 0; i < 3; i++) {
FileWriter<StreamEvent> writer = writers.get(i);
for (int j = 0; j < 10; j++) {
long timestamp = 1000 + j * 3 + i;
writer.append(StreamFileTestUtils.createEvent(timestamp, "Testing " + timestamp));
}
}
// Close all writers
for (FileWriter<StreamEvent> writer : writers) {
writer.close();
}
// Continue to read
Assert.assertEquals(30, reader.read(events, 30, 2, TimeUnit.SECONDS));
Assert.assertEquals(30, events.size());
for (StreamEvent event : events) {
Assert.assertEquals(expectedTimestamp, event.getTimestamp());
Assert.assertEquals("Testing " + expectedTimestamp, Charsets.UTF_8.decode(event.getBody()).toString());
expectedTimestamp++;
}
// Should get no more events.
Assert.assertEquals(0, reader.read(events, 1, 1, TimeUnit.SECONDS));
reader.close();
}
use of co.cask.cdap.data.file.FileWriter in project cdap by caskdata.
the class InMemoryStreamFileWriterFactory method create.
@Override
public FileWriter<StreamEvent> create(StreamConfig config, int generation) throws IOException {
final QueueProducer producer = queueClientFactory.createProducer(QueueName.fromStream(config.getStreamId()));
final List<TransactionAware> txAwares = Lists.newArrayList();
if (producer instanceof TransactionAware) {
txAwares.add((TransactionAware) producer);
}
final TransactionExecutor txExecutor = executorFactory.createExecutor(txAwares);
// Adapt the FileWriter interface into Queue2Producer
return new FileWriter<StreamEvent>() {
private final List<StreamEvent> events = Lists.newArrayList();
@Override
public void append(StreamEvent event) throws IOException {
events.add(event);
}
@Override
public void appendAll(Iterator<? extends StreamEvent> events) throws IOException {
Iterators.addAll(this.events, events);
}
@Override
public void close() throws IOException {
producer.close();
}
@Override
public void flush() throws IOException {
try {
txExecutor.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
for (StreamEvent event : events) {
producer.enqueue(new QueueEntry(STREAM_EVENT_CODEC.encodePayload(event)));
}
events.clear();
}
});
} catch (TransactionFailureException e) {
throw new IOException(e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new InterruptedIOException();
}
}
};
}
Aggregations