版本

# eZooDB-JAVA-SDK使用手册

本文档维护了eZooDB-JAVA-SDK的安装,使用教程。


# JAVA SDK

首先下载需要的SDK eZooDB-JAVA-SDK,然后安装下载好的SDK。

# mvn离线导入jar包到本地maven仓库

mvn install:install-file -Dfile=eZooDB-JAVA-SDK.jar -DgroupId=com.ezoodb.javasdk -DartifactId=client -Dversion=1.0.0-SNAPSHOT -Dpackaging=jar

# Maven

在项目的pom.xml的dependencies中加入以下内容:

<dependency>
    <groupId>com.ezoodb.javasdk</groupId>
    <artifactId>client</artifactId>
	<version>1.0.0-SNAPSHOT</version>
</dependency>

# Gradle

implementation 'com.ezoodb.javasdk:client:1.0.0-SNAPSHOT'

# 使用示例

# spring boot

在项目application.yml中加入以下内容:

com:
  ezoodb:
    urls: ezoodb://192.168.1.137:9090                                #ip:port请替换为ezoodb的部署机器ip以及启动端口
    dbname: modelroom1                                               #dbname,可以不指定
    initializationFailTimeout: 0                                     #小于0时,初始化不检查db是否可连接性
    connectionTimeout: 5000                                          #单位毫秒
    class-name: com.ezoodb.javasdk.client.datasource.EZooDataSource  

spring properties:

@Data
@Component
@ConfigurationProperties(prefix = "com.ezoodb")
public class EZooDBProperties {
    private String className;
    private String urls;
    private String dbname;
    private int initializationFailTimeout;
    private int connectionTimeout;
}

spring configuration:

@Configuration
@EnableConfigurationProperties({EzooDBProperties.class})
public class EzooDBAutoConfiguration {

    @Bean
    @Qualifier("EZooDataSource")
    public EZooHikariDataSource getEZooDBSource(EZooDBProperties properties) {
        Properties property = new Properties();
        property.setProperty("dataSourceClassName", properties.getClassName());
        property.setProperty("dataSource.connectUrl", properties.getUrls());
        property.setProperty("initializationFailTimeout", String.valueOf(properties.getInitializationFailTimeout()));
        property.setProperty("connectionTimeout", String.valueOf(properties.getConnectionTimeout()));
        EZooHikariConfig config = new EZooHikariConfig(property);
        return new EZooHikariDataSource(config);
    }
}

查询指定节点邻居数量(api请见query_neighbour_count)

@Service
public class Test {

    @Autowired
    @Qualifier("EZooDataSource")
    private EZooHikariDataSource ezooDBSource;
    
    public neighbour_count countNeighbours() {
        try (Connection conn = ezooDBSource.getConnection()) {
            neighbour_count neighbour_count = ((EnhancedConnection)conn).getEZooClient().query_neighbour("dbName", "nodeId", 1, 3, "", 2);
            return neighbour_count;
        } catch (TException | SQLException ex) {
            
        }
    }
}

查询指定节点邻居(api请见query_neighbours

public void queryNeighbours() {
        try (Connection conn = ezooDBSource.getConnection()) {
            neighbour_v2 neighbours = ((EnhancedConnection) conn).getEZooClient().query_neighbour("dbName",
                    "nodeId",
                    1,
                    10,
                    search_direction_type.search_direction_in_out,
                    "");
            if (0 == neighbours.getStatus()) {
                List<Map<Integer, Set<Integer>>> nodes = neighbours.getNodes();
                for (Map<Integer, Set<Integer>> nodeNeighborMap : nodes) {
                    //每一层的map,key对于上一层邻居的点,value对应该点的邻居,第一层的key是查询时的node_id
                    for (Map.Entry<Integer, Set<Integer>> entry : nodeNeighborMap.entrySet()) {
                        System.out.println("点id :" + entry.getKey() + " 邻居为: " + entry.getValue().stream().map(Object::toString).collect(Collectors.joining(",")));
                    }
                }
            }
        }
    }

查询子图(api请见query_subgraph

public void querySubGraph() {
        try (Connection conn = ezooDBSource.getConnection()) {
            graph_v2 graph = ((EnhancedConnection) conn).getEZooClient().query_subgraph("dbName",
                    "nodeId",
                    1,
                    5,
                    search_direction_type.search_direction_out,
                    "");
            if (0 == graph.getStatus()) {
                List<edge_basic> edgeBasicList = graph.getRelations();
                edgeBasicList.forEach(x -> {
                    System.out.println("边id: " + x.getEdge_id() + ",起点id: " + x.getSrc_node_id() + ",终点id: " + x.getDest_node_id());
                });
            }

        }
    }

更多api请见 API


注意:EZooHikariDataSource实现了java.sql.DataSource接口,如果要使用本sdk的项目中有使用别的实现JDBC标准的数据库,请配置多数据源,分别注入datasource,避免冲突。

最近一次更新时间: 6/15/2022, 3:22:10 AM