8、Flumeハイレベルカスタムコンポーネント_FlumeカスタムSink
2754 ワード
1.1.カスタムSinkの説明
必要なのは、ネットワークポートからデータを送信し、sinkをカスタマイズし、sinkを使用してネットワークポートからデータを受信し、ローカルファイルにデータを保存することです.
1.2.カスタムSink原理実現
カスタムMySink
1.3機能テスト
コードをパッケージプラグインを使用してjarパッケージに打ち込み、commons-langsという依存パッケージを打ち込み、flumeのlibディレクトリの下に置いてflumeのプロファイルを開発することに注意してください.
1.4 flumeを起動し、telnetテストを使用します.
Telnet node-1 5678はマシンポートに接続してデータを入力します.
sink flume 。 , sink , sink , kafka, mysql,
必要なのは、ネットワークポートからデータを送信し、sinkをカスタマイズし、sinkを使用してネットワークポートからデータを受信し、ローカルファイルにデータを保存することです.
1.2.カスタムSink原理実現
カスタムMySink
public class MySink extends AbstractSink implements Configurable {
private Context context ;
private String filePath = "";
private String fileName = "";
private File fileDir;
// , Context,
@Override
public void configure(Context context) {
try {
this.context = context;
filePath = context.getString("filePath");
fileName = context.getString("fileName");
fileDir = new File(filePath);
if(!fileDir.exists()){
fileDir.mkdirs();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//
@Override
public Status process() throws EventDeliveryException {
Event event = null;
Channel channel = this.getChannel();
Transaction transaction = channel.getTransaction();
transaction.begin();
while(true){
event = channel.take();
if(null != event){
break;
}
}
byte[] body = event.getBody();
String line = new String(body);
try {
FileUtils.write(new File(filePath+File.separator+fileName),line,true);
transaction.commit();
} catch (IOException e) {
transaction.rollback();
e.printStackTrace();
return Status.BACKOFF;
}finally {
transaction.close();
}
return Status.READY;
}
}
1.3機能テスト
コードをパッケージプラグインを使用してjarパッケージに打ち込み、commons-langsという依存パッケージを打ち込み、flumeのlibディレクトリの下に置いてflumeのプロファイルを開発することに注意してください.
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
a1.sources.r1.type = netcat
a1.sources.r1.bind = node-1
a1.sources.r1.port = 5678
a1.sources.r1.channels = c1
# # Describe the sink
a1.sinks.k1.type = cn.itcast.flumesink.MySink
a1.sinks.k1.filePath=/export/servers
a1.sinks.k1.fileName=filesink.txt
# # Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
# # Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
1.4 flumeを起動し、telnetテストを使用します.
bin/flume-ng agent -c conf -f conf/filesink.conf -n a1 -Dflume.root.logger=INFO,console
Telnet node-1 5678はマシンポートに接続してデータを入力します.