数据采集1

////接口自定义,只要提供一个方法便可

package org.gather.collection.Impl;

import java.io.*;
import java.util.*;
import org.gather.collection.GatherIf;
import java.sql.Timestamp;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.gather.collection.bean.BetailBean;

public class GatherImpl implements GatherIf
{
 // 以登陆帐号名为Key,以BetailBean为value方便以key找到相对应的帐号做上、下线合并数据
 private Map map = new HashMap();
 // 用Vector集合保存最后数据合并后的对象,并返回给入库模块
 //private Vector vector = new Vector();
 Vector vector = new Vector();
 // 利用随机读写文件类,方便读取文件的字符定位,使用getFilePointer()方法可得到当前位置。
 // 调用readLine()方法可得到当前位置的下一行的字符
 private RandomAccessFile raf;
 // 用来保存一行字符串然后调用split(":")方法做分析得到的字符串
 private String line;
 // 保存一个属性集
 private Properties pro;
 // 以后用XML获取
 private String tempfile = "d:" + File.separator + "tempfile.txt";
 // 以后用XML获取,存放指针
 private String pointfile = "d:" + File.separator + "pointfile.txt";
 // 以后用XML获取,出现异常时做备份的文件
 private String backupfile = "d:" + File.separator + "backupfile.txt";
 //private GatherImpl gatherimpl = new GatherImpl();

 // private ConfigIf config;
 public GatherImpl()
 {}

 public GatherImpl(Properties pro)
 {
  this.pro = pro;
 }

 public Collection getData()
 {
  String Login_name = new String();// 登陆帐号
  String Login_ip = new String(); // 登陆IP地址
  Timestamp Login_date; // 上线时间
  Timestamp Logout_date; // 下线时间
  String Lab_ip = new String(); // 3A服务器IP地址
  long Time_duration; // 上网时间
  // 先从文件中,把已经序列化了hashmap读出来,放在内存里
  File tempfile1 = new File(tempfile);
  if (tempfile1.exists() && tempfile1.length() != 0)
  {
   FileInputStream fis = null;
   try
   {
    fis = new FileInputStream(tempfile);
   }
   catch (FileNotFoundException ex4)
   {}
   BufferedInputStream bis = new BufferedInputStream(fis);
   ObjectInputStream ois = null;
   try
   {
    ois = new ObjectInputStream(bis);
   }
   catch (IOException ex5)
   {}
   try
   {
    map = (HashMap) ois.readObject();
    tempfile1.delete();
    ois.close();
   }
   catch (ClassNotFoundException ex6)
   {}
   catch (IOException ex6)
   {}
  }
  // 获得数据记录文件的指针,再移动,以从正确的位置开始读取。
  // 最开始的时候,指针的位置在0
  long point = getPoint(pointfile);
  File file = new File(pointfile);
  RandomAccessFile raf = null;
  try
  {
   raf = new RandomAccessFile(file, "r");
  }
  catch (FileNotFoundException ex7)
  {}
  try
  {
   raf.seek(point);
  }
  catch (IOException ex8)
  {}
  // 把敏感的用户名,先放在一个ArrayList 里,在下面取出来的时候,与之匹配
  ArrayList list = new ArrayList();
  String[] dropStr = { "root", "LOGIN", "system boot", "run-level",
    "rc2", "rc3", "sac", "ttymon", "zsmon", "shutdown" };
  for (int i = 0; i < dropStr.length; i++)
  {
   list.add(dropStr[i]);
  }
  try
  {
   raf = new RandomAccessFile("d:" + File.separator + "wtmpx.txt", "r");
   try
   {
    while (((line = raf.readLine()) != null))
    {
     String[] str = line.split(":");
     System.out.println("分解数据成功");
     System.out.println(str.length);
     if (!list.contains(str[0]))
     {
      System.out.println("标志位1");
      if (str.length > 1)
      {
       System.out.println("过滤数据成功");
        Login_name=str[0];
        Login_ip=str[8];
       Lab_ip = getlab_ip();
       if (str[4].equals("7"))
       {        
        Login_date = new Timestamp(Long
          .parseLong(str[5]));
        BetailBean betailbean = new BetailBean(str[0],
          str[8], Login_date, null, Lab_ip, 0);
        map.put(Login_name, betailbean);
        System.out.println("获得一个标志位是7的数据到map****"+map);
       }
       if (str[4].equals("8"))
       {
        BetailBean betailbean = (BetailBean) map
          .get(Login_name);
        Time_duration = (Long.parseLong(str[5]) - betailbean
          .getLogin_date().getTime()) / 60;
        Logout_date = new Timestamp(Long
          .parseLong(str[5]));
        BetailBean finalbetailbean = new BetailBean(
          str[0], str[8], betailbean
            .getLogin_date(), Logout_date,
          Lab_ip, Time_duration);
        vector.add(finalbetailbean);
        System.out.println("存放一组整合过的数据到vector*****"+vector);
        map.remove(str[0]);
       }
      }
     }
    }
   }
   catch (IOException ex1)
   {}
  }
  catch (FileNotFoundException ex)
  {}
  long cpoint = 0L;
  try
  {
   cpoint = raf.getFilePointer(); // 获取移动之后的指针的位置
  }
  catch (IOException ex9)
  {}
  savePoint(pointfile, cpoint); // 将现在的指针的位置写入文件保存起来
  if (!map.isEmpty())
  {
   FileOutputStream fos = null;
   try
   {
    fos = new FileOutputStream(tempfile);
   }
   catch (FileNotFoundException ex2)
   {}
   ObjectOutputStream oos = null;
   try
   {
    oos = new ObjectOutputStream(fos);
    oos.writeObject(map);
    map.clear();
    oos.close();
   }
   catch (IOException ex3)
   {}
  }
  return vector;
 }

 // 保存point
 private void savePoint(String fileUri, long point)
 {
  String p = new Long(point).toString();
  File file = new File(fileUri);
  try
  {
   file.createNewFile();
  }
  catch (IOException ex)
  {}
  BufferedWriter bw = null;
  try
  {
   bw = new BufferedWriter(new OutputStreamWriter(
     new FileOutputStream(file)));
  }
  catch (FileNotFoundException ex1)
  {}
  try
  {
   bw.write(p);
   bw.flush();
  }
  catch (IOException ex2)
  {}
  finally
  {
   closeStream(raf);
  }
 }

 // 获得point
 private long getPoint(String fileUri)
 {
  RandomAccessFile raf = null;
  try
  {
   raf = new RandomAccessFile(fileUri, "r");
  }
  catch (FileNotFoundException ex1)
  {}
  long p = 0L;
  try
  {
   p = Long.parseLong(raf.readLine());
  }
  catch (IOException ex)
  {}
  catch (NumberFormatException ex)
  {}
  finally
  {
   closeStream(raf);
   return p;
  }
 }

 // 获得本地3A服务器的地址
 private String getlab_ip()
 {
  try
  {
   return InetAddress.getLocalHost().getHostAddress();
  }
  catch (UnknownHostException ex)
  {
   return "UnknownHost";
  }
 }

 // 该方法用于关闭文件流
 private void closeStream(RandomAccessFile rafile)
 {
  if (rafile != null)
  {
   try
   {
    rafile.close();
   }
   catch (IOException ex)
   {
    System.out.println("不能关闭该流");
   }
  }
 }
}
 /////////

<?xml version="1.0" encoding="UTF-8"?>
<netctoss>
 <gather single="false">
  <className>org.bazu.gathersystem.gather.GatherImpl</className>
  <pointfile>d://point.txt</pointfile>
  <temp7file>d://temp7.txt</temp7file>
  <wtmpxfile>d://wtmpx.txt</wtmpxfile>  
 </gather>
 
 <db single="false">
  <className>org.bazu.gathersystem.dbstore.DBStoreImpl</className>
  <methodName>getInstance</methodName>
  <batchSize>10</batchSize>
  <driver>oracle.jdbc.driver.OracleDriver</driver>
  <url>jdbc:oracle:thin:@192.168.2.100:1521:tarena</url>
  <username>tarena</username>
  <password>tarena</password>
 </db> 
</netctoss>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值