Skip to content

Commit

Permalink
Merge pull request #316 from CorvusYe/master
Browse files Browse the repository at this point in the history
demo: add a demo project for kg api
  • Loading branch information
CorvusYe authored Sep 2, 2024
2 parents 4f6dba5 + 19dcdbd commit 21f1870
Show file tree
Hide file tree
Showing 13 changed files with 452 additions and 0 deletions.
66 changes: 66 additions & 0 deletions examples/kg-api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<artifactId>kg-api</artifactId>
<packaging>jar</packaging>

<name>kg-api</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>


<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.nebula-contrib</groupId>
<artifactId>ngbatis</artifactId>
<version>1.2.2</version>
</dependency>
</dependencies>

</project>
22 changes: 22 additions & 0 deletions examples/kg-api/src/main/java/org/nebula/contrib/kg/KgApiApp.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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
* <br>Now is history!
*/
@RestController
@RequestMapping("/kg/data")
public class DataController {

@Autowired
private DataService service;

@RequestMapping("next")
public R<List<Triplet<String>>> next(String id) {
return R.ok(service.next(id));
}

}
Original file line number Diff line number Diff line change
@@ -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
* <br>Now is history!
*/
@RestController
@RequestMapping("/kg/schema")
public class SchemaController {

// TODO

}
Original file line number Diff line number Diff line change
@@ -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
* <br>Now is history!
*/
public interface DataDao<T> {

List<Triplet<T>> selectTriplets(@Param("id") T id);

}
Original file line number Diff line number Diff line change
@@ -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
* <br>Now is history!
*/
@Component
public class CollectionTripletResultHandler extends
AbstractResultHandler<Collection, Triplet<String>> {

@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<String> triplet = new Triplet<>();
triplet = singleHandler.handle(triplet, row);
newResult.add(triplet);
}
return newResult;
}

}
Original file line number Diff line number Diff line change
@@ -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
* <br>Now is history!
*/
@Component
public class TripletResultHandler extends AbstractResultHandler<Triplet<String>, Triplet<String>> {

@Autowired
private NgVertexResultHandler vertexHandler;
@Autowired
private NgEdgeResultHandler edgeHandler;

@Override
public Triplet<String> handle(
Triplet<String> newResult, ResultSet result, Class resultType) {
return handle(newResult, result.rowValues(0));
}

public Triplet<String> handle(Triplet<String> newResult, Record row) {

NgVertex<String> src = new NgVertex<>();

ValueWrapper valueWrapper = row.get(0);
vertexHandler.handle(src, valueWrapper);
newResult.setSrc(src);

NgEdge<String> edge = new NgEdge<>();
valueWrapper = row.get(1);
edgeHandler.handle(edge, valueWrapper);
newResult.setEdge(edge);

NgVertex<String> dst = new NgVertex<>();
valueWrapper = row.get(2);
vertexHandler.handle(dst, valueWrapper);
newResult.setDst(dst);

return newResult;
}
}
Original file line number Diff line number Diff line change
@@ -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
* <br>Now is history!
*/

public class Triplet<T> {

private NgVertex<T> src;
private NgVertex<T> dst;
private NgEdge<T> edge;

public NgVertex<T> getSrc() {
return src;
}

public void setSrc(NgVertex<T> src) {
this.src = src;
}

public NgVertex<T> getDst() {
return dst;
}

public void setDst(NgVertex<T> dst) {
this.dst = dst;
}

public NgEdge<T> getEdge() {
return edge;
}

public void setEdge(NgEdge<T> edge) {
this.edge = edge;
}
}
Original file line number Diff line number Diff line change
@@ -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
* <br>Now is history!
*/
public interface DataService {

List<Triplet<String>> next(String id);

}
Original file line number Diff line number Diff line change
@@ -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
* <br>Now is history!
*/
@Service
public class DataServiceImpl implements DataService {

@Autowired
private DataDao<String> dao;

@Override
public List<Triplet<String>> next(String id) {
return dao.selectTriplets(id);
}
}
Loading

0 comments on commit 21f1870

Please sign in to comment.