社区论坛
安装首页-Galaxybase论坛
galaxybase官网登陆账号密码都是手机号
demo实战文档 - Galaxybasehttps://galaxybase.com/document?file=v3.4.1%2Fstandalone-doc&docid=26
192.168.208.60http://192.168.208.60:51314/按照官方安装文档提示操作,正常情况下服务就能使用,如果服务器异常关机。再次启动会报错。此时检查docker运行
[root@galaxybase bin]# docker ps
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
# 然后使用脚本启动服务
[root@galaxybase bin]# ./galaxybase-deploy start graph
$ ./galaxybase-deploy stop graph
$ ./galaxybase-deploy restart graph
服务--> 其他服务-->数据可视化服务(启动后可访问:8888端口)
需求描述:使用galaxybase 驱动包,通过接口、文件、数据库等数据源,获取数据解析入库。构建关系图
创建SpringBoot项目,配置及代码如下:
4.0.0 org.springframework.boot spring-boot-starter-parent2.7.2 com.ddwang learngalaxybase0.0.1-SNAPSHOT learngalaxybase Demo project for Spring Boot 1.8 1.17.3 org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-devtoolsruntime true org.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest com.galaxybase galaxybase-bolt-driver3.4.0-SNAPSHOT system ${basedir}/lib/graphdbapi-bolt-driver-3.4.0.jar org.springframework.boot spring-boot-maven-pluginorg.projectlombok lombok
server: port: 8080 galaxybase: uri: bolt://192.168.208.60:7687 # 自行部署galaxybase-s 图数据库 username: admin password: admin database: learn # 创建的图数据库名称
import com.graphdbapi.driver.Graph;
import com.graphdbapi.driver.GraphDb;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ApplicationConfig {
@Value("${galaxybase.uri}")
private String uri;
@Value("${galaxybase.username}")
private String username;
@Value("${galaxybase.password}")
private String password;
@Value("${galaxybase.database}")
private String database;
@Bean
public Graph getGraph() {
return GraphDb.driver(uri, database, username, password);
}
}
测试代码
package com.ddwang;
import com.graphdbapi.driver.Edge;
import com.graphdbapi.driver.Graph;
import com.graphdbapi.driver.GraphSchema;
import com.graphdbapi.driver.Vertex;
import com.graphdbapi.driver.v1.graph.EdgeInfoByVertexPk;
import com.graphdbapi.driver.v1.graph.PropertyType;
import com.graphdbapi.driver.v1.graph.ResponseItem;
import com.graphdbapi.driver.v1.graph.VertexInfoByPk;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.*;
@SpringBootTest
@Slf4j
public class GraphOperateTest {
// 注入一个图实例
@Autowired
public Graph graph;
@Test
public void createVertexType(){
GraphSchema graphSchema = graph.schema();
// 构建点属性
Map propertyTypeSchema = new HashMap<>();
// 号码
propertyTypeSchema.put("telephone_number", PropertyType.STRING);
// 手否可用
propertyTypeSchema.put("is_use", PropertyType.BOOLEAN);
// 重复创建会抛异常,注意处理
// com.graphdbapi.driver.v1.exceptions.TypeFoundException: 点类型名称: 手机号码 已经存在了
graphSchema.createVertexType("手机号码", "id", propertyTypeSchema);
}
@Test
public void createCombinedEdgeType(){
// 按上述操作,再添加一个人 属性为名称name
// 如果不是通过程序操作,通过可视化界面操作的点类型,添加后一定要保存模型,否则程序操作时看不到
GraphSchema graphSchema = graph.schema();
// 创建一个无指向的关系类型 人--拥有--手机号码
// direct 设置为TRUE 标识该边是有指向的
graphSchema.createEdgeType("拥有", "人", "手机号码", true, null, null);
}
@Test
public void checkVertexTypeExist() {
Collection temp = graph.schema().getVertexTypes();
if(temp.contains("人")){
log.info("人这个节点类型已经存在");
}else{
log.info("人节点不存在,请先创建后再使用");
}
if(temp.contains("动物")){
log.info("动物这个节点类型已经存在");
}else{
log.info("动物节点不存在,请先创建后再使用");
}
}
@Test
public void createVertexInfoByPk(){
ArrayList nodes = new ArrayList<>();
Map personProperty1 = new HashMap<>();
personProperty1.put("name", "张三");
VertexInfoByPk nodeObj1 = new VertexInfoByPk("1","人",personProperty1);
nodes.add(nodeObj1);
Map personProperty2 = new HashMap<>();
personProperty2.put("name", "李四");
VertexInfoByPk nodeObj2 = new VertexInfoByPk("2","人",personProperty2); // 如果pk一样,则无法添加,注意返回提示添加成功,但是数据并未覆盖原始数据。新的数据也没更新到数据库中
nodes.add(nodeObj2);
Map mobileProperty1 = new HashMap<>();
mobileProperty1.put("telephone_number", "15126751890");
mobileProperty1.put("is_use", true);
VertexInfoByPk nodeObj3 = new VertexInfoByPk("1","手机号码",mobileProperty1); // 如果pk一样,则无法添加,注意返回提示添加成功,但是数据并未覆盖原始数据。新的数据也没更新到数据库中
nodes.add(nodeObj3);
Map mobileProperty2 = new HashMap<>();
mobileProperty2.put("telephone_number", "13816428651");
mobileProperty2.put("is_use", false);
VertexInfoByPk nodeObj4 = new VertexInfoByPk("2","手机号码",mobileProperty2); // 如果pk一样,则无法添加,注意返回提示添加成功,但是数据并未覆盖原始数据。新的数据也没更新到数据库中
nodes.add(nodeObj4);
List> responseItems = graph.insertVertexesByPk(nodes);// 添加完成后,查看可视化界面,确认点数据添加成功
log.info("成功添加节点:"+responseItems.size());
}
@Test
public void EdgeInfoByVertexPk(){
List edgeItems = new ArrayList<>();
// EdgeInfoByVertexPk(String fromPk, String fromType, String toPk, String toType, String type, Map property)
EdgeInfoByVertexPk edge1 = new EdgeInfoByVertexPk("1", "人", "1", "手机号码", "拥有", Collections.emptyMap());
EdgeInfoByVertexPk edge2 = new EdgeInfoByVertexPk("1", "人", "2", "手机号码", "拥有", Collections.emptyMap());
EdgeInfoByVertexPk edge3 = new EdgeInfoByVertexPk("2", "人", "1", "手机号码", "拥有", Collections.emptyMap());
EdgeInfoByVertexPk edge4 = new EdgeInfoByVertexPk("2", "人", "2", "手机号码", "拥有", Collections.emptyMap());
edgeItems.add(edge1);
edgeItems.add(edge2);
edgeItems.add(edge3);
edgeItems.add(edge4);
List> responseItems = graph.insertEdgesByVertexPk(edgeItems);
log.info("成功添加关系:"+ responseItems.size());
}
}
测试结果(创建操作也可在可视化界面操作,详情参看官方文档)
代码下载地址:
(29条消息) SpringBoot+galaxybase图数据库测试代码-Java文档类资源-CSDN文库https://download.csdn.net/download/oJuHao12345/86265838



