目的

实时监听某目录下的日志文件,如有新文件切换到新文件,并同步写入kafka,同时记录日志文件的行位置,以应对进程异常退出,能从上次的文件位置开始读取(考虑到效率,这里是每100条记一次,可调整)
 

源码:

  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileReader;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9. import java.io.LineNumberReader;
  10. import java.io.PrintWriter;
  11. import java.io.RandomAccessFile;
  12. import java.net.NoRouteToHostException;
  13. import java.util.ArrayList;
  14. import java.util.Collection;
  15. import java.util.List;
  16. import java.util.Properties;
  17. import java.util.Random;
  18. import java.util.concurrent.Executors;
  19. import java.util.concurrent.ScheduledExecutorService;
  20. import java.util.concurrent.TimeUnit;
  21. import kafka.javaapi.producer.Producer;
  22. import kafka.producer.KeyedMessage;
  23. import kafka.producer.ProducerConfig;
  24. /*
  25. * 自己在源服务器写生产者往kafka插入数据,注意文件"producer.properties放在linux下该jar文件同一目录
  26. * 监听某个目录下的文件数据然后写入kafka
  27. * nohup java -jar portallog_producer.jar portallog /var/apache/logs portallog.position  >/home/sre/portalhandler/handler.log 2>&1 &
  28. *
  29. *
  30. */
  31. public class PortalLogTail_Line {
  32. private Producer<String,String> inner;
  33. java.util.Random ran = new Random();
  34. public PortalLogTail_Line() throws FileNotFoundException, IOException {
  35. Properties properties = new Properties();
  36. //   properties.load(ClassLoader.getSystemResourceAsStream("producer.properties"));
  37. properties.load(new FileInputStream("producer.properties"));
  38. ProducerConfig config = new ProducerConfig(properties);
  39. inner = new Producer<String, String>(config);
  40. }
  41. public void send(String topicName,String message) {
  42. if(topicName == null || message == null){
  43. return;
  44. }
  45. //   KeyedMessage<String, String> km = new KeyedMessage<String, String>(topicName,message);
  46. //随机作为key,hash分散到各个分区
  47. KeyedMessage<String, String> km = new KeyedMessage<String, String>(topicName,String.valueOf(ran.nextInt(9)),message);
  48. //   KeyedMessage<String, String> km = new KeyedMessage<String, String>(topicName,message,message);
  49. inner.send(km);
  50. }
  51. public void send(String topicName,Collection<String> messages) {
  52. if(topicName == null || messages == null){
  53. return;
  54. }
  55. if(messages.isEmpty()){
  56. return;
  57. }
  58. List<KeyedMessage<String, String>> kms = new ArrayList<KeyedMessage<String, String>>();
  59. for(String entry : messages){
  60. KeyedMessage<String, String> km = new KeyedMessage<String, String>(topicName,entry);
  61. kms.add(km);
  62. }
  63. inner.send(kms);
  64. }
  65. public void close(){
  66. inner.close();
  67. }
  68. public String getNewFile(File file)
  69. {
  70. File[] fs=file.listFiles();
  71. long maxtime=0;
  72. String newfilename="";
  73. for (int i=0;i<fs.length;i++)
  74. {
  75. if (fs[i].lastModified()>maxtime && fs[i].getName().contains("access"))
  76. {
  77. maxtime=fs[i].lastModified();
  78. newfilename=fs[i].getAbsolutePath();
  79. }
  80. }
  81. return newfilename;
  82. }
  83. //写入文件名及行号
  84. public void writePosition(String path,int rn,String positionpath)
  85. {
  86. try {
  87. BufferedWriter out = new BufferedWriter(new FileWriter(positionpath));
  88. out.write(path+","+rn);
  89. out.close();
  90. } catch (IOException e) {
  91. }
  92. }
  93. LineNumberReader randomFile=null;
  94. String newfile=null;
  95. String thisfile=null;
  96. String prefile=null;
  97. int ln=0;
  98. int beginln=0;
  99. public void realtimeShowLog(final File file,final String topicname, final String positionpath) throws IOException{
  100. //启动一个线程每1秒钟读取新增的日志信息
  101. new Thread(new Runnable(){
  102. public void run() {
  103. thisfile=getNewFile(file);
  104. prefile=thisfile;
  105. //访问position文件,如果记录了文件路径,及行号,则定位,否则使用最新的文件
  106. try {
  107. BufferedReader br=new BufferedReader(new FileReader(positionpath));
  108. String line=br.readLine();
  109. if (line!=null &&line.contains(","))
  110. {
  111. thisfile=line.split(",")[0];
  112. prefile=thisfile;
  113. beginln=Integer.parseInt(line.split(",")[1]);
  114. }
  115. } catch (FileNotFoundException e2) {
  116. // TODO Auto-generated catch block
  117. e2.printStackTrace();
  118. }
  119. catch (IOException e2) {
  120. // TODO Auto-generated catch block
  121. e2.printStackTrace();
  122. }
  123. //指定文件可读可写
  124. try {
  125. randomFile = new LineNumberReader(new FileReader(thisfile));
  126. } catch (FileNotFoundException e) {
  127. // TODO Auto-generated catch block
  128. e.printStackTrace();
  129. }
  130. while (true)
  131. {
  132. try {
  133. Thread.sleep(100);
  134. } catch (InterruptedException e1) {
  135. // TODO Auto-generated catch block
  136. e1.printStackTrace();
  137. }
  138. try {
  139. //获得变化部分的
  140. //  randomFile.seek(lastTimeFileSize);
  141. String tmp = "";
  142. while( (tmp = randomFile.readLine())!= null) {
  143. int currln=randomFile.getLineNumber();
  144. //beginln默认为0
  145. if (currln>beginln)
  146. send(topicname,new String(tmp.getBytes("utf8")));
  147. ln++;
  148. //每发生一条写一次影响效率,连续发100次后再记录位置
  149. if (ln>100)
  150. {
  151. writePosition(thisfile,currln,positionpath);
  152. ln=0;
  153. }
  154. }
  155. thisfile=getNewFile(file);
  156. if(!thisfile.equals(prefile))
  157. {
  158. randomFile.close();
  159. randomFile = new LineNumberReader(new FileReader(thisfile));
  160. prefile=thisfile;
  161. beginln=0;
  162. }
  163. } catch (IOException e) {
  164. throw new RuntimeException(e);
  165. }
  166. }
  167. }}).start();
  168. }
  169. /**
  170. * @param args
  171. * @throws Exception
  172. */
  173. public static void main(String[] args) throws Exception {
  174. PortalLogTail_Line producer = new PortalLogTail_Line();
  175. if (args.length!=3)
  176. {
  177. System.out.println("usage:topicname pathname positionpath");
  178. System.exit(1);
  179. }
  180. String topicname=args[0];
  181. String pathname=args[1];
  182. String positionpath=args[2];
  183. final File tmpLogFile = new File(pathname);
  184. producer.realtimeShowLog(tmpLogFile,topicname,positionpath);
  185. }
  186. }
 
producer.properties文件放在同级目录下
  1. metadata.broker.list=xxx:10909,xxx:10909
  2. # name of the partitioner class for partitioning events; default partition spreads data randomly
  3. #partitioner.class=
  4. # specifies whether the messages are sent asynchronously (async) or synchronously (sync)
  5. producer.type=sync
  6. #producer.type=async
  7. # specify the compression codec for all data generated: none , gzip, snappy.
  8. # the old config values work as well: 0, 1, 2 for none, gzip, snappy, respectivally
  9. compression.codec=none
  10. #compression.codec=gzip
  11. # message encoder
  12. serializer.class=kafka.serializer.StringEncoder
 

测试

最后执行:

  1. nohup java -jar portallog_producer.jar portallog /var/apache/logs portallog.position  >/home/sre/portalhandler/handler.log 2>&1 &
  2. 转:http://blog.csdn.net/u011750989/article/details/21237251

java实时监听日志写入kafka的更多相关文章

  1. java实时监听日志写入kafka(转)

    原文链接:http://www.sjsjw.com/kf_cloud/article/020376ABA013802.asp 目的 实时监听某目录下的日志文件,如有新文件切换到新文件,并同步写入kaf ...

  2. java实时监听日志写入kafka(多目录)

    目的 实时监听多个目录下的日志文件,如有新文件切换到新文件,并同步写入kafka,同时记录日志文件的行位置,以应对进程异常退出,能从上次的文件位置开始读取(考虑到效率,这里是每100条记一次,可调整) ...

  3. 20180530利用Maxwell组件实时监听Mysql的binlog日志

    转自:https://blog.csdn.net/qq_30921461/article/details/78320750 http://kafka.apache.org/quickstart htt ...

  4. js 实时监听input中值变化

    注意:用到了jquery需要引入jquery.min.js. 需求: 1.每个地方需要分别打分,总分为100; 2.第一个打分总分为40; 3.第二个打分总分为60. 注意:需要判断null.&quo ...

  5. ORACLE清理、截断监听日志文件(listener.log)

    在ORACLE数据库中,如果不对监听日志文件(listener.log)进行截断,那么监听日志文件(listener.log)会变得越来越大,想必不少人听说过关于"LISTENER.LOG日 ...

  6. Java线程监听,意外退出线程后自动重启

    Java线程监听,意外退出线程后自动重启 某日,天朗气清,回公司,未到9点,刷微博,顿觉问题泛滥,惊恐万分! 前一天写了一个微博爬行程序,主要工作原理就是每隔2分钟爬行一次微博,获取某N个关注朋友微博 ...

  7. Android实时监听网络状态

    Android实时监听网络状态(1)   其实手机在网络方面的的监听也比较重要,有时候我们必须实时监控这个程序的实时网络状态,android在网络断开与连接的时候都会发出广播,我们通过接收系统的广播就 ...

  8. Oracle数据库运维:要对监听日志文件(listener.log)进行定期清理,如果不定期清理,会遇到下面一些麻烦

    原文链接: http://www.lookdaima.com/WebForms/WebPages/Blanks/Pm/Docs/DocItemDetail.aspx?EmPreviewTypeV=2& ...

  9. 移动端用js与jquery实时监听输入框值的改动

    背景: 在一次移动端H5开发中,需要监听输入框值的实时变动. onchange事件肯定抛弃,因为只能失去焦点才触发. 而keyPress在Android可以触发,iOS不可以. 又不想用Android ...

随机推荐

  1. Android 使用xml实现边框阴影,背景渐变效果(附有RGB颜色查询对照表)

    上图是显示效果,下面是代码实现: 个人理解就是使用layer-list实现两层view的叠加,其中top,left,bottom,left控制阴影 <?xml version="1.0 ...

  2. android错误整理

    1.Caused by: java.lang.IllegalStateException: Only fullscreen opaque activities can request orientat ...

  3. C#变量、常量

    变量 一.命名变量:标识符 为变量命名时要遵循C#语言的命名规范: 1.变量名只能由字母.数字和下划线组成,而不能包含空格.标点符号.运算符等其他符号 2.变量名不能与C#中的关键字名称相同   二. ...

  4. 如何在windows下安装配置pyspark notebook

    第一步:安装anaconda anaconda自带一系列科学计算包 下载链接:http://pan.baidu.com/s/1b4jWlg 密码:fqq3 接着配置环境变量:如我安装在D盘下   试一 ...

  5. 笨办法学Python(七)

    习题 7: 更多打印 现在我们将做一批练习,在练习的过程中你需要键入代码,并且让它们运行起来.我不会解释太多,因为这节的内容都是以前熟悉过的.这节练习的目的是巩固你学到的东西.我们几个练习后再见.不要 ...

  6. IOS照相机的启动,图片的读取,存储demo

    #import @interface ViewController : UIViewController@property (retain, nonatomic) IBOutlet UIImageVi ...

  7. 幻灯片的JQuqey的制作效果,只要几行代码

    使用jquery.KinSlideshow.js就可以很轻松的实现幻灯片效果   htm代码: [html]   <div id="focusNews" style=&quo ...

  8. iis 发布失败原因总结

    3篇文章 1. https://www.cnblogs.com/adzhouyang/p/7357086.html 2..https://blog.csdn.net/li_ser/article/de ...

  9. @NotEmpty@NotNull和@NotBlank的区别

    这几个可以为对象,不只是字符串 1.@NotNull 不能为null,但可以为empty (""," "," ") 2.@NotEmpty ...

  10. Mybatis中的DataSource配置

    dataSource 的类型可以配置成其内置类型之一,如 UNPOOLED,POOLED,JNDI. 1.如果将类型设置成 UNPOOLED,MyBatis 会为每一个数据库操作创建一个新的连接,并关 ...