编撰talend routines 使用本地文件获得每个日期的累加序列号
编写talend routines 使用本地文件获得每个日期的累加序列号
最近做了一个数据交换 生成序列号规则 需要按每天进行序号的累加。我又不想从数据库中读取表进行设置,于是写了下面的从文件进行读取生成。
新建了一个 routines 方法:
/** * 从一个文件读取当天日期已经有的序列号 * 如果没有该文件则建立该文件 * * {talendTypes} String * * {Category} User Defined * * {param} string("filename") input: 需要查找建立的文件. * {param} string("date") input: 需要查找的日期. * * {example} getXLHfromFile("filename") # hello world !. */ public static String getXLHfromFile(String filename,String date) { String xlh="0"; File file=new File(filename); try{ if(!file.exists()){ //如果该文件不存在则建立该文件 Document doctemp = DocumentHelper.createDocument(); doctemp.setXMLEncoding("utf-8"); Element root = doctemp.addElement("root"); Element jishu = root.addElement("jishu"); jishu.addAttribute("date","0"); jishu.addAttribute("count","0"); FileWriter out = new FileWriter(filename); doctemp.write(out); out.flush(); out.close(); } SAXReader reader = new SAXReader(); Document doc = reader.read(file); Element root = doc.getRootElement(); Iterator itroot = root.elementIterator(); boolean findflag=false; while(itroot.hasNext()){ //System.out.println("root 下一个"); Element info = (Element)itroot.next(); String rq = info.attributeValue("date"); if(rq.equals(date)){ findflag=true; xlh=info.attributeValue("count"); info.setAttributeValue("count", Integer.toString((Integer.parseInt(xlh)+1))); } } if(!findflag){ //如果没有找到 则建立该基数 Element jishu = root.addElement("jishu"); jishu.addAttribute("date", date); jishu.addAttribute("count", "1"); //0已经用过了 break; } FileWriter out = new FileWriter(filename); doc.write(out); out.flush(); out.close(); }catch(Exception e){ e.printStackTrace(); } return xlh; }