应用场景
社区论坛
galaxybase官网登陆账号密码都是手机号
安装
文档 – 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端口)
demo实战
需求描述:使用galaxybase 驱动包,通过接口、文件、数据库等数据源,获取数据解析入库。构建关系图
创建SpringBoot项目,配置及代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://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.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ddwang</groupId>
<artifactId>learngalaxybase</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>learngalaxybase</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<testcontainers.version>1.17.3</testcontainers.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.galaxybase</groupId>
<artifactId>galaxybase-bolt-driver</artifactId>
<version>3.4.0-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${basedir}/lib/graphdbapi-bolt-driver-3.4.0.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
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);
}
}
上述创建,无法自定义设置连接参数
@Value("${galaxybase.maxConnections}")
private Integer maxConnections;
@Bean
public Graph getGraph() {
Config config = Config.build().
withMaxConnectionPoolSize(maxConnections).
withConnectionTimeout(2147483647L, TimeUnit.MILLISECONDS).
withLogging(new ConsoleLogging(Level.OFF)).toConfig();
return GraphDb.driver(GraphDatabase.driver(URI.create(uri), AuthTokens.basic(username, password), config),database);
}
测试代码
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<String, PropertyType> 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<String> 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<VertexInfoByPk> nodes = new ArrayList<>();
Map<String, Object> personProperty1 = new HashMap<>();
personProperty1.put("name", "张三");
VertexInfoByPk nodeObj1 = new VertexInfoByPk("1","人",personProperty1);
nodes.add(nodeObj1);
Map<String, Object> personProperty2 = new HashMap<>();
personProperty2.put("name", "李四");
VertexInfoByPk nodeObj2 = new VertexInfoByPk("2","人",personProperty2); // 如果pk一样,则无法添加,注意返回提示添加成功,但是数据并未覆盖原始数据。新的数据也没更新到数据库中
nodes.add(nodeObj2);
Map<String, Object> mobileProperty1 = new HashMap<>();
mobileProperty1.put("telephone_number", "15126751890");
mobileProperty1.put("is_use", true);
VertexInfoByPk nodeObj3 = new VertexInfoByPk("1","手机号码",mobileProperty1); // 如果pk一样,则无法添加,注意返回提示添加成功,但是数据并未覆盖原始数据。新的数据也没更新到数据库中
nodes.add(nodeObj3);
Map<String, Object> mobileProperty2 = new HashMap<>();
mobileProperty2.put("telephone_number", "13816428651");
mobileProperty2.put("is_use", false);
VertexInfoByPk nodeObj4 = new VertexInfoByPk("2","手机号码",mobileProperty2); // 如果pk一样,则无法添加,注意返回提示添加成功,但是数据并未覆盖原始数据。新的数据也没更新到数据库中
nodes.add(nodeObj4);
List<ResponseItem<Vertex>> responseItems = graph.insertVertexesByPk(nodes);// 添加完成后,查看可视化界面,确认点数据添加成功
log.info("成功添加节点:"+responseItems.size());
}
/**
* 创建边数据
*/
@Test
public void EdgeInfoByVertexPk(){
List<EdgeInfoByVertexPk> edgeItems = new ArrayList<>();
// EdgeInfoByVertexPk(String fromPk, String fromType, String toPk, String toType, String type, Map<String, Object> 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<ResponseItem<Edge>> responseItems = graph.insertEdgesByVertexPk(edgeItems);
log.info("成功添加关系:"+ responseItems.size());
}
}
测试结果(创建操作也可在可视化界面操作,详情参看官方文档)
代码下载地址:
今天的文章【图数据库】galaxybase介绍与使用分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/24544.html