代码:
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.dmp.hdfs;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.interceptor.Interceptor;
import static com.dmp.hdfs.TimeInterceptor.Constants.*;
/**
* Simple Interceptor class that sets the current system timestamp on all events
* that are intercepted. By convention, this timestamp header is named
* "timestamp" and its format is a "stringified" long timestamp in milliseconds
* since the UNIX epoch.
*/
public class TimeInterceptor implements Interceptor {
private final boolean preserveExisting;
/**
* Only {@link TimeInterceptor.Builder} can build me
*/
private TimeInterceptor(boolean preserveExisting) {
this.preserveExisting = preserveExisting;
}
@Override
public void initialize() {
// value: split + _ + no
readProperties("/dmp/flume-1.3.1/conf/buss.properties");
}
@SuppressWarnings("rawtypes")
public static void readProperties(String filePath) {
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream(new FileInputStream(
filePath));
props.load(in);
Enumeration en = props.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = props.getProperty(key);
bussMap.put(key, Property);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Modifies events in-place.
*/
@Override
public Event intercept(Event event) {
Map<String, String> headers = event.getHeaders();
byte[] body = event.getBody();
String name = headers.get(PLATBUSS);
String value = bussMap.get(name);
String[] array = value.split(SPLIT);
String[] split = new String(body).split(array[0]);
long time = Long.parseLong(split[Integer.parseInt(array[1])]);
headers.put(TIMESTAMP, Long.toString(time));
return event;
}
/**
* Delegates to {@link #intercept(Event)} in a loop.
*
* @param events
* @return
*/
@Override
public List<Event> intercept(List<Event> events) {
for (Event event : events) {
intercept(event);
}
return events;
}
@Override
public void close() {
// no-op
}
/**
* Builder which builds new instances of the TimestampInterceptor.
*/
public static class Builder implements Interceptor.Builder {
private boolean preserveExisting = PRESERVE_DFLT;
@Override
public Interceptor build() {
return new TimeInterceptor(preserveExisting);
}
@Override
public void configure(Context context) {
preserveExisting = context.getBoolean(PRESERVE, PRESERVE_DFLT);
}
}
public static class Constants {
public static String TIMESTAMP = "timestamp";
public static String PRESERVE = "preserveExisting";
public static boolean PRESERVE_DFLT = false;
public static final String PLATBUSS = "platName_bussName";
public static final String SPLIT = "_";
public static Map<String, String> bussMap;
}
}