D:\develop\MongoDB\bin\mongod --dbpath=D:\develop\MongoDB\mydata
agent.sources = so1 agent.channels = c1 agent.sinks = s1 agent.sources.so1.type = avro agent.sources.so1.bind = 0.0.0.0 agent.sources.so1.port = 44444 agent.sources.so1.channels = c1 agent.channels.c1.type = memory agent.channels.c1.capacity = 1000 agent.channels.c1.transactionCapacity = 100 agent.sinks.s1.type = org.riderzen.flume.sink.MongoSink agent.sinks.s1.host = localhost agent.sinks.s1.port = 27017 agent.sinks.s1.model = single agent.sinks.s1.db = test agent.sinks.s1.collection = log agent.sinks.s1.batch = 100 agent.sinks.s1.channel = c1
简单说明:
src/main/java
|---- cn.sinobest.flume.client.demo
|---- LogDemo.java
src/main/resources
|---- log4j.properties
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.sinobest.asj</groupId> <artifactId>fluent-client-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.apache.flume.flume-ng-clients</groupId> <artifactId>flume-ng-log4jappender</artifactId> <version>1.6.0</version> </dependency> </dependencies> </project>
# 配置Log4jAppender,能写日志到Flume log4j.appender.flumeAvro=org.apache.flume.clients.log4jappender.Log4jAppender log4j.appender.flumeAvro.Hostname=localhost log4j.appender.flumeAvro.Port=44444 log4j.appender.flumeAvro.UnsafeMode=true log4j.appender.flumeAvro.layout=org.apache.log4j.PatternLayout log4j.appender.flumeAvro.layout.ConversionPattern=%m # 配置根logger 此配置的日志级别和appender会被所有的logger继承 log4j.rootLogger=INFO, flumeAvro
package cn.sinobest.flume.client.demo; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class LogDemo { static final Log log = LogFactory.getLog(LogDemo.class); public static void main(String[] args) { log.info("{'name':'Adam', 'age':'26', 'skill':'reading'}"); } }
D:\develop\MongoDB\bin\mongod --dbpath=D:\develop\MongoDB\mydata
# cd F:\temp\apache-flume-1.6.0-bin\bin flume-ng.cmd agent --conf ..\conf -f ..\conf\mongo-agent.properties -n agent
D:\tools\PowerCmd>D:\develop\MongoDB\bin\mongo 2016-03-08T18:05:33.168+0800 I CONTROL [main] Hotfix KB2731284 or later update is not installed, will zero-out data files MongoDB shell version: 3.2.3 connecting to: test > db.log.find() { "_id" : ObjectId("56dea2b18449e36553652bc3"), "name" : "Adam", "age" : "26", "skill" : "reading" } >