From ce64bf11e6632a16f6a66f3fe4640e1148a198bb Mon Sep 17 00:00:00 2001 From: ChaunceyJ <33971563+ChaunceyJ@users.noreply.github.com> Date: Sun, 20 Oct 2019 11:33:13 +0800 Subject: [PATCH 01/11] Accept_file;Json2owl;Save_event --- .../src/main/java/neo4j/FusekiDriver.java | 27 ++- .../src/main/java/neo4j/MongoDriver.java | 38 ++++ .../src/main/java/neo4j/prometheusDriver.java | 2 +- BackEnd19v/src/main/java/util/JsonUtil.java | 164 ++++++++++++++++++ .../src/main/java/web/CsvController.java | 1 - .../src/main/java/web/FileController.java | 90 ++++++++++ .../src/main/java/web/MongoDBController.java | 5 + 7 files changed, 320 insertions(+), 7 deletions(-) create mode 100644 BackEnd19v/src/main/java/util/JsonUtil.java create mode 100644 BackEnd19v/src/main/java/web/FileController.java diff --git a/BackEnd19v/src/main/java/neo4j/FusekiDriver.java b/BackEnd19v/src/main/java/neo4j/FusekiDriver.java index 7acf9d2ef..f7f6229a5 100755 --- a/BackEnd19v/src/main/java/neo4j/FusekiDriver.java +++ b/BackEnd19v/src/main/java/neo4j/FusekiDriver.java @@ -20,6 +20,7 @@ import org.springframework.stereotype.Component; import java.io.BufferedReader; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; @@ -1690,8 +1691,8 @@ public static void addLinkEvent2S(){ Date start = new Date(); Date end = new Date(); try { - start = DateFormat.parse("2019-08-06 00:00:00"); - end = DateFormat.parse("2019-08-06 23:59:59"); + start = DateFormat.parse("2019-09-25 00:00:00"); + end = DateFormat.parse("2019-09-25 23:59:59"); } catch(ParseException px) { px.printStackTrace(); } @@ -1705,10 +1706,11 @@ public static void addLinkEvent2S(){ times.add(i.getTime()/1000); } Collections.sort(times); + StringBuffer stringBuffer = new StringBuffer(); for (Resource i:resources ) { Statement statement = i.getProperty(model.createProperty(i.toString()+"/query")); - System.out.println(start.getTime()/1000 + " " + end.getTime()/1000); + //System.out.println(start.getTime()/1000 + " " + end.getTime()/1000); JSONArray proInfor = getProInfor(statement.getString().replace(" ",""),start.getTime()/1000 + "", end.getTime()/1000 + ""); //JSONArray proInfor = getProInfor(statement.getString().replace(" ",""),times.get(0)+"", times.get(times.size()-1)+""); @@ -1726,12 +1728,17 @@ public static void addLinkEvent2S(){ } } } - System.out.println("request---------"); + String[] strings = i.toString().split("/"); + System.out.println(strings[strings.length-1]); System.out.println(timeList); System.out.println(proInfor); +// stringBuffer.append(strings[strings.length-1]); JSONObject jsonObject = new JSONObject(); jsonObject.put("file1",timeList); jsonObject.put("file2", proInfor); +// stringBuffer.append("\r\n"); +// stringBuffer.append(jsonObject.toString()); +// stringBuffer.append("\r\n"); String re = util.HttpPostUtil.postData(jsonObject.toJSONString()); System.out.println(re); if (re != null){ @@ -1741,6 +1748,13 @@ public static void addLinkEvent2S(){ } } } +/* try { + FileOutputStream fos = new FileOutputStream("/Users/jiang/data.txt"); + fos.write(stringBuffer.toString().getBytes()); + } + catch (Exception e) { + System.out.println(e.getMessage()); + }*/ @@ -1762,7 +1776,9 @@ public static Map getEventInFuseki(Date startTime,Date endTim Resource r = iter.nextResource(); if (r.toString().contains("event")) { - String timeProperty=r.getProperty(model.createProperty(r.toString()+"/starts_at")).getResource().toString(); + Statement statement = r.getProperty(model.createProperty(r.toString()+"/starts_at")); + if (statement == null) continue; + String timeProperty=statement.getResource().toString(); //截取出时间字符串,去掉中间的“-” int length=timeProperty.length(); String time=timeProperty.substring(length-19,length-9)+" "+timeProperty.substring(length-8); @@ -1823,5 +1839,6 @@ public static boolean addCorrelation(String fromUrl, String toUrl, String influe public static void main(String[] args) { + // addLinkEvent2S(); } } diff --git a/BackEnd19v/src/main/java/neo4j/MongoDriver.java b/BackEnd19v/src/main/java/neo4j/MongoDriver.java index cfd113038..47578f714 100644 --- a/BackEnd19v/src/main/java/neo4j/MongoDriver.java +++ b/BackEnd19v/src/main/java/neo4j/MongoDriver.java @@ -135,6 +135,44 @@ public static boolean saveKapacitor2Mongo(String message){ return true; } + public static boolean saveEvent2Mongo(String content, String source){ + try { + //连接到mongodb服务 + MongoClient mongoClient = new MongoClient(globalvalue.mongoapi, 27017); + //MongoClient mongoClient = new MongoClient("10.60.38.173", 27020); + //连接到数据库 + MongoDatabase mongoDatabase = mongoClient.getDatabase("knowledgegraph"); + MongoCollection collection = mongoDatabase.getCollection("Event"); + //获取当前时间 + Date day=new Date(); + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String time = df.format(day); + System.out.println(time); + Map data = new HashMap<>(); + data.put("content", content); + data.put("source", source); + switch (source) { + case "Kapacitor": + data.put("type", 1); + break; + case "K8s": + data.put("type", 2); + break; + default: + data.put("type", 0); + } + //插入文档 + Document document = new Document(data). + append("time", time); + collection.insertOne(document); + System.out.println("文档插入成功"); + } catch (Exception e){ + e.printStackTrace(); + return false; + } + return true; + } + public static Map getOneFromMongo(String time){ try { //连接到mongodb服务 diff --git a/BackEnd19v/src/main/java/neo4j/prometheusDriver.java b/BackEnd19v/src/main/java/neo4j/prometheusDriver.java index c66020412..9a19c46c2 100644 --- a/BackEnd19v/src/main/java/neo4j/prometheusDriver.java +++ b/BackEnd19v/src/main/java/neo4j/prometheusDriver.java @@ -212,7 +212,7 @@ public static JSONArray getProInfor(String urlNode, String start, String end) { try { url = "http://10.60.38.181:30003/api/v1/query_range?query=" + URLEncoder.encode(urlNode, "UTF-8") + "&start=" + start + "&end=" + end + "&step=60"; // url = java.net.URLEncoder.encode(url); - System.out.println(url); + //System.out.println(url); }catch (Exception e){ System.out.println(); } diff --git a/BackEnd19v/src/main/java/util/JsonUtil.java b/BackEnd19v/src/main/java/util/JsonUtil.java new file mode 100644 index 000000000..58ec44e51 --- /dev/null +++ b/BackEnd19v/src/main/java/util/JsonUtil.java @@ -0,0 +1,164 @@ +package util; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import org.apache.commons.io.FileUtils; +import org.dom4j.Attribute; +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; +import org.xml.sax.SAXException; + +import java.io.*; +import java.util.*; + +import static javax.xml.transform.OutputKeys.ENCODING; + +public class JsonUtil { + + Map map=new HashMap(); + + public String jsonToXml(JSONObject jObj){ + try { + StringBuffer buffer = new StringBuffer(); + buffer.append(""); + jsonToXmlstr(jObj,buffer); + return buffer.toString(); + } catch (Exception e) { + e.printStackTrace(); + return ""; + } + } + + public String jsonToXmlstr(JSONObject jObj,StringBuffer buffer ){ + Set> se = jObj.entrySet(); + for(Iterator> it = se.iterator(); it.hasNext(); ) + { + Map.Entry en = it.next(); + if(en.getValue()instanceof JSONObject){ + buffer.append("<"+en.getKey()+" "+map.get(en.getKey())+">"); + JSONObject jo = jObj.getJSONObject(en.getKey()); + jsonToXmlstr(jo,buffer); + buffer.append(""); + }else if(en.getValue()instanceof JSONArray){ + JSONArray jarray = jObj.getJSONArray(en.getKey()); + for (int i = 0; i < jarray.size(); i++) { + buffer.append("<"+en.getKey()+" "+map.get(en.getKey())+">"); + JSONObject jsonobject = jarray.getJSONObject(i); + jsonToXmlstr(jsonobject,buffer); + buffer.append(""); + } + }else{ + buffer.append("<"+en.getKey()+" "+map.get(en.getKey())+">"+en.getValue()); + buffer.append(""); + } + } + return buffer.toString(); + } + + public void readJson() + { + File file = new File("F:\\Xlab\\test.json"); + try { + String data = FileUtils.readFileToString(file); + JSONArray array=JSON.parseArray(data); + for(int i=0;i myResult =parseJSON2Map(object); + String xmlstr = jsonToXml(object); + System.out.println(xmlstr); + + + } + + + }catch (Exception e){ + e.printStackTrace(); + } + } + + /* + public Map parseJSON2Map(JSONObject json) { + Map map = new HashMap(); + // 最外层解析 + for (Object k : json.keySet()) { + Object v = json.get(k); + // 如果内层还是json数组的话,继续解析 + if (v instanceof JSONArray) { + List> list = new ArrayList>(); + for (int i=0;i<((JSONArray) v).size();i++) { + JSONObject json2 =((JSONArray) v).getJSONObject(i); + list.add(parseJSON2Map(json2)); + } + map.put(k.toString(), list); + } else if (v instanceof JSONObject) { + // 如果内层是json对象的话,继续解析 + map.put(k.toString(), parseJSON2Map((JSONObject) v)); + } else { + // 如果内层是普通对象的话,直接放入map中 + map.put(k.toString(), v); + } + } + return map; + } + */ + + public Element readXmlRoot(String soucePath){ + try { + File file = new File(soucePath); + SAXReader read = new SAXReader(); + org.dom4j.Document doc = read.read(file); + Element root = doc.getRootElement(); + return root; + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + public void getNodes(Element node){ + //当前节点的名称、文本内容和属性 + String name=node.getName(); + String context=node.getTextTrim(); + if(context!=null){ + map.put(context,name); + } + //List listAttr=node.attributes();//当前节点的所有属性的list + /* + for(Attribute attr:listAttr){//遍历当前节点的所有属性 + + String name=attr.getName();//属性名称 + String value=attr.getValue();//属性的值 + System.out.println("属性名称:"+name+"属性值:"+value); + } + */ + + //递归遍历当前节点所有的子节点 + List listElement=node.elements();//所有一级子节点的list + for(Element e:listElement){//遍历所有一级子节点 + getNodes(e);//递归 + } + } + + public void readXMLModel(){ + Element root=readXmlRoot("F:\\Xlab\\testModel.XML"); + Map map=new HashMap(); + getNodes(root); + + + + } + + + public static void main(String[] args) { + JsonUtil exm=new JsonUtil(); + exm.readXMLModel(); + exm.readJson(); + } +} + + diff --git a/BackEnd19v/src/main/java/web/CsvController.java b/BackEnd19v/src/main/java/web/CsvController.java index fd2044ec7..17a7d3e73 100644 --- a/BackEnd19v/src/main/java/web/CsvController.java +++ b/BackEnd19v/src/main/java/web/CsvController.java @@ -1,6 +1,5 @@ package web; - import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; diff --git a/BackEnd19v/src/main/java/web/FileController.java b/BackEnd19v/src/main/java/web/FileController.java new file mode 100644 index 000000000..d0185ccc5 --- /dev/null +++ b/BackEnd19v/src/main/java/web/FileController.java @@ -0,0 +1,90 @@ +package web; + +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.multipart.MultipartHttpServletRequest; +import org.springframework.web.multipart.commons.CommonsMultipartResolver; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.File; +import java.io.IOException; +import java.util.*; + +@RestController +public class FileController { + + @RequestMapping(value = "/api/uploadTypeFile",method = RequestMethod.POST,produces = "application/json") + //上传系统的tpye文件 + public Map postType(HttpServletRequest request, HttpServletResponse response){ + String savePath = FileController.class.getResource("/").getPath().replace("classes","upload/type"); + Map res = new HashMap(); + try{ + if (springUpload(request, savePath)) { + res.put("succees",1); + } + }catch (Exception e) { + e.printStackTrace(); + res.put("succees", 0); + res.put("Reason",e.toString()); + } + return res; + } + + + + @RequestMapping(value = "/api/uploadSystemFile",method = RequestMethod.POST,produces = "application/json") + //上传系统的tpye文件 + public Map postSystem(HttpServletRequest request, HttpServletResponse response){ + String savePath = FileController.class.getResource("/").getPath().replace("classes","upload/system"); + Map res = new HashMap(); + try{ + if (springUpload(request, savePath)) { + res.put("succees",1); + } + }catch (Exception e) { + e.printStackTrace(); + res.put("succees", 0); + res.put("Reason",e.toString()); + } + return res; + } + + private boolean springUpload(HttpServletRequest request, String savePath) throws IllegalStateException, IOException + { + //将当前上下文初始化给 CommonsMutipartResolver (多部分解析器) + CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver( + request.getSession().getServletContext()); + //检查form中是否有enctype="multipart/form-data" + if(multipartResolver.isMultipart(request)) + { + //将request变成多部分request + MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request; + //获取multiRequest 中所有的文件名 + Iterator iter=multiRequest.getFileNames(); + + while(iter.hasNext()) + { + + //一次遍历所有文件 + MultipartFile file=multiRequest.getFile(iter.next().toString()); + if(file!=null) + { + String path = savePath + file.getOriginalFilename(); + System.out.println(path); + File filePath = new File(path); + //判断路径是否存在,如果不存在就创建一个 + if(!filePath.getParentFile().exists()){ + filePath.getParentFile().mkdir(); + } + //上传 + file.transferTo(filePath); + } + + } + + } + return true; + } + +} diff --git a/BackEnd19v/src/main/java/web/MongoDBController.java b/BackEnd19v/src/main/java/web/MongoDBController.java index 1ab673279..def86b24b 100644 --- a/BackEnd19v/src/main/java/web/MongoDBController.java +++ b/BackEnd19v/src/main/java/web/MongoDBController.java @@ -79,4 +79,9 @@ public Boolean addKapacitorEvent(@RequestBody String message){ return saveKapacitor2Mongo(message); } + @RequestMapping(value = "/api/addVariousEvent", method = RequestMethod.POST, produces = "application/json") + public Boolean addEvent(@RequestParam("Source") String source, @RequestParam("Content") String content){ + return saveEvent2Mongo(content, source); + } + } From 95f0b108419d51120a2ab3c947f1fbda034bec1c Mon Sep 17 00:00:00 2001 From: ChaunceyJ <33971563+ChaunceyJ@users.noreply.github.com> Date: Fri, 25 Oct 2019 09:49:41 +0800 Subject: [PATCH 02/11] change file update --- .../src/main/java/web/FileController.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/BackEnd19v/src/main/java/web/FileController.java b/BackEnd19v/src/main/java/web/FileController.java index d0185ccc5..1271bd59f 100644 --- a/BackEnd19v/src/main/java/web/FileController.java +++ b/BackEnd19v/src/main/java/web/FileController.java @@ -16,11 +16,11 @@ public class FileController { @RequestMapping(value = "/api/uploadTypeFile",method = RequestMethod.POST,produces = "application/json") //上传系统的tpye文件 - public Map postType(HttpServletRequest request, HttpServletResponse response){ + public Map postType(HttpServletRequest request, HttpServletResponse response, @RequestParam("name") String name){ String savePath = FileController.class.getResource("/").getPath().replace("classes","upload/type"); - Map res = new HashMap(); + Map res = new HashMap<>(); try{ - if (springUpload(request, savePath)) { + if (springUpload(request, savePath, name)) { res.put("succees",1); } }catch (Exception e) { @@ -35,11 +35,11 @@ public Map postType(HttpServletRequest request, HttpServletRespo @RequestMapping(value = "/api/uploadSystemFile",method = RequestMethod.POST,produces = "application/json") //上传系统的tpye文件 - public Map postSystem(HttpServletRequest request, HttpServletResponse response){ + public Map postSystem(HttpServletRequest request, HttpServletResponse response, @RequestParam("name") String name, @RequestParam("type") String type){ String savePath = FileController.class.getResource("/").getPath().replace("classes","upload/system"); - Map res = new HashMap(); + Map res = new HashMap<>(); try{ - if (springUpload(request, savePath)) { + if (springUpload(request, savePath, name)) { res.put("succees",1); } }catch (Exception e) { @@ -50,7 +50,7 @@ public Map postSystem(HttpServletRequest request, HttpServletRes return res; } - private boolean springUpload(HttpServletRequest request, String savePath) throws IllegalStateException, IOException + private boolean springUpload(HttpServletRequest request, String savePath, String fileName) throws IllegalStateException, IOException { //将当前上下文初始化给 CommonsMutipartResolver (多部分解析器) CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver( @@ -70,7 +70,8 @@ private boolean springUpload(HttpServletRequest request, String savePath) throws MultipartFile file=multiRequest.getFile(iter.next().toString()); if(file!=null) { - String path = savePath + file.getOriginalFilename(); + String oldName = file.getOriginalFilename(); + String path = savePath + fileName + oldName.substring(oldName.lastIndexOf(".")); System.out.println(path); File filePath = new File(path); //判断路径是否存在,如果不存在就创建一个 @@ -79,6 +80,7 @@ private boolean springUpload(HttpServletRequest request, String savePath) throws } //上传 file.transferTo(filePath); + } } From da9b083d9e33fdcb1862adddd0e91363f33f8914 Mon Sep 17 00:00:00 2001 From: ChaunceyJ <33971563+ChaunceyJ@users.noreply.github.com> Date: Fri, 25 Oct 2019 21:36:48 +0800 Subject: [PATCH 03/11] fix bug --- BackEnd19v/src/main/java/web/FileController.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/BackEnd19v/src/main/java/web/FileController.java b/BackEnd19v/src/main/java/web/FileController.java index 1271bd59f..ec4ec956a 100644 --- a/BackEnd19v/src/main/java/web/FileController.java +++ b/BackEnd19v/src/main/java/web/FileController.java @@ -73,13 +73,18 @@ private boolean springUpload(HttpServletRequest request, String savePath, String String oldName = file.getOriginalFilename(); String path = savePath + fileName + oldName.substring(oldName.lastIndexOf(".")); System.out.println(path); - File filePath = new File(path); + File folder = new File(savePath); + //文件夹路径不存在 + if (!folder.exists() && !folder.isDirectory()) { + folder.mkdirs(); + } + File newFile = new File(path); //判断路径是否存在,如果不存在就创建一个 - if(!filePath.getParentFile().exists()){ - filePath.getParentFile().mkdir(); + if(!newFile.exists()){ + newFile.mkdir(); } //上传 - file.transferTo(filePath); + file.transferTo(newFile); } From 6dd8eb1deaa22bb48d10d3226f89eb9a1b186c70 Mon Sep 17 00:00:00 2001 From: ChaunceyJ <33971563+ChaunceyJ@users.noreply.github.com> Date: Mon, 28 Oct 2019 19:55:55 +0800 Subject: [PATCH 04/11] getSystemTypeAndNameFile --- .../src/main/java/global/globalvalue.java | 2 +- .../src/main/java/neo4j/FusekiDriver.java | 2 +- .../src/main/java/neo4j/MongoDriver.java | 70 +++++++++++++++++++ .../src/main/java/neo4j/Neo4jDriver.java | 5 +- BackEnd19v/src/main/java/neo4j/Neo4jTest.java | 27 +++++++ .../src/main/java/service/MongoService.java | 42 +++++++++++ .../src/main/java/util/HttpPostUtil.java | 1 - .../src/main/java/web/FileController.java | 6 +- .../src/main/java/web/MongoDBController.java | 5 ++ 9 files changed, 154 insertions(+), 6 deletions(-) create mode 100644 BackEnd19v/src/main/java/neo4j/Neo4jTest.java diff --git a/BackEnd19v/src/main/java/global/globalvalue.java b/BackEnd19v/src/main/java/global/globalvalue.java index 889eb76dd..f468a9255 100644 --- a/BackEnd19v/src/main/java/global/globalvalue.java +++ b/BackEnd19v/src/main/java/global/globalvalue.java @@ -5,6 +5,6 @@ public class globalvalue { public static String fusekiapi = "http://fuseki"; //public static String fusekiapi = "http://10.60.38.173"; public static String mongoapi = "mongo"; - //public static String fusekiapi = "10.60.38.173"; + //public static String mongoapi = "10.60.38.173"; public static String causeapi = "http://causeapi"; } \ No newline at end of file diff --git a/BackEnd19v/src/main/java/neo4j/FusekiDriver.java b/BackEnd19v/src/main/java/neo4j/FusekiDriver.java index f7f6229a5..26c172bb7 100755 --- a/BackEnd19v/src/main/java/neo4j/FusekiDriver.java +++ b/BackEnd19v/src/main/java/neo4j/FusekiDriver.java @@ -56,7 +56,7 @@ public static Map getAllNodesAndLinks(){ QuerySolution qs = rs.next() ; String subject = qs.get("s").toString(); if(subject.contains("http")){ - System.out.println("Subject: " + subject); + //System.out.println("Subject: " + subject); if(subject.contains("server")){ result.add(getServer(subject)); linkList.addAll(getLink(subject, "manage")); diff --git a/BackEnd19v/src/main/java/neo4j/MongoDriver.java b/BackEnd19v/src/main/java/neo4j/MongoDriver.java index 47578f714..fe9773f51 100644 --- a/BackEnd19v/src/main/java/neo4j/MongoDriver.java +++ b/BackEnd19v/src/main/java/neo4j/MongoDriver.java @@ -2,6 +2,7 @@ import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.mongodb.BasicDBObject; import com.mongodb.MongoClient; import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; @@ -301,4 +302,73 @@ public static List getTimesFromMongo(){ return result; } + public static boolean saveSystemTypeAndNameFile(String type, String name){ + try { + //连接到mongodb服务 + MongoClient mongoClient = new MongoClient(globalvalue.mongoapi, 27017); + //MongoClient mongoClient = new MongoClient("10.60.38.173", 27020); + //连接到数据库 + MongoDatabase mongoDatabase = mongoClient.getDatabase("knowledgegraph"); + MongoCollection collection = mongoDatabase.getCollection("SystemTypeAndName"); + //判断是否当前系统类型是否存在 + BasicDBObject query = new BasicDBObject(); + query.put("type",type); + FindIterable findIterable = collection.find(query); + MongoCursor mongoCursor = findIterable.iterator(); + if (!mongoCursor.hasNext()){ + Map data = new HashMap<>(); + data.put("type",type); + data.put("name",new ArrayList()); + //插入文档 + Document document = new Document(data); + collection.insertOne(document); + System.out.println("文档插入成功"); + } + else { + Document d=mongoCursor.next(); + //System.out.println(d); + JSONObject json=JSONObject.parseObject(d.toJson()); + JSONArray jsonArray = json.getJSONArray("name"); + jsonArray.add(name); +// DBCollection dbCol = db.getCollection(COLLECTION_NAME); +// DBCursor ret = dbCol.find(); + BasicDBObject doc = new BasicDBObject(); + BasicDBObject res = new BasicDBObject(); + res.put("name", JSONObject.parseArray(jsonArray.toString(), String.class)); +// System.out.println("将数据集中的所有文档的age修改成40!"); + doc.put("$set", res); + collection.findOneAndUpdate(query,doc); + System.out.println("文档修改成功"); + } + } catch (Exception e){ + e.printStackTrace(); + return false; + } + return true; + } + + public static JSONArray getAllTypeAndName(){ + JSONArray re = new JSONArray(); + try { + //连接到mongodb服务 + //MongoClient mongoClient = new MongoClient(globalvalue.mongoapi, 27017); + MongoClient mongoClient = new MongoClient("10.60.38.173", 27020); + //连接到数据库 + MongoDatabase mongoDatabase = mongoClient.getDatabase("knowledgegraph"); + MongoCollection collection = mongoDatabase.getCollection("SystemTypeAndName"); + FindIterable findIterable = collection.find(); + MongoCursor mongoCursor = findIterable.iterator(); + while (mongoCursor.hasNext()){ + Document d=mongoCursor.next(); + re.add(JSONObject.parseObject(d.toJson())); + } + } catch (Exception e){ + e.printStackTrace(); + } + return re; + } + + public static void main(String[] args) { + saveSystemTypeAndNameFile("1","2"); + } } diff --git a/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java b/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java index e9d53848f..91da60a9c 100755 --- a/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java +++ b/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java @@ -105,7 +105,7 @@ public HashMap>> getOneNoderesult(String nam return resultgraph; } public HashMap>> getAllNodes() { - Driver driver = GraphDatabase.driver("bolt://10.60.38.173:7687", + Driver driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j","1234")); Map map = new HashMap<>(); HashMap>> resultgraph = new HashMap<>(); @@ -142,6 +142,7 @@ public HashMap>> getAllNodes() { } } driver.close(); + System.out.println(resultgraph); return resultgraph; } public HashMap> getAllLabel(){ @@ -1126,6 +1127,6 @@ public static HashMap getElement(){ public static void main(String[] args) { - + new Neo4jDriver().getAllNodes(); } } diff --git a/BackEnd19v/src/main/java/neo4j/Neo4jTest.java b/BackEnd19v/src/main/java/neo4j/Neo4jTest.java new file mode 100644 index 000000000..b535d4209 --- /dev/null +++ b/BackEnd19v/src/main/java/neo4j/Neo4jTest.java @@ -0,0 +1,27 @@ +package neo4j; + +import org.neo4j.driver.v1.*; + +import static org.neo4j.driver.v1.Values.parameters; + +public class Neo4jTest { + public static void main(String[] args) { + Driver driver = GraphDatabase.driver( "bolt://localhost:7687", AuthTokens.basic( "neo4j", "1234" ) ); + Session session = driver.session(); + + session.run( "CREATE (a:Person {name: {name}, title: {title}})", + parameters( "name", "Arthur", "title", "King" ) ); + + StatementResult result = session.run( "MATCH (a:Person) WHERE a.name = {name} " + + "RETURN a.name AS name, a.title AS title", + parameters( "name", "Arthur" ) ); + while ( result.hasNext() ) + { + Record record = result.next(); + System.out.println( record.get( "title" ).asString() + " " + record.get( "name" ).asString() ); + } + + session.close(); + driver.close(); + } +} diff --git a/BackEnd19v/src/main/java/service/MongoService.java b/BackEnd19v/src/main/java/service/MongoService.java index 3097f0cbe..f6f7a723f 100644 --- a/BackEnd19v/src/main/java/service/MongoService.java +++ b/BackEnd19v/src/main/java/service/MongoService.java @@ -1,8 +1,15 @@ package service; +import com.alibaba.fastjson.*; import neo4j.MongoDriver; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import static neo4j.FusekiDriver.*; +import static neo4j.MongoDriver.getAllTypeAndName; public class MongoService { public Boolean storeAllService(String masterName, String podName, String serviceName, String address, String namespace){ @@ -20,4 +27,39 @@ public Boolean storeAllService(String masterName, String podName, String servic result &= podToServer(address, namespace); return result; } + + public static Map jsonarray2Map(){ + Map re = new HashMap(); + ArrayList opts = new ArrayList(); + ArrayList types = new ArrayList(); + JSONArray jsonArray = getAllTypeAndName(); + for (int i = 0; i < jsonArray.size(); i++) { + String typename = jsonArray.getJSONObject(i).getString("type"); + List list = JSONObject.parseArray(jsonArray.getJSONObject(i).getString("name"), String.class); + List children = new ArrayList(); + for (String j:list + ) { + HashMap child = new HashMap<>(); + child.put("value",j); + child.put("label",j); + children.add(child); + } + HashMap op = new HashMap(); + op.put("value",typename); + op.put("label",typename); + op.put("children",children); + opts.add(op); + HashMap type = new HashMap(); + type.put("value",typename); + type.put("label",typename); + types.add(type); + } + re.put("options",opts); + re.put("types",types); + return re; + } + + public static void main(String[] args) { + System.out.println(jsonarray2Map()); + } } diff --git a/BackEnd19v/src/main/java/util/HttpPostUtil.java b/BackEnd19v/src/main/java/util/HttpPostUtil.java index 47712129c..886d7ecce 100644 --- a/BackEnd19v/src/main/java/util/HttpPostUtil.java +++ b/BackEnd19v/src/main/java/util/HttpPostUtil.java @@ -35,6 +35,5 @@ public static String postData(String con1) { } public static void main(String[] args) { - } } \ No newline at end of file diff --git a/BackEnd19v/src/main/java/web/FileController.java b/BackEnd19v/src/main/java/web/FileController.java index ec4ec956a..6cf695931 100644 --- a/BackEnd19v/src/main/java/web/FileController.java +++ b/BackEnd19v/src/main/java/web/FileController.java @@ -11,6 +11,8 @@ import java.io.IOException; import java.util.*; +import static neo4j.MongoDriver.saveSystemTypeAndNameFile; + @RestController public class FileController { @@ -22,6 +24,7 @@ public Map postType(HttpServletRequest request, HttpServletRespo try{ if (springUpload(request, savePath, name)) { res.put("succees",1); + saveSystemTypeAndNameFile(name,""); } }catch (Exception e) { e.printStackTrace(); @@ -34,13 +37,14 @@ public Map postType(HttpServletRequest request, HttpServletRespo @RequestMapping(value = "/api/uploadSystemFile",method = RequestMethod.POST,produces = "application/json") - //上传系统的tpye文件 + //上传系统的system文件 public Map postSystem(HttpServletRequest request, HttpServletResponse response, @RequestParam("name") String name, @RequestParam("type") String type){ String savePath = FileController.class.getResource("/").getPath().replace("classes","upload/system"); Map res = new HashMap<>(); try{ if (springUpload(request, savePath, name)) { res.put("succees",1); + saveSystemTypeAndNameFile(type,name); } }catch (Exception e) { e.printStackTrace(); diff --git a/BackEnd19v/src/main/java/web/MongoDBController.java b/BackEnd19v/src/main/java/web/MongoDBController.java index def86b24b..72e6acd44 100644 --- a/BackEnd19v/src/main/java/web/MongoDBController.java +++ b/BackEnd19v/src/main/java/web/MongoDBController.java @@ -84,4 +84,9 @@ public Boolean addEvent(@RequestParam("Source") String source, @RequestParam("Co return saveEvent2Mongo(content, source); } + @RequestMapping(value = "/api/getSystemTypeAndNameFile",method = RequestMethod.GET,produces = "application/json") + public Map getAllSystemTypeAndNameFile(){ + return mongoService.jsonarray2Map(); + } + } From 08479af63ee6d299ae55fe8c66990ffa3a908e05 Mon Sep 17 00:00:00 2001 From: ChaunceyJ <33971563+ChaunceyJ@users.noreply.github.com> Date: Mon, 28 Oct 2019 21:41:06 +0800 Subject: [PATCH 05/11] fix bugs --- BackEnd19v/src/main/java/global/globalvalue.java | 4 ++-- BackEnd19v/src/main/java/neo4j/FusekiDriver.java | 4 ++-- BackEnd19v/src/main/java/web/FileController.java | 7 +++++-- BackEnd19v/src/main/java/web/MongoDBController.java | 2 +- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/BackEnd19v/src/main/java/global/globalvalue.java b/BackEnd19v/src/main/java/global/globalvalue.java index f468a9255..86783b351 100644 --- a/BackEnd19v/src/main/java/global/globalvalue.java +++ b/BackEnd19v/src/main/java/global/globalvalue.java @@ -2,8 +2,8 @@ public class globalvalue { - public static String fusekiapi = "http://fuseki"; - //public static String fusekiapi = "http://10.60.38.173"; + //public static String fusekiapi = "http://fuseki"; + public static String fusekiapi = "http://10.60.38.173"; public static String mongoapi = "mongo"; //public static String mongoapi = "10.60.38.173"; public static String causeapi = "http://causeapi"; diff --git a/BackEnd19v/src/main/java/neo4j/FusekiDriver.java b/BackEnd19v/src/main/java/neo4j/FusekiDriver.java index 26c172bb7..8c862583e 100755 --- a/BackEnd19v/src/main/java/neo4j/FusekiDriver.java +++ b/BackEnd19v/src/main/java/neo4j/FusekiDriver.java @@ -1691,7 +1691,7 @@ public static void addLinkEvent2S(){ Date start = new Date(); Date end = new Date(); try { - start = DateFormat.parse("2019-09-25 00:00:00"); + start = DateFormat.parse("2019-10-25 00:00:00"); end = DateFormat.parse("2019-09-25 23:59:59"); } catch(ParseException px) { px.printStackTrace(); @@ -1839,6 +1839,6 @@ public static boolean addCorrelation(String fromUrl, String toUrl, String influe public static void main(String[] args) { - // addLinkEvent2S(); + addLinkEvent2S(); } } diff --git a/BackEnd19v/src/main/java/web/FileController.java b/BackEnd19v/src/main/java/web/FileController.java index 6cf695931..40ba91a98 100644 --- a/BackEnd19v/src/main/java/web/FileController.java +++ b/BackEnd19v/src/main/java/web/FileController.java @@ -31,7 +31,9 @@ public Map postType(HttpServletRequest request, HttpServletRespo res.put("succees", 0); res.put("Reason",e.toString()); } - return res; + System.out.println(res); + return service.MongoService.jsonarray2Map(); + } @@ -51,7 +53,8 @@ public Map postSystem(HttpServletRequest request, HttpServletRes res.put("succees", 0); res.put("Reason",e.toString()); } - return res; + System.out.println(res); + return service.MongoService.jsonarray2Map(); } private boolean springUpload(HttpServletRequest request, String savePath, String fileName) throws IllegalStateException, IOException diff --git a/BackEnd19v/src/main/java/web/MongoDBController.java b/BackEnd19v/src/main/java/web/MongoDBController.java index 72e6acd44..25b4db73f 100644 --- a/BackEnd19v/src/main/java/web/MongoDBController.java +++ b/BackEnd19v/src/main/java/web/MongoDBController.java @@ -86,7 +86,7 @@ public Boolean addEvent(@RequestParam("Source") String source, @RequestParam("Co @RequestMapping(value = "/api/getSystemTypeAndNameFile",method = RequestMethod.GET,produces = "application/json") public Map getAllSystemTypeAndNameFile(){ - return mongoService.jsonarray2Map(); + return MongoService.jsonarray2Map(); } } From bb2ff54a1e57d1774c55724ee88e90e764c863d2 Mon Sep 17 00:00:00 2001 From: ChaunceyJ <33971563+ChaunceyJ@users.noreply.github.com> Date: Mon, 28 Oct 2019 21:53:40 +0800 Subject: [PATCH 06/11] fix bugsss --- BackEnd19v/src/main/java/web/FileController.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/BackEnd19v/src/main/java/web/FileController.java b/BackEnd19v/src/main/java/web/FileController.java index 40ba91a98..a3208c132 100644 --- a/BackEnd19v/src/main/java/web/FileController.java +++ b/BackEnd19v/src/main/java/web/FileController.java @@ -31,9 +31,7 @@ public Map postType(HttpServletRequest request, HttpServletRespo res.put("succees", 0); res.put("Reason",e.toString()); } - System.out.println(res); - return service.MongoService.jsonarray2Map(); - + return res; } @@ -53,8 +51,8 @@ public Map postSystem(HttpServletRequest request, HttpServletRes res.put("succees", 0); res.put("Reason",e.toString()); } - System.out.println(res); - return service.MongoService.jsonarray2Map(); + return res; + } private boolean springUpload(HttpServletRequest request, String savePath, String fileName) throws IllegalStateException, IOException From 49f949dd0c103a4ed0fbccb67f8926e2a9e7a52e Mon Sep 17 00:00:00 2001 From: ChaunceyJ <33971563+ChaunceyJ@users.noreply.github.com> Date: Sat, 9 Nov 2019 12:01:21 +0800 Subject: [PATCH 07/11] get nnode and links --- .../src/main/java/global/globalvalue.java | 1 + .../src/main/java/neo4j/FusekiDriver.java | 39 ++-- .../src/main/java/neo4j/MongoDriver.java | 4 +- .../src/main/java/neo4j/Neo4jDriver.java | 77 ++++++- BackEnd19v/src/main/java/neo4j/Neo4jTest.java | 27 --- BackEnd19v/src/main/java/util/TurtleUtil.java | 204 ++++++++++++++++++ .../src/main/java/web/FileController.java | 7 +- .../src/main/java/web/Neo4jController.java | 6 + 8 files changed, 315 insertions(+), 50 deletions(-) delete mode 100644 BackEnd19v/src/main/java/neo4j/Neo4jTest.java create mode 100644 BackEnd19v/src/main/java/util/TurtleUtil.java diff --git a/BackEnd19v/src/main/java/global/globalvalue.java b/BackEnd19v/src/main/java/global/globalvalue.java index 86783b351..5b8046908 100644 --- a/BackEnd19v/src/main/java/global/globalvalue.java +++ b/BackEnd19v/src/main/java/global/globalvalue.java @@ -7,4 +7,5 @@ public class globalvalue { public static String mongoapi = "mongo"; //public static String mongoapi = "10.60.38.173"; public static String causeapi = "http://causeapi"; + public static String neo4japi = "bolt:://10.60.38.173"; } \ No newline at end of file diff --git a/BackEnd19v/src/main/java/neo4j/FusekiDriver.java b/BackEnd19v/src/main/java/neo4j/FusekiDriver.java index 8c862583e..2e6ce2c79 100755 --- a/BackEnd19v/src/main/java/neo4j/FusekiDriver.java +++ b/BackEnd19v/src/main/java/neo4j/FusekiDriver.java @@ -28,6 +28,8 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; +import java.util.regex.Pattern; + import global.globalvalue; import static neo4j.MongoDriver.*; @@ -1691,8 +1693,8 @@ public static void addLinkEvent2S(){ Date start = new Date(); Date end = new Date(); try { - start = DateFormat.parse("2019-10-25 00:00:00"); - end = DateFormat.parse("2019-09-25 23:59:59"); + start = DateFormat.parse("2019-10-20 00:00:00"); + end = DateFormat.parse("2019-10-20 23:59:59"); } catch(ParseException px) { px.printStackTrace(); } @@ -1732,36 +1734,35 @@ public static void addLinkEvent2S(){ System.out.println(strings[strings.length-1]); System.out.println(timeList); System.out.println(proInfor); -// stringBuffer.append(strings[strings.length-1]); + stringBuffer.append(strings[strings.length-1]); JSONObject jsonObject = new JSONObject(); jsonObject.put("file1",timeList); jsonObject.put("file2", proInfor); -// stringBuffer.append("\r\n"); -// stringBuffer.append(jsonObject.toString()); -// stringBuffer.append("\r\n"); - String re = util.HttpPostUtil.postData(jsonObject.toJSONString()); - System.out.println(re); - if (re != null){ - for (String ev:(ArrayList)dates.get("Event") - ) { - addCorrelation(ev, i.toString(), re); - } - } + stringBuffer.append("\r\n"); + stringBuffer.append(jsonObject.toString()); + stringBuffer.append("\r\n"); +// String re = util.HttpPostUtil.postData(jsonObject.toJSONString()); +// System.out.println(re); +// if (re != null){ +// for (String ev:(ArrayList)dates.get("Event") +// ) { +// addCorrelation(ev, i.toString(), re); +// } +// } } -/* try { + try { FileOutputStream fos = new FileOutputStream("/Users/jiang/data.txt"); fos.write(stringBuffer.toString().getBytes()); } catch (Exception e) { System.out.println(e.getMessage()); - }*/ - + } } - //查询指定范围内发生的时间 + //查询指定范围内发生的时间 只有 HW 事件 public static Map getEventInFuseki(Date startTime,Date endTime) { SimpleDateFormat DateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //加上时间 @@ -1774,7 +1775,7 @@ public static Map getEventInFuseki(Date startTime,Date endTim ResIterator iter = model.listSubjects(); while (iter.hasNext()) { Resource r = iter.nextResource(); - if (r.toString().contains("event")) + if (Pattern.matches(".*event.*HW.*", r.toString())) { Statement statement = r.getProperty(model.createProperty(r.toString()+"/starts_at")); if (statement == null) continue; diff --git a/BackEnd19v/src/main/java/neo4j/MongoDriver.java b/BackEnd19v/src/main/java/neo4j/MongoDriver.java index fe9773f51..c0e3f634a 100644 --- a/BackEnd19v/src/main/java/neo4j/MongoDriver.java +++ b/BackEnd19v/src/main/java/neo4j/MongoDriver.java @@ -305,8 +305,8 @@ public static List getTimesFromMongo(){ public static boolean saveSystemTypeAndNameFile(String type, String name){ try { //连接到mongodb服务 - MongoClient mongoClient = new MongoClient(globalvalue.mongoapi, 27017); - //MongoClient mongoClient = new MongoClient("10.60.38.173", 27020); + //MongoClient mongoClient = new MongoClient(globalvalue.mongoapi, 27017); + MongoClient mongoClient = new MongoClient("10.60.38.173", 27020); //连接到数据库 MongoDatabase mongoDatabase = mongoClient.getDatabase("knowledgegraph"); MongoCollection collection = mongoDatabase.getCollection("SystemTypeAndName"); diff --git a/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java b/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java index 91da60a9c..34d101215 100755 --- a/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java +++ b/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java @@ -15,6 +15,7 @@ import java.io.*; import java.util.*; +import static global.globalvalue.neo4japi; import static org.neo4j.driver.v1.Values.parameters; import static service.CsvService.csvTimestamp; @@ -1125,8 +1126,82 @@ public static HashMap getElement(){ } + //第一次适用需要运行 CREATE INDEX ON :Resource(uri) + public static void importTtl(String typePath, String systemPath) { + Driver driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic( "neo4j", "1234" )); + //初始化驱动器 + try (Session session = driver.session()) { + try (Transaction tx = session.beginTransaction()) { + String namespace="CREATE (:NamespacePrefixDefinition " + + "{`http://localhost/KGns/#`:''," + + "`http://www.w3.org/1999/02/22-rdf-syntax-ns#`:'rdf'," + + "`http://www.w3.org/2000/01/rdf-schema#`:'rdfs'," + + "`http://xmlns.com/foaf/0.1/`:'foaf'," + + "`http://localhost/KGns/relationship#`:'rel'," + + "`http://localhost/KGns/Container_attributes#`:'Container_attributes'," + + "`http://localhost/KGns/Service_attributes#`:'Service_attributes'})"; + String ontology="CALL semantics.importRDF(\"file://" + typePath + "\", \"Turtle\")"; + String system="CALL semantics.importRDF(\"file://" + systemPath + "\", \"Turtle\")"; + System.out.println(ontology); + System.out.println(system); + + StatementResult result1 = tx.run(namespace); + StatementResult result2 = tx.run(ontology); + StatementResult result3 = tx.run(system); + System.out.println(result2.toString()); + tx.success(); + } + } + driver.close(); + } + + public static HashMap>> getAllNodesandlinks() { + Driver driver = GraphDatabase.driver(neo4japi+":7687", + AuthTokens.basic("neo4j","1234")); + HashMap>> resultgraph = new HashMap<>(); + try(Session session = driver.session()) { + try (Transaction tx = session.beginTransaction()) { + StatementResult result = tx.run("Match p=(n)-[r]-(m) return p as nodesrelation"); + List> allnodes = new ArrayList<>(); + List> allrelations = new ArrayList<>(); + while(result.hasNext()){ + Record record = result.next(); + Path path = record.get("nodesrelation").asPath(); + Iterable nodes = path.nodes(); + for(Node node:nodes) { + HashMap nod = new HashMap(); + nod.put("id",node.id()); + nod.put("properties", node.asMap()); + String[] name = node.asMap().get("uri").toString().split("#"); + nod.put("name", name[name.length-1]); + nod.put("type",node.asMap().get("type")); + if(!allnodes.contains(nod)) + allnodes.add(nod); + } + Iterable relations = path.relationships(); + for(Relationship relationship:relations) { + HashMap rela = new HashMap(); + rela.put("sid",relationship.startNodeId()); + rela.put("tid",relationship.endNodeId()); + rela.put("type",relationship.type()); + rela.put("name",relationship.type()); + allrelations.add(rela); + } + } + resultgraph.put("nodes",allnodes); + resultgraph.put("links",allrelations); + } + } + driver.close(); + System.out.println(resultgraph); + return resultgraph; + } + public static void main(String[] args) { - new Neo4jDriver().getAllNodes(); + + new Neo4jDriver().getAllNodesandlinks(); + //importTtl("/Users/jiang/Operation_KnowledgeGraph/BackEnd19v/target/turtle/type/ontology.ttl","/Users/jiang/Operation_KnowledgeGraph/BackEnd19v/target/turtle/system/system.ttl"); + } } diff --git a/BackEnd19v/src/main/java/neo4j/Neo4jTest.java b/BackEnd19v/src/main/java/neo4j/Neo4jTest.java deleted file mode 100644 index b535d4209..000000000 --- a/BackEnd19v/src/main/java/neo4j/Neo4jTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package neo4j; - -import org.neo4j.driver.v1.*; - -import static org.neo4j.driver.v1.Values.parameters; - -public class Neo4jTest { - public static void main(String[] args) { - Driver driver = GraphDatabase.driver( "bolt://localhost:7687", AuthTokens.basic( "neo4j", "1234" ) ); - Session session = driver.session(); - - session.run( "CREATE (a:Person {name: {name}, title: {title}})", - parameters( "name", "Arthur", "title", "King" ) ); - - StatementResult result = session.run( "MATCH (a:Person) WHERE a.name = {name} " + - "RETURN a.name AS name, a.title AS title", - parameters( "name", "Arthur" ) ); - while ( result.hasNext() ) - { - Record record = result.next(); - System.out.println( record.get( "title" ).asString() + " " + record.get( "name" ).asString() ); - } - - session.close(); - driver.close(); - } -} diff --git a/BackEnd19v/src/main/java/util/TurtleUtil.java b/BackEnd19v/src/main/java/util/TurtleUtil.java new file mode 100644 index 000000000..0a5ce4856 --- /dev/null +++ b/BackEnd19v/src/main/java/util/TurtleUtil.java @@ -0,0 +1,204 @@ +package util; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; + +import org.apache.commons.io.FileUtils; + +import org.dom4j.Document; +import org.dom4j.DocumentHelper; + +import javax.swing.*; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.security.KeyStore; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class TurtleUtil { + public static void readOntologyJson(String fileName) throws IOException { + File fileOntology = new File(TurtleUtil.class.getResource("/").getPath().replace("classes","upload/type")+fileName+".json"); + String ontologyData = FileUtils.readFileToString(fileOntology); + JSONObject ontology=JSON.parseObject(ontologyData); + String savePath = TurtleUtil.class.getResource("/").getPath().replace("classes","turtle/type"); + File folder = new File(savePath); + //文件夹路径不存在 + if (!folder.exists() && !folder.isDirectory()) { + folder.mkdirs(); + } + File ontologyTtlFile=new File(savePath + fileName + ".ttl"); + if(!ontologyTtlFile.exists()){ + ontologyTtlFile.createNewFile(); + } + FileWriter ontologyTtlWriter =new FileWriter(ontologyTtlFile); + //命名空间声明 + ontologyTtlWriter.write("@prefix : .\n" + + "@prefix rdf: .\n" + + "@prefix rdfs: .\n" + + "@prefix foaf: .\n" + + "@prefix owl: .\n" + + "@prefix rel: .\n" + + "@prefix Container_attributes: .\n" + + "@prefix Service_attributes: .\n\n"); + ontologyTtlWriter.flush(); + ontologyJsonToTurtle(ontology,ontologyTtlWriter); + + } + + public static void ontologyJsonToTurtle(JSONObject object,FileWriter writer) throws IOException { + JSONArray types = object.getJSONArray("types"); + ListattributeList=new ArrayList<>(); + for (int i = 0; i < types.size(); i++) { + String type = types.getString(i); + writer.write(":" + type + " rdf:type owl:Class .\n"); + writer.flush(); + } + writer.write("\n"); + writer.flush(); + JSONArray relInfo = object.getJSONArray("rel_info"); + for (int i = 0; i < relInfo.size(); i++) { + JSONObject rel = relInfo.getJSONObject(i); + String relType = rel.getString("type"); + String domain=relType+"_attributes"; + JSONArray linkNodes = rel.getJSONArray("link_to_other_nodes"); + for (int j = 0; j < linkNodes.size(); j++) { + JSONObject obj = linkNodes.getJSONObject(j); + String relation = obj.getString("relation"); + String to = obj.getString("to"); + writer.write("rel:" + relation + " rdf:type owl:ObjectProperty;\n\t\t"); + writer.flush(); + writer.write("rdfs:domain :" + relType + ";\n\t\t"); + writer.flush(); + writer.write("rdfs:range :" + to + " .\n\n"); + } + JSONArray attributes = rel.getJSONArray("attributes"); + for (int k = 0; k < attributes.size(); k++) { + String attribute=attributes.getString(k); + attributeList.add(domain+"|"+attribute+"|"+relType); + } + } + for (String s:attributeList){ + String[]input=s.split("\\|"); + writer.write(input[0]+":"+input[1]+" rdf:type owl:ObjectProperty;\n\t\trdfs:domain :"+input[2]+" .\n\n"); + writer.flush(); + } + } + + public static void readSystemJson(String fileName) throws IOException { + File fileSystem = new File(TurtleUtil.class.getResource("/").getPath().replace("classes","upload/system")+fileName+".json"); + String systemData = FileUtils.readFileToString(fileSystem); + JSONObject system=JSON.parseObject(systemData); + String savePath = TurtleUtil.class.getResource("/").getPath().replace("classes","turtle/system"); + File folder = new File(savePath); + //文件夹路径不存在 + if (!folder.exists() && !folder.isDirectory()) { + folder.mkdirs(); + } + File systemTtlFile=new File(savePath + fileName + ".ttl"); + if(!systemTtlFile.exists()){ + systemTtlFile.createNewFile(); + } + FileWriter systemTtlWriter =new FileWriter(systemTtlFile); + //命名空间声明 + systemTtlWriter.write("@prefix : .\n" + + "@prefix rdf: .\n" + + "@prefix rdfs: .\n" + + "@prefix foaf: .\n" + + "@prefix rel: .\n" + + "@prefix Container_attributes: .\n" + + "@prefix Service_attributes: .\n" + + "@prefix owl: .\n\n"); + systemTtlWriter.flush(); + systemJsonToTurtle(system,systemTtlWriter); + } + + public static void systemJsonToTurtle(JSONObject object,FileWriter writer) throws IOException { + List instanceTypes = new ArrayList<>(); + HashMap instanceNames = new HashMap<>(); + //写入 处理第一部分 + for (String instanceType : object.keySet()) { + instanceTypes.add(instanceType); + JSONArray instances = object.getJSONArray(instanceType); + for (int i = 0; i < instances.size(); i++) { + JSONObject instance = instances.getJSONObject(i); + //节点名字 + String name = instance.getString("name"); + instanceNames.put(name, instanceType); + } + } + for (int i = 0; i < instanceTypes.size(); i++) { + writer.write(":" + instanceTypes.get(i) + " rdf:type owl:Class .\n"); + writer.flush(); + } + for (String key : instanceNames.keySet()) { + String value = instanceNames.get(key); + writer.write(":" + key + " rdf:type :" + value + " .\n"); + writer.flush(); + } + writer.write("\n"); + writer.flush(); + for (String instanceType : object.keySet()) { + JSONArray instances = object.getJSONArray(instanceType); + for (int i = 0; i < instances.size(); i++) { + JSONObject instance = instances.getJSONObject(i); + //节点名字 + String name = instance.getString("name"); + writer.write("<#" + name + ">\n\t\t"); + writer.flush(); + //属性 + JSONObject attributes=instance.getJSONObject("attributes"); + for(String key:attributes.keySet()){ + String attribute=attributes.getString(key); + writer.write(instanceType+"_attributes:"+key+" "+attribute+";\n\t\t"); + writer.flush(); + } + JSONObject relations=instance.getJSONObject("relations"); + for (String k : relations.keySet()) { + List keyList=new ArrayList(relations.keySet()); + String lastKey=keyList.get(keyList.size()-1).toString(); + Object v = relations.get(k); + if (v instanceof JSONObject) { + for (String key :((JSONObject)v).keySet()){ + JSONArray array=((JSONObject)v).getJSONArray(key); + for(int j=0;j postType(HttpServletRequest request, HttpServletRespo try{ if (springUpload(request, savePath, name)) { res.put("succees",1); + readOntologyJson(name); saveSystemTypeAndNameFile(name,""); } }catch (Exception e) { @@ -44,6 +47,7 @@ public Map postSystem(HttpServletRequest request, HttpServletRes try{ if (springUpload(request, savePath, name)) { res.put("succees",1); + readSystemJson(name); saveSystemTypeAndNameFile(type,name); } }catch (Exception e) { @@ -51,8 +55,9 @@ public Map postSystem(HttpServletRequest request, HttpServletRes res.put("succees", 0); res.put("Reason",e.toString()); } + importTtl(FileController.class.getResource("/").getPath().replace("classes","turtle/type")+type+".ttl", + FileController.class.getResource("/").getPath().replace("classes","turtle/system")+name+".ttl"); return res; - } private boolean springUpload(HttpServletRequest request, String savePath, String fileName) throws IllegalStateException, IOException diff --git a/BackEnd19v/src/main/java/web/Neo4jController.java b/BackEnd19v/src/main/java/web/Neo4jController.java index 2ce848bd2..f64dc7539 100644 --- a/BackEnd19v/src/main/java/web/Neo4jController.java +++ b/BackEnd19v/src/main/java/web/Neo4jController.java @@ -161,6 +161,12 @@ public HashMap getElementName(HttpServletRequest request, Htt return getElement(); } + //获取结点、连接 + @RequestMapping(value = "/api/getSystemNodesAndLinks",method = RequestMethod.GET,produces = "application/json") + public Map getNodeAndLink(){ + return getAllNodesandlinks(); + } + //以下为Fuseki部分 @RequestMapping(value = "/api/getNodesAndLinks",method = RequestMethod.GET,produces = "application/json") From f59ff0efb3ee50f3f02d90774a50c59e4e37a79f Mon Sep 17 00:00:00 2001 From: ChaunceyJ <33971563+ChaunceyJ@users.noreply.github.com> Date: Mon, 11 Nov 2019 00:25:08 +0800 Subject: [PATCH 08/11] fix bug --- BackEnd19v/src/main/java/global/globalvalue.java | 4 +++- BackEnd19v/src/main/java/neo4j/Neo4jDriver.java | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/BackEnd19v/src/main/java/global/globalvalue.java b/BackEnd19v/src/main/java/global/globalvalue.java index 5b8046908..6bfc5196a 100644 --- a/BackEnd19v/src/main/java/global/globalvalue.java +++ b/BackEnd19v/src/main/java/global/globalvalue.java @@ -7,5 +7,7 @@ public class globalvalue { public static String mongoapi = "mongo"; //public static String mongoapi = "10.60.38.173"; public static String causeapi = "http://causeapi"; - public static String neo4japi = "bolt:://10.60.38.173"; + //public static String neo4japi = "bolt://10.60.38.173"; + public static String neo4japi = "bolt://localhost"; + } \ No newline at end of file diff --git a/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java b/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java index 34d101215..85865f021 100755 --- a/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java +++ b/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java @@ -1174,7 +1174,13 @@ public static HashMap>> getAllNodesandlinks( nod.put("properties", node.asMap()); String[] name = node.asMap().get("uri").toString().split("#"); nod.put("name", name[name.length-1]); - nod.put("type",node.asMap().get("type")); + Iterator iterator = node.labels().iterator(); + iterator.next(); + if (iterator.hasNext()){ + nod.put("type",iterator.next().toString()); + } else { + nod.put("type", "null"); + } if(!allnodes.contains(nod)) allnodes.add(nod); } From 3f44e9139cbc20fa6298197ad939233897ff1a79 Mon Sep 17 00:00:00 2001 From: ChaunceyJ <33971563+ChaunceyJ@users.noreply.github.com> Date: Mon, 11 Nov 2019 11:37:11 +0800 Subject: [PATCH 09/11] fix bugs --- .../src/main/java/neo4j/FusekiDriver.java | 32 ++++++++++--------- .../src/main/java/neo4j/MongoDriver.java | 10 +++++- .../src/main/java/neo4j/Neo4jDriver.java | 4 ++- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/BackEnd19v/src/main/java/neo4j/FusekiDriver.java b/BackEnd19v/src/main/java/neo4j/FusekiDriver.java index 2e6ce2c79..2f836e5ee 100755 --- a/BackEnd19v/src/main/java/neo4j/FusekiDriver.java +++ b/BackEnd19v/src/main/java/neo4j/FusekiDriver.java @@ -1586,6 +1586,7 @@ public static boolean judgeExist(String url){ return false; } } + qExec.close(); } return true; } @@ -1741,22 +1742,23 @@ public static void addLinkEvent2S(){ stringBuffer.append("\r\n"); stringBuffer.append(jsonObject.toString()); stringBuffer.append("\r\n"); -// String re = util.HttpPostUtil.postData(jsonObject.toJSONString()); -// System.out.println(re); -// if (re != null){ -// for (String ev:(ArrayList)dates.get("Event") -// ) { -// addCorrelation(ev, i.toString(), re); -// } -// } - } - try { - FileOutputStream fos = new FileOutputStream("/Users/jiang/data.txt"); - fos.write(stringBuffer.toString().getBytes()); - } - catch (Exception e) { - System.out.println(e.getMessage()); + String re = util.HttpPostUtil.postData(jsonObject.toJSONString()); + System.out.println(re); + if (re != null){ + for (String ev:(ArrayList)dates.get("Event") + ) { + addCorrelation(ev, i.toString(), re); + } + } } +// try { +// FileOutputStream fos = new FileOutputStream("/Users/jiang/data.txt"); +// fos.write(stringBuffer.toString().getBytes()); +// fos.close(); +// } +// catch (Exception e) { +// System.out.println(e.getMessage()); +// } } diff --git a/BackEnd19v/src/main/java/neo4j/MongoDriver.java b/BackEnd19v/src/main/java/neo4j/MongoDriver.java index c0e3f634a..0607469e8 100644 --- a/BackEnd19v/src/main/java/neo4j/MongoDriver.java +++ b/BackEnd19v/src/main/java/neo4j/MongoDriver.java @@ -79,6 +79,7 @@ public static boolean save2Mongo(Map data){ System.out.println("文档插入成功"); // if(storeTimestamp(time)==null) // return false; + mongoClient.close(); } catch (Exception e){ e.printStackTrace(); return false; @@ -100,6 +101,7 @@ public static boolean save2MongoByTime(Map data, String time){ System.out.println("文档插入成功"); // if(storeTimestamp(time)==null) // return false; + mongoClient.close(); } catch (Exception e){ e.printStackTrace(); return false; @@ -129,6 +131,7 @@ public static boolean saveKapacitor2Mongo(String message){ System.out.println("文档插入成功"); // if(storeTimestamp(time)==null) // return false; + mongoClient.close(); } catch (Exception e){ e.printStackTrace(); return false; @@ -167,6 +170,7 @@ public static boolean saveEvent2Mongo(String content, String source){ append("time", time); collection.insertOne(document); System.out.println("文档插入成功"); + mongoClient.close(); } catch (Exception e){ e.printStackTrace(); return false; @@ -193,6 +197,7 @@ public static Map getOneFromMongo(String time){ map.putAll(d); result.add(map); } + mongoClient.close(); if(result.size()==0) return null; System.out.println(result.get(0)); return result.get(0); @@ -258,9 +263,9 @@ public static List getEventMongByTime(String startDate,String startTime, S aL.add(d.getInteger("type")); result.add(aL); } + mongoClient.close(); if(result.size()==0) return null; //System.out.println(result); - return result; } catch (Exception e){ e.printStackTrace(); @@ -299,6 +304,7 @@ public static List getTimesFromMongo(){ System.out.println(d.get("time")); result.add(d.get("time").toString()); } + mongoClient.close(); return result; } @@ -339,6 +345,7 @@ public static boolean saveSystemTypeAndNameFile(String type, String name){ doc.put("$set", res); collection.findOneAndUpdate(query,doc); System.out.println("文档修改成功"); + mongoClient.close(); } } catch (Exception e){ e.printStackTrace(); @@ -362,6 +369,7 @@ public static JSONArray getAllTypeAndName(){ Document d=mongoCursor.next(); re.add(JSONObject.parseObject(d.toJson())); } + mongoClient.close(); } catch (Exception e){ e.printStackTrace(); } diff --git a/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java b/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java index 85865f021..f320272c0 100755 --- a/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java +++ b/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java @@ -1177,7 +1177,9 @@ public static HashMap>> getAllNodesandlinks( Iterator iterator = node.labels().iterator(); iterator.next(); if (iterator.hasNext()){ - nod.put("type",iterator.next().toString()); + String type = iterator.next().toString(); + if (type.contains("owl")) continue; + nod.put("type",type.substring(2)); } else { nod.put("type", "null"); } From 6b0a594d56f43fbde9cc79e8c8c360c1cc8c08a0 Mon Sep 17 00:00:00 2001 From: ChaunceyJ <33971563+ChaunceyJ@users.noreply.github.com> Date: Mon, 18 Nov 2019 15:54:15 +0800 Subject: [PATCH 10/11] 0.0.0.0:8088/bbs/api/getSystemNodesAndLinks?systemName=system --- .../src/main/java/global/globalvalue.java | 4 +- .../src/main/java/neo4j/FusekiDriver.java | 76 ++++++++++++++++++- .../src/main/java/neo4j/Neo4jDriver.java | 64 +++++++++------- .../src/main/java/neo4j/prometheusDriver.java | 2 +- BackEnd19v/src/main/java/util/TurtleUtil.java | 8 +- .../src/main/java/web/Neo4jController.java | 4 +- 6 files changed, 120 insertions(+), 38 deletions(-) mode change 100755 => 100644 BackEnd19v/src/main/java/neo4j/Neo4jDriver.java diff --git a/BackEnd19v/src/main/java/global/globalvalue.java b/BackEnd19v/src/main/java/global/globalvalue.java index 6bfc5196a..fb39862f1 100644 --- a/BackEnd19v/src/main/java/global/globalvalue.java +++ b/BackEnd19v/src/main/java/global/globalvalue.java @@ -7,7 +7,7 @@ public class globalvalue { public static String mongoapi = "mongo"; //public static String mongoapi = "10.60.38.173"; public static String causeapi = "http://causeapi"; - //public static String neo4japi = "bolt://10.60.38.173"; - public static String neo4japi = "bolt://localhost"; + public static String neo4japi = "bolt://10.60.38.173"; + //public static String neo4japi = "bolt://localhost"; } \ No newline at end of file diff --git a/BackEnd19v/src/main/java/neo4j/FusekiDriver.java b/BackEnd19v/src/main/java/neo4j/FusekiDriver.java index 2f836e5ee..2b8dcda87 100755 --- a/BackEnd19v/src/main/java/neo4j/FusekiDriver.java +++ b/BackEnd19v/src/main/java/neo4j/FusekiDriver.java @@ -1684,7 +1684,7 @@ public static ArrayList getResourcesWithQuery(){ result.add(r); } } - System.out.println(result); + //System.out.println(result); return result; } @@ -1840,8 +1840,80 @@ public static boolean addCorrelation(String fromUrl, String toUrl, String influe } + //供算法导出数据使用 + public static void getDate(int month, int day){ + SimpleDateFormat DateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //加上时间 + Date start = new Date(); + Date end = new Date(); + try { + start = DateFormat.parse("2019-"+month+"-"+day+" 00:00:00"); + end = DateFormat.parse("2019-"+month+"-"+day+" 23:59:59"); + } catch(ParseException px) { + px.printStackTrace(); + } + Model model = DataAccessor.getInstance().getModel(); + ArrayList resources = getResourcesWithQuery(); + //受到时区的影响 + Map dates = getEventInFuseki(start, end); + List times = new ArrayList<>(); + for (Date i:(ArrayList)dates.get("Date") + ) { + times.add(i.getTime()/1000); + } + Collections.sort(times); + StringBuffer stringBuffer = new StringBuffer(); + for (Resource i:resources + ) { + Statement statement = i.getProperty(model.createProperty(i.toString()+"/query")); + //System.out.println(start.getTime()/1000 + " " + end.getTime()/1000); + + JSONArray proInfor = getProInfor(statement.getString().replace(" ",""),start.getTime()/1000 + "", end.getTime()/1000 + ""); + //JSONArray proInfor = getProInfor(statement.getString().replace(" ",""),times.get(0)+"", times.get(times.size()-1)+""); + if (proInfor == null)continue; + List timeList = new ArrayList(); + for (Object j:times + ) { + for (Object k:proInfor + ) { + if ((Long)j <= ((JSONArray)k).getLong(0)){ + if (!timeList.contains(((JSONArray)k).getLong(0))){ + timeList.add(((JSONArray)k).getLong(0)); + } + break; + } + } + } + String[] strings = i.toString().split("/"); + System.out.println(strings[strings.length-1]); + System.out.println(timeList); + System.out.println(proInfor); + stringBuffer.append(strings[strings.length-1]); + JSONObject jsonObject = new JSONObject(); + jsonObject.put("file1",timeList); + jsonObject.put("file2", proInfor); + stringBuffer.append("\r\n"); + stringBuffer.append(jsonObject.toString()); + stringBuffer.append("\r\n"); + } + if (stringBuffer.length() == 0){ + return; + } + try { + FileOutputStream fos = new FileOutputStream("/Users/jiang/data/data"+month+"-"+day+".txt"); + fos.write(stringBuffer.toString().getBytes()); + fos.close(); + } + catch (Exception e) { + System.out.println(e.getMessage()); + } + } public static void main(String[] args) { - addLinkEvent2S(); + for (int i = 30; i <= 31; i++) { + getDate(10,i); + } + for (int i = 1; i <= 14; i++) { + getDate(11,i); + } } } diff --git a/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java b/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java old mode 100755 new mode 100644 index f320272c0..86c589d21 --- a/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java +++ b/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java @@ -257,7 +257,7 @@ public int AddContainerNode(String label, String name, String volumeMount, Array "SET n.volumeMount = $volumeMount "+"SET n.name = $name "+"SET n.arrayListAdd = $arrayListAdd "+"SET n.arrayListDrop = $arrayListDrop "+ "SET n.type = 'Container_Node' "+"SET n.performance = $performance "+ "return id(n)", parameters("volumeMount",volumeMount,"name",name,"arrayListAdd",arrayListAdd,"arrayListDrop",arrayListDrop, - "performance","name:"+name+";volumeMount:"+volumeMount+";arrayListAdd:"+arrayListAdd+";arrayListDrop:"+arrayListDrop+";type:Container_Node")); + "performance","name:"+name+";volumeMount:"+volumeMount+";arrayListAdd:"+arrayListAdd+";arrayListDrop:"+arrayListDrop+";type:Container_Node")); trueId = newID.single().get(0).asInt(); System.out.println("trueId:"+trueId); //System.out.println(relations); @@ -437,19 +437,19 @@ public static Boolean MetricToDataset(int ID,String dataBaseName,String resultNa Boolean flag = true; try(Session session = driver.session()) { - StatementResult result0 = session.run("Match (n:Dataset) where n.name = $name return ID(n)" - ,parameters("name",dataBaseName)); - if (!result0.hasNext()){ - return false; - } - while (result0.hasNext()){ - Record record = result0.next(); - System.out.println(record); - int curID = record.get(0).asInt(); - System.out.println(curID); - session.run( "Start a=node("+ID+"),b=node("+curID+") Merge (a)-[r:collected_by{type:'collected_by',name:$name}]->(b)" - ,parameters("name",resultName)); - } + StatementResult result0 = session.run("Match (n:Dataset) where n.name = $name return ID(n)" + ,parameters("name",dataBaseName)); + if (!result0.hasNext()){ + return false; + } + while (result0.hasNext()){ + Record record = result0.next(); + System.out.println(record); + int curID = record.get(0).asInt(); + System.out.println(curID); + session.run( "Start a=node("+ID+"),b=node("+curID+") Merge (a)-[r:collected_by{type:'collected_by',name:$name}]->(b)" + ,parameters("name",resultName)); + } } return true; @@ -506,7 +506,7 @@ private static Boolean DeploymentToNode(String timeStamp,String hostIP, Boolean flag = true; try(Session session = driver.session()) { StatementResult result0 = session.run("Match (n:Deployment_Node) where n.name = $name return ID(n)" - , parameters("name", deploymentName)); + , parameters("name", deploymentName)); if (!result0.hasNext()) { return false; } @@ -525,7 +525,7 @@ private static Boolean DeploymentToNode(String timeStamp,String hostIP, parameters("timeStamp",timeStamp,"hostIP",hostIP,"nameSpace",nameSpace, "name",name,"podIP",podIP, "performance","name:"+name+";timeStamp:"+timeStamp+";hostIP:"+hostIP+";nameSpace:"+nameSpace+ - ";podIP:"+podIP+";type:Node")); + ";podIP:"+podIP+";type:Node")); } int ID = result1.next().get(0).asInt(); session.run("Start a=node(" + curID + "),b=node(" + ID + ") Merge (a)-[r:deploys_at{type:'deploys_at'}]->(b)"); @@ -1128,7 +1128,8 @@ public static HashMap getElement(){ //第一次适用需要运行 CREATE INDEX ON :Resource(uri) public static void importTtl(String typePath, String systemPath) { - Driver driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic( "neo4j", "1234" )); + Driver driver = GraphDatabase.driver(neo4japi+":7687", + AuthTokens.basic("neo4j","1234")); //初始化驱动器 try (Session session = driver.session()) { try (Transaction tx = session.beginTransaction()) { @@ -1140,14 +1141,21 @@ public static void importTtl(String typePath, String systemPath) { "`http://localhost/KGns/relationship#`:'rel'," + "`http://localhost/KGns/Container_attributes#`:'Container_attributes'," + "`http://localhost/KGns/Service_attributes#`:'Service_attributes'})"; - String ontology="CALL semantics.importRDF(\"file://" + typePath + "\", \"Turtle\")"; - String system="CALL semantics.importRDF(\"file://" + systemPath + "\", \"Turtle\")"; + String ontology="CALL semantics.importRDF(\"file:///" + typePath + "\", \"Turtle\")"; + String system="CALL semantics.importRDF(\"file:///" + systemPath + "\", \"Turtle\")"; + String systemFileName=systemPath.substring(systemPath.lastIndexOf("/")+1); + String systemName=systemFileName.substring(0,systemFileName.lastIndexOf(".")); + String createSystemNode="create(n:System{name:'"+systemName+"'})return n"; + String addRelation= "match(a:System),(b)where(not b:System and not (a)-->(b))create (a)-[r:has]->(b) return r"; System.out.println(ontology); System.out.println(system); - StatementResult result1 = tx.run(namespace); + tx.run(namespace); StatementResult result2 = tx.run(ontology); - StatementResult result3 = tx.run(system); + tx.run(system); + tx.run(createSystemNode); + tx.run(addRelation); + System.out.println(result2.toString()); tx.success(); } @@ -1155,13 +1163,13 @@ public static void importTtl(String typePath, String systemPath) { driver.close(); } - public static HashMap>> getAllNodesandlinks() { + public static HashMap>> getAllNodesandlinks(String systemName) { Driver driver = GraphDatabase.driver(neo4japi+":7687", AuthTokens.basic("neo4j","1234")); HashMap>> resultgraph = new HashMap<>(); try(Session session = driver.session()) { try (Transaction tx = session.beginTransaction()) { - StatementResult result = tx.run("Match p=(n)-[r]-(m) return p as nodesrelation"); + StatementResult result = tx.run("Match p=(n)-[r]-(m) , (a:System)-[e:has]-(n) where a.name = '"+systemName+"' return p as nodesrelation"); List> allnodes = new ArrayList<>(); List> allrelations = new ArrayList<>(); while(result.hasNext()){ @@ -1181,7 +1189,8 @@ public static HashMap>> getAllNodesandlinks( if (type.contains("owl")) continue; nod.put("type",type.substring(2)); } else { - nod.put("type", "null"); +// nod.put("type", "null"); + continue; } if(!allnodes.contains(nod)) allnodes.add(nod); @@ -1208,8 +1217,9 @@ public static HashMap>> getAllNodesandlinks( public static void main(String[] args) { - new Neo4jDriver().getAllNodesandlinks(); - //importTtl("/Users/jiang/Operation_KnowledgeGraph/BackEnd19v/target/turtle/type/ontology.ttl","/Users/jiang/Operation_KnowledgeGraph/BackEnd19v/target/turtle/system/system.ttl"); + new Neo4jDriver().getAllNodesandlinks("system"); +// importTtl("F:/Xlab/ontology.ttl", +// "F:/Xlab/system.ttl"); } -} +} \ No newline at end of file diff --git a/BackEnd19v/src/main/java/neo4j/prometheusDriver.java b/BackEnd19v/src/main/java/neo4j/prometheusDriver.java index 9a19c46c2..c5801fc9a 100644 --- a/BackEnd19v/src/main/java/neo4j/prometheusDriver.java +++ b/BackEnd19v/src/main/java/neo4j/prometheusDriver.java @@ -210,7 +210,7 @@ public static JSONArray getProInfor(String urlNode, String start, String end) { // query=APIServiceOpenAPIAggregationControllerQueue1_adds{instance="192.168.199.191:6443",job="kubernetes-apiservers"} String url = new String(); try { - url = "http://10.60.38.181:30003/api/v1/query_range?query=" + URLEncoder.encode(urlNode, "UTF-8") + "&start=" + start + "&end=" + end + "&step=60"; + url = "http://10.60.38.181:31003/api/v1/query_range?query=" + URLEncoder.encode(urlNode, "UTF-8") + "&start=" + start + "&end=" + end + "&step=60"; // url = java.net.URLEncoder.encode(url); //System.out.println(url); }catch (Exception e){ diff --git a/BackEnd19v/src/main/java/util/TurtleUtil.java b/BackEnd19v/src/main/java/util/TurtleUtil.java index 0a5ce4856..62d1dfdfb 100644 --- a/BackEnd19v/src/main/java/util/TurtleUtil.java +++ b/BackEnd19v/src/main/java/util/TurtleUtil.java @@ -43,8 +43,8 @@ public static void readOntologyJson(String fileName) throws IOException { "@prefix rel: .\n" + "@prefix Container_attributes: .\n" + "@prefix Service_attributes: .\n\n"); - ontologyTtlWriter.flush(); - ontologyJsonToTurtle(ontology,ontologyTtlWriter); + ontologyTtlWriter.flush(); + ontologyJsonToTurtle(ontology,ontologyTtlWriter); } @@ -146,7 +146,7 @@ public static void systemJsonToTurtle(JSONObject object,FileWriter writer) throw JSONObject instance = instances.getJSONObject(i); //节点名字 String name = instance.getString("name"); - writer.write("<#" + name + ">\n\t\t"); + writer.write(":" + name + "\n\t\t"); writer.flush(); //属性 JSONObject attributes=instance.getJSONObject("attributes"); @@ -201,4 +201,4 @@ public static void main(String[] args) throws IOException { // readOntologyJson(); // readSystemJson(); } -} +} \ No newline at end of file diff --git a/BackEnd19v/src/main/java/web/Neo4jController.java b/BackEnd19v/src/main/java/web/Neo4jController.java index f64dc7539..012171f75 100644 --- a/BackEnd19v/src/main/java/web/Neo4jController.java +++ b/BackEnd19v/src/main/java/web/Neo4jController.java @@ -163,8 +163,8 @@ public HashMap getElementName(HttpServletRequest request, Htt //获取结点、连接 @RequestMapping(value = "/api/getSystemNodesAndLinks",method = RequestMethod.GET,produces = "application/json") - public Map getNodeAndLink(){ - return getAllNodesandlinks(); + public Map getNodeAndLink(@RequestParam("systemName")String systemName){ + return getAllNodesandlinks(systemName); } From ecf8e78ccfdc10e506c6e96b685d99745e54d206 Mon Sep 17 00:00:00 2001 From: ChaunceyJ <33971563+ChaunceyJ@users.noreply.github.com> Date: Sun, 24 Nov 2019 19:23:57 +0800 Subject: [PATCH 11/11] save cluster result --- .../src/main/java/global/globalvalue.java | 4 +- .../src/main/java/neo4j/FusekiDriver.java | 96 +++++++++++++++---- .../src/main/java/neo4j/MongoDriver.java | 0 .../src/main/java/neo4j/Neo4jDriver.java | 9 +- .../src/main/java/neo4j/prometheusDriver.java | 0 .../main/java/neo4jentities/DataAccessor.java | 0 .../src/main/java/service/CsvService.java | 0 .../src/main/java/service/MongoService.java | 0 .../src/main/java/service/Neo4jService.java | 0 BackEnd19v/src/main/java/util/FileUtil.java | 33 +++++++ .../src/main/java/util/HttpPostUtil.java | 76 +++++++++++++-- BackEnd19v/src/main/java/util/JsonUtil.java | 0 BackEnd19v/src/main/java/util/TimerUtil.java | 0 BackEnd19v/src/main/java/util/TurtleUtil.java | 0 .../src/main/java/web/CsvController.java | 0 .../src/main/java/web/FileController.java | 0 .../src/main/java/web/MongoDBController.java | 0 .../src/main/java/web/Neo4jController.java | 0 18 files changed, 184 insertions(+), 34 deletions(-) mode change 100644 => 100755 BackEnd19v/src/main/java/global/globalvalue.java mode change 100644 => 100755 BackEnd19v/src/main/java/neo4j/MongoDriver.java mode change 100644 => 100755 BackEnd19v/src/main/java/neo4j/Neo4jDriver.java mode change 100644 => 100755 BackEnd19v/src/main/java/neo4j/prometheusDriver.java mode change 100644 => 100755 BackEnd19v/src/main/java/neo4jentities/DataAccessor.java mode change 100644 => 100755 BackEnd19v/src/main/java/service/CsvService.java mode change 100644 => 100755 BackEnd19v/src/main/java/service/MongoService.java mode change 100644 => 100755 BackEnd19v/src/main/java/service/Neo4jService.java create mode 100644 BackEnd19v/src/main/java/util/FileUtil.java mode change 100644 => 100755 BackEnd19v/src/main/java/util/HttpPostUtil.java mode change 100644 => 100755 BackEnd19v/src/main/java/util/JsonUtil.java mode change 100644 => 100755 BackEnd19v/src/main/java/util/TimerUtil.java mode change 100644 => 100755 BackEnd19v/src/main/java/util/TurtleUtil.java mode change 100644 => 100755 BackEnd19v/src/main/java/web/CsvController.java mode change 100644 => 100755 BackEnd19v/src/main/java/web/FileController.java mode change 100644 => 100755 BackEnd19v/src/main/java/web/MongoDBController.java mode change 100644 => 100755 BackEnd19v/src/main/java/web/Neo4jController.java diff --git a/BackEnd19v/src/main/java/global/globalvalue.java b/BackEnd19v/src/main/java/global/globalvalue.java old mode 100644 new mode 100755 index fb39862f1..cef85924e --- a/BackEnd19v/src/main/java/global/globalvalue.java +++ b/BackEnd19v/src/main/java/global/globalvalue.java @@ -2,8 +2,8 @@ public class globalvalue { - //public static String fusekiapi = "http://fuseki"; - public static String fusekiapi = "http://10.60.38.173"; + public static String fusekiapi = "http://fuseki"; + //public static String fusekiapi = "http://10.60.38.173"; public static String mongoapi = "mongo"; //public static String mongoapi = "10.60.38.173"; public static String causeapi = "http://causeapi"; diff --git a/BackEnd19v/src/main/java/neo4j/FusekiDriver.java b/BackEnd19v/src/main/java/neo4j/FusekiDriver.java index 2b8dcda87..b17c5f560 100755 --- a/BackEnd19v/src/main/java/neo4j/FusekiDriver.java +++ b/BackEnd19v/src/main/java/neo4j/FusekiDriver.java @@ -1,5 +1,6 @@ package neo4j; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import neo4jentities.DataAccessor; @@ -34,6 +35,8 @@ import static neo4j.MongoDriver.*; import static neo4j.prometheusDriver.getProInfor; +import static util.FileUtil.saveClusterResult; +import static util.HttpPostUtil.getImage; @Component @@ -1735,13 +1738,9 @@ public static void addLinkEvent2S(){ System.out.println(strings[strings.length-1]); System.out.println(timeList); System.out.println(proInfor); - stringBuffer.append(strings[strings.length-1]); JSONObject jsonObject = new JSONObject(); jsonObject.put("file1",timeList); jsonObject.put("file2", proInfor); - stringBuffer.append("\r\n"); - stringBuffer.append(jsonObject.toString()); - stringBuffer.append("\r\n"); String re = util.HttpPostUtil.postData(jsonObject.toJSONString()); System.out.println(re); if (re != null){ @@ -1751,16 +1750,6 @@ public static void addLinkEvent2S(){ } } } -// try { -// FileOutputStream fos = new FileOutputStream("/Users/jiang/data.txt"); -// fos.write(stringBuffer.toString().getBytes()); -// fos.close(); -// } -// catch (Exception e) { -// System.out.println(e.getMessage()); -// } - - } @@ -1818,8 +1807,8 @@ public static boolean addCorrelation(String fromUrl, String toUrl, String influe System.out.println(addRelation); System.out.println(setInfluenceValue); RDFConnectionRemoteBuilder builderAddRelation = RDFConnectionFuseki.create() - // .destination("http://10.60.38.173:3030/DevKGData/update"); - .destination("http://localhost:3030/gundam/update"); + .destination("http://10.60.38.173:3030/DevKGData/update"); + //.destination("http://localhost:3030/gundam/update"); // CredentialsProvider credsProvider = new BasicCredentialsProvider(); // Credentials credentials = new UsernamePasswordCredentials("admin", "D0rlghQl5IAgYOm"); // credsProvider.setCredentials(AuthScope.ANY, credentials); @@ -1908,12 +1897,77 @@ public static void getDate(int month, int day){ } } - public static void main(String[] args) { - for (int i = 30; i <= 31; i++) { - getDate(10,i); + //向算法部分发送数据,返回一个String、两个时间序列 + public static void getClusterResult(){ + SimpleDateFormat DateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //加上时间 + Date start = new Date(); + Date end = new Date(); + try { + start = DateFormat.parse("2019-11-18 00:00:00"); + end = DateFormat.parse("2019-11-18 23:59:59"); + } catch(ParseException px) { + px.printStackTrace(); + } + Model model = DataAccessor.getInstance().getModel(); + ArrayList resources = getResourcesWithQuery(); + //受到时区的影响 + Map dates = getEventInFuseki(start, end); + List times = new ArrayList<>(); + for (Date i:(ArrayList)dates.get("Date") + ) { + times.add(i.getTime()/1000); } - for (int i = 1; i <= 14; i++) { - getDate(11,i); + Collections.sort(times); + StringBuffer stringBuffer = new StringBuffer(); + for (Resource i:resources + ) { + Statement statement = i.getProperty(model.createProperty(i.toString()+"/query")); + JSONArray proInfor = getProInfor(statement.getString().replace(" ",""),start.getTime()/1000 + "", end.getTime()/1000 + ""); + if (proInfor == null)continue; + List timeList = new ArrayList(); + for (Object j:times + ) { + for (Object k:proInfor + ) { + if ((Long)j <= ((JSONArray)k).getLong(0)){ + if (!timeList.contains(((JSONArray)k).getLong(0))){ + timeList.add(((JSONArray)k).getLong(0)); + } + break; + } + } + } + String[] strings = i.toString().split("/"); + System.out.println(strings[strings.length-1]); + System.out.println(timeList); + System.out.println(proInfor); + JSONObject jsonObject = new JSONObject(); + jsonObject.put("file4", 1); + jsonObject.put("file3", strings[strings.length-1]); + jsonObject.put("file1",timeList); + jsonObject.put("file2", proInfor); + + String re = util.HttpPostUtil.postData(jsonObject.toJSONString()); + System.out.println(re); + if (re != null){ + JSONObject jsonRe = JSON.parseObject(re); +// String correlation = jsonRe.getString("Correlation"); +// List SST = JSONArray.parseArray(jsonRe.getString("SST"), List.class); +// List alarm = JSONArray.parseArray(jsonRe.getString("Alarm"), List.class); + saveClusterResult(jsonRe.toJSONString(), strings[strings.length-1]); + + } } } + + public static void main(String[] args) { +// for (int i = 30; i <= 31; i++) { +// getDate(10,i); +// } +// for (int i = 1; i <= 14; i++) { +// getDate(11,i); +// } + + + } } diff --git a/BackEnd19v/src/main/java/neo4j/MongoDriver.java b/BackEnd19v/src/main/java/neo4j/MongoDriver.java old mode 100644 new mode 100755 diff --git a/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java b/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java old mode 100644 new mode 100755 index 86c589d21..a57f0ba96 --- a/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java +++ b/BackEnd19v/src/main/java/neo4j/Neo4jDriver.java @@ -1141,11 +1141,11 @@ public static void importTtl(String typePath, String systemPath) { "`http://localhost/KGns/relationship#`:'rel'," + "`http://localhost/KGns/Container_attributes#`:'Container_attributes'," + "`http://localhost/KGns/Service_attributes#`:'Service_attributes'})"; - String ontology="CALL semantics.importRDF(\"file:///" + typePath + "\", \"Turtle\")"; - String system="CALL semantics.importRDF(\"file:///" + systemPath + "\", \"Turtle\")"; + String ontology="CALL semantics.importRDF(\"file://" + typePath + "\", \"Turtle\")"; + String system="CALL semantics.importRDF(\"file://" + systemPath + "\", \"Turtle\")"; String systemFileName=systemPath.substring(systemPath.lastIndexOf("/")+1); String systemName=systemFileName.substring(0,systemFileName.lastIndexOf(".")); - String createSystemNode="create(n:System{name:'"+systemName+"'})return n"; + String createSystemNode="create(n:System{name:'"+systemName+"',uri:'www.tongji.edu.cn/"+systemName+"'})return n"; String addRelation= "match(a:System),(b)where(not b:System and not (a)-->(b))create (a)-[r:has]->(b) return r"; System.out.println(ontology); System.out.println(system); @@ -1170,6 +1170,7 @@ public static HashMap>> getAllNodesandlinks( try(Session session = driver.session()) { try (Transaction tx = session.beginTransaction()) { StatementResult result = tx.run("Match p=(n)-[r]-(m) , (a:System)-[e:has]-(n) where a.name = '"+systemName+"' return p as nodesrelation"); + // StatementResult result = tx.run("match (:System{name:'"+systemName+"'})--(n)return n as nodesrelation"); List> allnodes = new ArrayList<>(); List> allrelations = new ArrayList<>(); while(result.hasNext()){ @@ -1217,7 +1218,7 @@ public static HashMap>> getAllNodesandlinks( public static void main(String[] args) { - new Neo4jDriver().getAllNodesandlinks("system"); + new Neo4jDriver().getAllNodesandlinks("0000"); // importTtl("F:/Xlab/ontology.ttl", // "F:/Xlab/system.ttl"); diff --git a/BackEnd19v/src/main/java/neo4j/prometheusDriver.java b/BackEnd19v/src/main/java/neo4j/prometheusDriver.java old mode 100644 new mode 100755 diff --git a/BackEnd19v/src/main/java/neo4jentities/DataAccessor.java b/BackEnd19v/src/main/java/neo4jentities/DataAccessor.java old mode 100644 new mode 100755 diff --git a/BackEnd19v/src/main/java/service/CsvService.java b/BackEnd19v/src/main/java/service/CsvService.java old mode 100644 new mode 100755 diff --git a/BackEnd19v/src/main/java/service/MongoService.java b/BackEnd19v/src/main/java/service/MongoService.java old mode 100644 new mode 100755 diff --git a/BackEnd19v/src/main/java/service/Neo4jService.java b/BackEnd19v/src/main/java/service/Neo4jService.java old mode 100644 new mode 100755 diff --git a/BackEnd19v/src/main/java/util/FileUtil.java b/BackEnd19v/src/main/java/util/FileUtil.java new file mode 100644 index 000000000..574dbfdd7 --- /dev/null +++ b/BackEnd19v/src/main/java/util/FileUtil.java @@ -0,0 +1,33 @@ +package util; + +import web.FileController; + +import java.io.File; +import java.io.FileOutputStream; +import java.util.Date; + +public class FileUtil { + + public static void saveClusterResult(String re, String name){ + try { + + String savePath = FileController.class.getResource("/").getPath().replace("classes","clusterResult"); + File folder = new File(savePath); + //文件夹路径不存在 + if (!folder.exists() && !folder.isDirectory()) { + folder.mkdirs(); + } + File file = new File(savePath + name + new Date().getTime() + ".json"); + //判断路径是否存在,如果不存在就创建一个 + if(!file.exists()){ + file.createNewFile(); + } + FileOutputStream fos = new FileOutputStream(file); + fos.write(re.getBytes()); + fos.close(); + } + catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/BackEnd19v/src/main/java/util/HttpPostUtil.java b/BackEnd19v/src/main/java/util/HttpPostUtil.java old mode 100644 new mode 100755 index 886d7ecce..3307fdbcb --- a/BackEnd19v/src/main/java/util/HttpPostUtil.java +++ b/BackEnd19v/src/main/java/util/HttpPostUtil.java @@ -1,6 +1,15 @@ package util; import okhttp3.*; +import web.FileController; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Date; public class HttpPostUtil { @@ -11,14 +20,8 @@ public static String postData(String con1) { RequestBody body = RequestBody.create(mediaType, con1); Request request = new Request.Builder() .url("http://10.60.38.173:10081") + //.url("http://192.168.31.205:8080") .post(body) - .addHeader("Content-Type", "application/json,application/json") - .addHeader("Accept", "*/*") - .addHeader("Cache-Control", "no-cache") - .addHeader("Host", "10.60.38.173:10081") - .addHeader("Accept-Encoding", "gzip, deflate") - .addHeader("Connection", "keep-alive") - .addHeader("cache-control", "no-cache") .build(); try { @@ -34,6 +37,65 @@ public static String postData(String con1) { } } + public static String getImage(String urlString) throws Exception { + //new一个URL对象 + URL url = new URL(urlString); + //打开链接 + HttpURLConnection conn = (HttpURLConnection)url.openConnection(); + //设置请求方式为"GET" + conn.setRequestMethod("GET"); + //超时响应时间为5秒 + conn.setConnectTimeout(5 * 1000); + //通过输入流获取图片数据 + InputStream inStream = conn.getInputStream(); + //得到图片的二进制数据,以二进制封装得到数据,具有通用性 + byte[] data = readInputStream(inStream); + //new一个文件对象用来保存图片,默认保存当前工程根目录 + String savePath = FileController.class.getResource("/").getPath().replace("classes","hierarchy"); + File folder = new File(savePath); + //文件夹路径不存在 + if (!folder.exists() && !folder.isDirectory()) { + folder.mkdirs(); + } + File imageFile = new File(savePath + "hierarchy" + new Date().toString() + ".png"); + //判断路径是否存在,如果不存在就创建一个 + if(!imageFile.exists()){ + imageFile.createNewFile(); + } + //创建输出流 + FileOutputStream outStream = new FileOutputStream(imageFile); + //写入数据 + outStream.write(data); + //关闭输出流 + outStream.close(); + + return imageFile.getName(); + } + + public static byte[] readInputStream(InputStream inStream) throws Exception{ + ByteArrayOutputStream outStream = new ByteArrayOutputStream(); + //创建一个Buffer字符串 + byte[] buffer = new byte[1024]; + //每次读取的字符串长度,如果为-1,代表全部读取完毕 + int len = 0; + //使用一个输入流从buffer里把数据读取出来 + while( (len=inStream.read(buffer)) != -1 ){ + //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度 + outStream.write(buffer, 0, len); + } + //关闭输入流 + inStream.close(); + //把outStream里的数据写入内存 + return outStream.toByteArray(); + } + + + public static void main(String[] args) { + try { + getImage("https://www.baidu.com/img/bd_logo1.png?where=super"); + }catch (Exception e){ + System.out.println(e); + } } } \ No newline at end of file diff --git a/BackEnd19v/src/main/java/util/JsonUtil.java b/BackEnd19v/src/main/java/util/JsonUtil.java old mode 100644 new mode 100755 diff --git a/BackEnd19v/src/main/java/util/TimerUtil.java b/BackEnd19v/src/main/java/util/TimerUtil.java old mode 100644 new mode 100755 diff --git a/BackEnd19v/src/main/java/util/TurtleUtil.java b/BackEnd19v/src/main/java/util/TurtleUtil.java old mode 100644 new mode 100755 diff --git a/BackEnd19v/src/main/java/web/CsvController.java b/BackEnd19v/src/main/java/web/CsvController.java old mode 100644 new mode 100755 diff --git a/BackEnd19v/src/main/java/web/FileController.java b/BackEnd19v/src/main/java/web/FileController.java old mode 100644 new mode 100755 diff --git a/BackEnd19v/src/main/java/web/MongoDBController.java b/BackEnd19v/src/main/java/web/MongoDBController.java old mode 100644 new mode 100755 diff --git a/BackEnd19v/src/main/java/web/Neo4jController.java b/BackEnd19v/src/main/java/web/Neo4jController.java old mode 100644 new mode 100755