java对比入库小程序
本文最后更新于 2016 天前,其中的信息可能已经有所发展或是发生改变。

需求:对比规则表和累计表,符合条件的进行写入文件。

程序结构

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>com.htjf</groupId>
  <artifactId>iot_ana</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <name>iot_ana</name>
  <url>http://maven.apache.org</url>
  
  <repositories>
   	<repository>
  		<id>alimaven</id>
  		<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
  	</repository>
  	<repository>
  		<id>hortonworks</id>
  		<url>http://repo.hortonworks.com/content/groups/public/</url>
  	</repository>
  </repositories>
  <dependencies>
	<dependency>
	    <groupId>commons-io</groupId>
	    <artifactId>commons-io</artifactId>
	    <version>2.4</version>
	</dependency>
  	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>3.8.1</version>
		<!-- 
		<scope>test</scope>
		 -->
	</dependency>
  	<dependency>
		<groupId>log4j</groupId>
		<artifactId>log4j</artifactId>
		<version>1.2.17</version>
	</dependency>
	<dependency>
		<groupId>dom4j</groupId>
		<artifactId>dom4j</artifactId>
		<version>1.6.1</version>
	</dependency>
	<dependency>  
        <groupId>com.oracle</groupId>  
        <artifactId>ojdbc14</artifactId>  
        <version>10.2.0.1.0</version>  
     </dependency>
  </dependencies>
  <build>
  	<plugins>
  		<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>  
			<version>3.2</version>
			<configuration>  
            	<source>1.7</source>  
            	<target>1.7</target>  
            	<encoding>UTF-8</encoding>
            </configuration> 
  		</plugin>
  	</plugins>
  </build>
  
</project>

ana包下的两个java文件(主类包)

AnalysisBean

package com.htjf.ana;

public class AnalysisBean {
	
	private String iotId;//iot设备ID
	private String ruleId;//规则唯一id
	private String iotRule;//iot规则
	private String matchType;//匹配规则0:完全匹配,1:模糊匹配
	
	private String imei;
	private String iotType;//可能是host/ua/apn...
	private String dt;//相差时间(LT-FT)
	
	public AnalysisBean(String iotId, String ruleId, String iotRule,
			String matchType) {
		super();
		this.iotId = iotId;
		this.ruleId = ruleId;
		this.iotRule = iotRule;
		this.matchType = matchType;
	}
	
	public void setData(String imei, String iotType, String dt) {
		this.imei = imei;
		this.iotType = iotType;
		this.dt = dt;
	}
	
	public AnalysisBean() {
		super();
	}
	
	public String getIotId() {
		return iotId;
	}
	public void setIotId(String iotId) {
		this.iotId = iotId;
	}
	public String getRuleId() {
		return ruleId;
	}
	public void setRuleId(String ruleId) {
		this.ruleId = ruleId;
	}
	public String getIotRule() {
		return iotRule;
	}
	public void setIotRule(String iotRule) {
		this.iotRule = iotRule;
	}
	public String getMatchType() {
		return matchType;
	}
	public void setMatchType(String matchType) {
		this.matchType = matchType;
	}
	public String getImei() {
		return imei;
	}
	public void setImei(String imei) {
		this.imei = imei;
	}
	public String getIotType() {
		return iotType;
	}
	public void setIotType(String iotType) {
		this.iotType = iotType;
	}
	public String getDt() {
		return dt;
	}
	public void setDt(String dt) {
		this.dt = dt;
	}

	@Override
	public String toString() {
		return iotId + "," + ruleId + "," +matchType+ "," + imei  + "," + dt+ "";
	}
	
}

IotAllAnalysis(主程序)

package com.htjf.ana;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import com.htjf.util.ConfigUtil;
import com.htjf.util.DBHiberUtil;
import com.htjf.util.DateUtil;
import com.htjf.util.FileUtil;

public class IotAllAnalysis {
	private static Logger logger = Logger.getLogger(IotAllAnalysis.class);
	
	private Map<String ,AnalysisBean>actuMap=new HashMap<String,AnalysisBean>();
	private Map<String ,AnalysisBean>likeMap=new HashMap<String,AnalysisBean>();
	private long maxFileSize = ConfigUtil.getMaxFileSise();//限制文件大小
	private int maxCountRow = ConfigUtil.getMaxCountRow();//限制行数大小
	private String initFileEndName = ConfigUtil.getInitFileEndName();//文件的后缀名(.temp)
	private String fileEndName = ConfigUtil.getFileEndName();//文件的后缀名(.csv)
	public IotAllAnalysis(){
		super();
	}
	public void doAction(){
		String[] dataList = ConfigUtil.getDataType().split(",");
		for(int i =0;i<dataList.length;i++){
			String dataType = dataList[i].toString();
			loadRule(dataType);
			readAllData(dataType);
		}
	}
	/**
	 * 匹配规则
	 * 精准优先(当精确匹配到不再匹配模糊)
	 * 模糊之后
	 * @param iotType
	 * @return
	 */
	public AnalysisBean matchIotType(String iotType){
		if(null==iotType||"".equals(iotType)){
			return null;
		}
		iotType = iotType.toUpperCase();//转化为大写
		AnalysisBean analysisBean = null;
		for (String actuMapStr : actuMap.keySet()) {
			if(iotType.equals(actuMapStr)){
				analysisBean = actuMap.get(actuMapStr);
				break;
			}
		}
		
		if(null!=analysisBean){
			return analysisBean;
		}
		
		for(String likeMapStr : likeMap.keySet()) {
			if(iotType.contains(likeMapStr)){
				return likeMap.get(iotType);
			}
		}
		
		return null;
	}
	
	/**
	 * 读取累计表的数据
	 */
	public void readAllData(String dataType){
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		List<AnalysisBean> list = new ArrayList<>();
		File writeFile = null;
		
		String sql  = ConfigUtil.getQueryDataType(dataType);
		logger.info("查询的sql语句"+sql);
		try {
			conn = DBHiberUtil.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			
			String initFilePathName = ConfigUtil.getFilePath()+File.separator+dataType+File.separator+dataType+"_"+
					DateUtil.nowTime()+"_"+ConfigUtil.getRandomNum()+initFileEndName;
			writeFile = new File(initFilePathName); //文件路径名
			logger.info("===>生成文件"+writeFile.getName());
			long start = System.currentTimeMillis();
			long countTime = 0;
			long countrow = 0;
			while(rs.next()){
				countrow++;
				String imei = rs.getString("imei");
				String iotType = rs.getString("iotType");//如果接入多个这里需要修改
				String dt = rs.getString("dt");
				//获取每一行数据-匹配规则
				AnalysisBean analysisBean = matchIotType(iotType);
				if(null!=analysisBean){
					//匹配到数据
					analysisBean.setData(imei, iotType, dt);
					list.add(analysisBean);
					try {
						if(list.size()%maxCountRow==0){
							countTime++;
							if(ConfigUtil.isAddFile()){
								if(writeFile.length()>maxFileSize){
									//从.termp临时文件转为.csv后缀文件
									writeFile = FileUtil.rename(writeFile, initFileEndName, fileEndName);
									logger.info(writeFile.getName()+",大于maxFileSize限制的"+(maxFileSize/1024/1024)+"M,需重新创建文件");
									
									long end = System.currentTimeMillis();
									logger.info("生成一个文件耗时:" + ((end - start)/1000) +" 秒");
									start = end;
									
									initFilePathName = ConfigUtil.getFilePath()+File.separator+dataType+File.separator+dataType+"_"+
											DateUtil.nowTime()+"_"+ConfigUtil.getRandomNum()+initFileEndName;
									writeFile = new File(initFilePathName); //文件路径名		
									logger.info("===>生成文件"+writeFile.getName());
								}
									FileUtils.writeLines(writeFile, list,true);
									logger.info("第"+countrow+"个,第"+countTime+"批次===>匹配到数据大于"+maxCountRow+"条了,并且写入了-->"+writeFile.getName());
							}else{
								initFilePathName = ConfigUtil.getFilePath()+File.separator+dataType+File.separator+dataType+"_"+
										DateUtil.nowTime()+"_"+ConfigUtil.getRandomNum()+initFileEndName;
								File file = new File(initFilePathName);
								FileUtils.writeLines(file, list);
								file = FileUtil.rename(file, initFileEndName, fileEndName);
							}
								list = new ArrayList<>();////清空数据
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}			 
				}
				
			}
			
			try {
				if(ConfigUtil.isAddFile()){
					logger.info("===>最后数据:"+list.size()+",并且写入了文件-->."+writeFile.getName());
					FileUtils.writeLines(writeFile, list,true);
				}else{
					initFilePathName = ConfigUtil.getFilePath()+File.separator+dataType+File.separator+dataType+"_"+
							DateUtil.nowTime()+"_"+ConfigUtil.getRandomNum()+initFileEndName;
					File file = new File(initFilePathName);
					FileUtils.writeLines(file, list);
					file = FileUtil.rename(file, initFileEndName, fileEndName);
				}
				list = new ArrayList<>();////清空数据
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//最后不符合限制大小文件也需要转换后缀名
			writeFile = FileUtil.rename(writeFile, initFileEndName, fileEndName);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("查询读取累计表的数据sql语句出错!请检查"+e);
		}finally{
			try {
				DBHiberUtil.close(rs, stmt, conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	/**
	 * 读取规则表数据
	 */
	public void loadRule(String dataType){
		logger.info("准备载入"+dataType+"规则数据!!!");
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		

		String sql = ConfigUtil.getQueryRule(dataType);
		logger.info("查询的sql语句"+sql);
		try {
			conn = DBHiberUtil.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				String iot_id = rs.getString("iot_id");
				String rule_id = rs.getString("rule_id");
				String iot_rule = rs.getString("iot_rule");
				String use_way = rs.getString("use_way");
				
				iot_rule = iot_rule.toUpperCase();//转化为大写
				if("0".equals(use_way)){
					//精确匹配规则
					AnalysisBean analysisBean = new AnalysisBean(iot_id, rule_id, iot_rule, use_way);
					actuMap.put(iot_rule, analysisBean);
					
				}else if("1".equals(use_way)){
					iot_rule = iot_rule.replace("%", "");
					//模糊匹配规则
					AnalysisBean analysisBean = new AnalysisBean(iot_id, rule_id, iot_rule, use_way);
					likeMap.put(iot_rule, analysisBean);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("查询载入规则sql语句出错!请检查"+e);
		}finally{
			try {
				DBHiberUtil.close(rs, stmt, conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
		IotAllAnalysis allAnalysis = new IotAllAnalysis();
		allAnalysis.doAction();
	}
}

util包(工具包)

ConfigureUtil

package com.htjf.util;

import java.util.Map;
import org.apache.log4j.Logger;


/**
 * 系统配置类
 * @author Jiomer
 *
 */
public class ConfigureUtil {
		private static Logger logger = Logger.getLogger(ConfigureUtil.class);
	    public static ConfigureUtil instance = null;
	    
	    @SuppressWarnings("rawtypes")
		private Map configureMap = null;

	    public ConfigureUtil() {
	    	
	    	String file = ClassLoader.getSystemResource("").getPath()+"cfg.xml";//正式环境
//	    	String file = this.getClass().getResource("/").getPath()+"cfg.xml";//测试环境
//	    	String file = ConfigureUtil.class.getClassLoader().getResource("cfg1.xml").getPath();//测试环境
	    	System.out.println(file);
	    	logger.info("获取了cfg.xml路径:"+file);
	    	try {
	    		configureMap = new XmlSupportImpl().readDocument(file);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }
	    public ConfigureUtil(String isHiber) {
	    	 String file = this.getClass().getResource("/").getPath()+"cfghiber.xml";
	    	try {
	    		configureMap = new XmlSupportImpl().readDocument(file);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }

	    /*
	     * (non-Javadoc)
	     * @see proc.gmcc.sso.config.Configure#getValue(java.lang.String)
	     */
	    public String getValue(String key) {
	    	return (String) configureMap.get(key);
	    }
	    
		public static ConfigureUtil  getInstance(){
			if(null== instance ){
				synchronized(ConfigureUtil.class){
					if(null== instance){
						instance = new ConfigureUtil ();
					}
				}
			}
			return instance;
		}
	    public static void main(String[] args) {
	    	System.out.println(new ConfigureUtil().getValue("driverURL"));
		}

}

ConfigUtil

package com.htjf.util;

import java.io.File;

import org.apache.log4j.Logger;

public class ConfigUtil {
	private static Logger logger = Logger.getLogger(ConfigUtil.class);
	
	public static String getXMLConfigValueBYName(String name) {
		return ConfigureUtil.getInstance().getValue(name);
	}
	
	/**
	 * 获取
	 * @return
	 */
	public static boolean isAddFile(){
		String isAddFile = getXMLConfigValueBYName("IS_ADD");
		return "true".equals(isAddFile);
	}
	
	/**
	 * 获取
	 * @return
	 */
	public static int getMaxCountRow(){
		String maxCountRow = getXMLConfigValueBYName("MAX_COUNT_ROW");
		int intMaxCountRow = Integer.parseInt(maxCountRow);
		return intMaxCountRow;
	}
	/**
	 * 初始生成文件的后缀名(.temp)
	 * @return
	 */
	public static String getInitFileEndName(){
		String initFileEndName = getXMLConfigValueBYName("INIT_FILE_ENDNAME");
		return initFileEndName;
	}
	/**
	 * 最终生成文件的后缀名(.csv)
	 * @return
	 */
	public static String getFileEndName(){
		String fileEndName = getXMLConfigValueBYName("FILE_ENDNAME");
		return fileEndName;
	}
	/**
	 * 获取存放生成文件的本地路径
	 * @return
	 */
	public static String getFilePath(){
		String filePath = getXMLConfigValueBYName("FILE_BASE_PATH");
		File file = new File(filePath);
		if(!file.exists()&&!file .isDirectory()){
				file.mkdir();
				return filePath;
		}
//		logger.info("获取filePath:"+filePath);
		return filePath;
	}
	
	/**
	 * 获取限制文件大小
	 * @return
	 */
	public static long getMaxFileSise(){
		String strMaxFileSise = getXMLConfigValueBYName("MAX_FILE_SIZE");
		long longMaxFileSise = Long.parseLong(strMaxFileSise)*1024*1024;
		logger.info("获取文件限制大小:"+strMaxFileSise+"(MB)");
		return longMaxFileSise;
	}
	
	/**
	 * 生成5位随机数
	 * @return
	 */
	public static int getRandomNum(){
		int randomNum = (int)((Math.random()*9+1)*10000);
		return randomNum;
	}
	/**
	 * 获取需要分析的类型
	 * @return
	 */
	public static String getDataType(){
		String dataType = getXMLConfigValueBYName("DATA_TYPE");
		//全部转成大写
		dataType = dataType.toUpperCase();
		return dataType;
	}
	/**
	 * 获取累计表的sql
	 * @return
	 */
	public static String getQueryDataType(String dataType){
		String sql = getXMLConfigValueBYName("QUERY_"+dataType+"_DATA");
		return sql;
	}
	/**
	 * 获取规则表的sql
	 * @return
	 */
	public static String getQueryRule(String dataType){
		String sql = getXMLConfigValueBYName("QUERY_"+dataType+"_RULE");
		return sql;
	}
	
	public static void main(String[] args) {
		System.out.println(getMaxFileSise());
	}
}

DateUtil

package com.htjf.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	
	/**
	 * 获取当前天的前一天
	 * @return
	 */
	public static String proFormatDay(){
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");    
		Date today = new Date();    
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(today);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		String proDay = simpleDateFormat.format(calendar.getTime()).toString();
		return proDay;
	}
	/**
	 * 获取当天 yyyymmdd
	 * @return
	 */
	public static String formatDay(){
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");    
		Date date = new Date();    
		String today = simpleDateFormat.format(date).toString();
		return today;
	}
	/**
	 * 获取当前时间yyyyMMddHHmmss
	 * @return
	 */
	public static String nowTime(){
		SimpleDateFormat simpleDateFormat = new java.text.SimpleDateFormat("yyyyMMddHHmmss");    
		Date currentTime1 = new java.util.Date();    
		String nowTime = simpleDateFormat.format(currentTime1).toString();
		return nowTime;
	}

}

DBHiberUtil

package com.htjf.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

/**
 * 数据库连接相关操作类
 * @author Jiomer
 *
 */
public class DBHiberUtil {
	//private static final transient Log log = LogFactory.getLog(DBHiberUtil.class);
	private static Logger logger = Logger.getLogger(DBHiberUtil.class);

	/**
	 * 获取数据库链接
	 * @return
	 */
    public static Connection getConnection() {
        Connection conn = null;
        ConfigureUtil configUtil = new ConfigureUtil();
        String driverURL = configUtil.getValue("driverURL").trim();
        String dataBaseAccount = configUtil.getValue("dataBaseAccount").trim();
        String dataBasePassWord = configUtil.getValue("dataBasePassWord").trim();;
        //System.out.println("getConnection-----"+driverURL+"--"+dataBaseAccount+"--"+dataBasePassWord);
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection(driverURL, dataBaseAccount, dataBasePassWord);
           //System.out.println("Connection 开启!");
            logger.info("Connection 开启!");
        }
        catch (Exception e) {
        	logger.error("Connection 失败"+e);
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 断开数据库连接
     * @param conn
     * @throws SQLException
     */
    public static void close(Connection conn) throws SQLException {
        if (conn != null) {
            conn.close();
        }
    }

    /**
     * 断开数据库连接
     * @param conn
     */
    public static void closeQuiet(Connection conn) {
        try {
            close(conn);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭查询结果集对象
     * @param rs
     */
    public static void closeQuiet(ResultSet rs) {
        try {
            close(rs);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭查询结果集对象
     * @param rs
     * @throws SQLException
     */
    public static void close(ResultSet rs) throws SQLException {
        if (rs != null) {
            rs.close();
        }
    }

    /**
     * 关闭指定Statement对象
     * @param stmt
     */
    public static void closeQuiet(Statement stmt) {
        try {
            close(stmt);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭指定Statement对象
     * @param stmt
     * @throws SQLException
     */
    public static void close(Statement stmt) throws SQLException {
        if (stmt != null) {
            stmt.close();
        }
    }

    /**
     * 关闭指定Statement对象和ResultSet对象
     * @param rs
     * @param stmt
     */
    public static void closeQuiet(ResultSet rs, Statement stmt) {
        closeQuiet(rs);
        closeQuiet(stmt);
    }

    /**
     * 关闭指定Statement对象和ResultSet对象
     * @param rs
     * @param stmt
     * @throws SQLException
     */
    public static void close(ResultSet rs, Statement stmt) throws SQLException {
        close(rs);
        close(stmt);
    }

    /**
     * 关闭指定Statement对象和断开数据库连接
     * @param stmt
     * @param conn
     */
    public static void closeQuiet(Statement stmt, Connection conn) {
        closeQuiet(stmt);
        closeQuiet(conn);
    }

    /**
     * 关闭指定Statement对象和断开数据库链接
     * @param stmt
     * @param conn
     * @throws SQLException
     */
    public static void close(Statement stmt, Connection conn) throws SQLException {
        close(stmt);
        close(conn);
    }

    /**
     * 关闭指定ResultSet、Statement对象和断开数据库链接
     * @param rs
     * @param stmt
     * @param conn
     */
    public static void closeQuiet(ResultSet rs, Statement stmt, Connection conn) {
        closeQuiet(rs);
        closeQuiet(stmt, conn);
    }

    /**
     * 关闭指定ResultSet、Statement对象和断开数据库链接
     * @param rs
     * @param stmt
     * @param conn
     * @throws SQLException
     */
    public static void close(ResultSet rs, Statement stmt, Connection conn) throws SQLException {
        close(rs);
        close(stmt, conn);
        logger.info("Connection 关闭!");
    }
    /**
     * 获取表中所有字段名称
     * @param tableName 表名
     * @return
     */
    public static List<String> getColumnNames(String tableName){
    	List<String> columnNames = new ArrayList<>();
    	Connection connection = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		ResultSetMetaData rsmd = null;
		String sql = "select * from "+tableName;
		try {
			connection = DBHiberUtil.getConnection();
			ps = connection.prepareStatement(sql);
			rs = ps.executeQuery(sql);
			rsmd = rs.getMetaData();
			for (int i = 1; i <=rsmd.getColumnCount() ; i++) {
				columnNames.add(rsmd.getColumnName(i));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				connection.close();
				ps.close();
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
    	return columnNames;
    }

}

FileUtil

package com.htjf.util;

import java.io.File;

public class FileUtil {
	
	/**
	 * 文件处理前重命名(以扩展名替换的形式)
	 * @param srcFile 需要重命名的文件
	 * @param tar 重命名目标字符串
	 * @param rep 替换字符串
	 * @return 重命名后文件
	 */
	public static File rename(File srcFile, String tar, String rep) {
		String perMatchFileName =  srcFile.getPath().replace(tar, rep);
		File newNameFile = new File(perMatchFileName);
		srcFile.renameTo(newNameFile);
		return newNameFile;
	}
}

ParseXmlUtil

package com.htjf.util;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
 * 
 * @author Jiomer
 *
 */
public class ParseXmlUtil {
	private static Logger logger = Logger.getLogger(ParseXmlUtil.class);
	private static ParseXmlUtil  instance = null;
	private Map<String,String> configureMap = null;
	private Document doc=null;
	private String path= ClassLoader.getSystemResource("").getPath() + "cfg.xml";
	
//	private String path2 = this.getClass().getResource("/").getPath()+"cfg.xml";
//	private String path3 = ConfigureUtil.class.getClassLoader().getResource("cfg.xml").getPath();//测试环境
	
	private ParseXmlUtil (){
		try {
			//打包成jar包,直接读取jar包同级目录的包外配置文件
//			path= "cfg.xml";
			reLoadData();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public String getPath() {
		return path;
	}
	
	public void setPath(String path) {
		this.path = path;
	}
	
	public Document getDoc() {
		return doc;
	}
	
	public void setDoc(Document doc) {
		this.doc = doc;
	}

	public void reLoadData(){
		try {
			logger.info("加载配置文件"+getPath());
			doc = new SAXReader().read(new File(getPath()));
			configureMap=initLoadConfig();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static ParseXmlUtil  getInstance(){
		if(null== instance ){
			synchronized(ParseXmlUtil.class){
				if(null== instance){
					instance = new ParseXmlUtil ();
				}
			}
		}
		return instance;
	}
	
	/**
	 * 指定配置文件目录初始化文件加
	 * @param path
	 * @return
	 */
	private Map<String,String> initLoadConfig(){
		 Map<String,String> map =new HashMap<String,String>();
		 try {
	           Element root = doc.getRootElement();
	           @SuppressWarnings("unchecked")
	           List<Element> list = root.elements();
	           for(Element actionNode : list)
	           {
	        	   map.put(actionNode.getName(),actionNode.getTextTrim());
	           }
	           return map;
	       } catch (Exception e) {
	           e.printStackTrace();
	           return null;
	       }
	}
	/**
	 * @param name
	 * @return
	 */
	public String getConfigValueBYName(String name){
		return configureMap.get(name);
	}
}

XmlSupport

package com.htjf.util;

import java.util.Map;

import org.dom4j.Document;
/**
 * @author Jiomer
 */
public interface XmlSupport {
	/**
	 * 创建xml文件
	 * @return
	 */
	public Document createDocument();
	
	/**
	 * 读xml文件
	 * @param file
	 * @return
	 * @throws Exception
	 */
	public Map readDocument(String file)throws Exception;
	
	/**
	 * 写xml文件
	 * @throws Exception
	 */
	public void writerDocument() throws Exception;
	/**
	 * 更新xml文件
	 */
	public void updateDocument() ;
}

XmlSupportImpl

package com.htjf.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
/**
 * 
 * @author Jiomer
 *
 */
public class XmlSupportImpl implements XmlSupport{

	@Override
	public Document createDocument() {
		// TODO Auto-generated method stub
		Document document = DocumentHelper.createDocument();
		Element rootElement = document.addElement("root");

		Element uploadFileElement = rootElement.addElement("uploadFile");

		Element path = uploadFileElement.addElement("path");
		path.setText("/hjtInfoUpload/");
		Element size = uploadFileElement.addElement("size");
		size.setText("10240");
		Element type = uploadFileElement.addElement("type");
		type.setText("zip,rar,png,jpg,gif");

		return document;
	}

	@Override
	public Map readDocument(String file) throws Exception {
		// TODO Auto-generated method stub
		Map map = new HashMap();
		SAXReader saxReader =  new SAXReader();
		
		Document document = saxReader.read(new FileInputStream(new File(file)));
		Element root = document.getRootElement();
		List list = root.elements();
		for(int i=0;i<list.size();i++){
			Element ele = (Element) list.get(i);
			map.put(ele.getName(), ele.getText());
		}
		return map;
	}

	@Override
	public void writerDocument() throws Exception {
		// TODO Auto-generated method stub
		OutputFormat of = new OutputFormat("   ", true);
		XMLWriter xmlWriter = new XMLWriter(new FileWriter("hjt_info.xml"), of);
		//LoggerUtil.info("XmlSupportImpl",new File("hjt_info.xml").getAbsolutePath());
		xmlWriter.write(this.createDocument());
		xmlWriter.close();
	}

	@Override
	public void updateDocument() {
		// TODO Auto-generated method stub
		
	}

}

log4j.properties

log4j.rootLogger=stdout,ERROR,logfile
#\u8f93\u51fa\u5230\u63a7\u5236\u53f0#  
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=INFO
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d %-5p [%-8t] [%-17c{1}]-%m%n

#\u8f93\u51fa\u5230\u9879\u76ee\u6587\u4ef6\u4e0b#
log4j.appender.logfile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.File = ./logs/info_log.log
log4j.appender.logfile.Append = true
log4j.appender.logfile.Threshold = INFO
log4j.appender.logfile.layout = org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n
##log4j.appender.logfile.layout.ConversionPattern= %d %-5p [%-8t] [%-17c{1}]-%m%n

#\u9519\u8bef\u4fe1\u606f\u8f93\u51fa\u5230\u9879\u76ee\u5b50\u6587\u4ef6\u5939\u4e0b#
log4j.appender.ERROR=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ERROR.File = ./logs/err_log.log
log4j.appenile.ERROR.Append = true
log4j.appender.ERROR.Threshold = ERROR
log4j.appender.ERROR.layout = org.apache.log4j.PatternLayout
log4j.appender.ERROR.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n

cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<cfgs>
    <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
    <!-- 本地测试环境 
		-->
    <driverURL>
      jdbc:oracle:thin:@192.168.20.201:1521:mmdsdb
    </driverURL> 
    <dataBaseAccount>T_MMDS</dataBaseAccount>
    <dataBasePassWord>testmmds</dataBasePassWord> 
    
    <!-- 基础配置项 -->
    <INIT_FILE_ENDNAME>.temp</INIT_FILE_ENDNAME><!-- 初始化文件后缀名 -->
    <FILE_ENDNAME>.csv</FILE_ENDNAME><!-- 生成文件的后缀名 .csv/.txt  -->
    <MAX_FILE_SIZE>10</MAX_FILE_SIZE><!-- 生成文件大小限制多大 单位MB -->
    <MAX_COUNT_ROW>10000</MAX_COUNT_ROW><!-- 设置多少行,生成/写入 -->
    <IS_ADD>true</IS_ADD><!-- 是否追加在源文件 -->
    <DATA_TYPE>host</DATA_TYPE>
	<FILE_BASE_PATH>G:/test12</FILE_BASE_PATH>
	<!-- 生成文件存放的父路径 
	<FILE_BASE_PATH>/export/a5duser/iot_ana/file</FILE_BASE_PATH>
	-->
	<QUERY_HOST_DATA>select imei,host iotType,(lt-ft)dt from IOT_DIG_HOST_COUNT</QUERY_HOST_DATA>
	<QUERY_HOST_RULE>select iot_id,rule_id,iot_rule,use_way from iot_base_hreg_info where enable = 0</QUERY_HOST_RULE>
	<QUERY_UA_DATA>select imei,ua iotType,(lt-ft)dt from IOT_DIG_UA_COUNT</QUERY_UA_DATA>
	<QUERY_UA_RULE>select iot_id,rule_id,iot_rule,use_way from iot_base_ureg_info where enable = 0</QUERY_UA_RULE>
</cfgs>

end..


暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇