Excel批量导入Neo4j

Excel批量导入Neo4j

Excel处理代码和Neo4j处理代码在另一篇文章有介绍此处不复述
代码:

 //上传Excel文件导入数据到Neo4j
    @Override
    public ResponseResult excelUpload(MultipartFile uploadFile, String type) {
        ResponseResult responseResult = new ResponseResult();
        try {
            //读取Excel文件
            Map<String, List<Map<String, Object>>> map = ExcelTool.readExcel(uploadFile);
            Result result = null;
            String cypher;
            if(null==type||"".equals(type)){
                responseResult.setData("code","500");
                responseResult.setData("message","数据类型不匹配");
            }else if ("节点".equals(type)){
                for (Map.Entry<String, List<Map<String, Object>>> entry : map.entrySet()) {
                    List list = NodeUtil.setProps1(entry.getValue());
                    cypher = "UNWIND {list} as row CALL apoc.create.node(row.labels, row.props) yield node RETURN count(*);";
                    result = DatabaseUtil.neo4jExecutes(cypher,list);
                }
                //插入mongodb(批量)
                excelInsertMany("nodes",map);
            }else if ("关系".equals(type)){
                for (Map.Entry<String, List<Map<String, Object>>> entry : map.entrySet()) {
                    List list = NodeUtil.setProps(entry.getValue());
                /*cypher = "UNWIND {list} as row MATCH (startNode) WHERE startNode.名称 = row.起始节点 " +
                        "WITH row,startNode MATCH (endNode) WHERE endNode.名称 = row.终止节点 Merge (startNode)-[rel:"+ type + "]->(endNode) " +
                        "SET rel += row.props";*/

                    cypher = "UNWIND {list} as row MATCH (startNode) WHERE startNode.名称 = row.起始节点 " +
                            "WITH row,startNode MATCH (endNode) WHERE endNode.名称 = row.终止节点 " +
                            "CALL apoc.create.relationship(startNode, row.关系标签, row.props, endNode) yield rel "+
                            "RETURN count(*);";
                    result = DatabaseUtil.neo4jExecutes(cypher,list);
                }
                //插入mongodb(批量)
                excelInsertMany("relations",map);
            }
            if(result != null){
                responseResult.setData("code", 200);
                responseResult.setData("message","Excel导入成功");

            }else {
                responseResult.setData("code", 500);
                responseResult.setData("message","Excel导入失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setData("code", 500);
            responseResult.setData("message", "You failed to upload  => " + e.getMessage());
        }
        return new ResponseResult(responseResult.getData());
    }

你可能感兴趣的:(Neo4j)