一切配置外部文件化。
创建gendata.conf
log.file=/home/centos/calllog/calllog.log call.duration.max=600 call.duration.format=000 call.year=2017 call.time.format=yyyy/MM/dd HH:mm:ss gen.data.interval.ms=2000
创建PropertiesUtil类,用于读取gendata.conf 中的配置
public class PropertiesUtil { static Properties prop ; static{ try { InputStream in = ClassLoader.getSystemResourceAsStream("gendata.conf"); prop = new Properties(); prop.load(in); } catch (Exception e) { e.printStackTrace(); } } public static String getProp(String key){ return prop.getProperty(key) ; } public static String getString(String key){ return prop.getProperty(key) ; } public static int getInt(String key){ return Integer.parseInt(prop.getProperty(key)) ; } }
创建GenDataAppMain类:
public class GenDataAppMain { static Random r = new Random(); public static List<String> phoneNumbers = new ArrayList<String>(); public static Map<String,String> callers = new HashMap<String, String>(); static{ callers.put("15220141678", "陈一"); callers.put("18046865067", "胡二"); callers.put("15119884501", "张三 "); callers.put("19263361219", "李四"); callers.put("18032291306", "王五"); callers.put("12711058869", "任六"); callers.put("15335593369", "梁七"); callers.put("12734214050", "刘八"); callers.put("14618206525", "朱九"); callers.put("17781348030", "郭十"); callers.put("18646281129", "邵十一"); callers.put("15031645440", "黎十二"); callers.put("13041909505", "肖十三"); callers.put("13664192665", "杨十四"); callers.put("18404589132", "骆十五"); callers.put("13920504953", "郑十六"); callers.put("18532662075", "黄十七"); callers.put("18120592721", "罗十八"); phoneNumbers.addAll(callers.keySet()); } public static void main(String[] args) throws Exception { genCallLog(); } public static void genCallLog() throws Exception { // FileWriter fw = new FileWriter("/home/centos/calllog/calllog.log",true); FileWriter fw = new FileWriter(PropertiesUtil.getString("log.file"),true); while(true){ //取主叫 String caller = phoneNumbers.get(r.nextInt(callers.size())); //主叫名字 String callerName = callers.get(caller); //被叫号 String callee = null; String calleeName = null; while (true) { callee = phoneNumbers.get(r.nextInt(callers.size())); if (!caller.equals(callee)) { break; } } calleeName = callers.get(callee); //通话时长 int duration = r.nextInt(PropertiesUtil.getInt("call.duration.max")) + 1; DecimalFormat df = new DecimalFormat(); df.applyPattern(PropertiesUtil.getString("call.duration.format")); String durStr = df.format(duration); //通话时间 int year = PropertiesUtil.getInt("call.year"); //月份(0~11) int month = r.nextInt(12); //天,范围(1~31) int day = r.nextInt(29) + 1; int hour = r.nextInt(24); int min = r.nextInt(60); int sec = r.nextInt(60); Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month); c.set(Calendar.DAY_OF_MONTH, day); c.set(Calendar.HOUR_OF_DAY, hour); c.set(Calendar.MINUTE, min); c.set(Calendar.SECOND, sec); Date date = c.getTime(); //如果时间超过今天就重新qushijian取时间. Date now = new Date(); if (date.compareTo(now) > 0) { continue ; } SimpleDateFormat dfs = new SimpleDateFormat(); dfs.applyPattern(PropertiesUtil.getString("call.time.format")); //通话时间 String dateStr = dfs.format(date); //String log = caller + "," + callerName + "," + callee + "," + calleeName + "," + dateStr + "," + durStr ; String log = caller + "," + callee + "," + dateStr + "," + durStr; System.out.println(log); fw.write(log + "\r\n"); fw.flush(); Thread.sleep(PropertiesUtil.getInt("gen.data.interval.ms")); } } }
打成jar包仍到s128:
事先要创建好calllog.sh脚本:
运行脚本:
数据生成到calllog.log文件