本文整理匯總了Java中org.apache.flume.Event.getBody方法的典型用法代碼示例。如果您正苦於以下問題:Java Event.getBody方法的具體用法?Java Event.getBody怎麽用?Java Event.getBody使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.flume.Event
的用法示例。
在下文中一共展示了Event.getBody方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: PersistableEvent
import org.apache.flume.Event; //導入方法依賴的package包/類
public PersistableEvent(String channel, Event event) {
this.channel = channel;
byte[] givenPayload = event.getBody();
if (givenPayload.length < ConfigurationConstants.PAYLOAD_LENGTH_THRESHOLD) {
basePayload = Arrays.copyOf(givenPayload, givenPayload.length);
spillPayload = null;
} else {
basePayload = Arrays.copyOfRange(givenPayload, 0,
ConfigurationConstants.PAYLOAD_LENGTH_THRESHOLD);
spillPayload = Arrays.copyOfRange(givenPayload,
ConfigurationConstants.PAYLOAD_LENGTH_THRESHOLD, givenPayload.length);
}
Map<String, String> headerMap = event.getHeaders();
if (headerMap != null && headerMap.size() > 0) {
headers = new ArrayList<HeaderEntry>();
for (Map.Entry<String, String> entry : headerMap.entrySet()) {
String name = entry.getKey();
String value = entry.getValue();
headers.add(new HeaderEntry(name, value));
}
}
}
示例2: testLayout
import org.apache.flume.Event; //導入方法依賴的package包/類
@Test
public void testLayout() throws IOException {
configureSource();
props.put("log4j.appender.out2.layout", "org.apache.log4j.PatternLayout");
props.put("log4j.appender.out2.layout.ConversionPattern",
"%-5p [%t]: %m%n");
PropertyConfigurator.configure(props);
Logger logger = LogManager.getLogger(TestLog4jAppender.class);
Thread.currentThread().setName("Log4jAppenderTest");
for (int count = 0; count <= 100; count++) {
/*
* Log4j internally defines levels as multiples of 10000. So if we
* create levels directly using count, the level will be set as the
* default.
*/
int level = ((count % 5) + 1) * 10000;
String msg = "This is log message number" + String.valueOf(count);
logger.log(Level.toLevel(level), msg);
Transaction transaction = ch.getTransaction();
transaction.begin();
Event event = ch.take();
Assert.assertNotNull(event);
StringBuilder builder = new StringBuilder();
builder.append("[").append("Log4jAppenderTest").append("]: ")
.append(msg);
//INFO seems to insert an extra space, so lets split the string.
String eventBody = new String(event.getBody(), "UTF-8");
String eventLevel = eventBody.split("\\s+")[0];
Assert.assertEquals(Level.toLevel(level).toString(), eventLevel);
Assert.assertEquals(
new String(event.getBody(), "UTF8").trim()
.substring(eventLevel.length()).trim(), builder.toString());
Map<String, String> hdrs = event.getHeaders();
Assert.assertNotNull(hdrs.get(Log4jAvroHeaders.TIMESTAMP.toString()));
Assert.assertEquals(Level.toLevel(level),
Level.toLevel(Integer.parseInt(hdrs.get(Log4jAvroHeaders.LOG_LEVEL
.toString()))));
Assert.assertEquals(logger.getName(),
hdrs.get(Log4jAvroHeaders.LOGGER_NAME.toString()));
Assert.assertEquals("UTF8",
hdrs.get(Log4jAvroHeaders.MESSAGE_ENCODING.toString()));
transaction.commit();
transaction.close();
}
}
示例3: serializeEvent
import org.apache.flume.Event; //導入方法依賴的package包/類
private byte[] serializeEvent(Event event, boolean useAvroEventFormat) throws IOException {
byte[] bytes;
if (useAvroEventFormat) {
if (!tempOutStream.isPresent()) {
tempOutStream = Optional.of(new ByteArrayOutputStream());
}
if (!writer.isPresent()) {
writer = Optional.of(new SpecificDatumWriter<AvroFlumeEvent>(AvroFlumeEvent.class));
}
tempOutStream.get().reset();
AvroFlumeEvent e = new AvroFlumeEvent(toCharSeqMap(event.getHeaders()),
ByteBuffer.wrap(event.getBody()));
encoder = EncoderFactory.get().directBinaryEncoder(tempOutStream.get(), encoder);
writer.get().write(e, encoder);
encoder.flush();
bytes = tempOutStream.get().toByteArray();
} else {
bytes = event.getBody();
}
return bytes;
}
示例4: serializeValue
import org.apache.flume.Event; //導入方法依賴的package包/類
private byte[] serializeValue(Event event, boolean parseAsFlumeEvent) throws IOException {
byte[] bytes;
if (parseAsFlumeEvent) {
if (!tempOutStream.isPresent()) {
tempOutStream = Optional.of(new ByteArrayOutputStream());
}
if (!writer.isPresent()) {
writer = Optional.of(new
SpecificDatumWriter<AvroFlumeEvent>(AvroFlumeEvent.class));
}
tempOutStream.get().reset();
AvroFlumeEvent e = new AvroFlumeEvent(
toCharSeqMap(event.getHeaders()),
ByteBuffer.wrap(event.getBody()));
encoder = EncoderFactory.get()
.directBinaryEncoder(tempOutStream.get(), encoder);
writer.get().write(e, encoder);
encoder.flush();
bytes = tempOutStream.get().toByteArray();
} else {
bytes = event.getBody();
}
return bytes;
}
示例5: write
import org.apache.flume.Event; //導入方法依賴的package包/類
/**
* Write data, update stats
* @param event
* @throws WriteException - other streaming io error
* @throws InterruptedException
*/
public synchronized void write(final Event event)
throws WriteException, InterruptedException {
if (closed) {
throw new IllegalStateException("Writer closed. Cannot write to : " + endPoint);
}
batch.add(event);
if (batch.size() == writeBatchSz) {
// write the event
writeEventBatchToSerializer();
}
// Update Statistics
processSize += event.getBody().length;
eventCounter++;
}
示例6: assertEquals
import org.apache.flume.Event; //導入方法依賴的package包/類
private static void assertEquals(Event e1, Event e2) {
byte[] pl1 = e1.getBody();
byte[] pl2 = e2.getBody();
Assert.assertArrayEquals(pl1, pl2);
Map<String, String> h1 = e1.getHeaders();
Map<String, String> h2 = e2.getHeaders();
if (h1 == null || h1.size() == 0) {
Assert.assertTrue(h2 == null || h2.size() == 0);
} else {
Assert.assertTrue(h1.size() == h2.size());
for (String key : h1.keySet()) {
Assert.assertTrue(h2.containsKey(key));
String v1 = h1.get(key);
String v2 = h2.remove(key);
Assert.assertEquals(v1, v2);
}
Assert.assertTrue(h2.size() == 0);
}
}
示例7: intercept
import org.apache.flume.Event; //導入方法依賴的package包/類
@Override
public Event intercept(Event event) {
String origBody = new String(event.getBody(), charset);
Matcher matcher = searchPattern.matcher(origBody);
String newBody = matcher.replaceAll(replaceString);
event.setBody(newBody.getBytes(charset));
return event;
}
示例8: testSearchReplace
import org.apache.flume.Event; //導入方法依賴的package包/類
private void testSearchReplace(Context context, String input, String output)
throws Exception {
Interceptor.Builder builder = InterceptorBuilderFactory.newInstance(
InterceptorType.SEARCH_REPLACE.toString());
builder.configure(context);
Interceptor interceptor = builder.build();
Event event = EventBuilder.withBody(input, Charsets.UTF_8);
event = interceptor.intercept(event);
String val = new String(event.getBody(), Charsets.UTF_8);
assertEquals(output, val);
logger.info(val);
}
示例9: testReconfigure
import org.apache.flume.Event; //導入方法依賴的package包/類
@Test
public void testReconfigure() throws InterruptedException, IOException {
final int NUM_RECONFIGS = 20;
for (int i = 0; i < NUM_RECONFIGS; i++) {
Context context = new Context();
File file = new File(tmpDir.getAbsolutePath() + "/file-" + i);
Files.write("File " + i, file, Charsets.UTF_8);
context.put(SpoolDirectorySourceConfigurationConstants.SPOOL_DIRECTORY,
tmpDir.getAbsolutePath());
Configurables.configure(source, context);
source.start();
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
Transaction txn = channel.getTransaction();
txn.begin();
try {
Event event = channel.take();
String content = new String(event.getBody(), Charsets.UTF_8);
Assert.assertEquals("File " + i, content);
txn.commit();
} catch (Throwable t) {
txn.rollback();
} finally {
txn.close();
}
source.stop();
Assert.assertFalse("Fatal error on iteration " + i, source.hasFatalError());
}
}
示例10: testBatchProcessWithLifeCycle
import org.apache.flume.Event; //導入方法依賴的package包/類
@Test
public void testBatchProcessWithLifeCycle() throws InterruptedException, LifecycleException,
EventDeliveryException {
int batchSize = 10;
Channel channel = new PseudoTxnMemoryChannel();
Context context = new Context();
context.put("logicalNode.name", "test");
context.put("batchSize", Integer.toString(batchSize));
Configurables.configure(source, context);
Configurables.configure(channel, context);
List<Channel> channels = new ArrayList<Channel>();
channels.add(channel);
ChannelSelector rcs = new ReplicatingChannelSelector();
rcs.setChannels(channels);
source.setChannelProcessor(new ChannelProcessor(rcs));
source.start();
for (long i = 0; i < 100; i++) {
source.process();
for (long j = batchSize; j > 0; j--) {
Event event = channel.take();
String expectedVal = String.valueOf(((i + 1) * batchSize) - j);
String resultedVal = new String(event.getBody());
Assert.assertTrue("Expected " + expectedVal + " is not equals to " +
resultedVal, expectedVal.equals(resultedVal));
}
}
source.stop();
}
示例11: estimateEventSize
import org.apache.flume.Event; //導入方法依賴的package包/類
private long estimateEventSize(Event event) {
byte[] body = event.getBody();
if (body != null && body.length != 0) {
return body.length;
}
//Each event occupies at least 1 slot, so return 1.
return 1;
}
示例12: binding
import org.apache.flume.Event; //導入方法依賴的package包/類
public Object binding(final Event event) {
if (header == null) {
final byte body[] = event.getBody();
return dataType.convert(new String(body, Charsets.UTF_8));
} else {
final Map<String, String> headers = event.getHeaders();
for (final Map.Entry<String, String> entry : headers.entrySet()) {
if (entry.getKey().equals(header)) {
return dataType.convert(entry.getValue());
}
}
}
log.trace("No bindable field found for {}", this);
return null;
}
示例13: initialize
import org.apache.flume.Event; //導入方法依賴的package包/類
@Override
public void initialize(Event event, byte[] cf) {
this.payload = event.getBody();
this.cf = cf;
}
示例14: assertEventBodyEquals
import org.apache.flume.Event; //導入方法依賴的package包/類
private void assertEventBodyEquals(String expected, Event event) {
String bodyStr = new String(event.getBody(), Charsets.UTF_8);
Assert.assertEquals(expected, bodyStr);
}
示例15: bodyAsString
import org.apache.flume.Event; //導入方法依賴的package包/類
static String bodyAsString(Event event) {
return new String(event.getBody());
}