java--数据采集系统

Gather采集类:

package com.briup.woss.client.impl;

import java.io.*;
import java.sql.Timestamp;
import java.util.*;
import com.briup.pojos.BIDR;
import com.briup.woss.client.Gather;

public class GatherImpl implements Gather {
private static Map map=new HashMap();
/*
 * 读取up.dat文件,up.dat文件存放的是用户没下线的信息,
 * 规定格式为:loginIp,userinfo;
*/
private static FileReader fr;
private static BufferedReader br;
static
{
try
{
fr=new FileReader("data/up.dat");
br=new BufferedReader(fr);
String str;
while((str=br.readLine())!=null)
{
String subStr[]=str.split(";");
for(int i=0;i<subStr.length;i++)
{
String subS[]=subStr[i].split(",");
map.put(subS[0], subS[1]);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
br.close();
fr.close();
}
catch(Exception e)
{
e.printStackTrace();
}

}
}
//采集数据
public Collection<BIDR> gather() throws Exception {
Collection<BIDR> c=new ArrayList<BIDR>();
FileReader fr=new FileReader("data/radwtmp");
BufferedReader br=new BufferedReader(fr);
int point=readPointFile();
int p=point;
String str=null;
int temp=1;
/*
 * 读取point文件来确定从哪一条记录开始读
 */
while((str=br.readLine())!=null)
{
if(temp<=p)
{
temp++;
continue;
}
point++;
String str1 []=str.split("[|]+");
if(str1[2].equals("7"))
{
map.put(str1[4], str);
}
else if(str1[2].equals("8"))
{
String sub=(String)map.get(str1[4]);
if(sub!=null)
{
String info[]=sub.split("[|]+");
String login_name=info[0].replace("#", "");//去除用户名前面的"#"

BIDR bidr=new BIDR();
//得到登陆用户名
bidr.setAAA_login_name(login_name);
//得到登陆IP
bidr.setLogin_ip(info[4]);
//得到NAS的IP
bidr.setNAS_ip(info[1]);
/*
* 创建Timestamp类型的ts1(上线),ts2(下线)
*/
Timestamp ts1=new Timestamp(Long.parseLong(info[3]+"000"));
Timestamp ts2=new Timestamp(Long.parseLong(str1[3]+"000"));

//得到上线时间
bidr.setLogin_date(ts1);
//得到下线时间
bidr.setLogout_date(ts2);

//得到上网时间, 以分钟计算
int secondTime=Integer.parseInt(str1[3])-Integer.parseInt(info[3]);
int minuTime=secondTime/60+1;
if(secondTime%60==0)
minuTime=secondTime/60;
bidr.setTime_deration(minuTime);
c.add(bidr);
map.remove(str1[4]);
}
}
if(point%100==0)
break;
}
br.close();
fr.close();
updatePointFile(point);//更新书签
updateUpFile();
return c;
}

//读取point书签
public int readPointFile()throws Exception
{

FileReader fis=new FileReader("data/point.dat");
BufferedReader br=new BufferedReader(fis);
String str=br.readLine();
if(str==null)
return 0;
int point=Integer.parseInt(str);
return point;
}

//更新point书签
public void updatePointFile(int point)throws Exception
{
FileWriter fw=new FileWriter("data/point.dat");
BufferedWriter bw=new BufferedWriter(fw);
bw.write(point+"");
bw.flush();
bw.close();
fw.close();
}

//更新up.dat文件
public void updateUpFile()throws Exception
{
FileWriter fw=new FileWriter("data/up.dat");
BufferedWriter bw=new BufferedWriter(fw);
Set enteries=map.entrySet();
Iterator iter=enteries.iterator();
StringBuffer buffer=new StringBuffer();
while(iter.hasNext())
{
Map.Entry entry=(Map.Entry)iter.next();
String ip=(String)entry.getKey();
String userInfo=(String)entry.getValue();
buffer.append(ip+","+userInfo+";"+"\n");
}
bw.write(new String(buffer));
bw.flush();
bw.close();
fw.close();
}

public void setProperties(Properties pop) {


}
}

ClientImpl类:

package com.briup.woss.client.impl;


import java.io.*;
import java.net.Socket;
import java.util.*;


import com.briup.pojos.BIDR;
import com.briup.util.impl.ConfigurationImpl;
import com.briup.woss.client.Client;
import com.briup.woss.client.Gather;


public class ClientImpl implements Client {


public void send(Collection<BIDR> c) throws Exception {

Socket client=new Socket("127.0.0.1",9001);
ObjectOutputStream object=new ObjectOutputStream(client.getOutputStream());
object.writeObject(c);
}


public void setProperties(Properties pop) {

}
}

client端的Main类:

package com.briup.woss.client;


import java.util.Collection;


import com.briup.pojos.BIDR;
import com.briup.util.BackUP;
import com.briup.util.Configuration;
import com.briup.util.Logger;
import static com.briup.util.impl.ConfigurationImpl.*;


public class Main {

public static Configuration conf =getConfiguration();


public static void main(String[] args) {


Logger logger = null;
Gather gather = null;
BackUP backup = null;
Client client = null;
Collection<BIDR> con = null;
try {
logger = conf.getClientLogger();
logger.info("客户端开始启动");
gather = conf.getGather();
logger.info("准备采集");
long a=System.currentTimeMillis();
con = gather.gather();
System.out.println(System.currentTimeMillis()-a);
logger.info("采集成功");
backup = conf.getBackup();
try {
client = conf.getClient();
logger.info("准备发送  ");
client.send(con);
logger.info("发送成功!");


} catch (Exception e) {
logger.error(e.getMessage());
logger.info("准备备份");
//backup.load();
backup.store(con);
logger.info("备份成功");
}
} catch (Exception e1) {
e1.printStackTrace();
logger.error(e1.getMessage());
logger.warn("采集失败");
}

}
}

ServerImpl类

package com.briup.woss.server.impl;


import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;
import java.io.*;
import com.briup.pojos.BIDR;
import com.briup.woss.server.DBStore;
import com.briup.woss.server.Server;
import com.sun.org.apache.xml.internal.serialize.Printer;


public class ServerImpl implements Server {


public void revicer(DBStore dbstore) throws Exception {
Collection<BIDR>c=new ArrayList<BIDR>();
ServerSocket server=new ServerSocket(9001);
Socket socket=server.accept();
ObjectInputStream object=new ObjectInputStream(socket.getInputStream());
c=(Collection)object.readObject();
for(BIDR bidr:c)
System.out.println(bidr);
dbstore.saveToDB(c);
object.close();
socket.close();
server.close();
}
public void setProperties(Properties pop) {
// TODO Auto-generated method stub

}
}


DBStoreImpl 类

package com.briup.woss.server.impl;


import static com.common.ConnectionFactory.*;
import java.sql.DriverManager;
import java.util.Collection;
import java.util.Properties;


import com.briup.pojos.BIDR;
import com.briup.woss.server.DBStore;
import java.sql.*;
public class DBStoreImpl implements DBStore {
private static Connection con;
static
{
con=getConnection();
}
public void saveToDB(Collection<BIDR> c) throws Exception{





PreparedStatement psmt=null;
CallableStatement stmt;
//设置为自动提交事物
con.setAutoCommit(false);
//插入到t_detail_x中
for(BIDR bidr:c)
{
String sql="insert into t_detail_"+bidr.getLogin_date().getDate()+" values(?,?,?,?,?,?)";
psmt=con.prepareStatement(sql);
psmt.setString(1, bidr.getAAA_login_name());
psmt.setString(2, bidr.getLogin_ip());
psmt.setTimestamp(3, bidr.getLogin_date());
psmt.setTimestamp(4, bidr.getLogout_date());
psmt.setString(5, bidr.getNAS_ip());
psmt.setInt(6, bidr.getTime_deration());
psmt.execute();
}
con.commit();
//调用daypro存储过程执行对t_day_x插入数据
stmt=con.prepareCall("{call daypro}");
stmt.execute();
//调用monthpro存储过程执行对t_month_x插入数据
stmt=con.prepareCall("{call monthpro}");
stmt.execute();
stmt.close();

//调用yearpro存储过程执行对t_year_x插入数据
/*CallableStatement stmt3=con.prepareCall("{call yearpro}");
stmt3.execute();
stmt3.close();*/

psmt.close();
con.close();
}

public void setProperties(Properties pop) {
// TODO Auto-generated method stub
}
}

server端的Main类:

package com.briup.woss.server;


import com.briup.util.Configuration;
import com.briup.util.Logger;
import static com.briup.util.impl.ConfigurationImpl.*;


public class Main {


/**
 * 服务器端的程序入口
 */
public static Configuration conf =getConfiguration();

public static void main(String[] args) {
DBStore dbstore = null;
Server server = null;
Logger logger=null;
try {
dbstore = conf.getDBStore();
server = conf.getServer();
logger=conf.getServerLogger();
logger.info("服务端等待接收数据");
server.revicer(dbstore);
logger.info("服务端接收数据成功");
} catch (Exception e) {
logger.error(e.getMessage());
logger.warn("服务端接收数据失败");
}
}
}

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页