diff --git a/examples/kg-api/pom.xml b/examples/kg-api/pom.xml new file mode 100644 index 0000000..7b35ec3 --- /dev/null +++ b/examples/kg-api/pom.xml @@ -0,0 +1,66 @@ + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.7.0 + + + + kg-api + jar + + kg-api + http://maven.apache.org + + + UTF-8 + + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-tomcat + + + + + org.springframework.boot + spring-boot-starter-undertow + + + + org.projectlombok + lombok + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.nebula-contrib + ngbatis + 1.2.2 + + + + diff --git a/examples/kg-api/src/main/java/org/nebula/contrib/kg/KgApiApp.java b/examples/kg-api/src/main/java/org/nebula/contrib/kg/KgApiApp.java new file mode 100644 index 0000000..1b72dee --- /dev/null +++ b/examples/kg-api/src/main/java/org/nebula/contrib/kg/KgApiApp.java @@ -0,0 +1,22 @@ +package org.nebula.contrib.kg; + +// Copyright (c) 2024 All project authors. All rights reserved. +// +// This source code is licensed under Apache 2.0 License. + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; + +/** + * Hello world! + */ +@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}, + scanBasePackages = {"org.nebula.contrib", "org.nebula.contrib.kg"}) +public class KgApiApp { + + public static void main(String[] args) { + SpringApplication app = new SpringApplication(KgApiApp.class); + app.run(args); + } +} diff --git a/examples/kg-api/src/main/java/org/nebula/contrib/kg/controller/DataController.java b/examples/kg-api/src/main/java/org/nebula/contrib/kg/controller/DataController.java new file mode 100644 index 0000000..9563fb7 --- /dev/null +++ b/examples/kg-api/src/main/java/org/nebula/contrib/kg/controller/DataController.java @@ -0,0 +1,32 @@ +package org.nebula.contrib.kg.controller; + +// Copyright (c) 2024 All project authors. All rights reserved. +// +// This source code is licensed under Apache 2.0 License. + +import java.util.List; +import org.nebula.contrib.kg.pojo.Triplet; +import org.nebula.contrib.kg.service.DataService; +import org.nebula.contrib.kg.utils.R; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author yeweicheng + * @since 2024-09-02 15:46 + *
Now is history! + */ +@RestController +@RequestMapping("/kg/data") +public class DataController { + + @Autowired + private DataService service; + + @RequestMapping("next") + public R>> next(String id) { + return R.ok(service.next(id)); + } + +} diff --git a/examples/kg-api/src/main/java/org/nebula/contrib/kg/controller/SchemaController.java b/examples/kg-api/src/main/java/org/nebula/contrib/kg/controller/SchemaController.java new file mode 100644 index 0000000..4e199d7 --- /dev/null +++ b/examples/kg-api/src/main/java/org/nebula/contrib/kg/controller/SchemaController.java @@ -0,0 +1,21 @@ +package org.nebula.contrib.kg.controller; + +// Copyright (c) 2024 All project authors. All rights reserved. +// +// This source code is licensed under Apache 2.0 License. + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author yeweicheng + * @since 2024-09-02 15:46 + *
Now is history! + */ +@RestController +@RequestMapping("/kg/schema") +public class SchemaController { + + // TODO + +} diff --git a/examples/kg-api/src/main/java/org/nebula/contrib/kg/dao/DataDao.java b/examples/kg-api/src/main/java/org/nebula/contrib/kg/dao/DataDao.java new file mode 100644 index 0000000..5af1e67 --- /dev/null +++ b/examples/kg-api/src/main/java/org/nebula/contrib/kg/dao/DataDao.java @@ -0,0 +1,20 @@ +package org.nebula.contrib.kg.dao; + +// Copyright (c) 2024 All project authors. All rights reserved. +// +// This source code is licensed under Apache 2.0 License. + +import java.util.List; +import org.nebula.contrib.kg.pojo.Triplet; +import org.springframework.data.repository.query.Param; + +/** + * @author yeweicheng + * @since 2024-09-02 15:54 + *
Now is history! + */ +public interface DataDao { + + List> selectTriplets(@Param("id") T id); + +} diff --git a/examples/kg-api/src/main/java/org/nebula/contrib/kg/ngbatis/CollectionTripletResultHandler.java b/examples/kg-api/src/main/java/org/nebula/contrib/kg/ngbatis/CollectionTripletResultHandler.java new file mode 100644 index 0000000..1eff4fe --- /dev/null +++ b/examples/kg-api/src/main/java/org/nebula/contrib/kg/ngbatis/CollectionTripletResultHandler.java @@ -0,0 +1,40 @@ +package org.nebula.contrib.kg.ngbatis; + +// Copyright (c) 2024 All project authors. All rights reserved. +// +// This source code is licensed under Apache 2.0 License. + +import com.vesoft.nebula.client.graph.data.ResultSet; +import java.util.Collection; +import org.nebula.contrib.kg.pojo.Triplet; +import org.nebula.contrib.ngbatis.handler.AbstractResultHandler; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +/** + * @author yeweicheng + * @since 2024-08-28 13:59 + *
Now is history! + */ +@Component +public class CollectionTripletResultHandler extends + AbstractResultHandler> { + + @Autowired + private TripletResultHandler singleHandler; + + @Override + public Collection handle(Collection newResult, ResultSet result, Class resultType) + throws NoSuchFieldException, IllegalAccessException, InstantiationException { + + int rowsSize = result.rowsSize(); + for (int i = 0; i < rowsSize; i++) { + ResultSet.Record row = result.rowValues(i); + Triplet triplet = new Triplet<>(); + triplet = singleHandler.handle(triplet, row); + newResult.add(triplet); + } + return newResult; + } + +} diff --git a/examples/kg-api/src/main/java/org/nebula/contrib/kg/ngbatis/TripletResultHandler.java b/examples/kg-api/src/main/java/org/nebula/contrib/kg/ngbatis/TripletResultHandler.java new file mode 100644 index 0000000..346db4e --- /dev/null +++ b/examples/kg-api/src/main/java/org/nebula/contrib/kg/ngbatis/TripletResultHandler.java @@ -0,0 +1,58 @@ +package org.nebula.contrib.kg.ngbatis; + +// Copyright (c) 2024 All project authors. All rights reserved. +// +// This source code is licensed under Apache 2.0 License. + +import com.vesoft.nebula.client.graph.data.ResultSet; +import com.vesoft.nebula.client.graph.data.ResultSet.Record; +import com.vesoft.nebula.client.graph.data.ValueWrapper; +import org.nebula.contrib.kg.pojo.Triplet; +import org.nebula.contrib.ngbatis.handler.AbstractResultHandler; +import org.nebula.contrib.ngbatis.handler.NgEdgeResultHandler; +import org.nebula.contrib.ngbatis.handler.NgVertexResultHandler; +import org.nebula.contrib.ngbatis.models.data.NgEdge; +import org.nebula.contrib.ngbatis.models.data.NgVertex; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +/** + * @author yeweicheng + * @since 2024-08-28 13:50 + *
Now is history! + */ +@Component +public class TripletResultHandler extends AbstractResultHandler, Triplet> { + + @Autowired + private NgVertexResultHandler vertexHandler; + @Autowired + private NgEdgeResultHandler edgeHandler; + + @Override + public Triplet handle( + Triplet newResult, ResultSet result, Class resultType) { + return handle(newResult, result.rowValues(0)); + } + + public Triplet handle(Triplet newResult, Record row) { + + NgVertex src = new NgVertex<>(); + + ValueWrapper valueWrapper = row.get(0); + vertexHandler.handle(src, valueWrapper); + newResult.setSrc(src); + + NgEdge edge = new NgEdge<>(); + valueWrapper = row.get(1); + edgeHandler.handle(edge, valueWrapper); + newResult.setEdge(edge); + + NgVertex dst = new NgVertex<>(); + valueWrapper = row.get(2); + vertexHandler.handle(dst, valueWrapper); + newResult.setDst(dst); + + return newResult; + } +} diff --git a/examples/kg-api/src/main/java/org/nebula/contrib/kg/pojo/Triplet.java b/examples/kg-api/src/main/java/org/nebula/contrib/kg/pojo/Triplet.java new file mode 100644 index 0000000..00842c1 --- /dev/null +++ b/examples/kg-api/src/main/java/org/nebula/contrib/kg/pojo/Triplet.java @@ -0,0 +1,45 @@ +package org.nebula.contrib.kg.pojo; + +// Copyright (c) 2024 All project authors. All rights reserved. +// +// This source code is licensed under Apache 2.0 License. + +import org.nebula.contrib.ngbatis.models.data.NgEdge; +import org.nebula.contrib.ngbatis.models.data.NgVertex; + +/** + * @author yeweicheng + * @since 2024-08-28 13:28 + *
Now is history! + */ + +public class Triplet { + + private NgVertex src; + private NgVertex dst; + private NgEdge edge; + + public NgVertex getSrc() { + return src; + } + + public void setSrc(NgVertex src) { + this.src = src; + } + + public NgVertex getDst() { + return dst; + } + + public void setDst(NgVertex dst) { + this.dst = dst; + } + + public NgEdge getEdge() { + return edge; + } + + public void setEdge(NgEdge edge) { + this.edge = edge; + } +} diff --git a/examples/kg-api/src/main/java/org/nebula/contrib/kg/service/DataService.java b/examples/kg-api/src/main/java/org/nebula/contrib/kg/service/DataService.java new file mode 100644 index 0000000..cfb61e5 --- /dev/null +++ b/examples/kg-api/src/main/java/org/nebula/contrib/kg/service/DataService.java @@ -0,0 +1,19 @@ +package org.nebula.contrib.kg.service; + +// Copyright (c) 2024 All project authors. All rights reserved. +// +// This source code is licensed under Apache 2.0 License. + +import java.util.List; +import org.nebula.contrib.kg.pojo.Triplet; + +/** + * @author yeweicheng + * @since 2024-09-02 16:08 + *
Now is history! + */ +public interface DataService { + + List> next(String id); + +} diff --git a/examples/kg-api/src/main/java/org/nebula/contrib/kg/service/impl/DataServiceImpl.java b/examples/kg-api/src/main/java/org/nebula/contrib/kg/service/impl/DataServiceImpl.java new file mode 100644 index 0000000..776060d --- /dev/null +++ b/examples/kg-api/src/main/java/org/nebula/contrib/kg/service/impl/DataServiceImpl.java @@ -0,0 +1,29 @@ +package org.nebula.contrib.kg.service.impl; + +// Copyright (c) 2024 All project authors. All rights reserved. +// +// This source code is licensed under Apache 2.0 License. + +import java.util.List; +import org.nebula.contrib.kg.dao.DataDao; +import org.nebula.contrib.kg.pojo.Triplet; +import org.nebula.contrib.kg.service.DataService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * @author yeweicheng + * @since 2024-09-02 16:12 + *
Now is history! + */ +@Service +public class DataServiceImpl implements DataService { + + @Autowired + private DataDao dao; + + @Override + public List> next(String id) { + return dao.selectTriplets(id); + } +} diff --git a/examples/kg-api/src/main/java/org/nebula/contrib/kg/utils/R.java b/examples/kg-api/src/main/java/org/nebula/contrib/kg/utils/R.java new file mode 100644 index 0000000..411f6a1 --- /dev/null +++ b/examples/kg-api/src/main/java/org/nebula/contrib/kg/utils/R.java @@ -0,0 +1,41 @@ +package org.nebula.contrib.kg.utils; + +// Copyright (c) 2024 All project authors. All rights reserved. +// +// This source code is licensed under Apache 2.0 License. + +import java.io.Serializable; +import lombok.Data; + +/** + * @author yeweicheng + * @since 2024-09-02 16:03 + *
Now is history! + */ +@Data +public class R implements Serializable { + + private static final long serialVersionUID = 1030781725265793055L; + + private T data; + + private String message; + + private boolean success = true; + + private String code; + + public static R ok(T o) { + return R.success( "200", null, o ); + } + + public static R success(String code, String message, T data) { + R result = new R<>(); + result.setData(data); + result.setCode(code); + result.setSuccess(true); + result.setMessage(message); + return result; + } + +} diff --git a/examples/kg-api/src/main/resources/application.yml b/examples/kg-api/src/main/resources/application.yml new file mode 100644 index 0000000..5d6dd3d --- /dev/null +++ b/examples/kg-api/src/main/resources/application.yml @@ -0,0 +1,42 @@ +#Copyright (c) 2024 All project authors. All rights reserved. +# +#This source code is licensed under Apache 2.0 License. + +server: + port: 8082 + + +spring: + profiles: + active: 4dev + +nebula: + ngbatis: + session-life-length: 299000 + check-fixed-rate: 300000 + # space name needs to be informed through annotations(@Space) or xml(space="test") + # default false(false: Session pool map will not be initialized) + use-session-pool: false + hosts: 127.0.0.1:9669 + username: root + password: nebula + space: test + pool-config: + min-conns-size: 1 + max-conns-size: 10 + timeout: 6000 + idle-time: 0 + interval-idle: -1 + wait-time: 0 + min-cluster-health-rate: 1.0 + enable-ssl: false + + +logging: + level: + org.nebula.contrib: DEBUG + +cql: + parser: + # 更换开发者自定义的 xml 所在位置 + mapper-locations: classpath:ng-mapper/**/*.xml # 默认为 mapper/**/*.xml \ No newline at end of file diff --git a/examples/kg-api/src/main/resources/ng-mapper/DataDao.xml b/examples/kg-api/src/main/resources/ng-mapper/DataDao.xml new file mode 100644 index 0000000..4727cc7 --- /dev/null +++ b/examples/kg-api/src/main/resources/ng-mapper/DataDao.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file