java操作TuGraph图数据库

  • Post author:
  • Post category:java


目前TuGraph关于java api相关的资料比较少,所以想做一个基于官网API的例子,结尾有相关代码的官方gitee地址,建议手动创建项目然后结合官方和本文的代码去实现。

package com.tugraph;
import java.io.IOException;
import com.antgroup.tugraph.TuGraphRpcClient;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TuGraphRpcClientDemo {
    static Logger log = LoggerFactory.getLogger(TuGraphRpcClientDemo.class);
    // 连接TuGraph 客户端对象
    TuGraphRpcClient client = new TuGraphRpcClient("list://ip:9090","admin", "73@TuGraph");

    /**
     * 先删除再创建
     * @param graphName
     */
    void deleteAndCreate(String graphName) {
        try {
            // delete graph
            client.callCypher(String.format("CALL dbms.graph.deleteGraph('%s')", graphName), "default", 1000);
        } catch (Exception e) {
            log.info(e.toString());
        }
        // create graph
        client.callCypher(String.format("CALL dbms.graph.createGraph('%s', 'this is a demo graph', 20)", graphName), "default", 1000);
    }

    @Test
    // import data by configuration file
    void demo1() throws IOException {
        String graphName = "demo1";
        deleteAndCreate(graphName);
        // create vertex and edge labels described in 'schema' section of `movie/import_for_java_demo.json`
        client.importSchemaFromFile("movie/import_for_java_demo.json", graphName, 1000);

        // get all vertex labels
        String res = client.callCypher("CALL db.vertexLabels()", graphName, 1000);
        log.info("CALL db.vertexLabels() : " + res);
        // get all edge labels
        res = client.callCypher("CALL db.edgeLabels()", graphName, 1000);
        log.info("CALL db.edgeLabels() : " + res);

        // import vertex and edge data described in 'files' section of `movie/import_for_java_demo.json`
        client.importDataFromFile("movie/import_for_java_demo.json",",", true, 4, 0, graphName, 10000);

        // count all vertexs
        String vertexCount = client.callCypher("MATCH (n) RETURN count(n)", graphName, 1000);
        log.info(vertexCount);
        // count all edges
        String edgeCount = client.callCypher("MATCH (n)-[r]->(m) RETURN count(r)", graphName, 1000);
        log.info(edgeCount);
    }

    //
    @Test
    // import data by data block
    void demo2() throws IOException {
        String graphName = "demo2";
        deleteAndCreate(graphName);
        // create vertex and edge labels described in 'schema' section of `movie/import_for_java_demo.json`
        client.importSchemaFromFile("movie/import_for_java_demo.json", graphName, 1000);
/*
{
    "files": [
        {
            "format": "CSV",
            "label": "person",
            "columns": ["id", "name", "born", "poster_image"]
        }
    ]
}
*/
        String personDesc = "{\n" +
                "            \"files\": [\n" +
                "                {\n" +
                "                    \"format\": \"CSV\",\n" +
                "                    \"label\": \"person\",\n" +
                "                    \"columns\": [\"id\", \"name\", \"born\", \"poster_image\"]\n" +
                "                }\n" +
                "            ]\n" +
                "        }";

/*
2,Laurence Fishburne,1961,https://image.tmdb.org/t/p/w185/mh0lZ1XsT84FayMNiT6Erh91mVu.jpg
3,Carrie-Anne Moss,1967,https://image.tmdb.org/t/p/w185/8iATAc5z5XOKFFARLsvaawa8MTY.jpg
4,Hugo Weaving,1960,https://image.tmdb.org/t/p/w185/3DKJSeTucd7krnxXkwcir6PgT88.jpg
5,Gloria Foster,1933,https://image.tmdb.org/t/p/w185/ahwiARgfOYctk6sOLBBk5w7cfH5.jpg
6,Joe Pantoliano,1951,https://image.tmdb.org/t/p/w185/zBvDX2HWepvW9im6ikgoyOL2Xj0.jpg
7,Marcus Chong,1967,https://image.tmdb.org/t/p/w185/zYfXjMszFajTb93phn2Fi6LwEGN.jpg
8,Matt Doran,1976,https://image.tmdb.org/t/p/w185/gLpWm3azLiXgDPRWo23AnG5WM7O.jpg
9,Anthony Ray Parker,1958,https://image.tmdb.org/t/p/w185/iMHr0onfM8v4uVdPVnXxXx2xwwN.jpg
10,Keanu Reeves,1964,https://image.tmdb.org/t/p/w185/id1qIb7cZs2eQno90KsKwG8VLGN.jpg
*/

        String personData = "2,Laurence Fishburne,1961,https://image.tmdb.org/t/p/w185/mh0lZ1XsT84FayMNiT6Erh91mVu.jpg\n" +
                "3,Carrie-Anne Moss,1967,https://image.tmdb.org/t/p/w185/8iATAc5z5XOKFFARLsvaawa8MTY.jpg\n" +
                "4,Hugo Weaving,1960,https://image.tmdb.org/t/p/w185/3DKJSeTucd7krnxXkwcir6PgT88.jpg\n" +
                "5,Gloria Foster,1933,https://image.tmdb.org/t/p/w185/ahwiARgfOYctk6sOLBBk5w7cfH5.jpg\n" +
                "6,Joe Pantoliano,1951,https://image.tmdb.org/t/p/w185/zBvDX2HWepvW9im6ikgoyOL2Xj0.jpg\n" +
                "7,Marcus Chong,1967,https://image.tmdb.org/t/p/w185/zYfXjMszFajTb93phn2Fi6LwEGN.jpg\n" +
                "8,Matt Doran,1976,https://image.tmdb.org/t/p/w185/gLpWm3azLiXgDPRWo23AnG5WM7O.jpg\n" +
                "9,Anthony Ray Parker,1958,https://image.tmdb.org/t/p/w185/iMHr0onfM8v4uVdPVnXxXx2xwwN.jpg\n" +
                "10,Keanu Reeves,1964,https://image.tmdb.org/t/p/w185/id1qIb7cZs2eQno90KsKwG8VLGN.jpg";

        // import vertex data of 'person' label
        // personData : data block in csv format
        // personDesc : describe the detailed format of the data block
        client.importDataFromContent(personDesc, personData, ",", true, 4, graphName, 10000);

/*
{
    "files": [
        {
          "format": "CSV",
          "label": "movie",
          "columns": ["id","title","tagline","summary","poster_image","duration","rated"]
        }
    ]
}
*/
        String moiveDesc = "{\n" +
                "    \"files\": [\n" +
                "        {\n" +
                "          \"format\": \"CSV\",\n" +
                "          \"label\": \"movie\",\n" +
                "          \"columns\": [\"id\",\"title\",\"tagline\",\"summary\",\"poster_image\",\"duration\",\"rated\"]\n" +
                "        }\n" +
                "    ]\n" +
                "}";

/*
82,Pulp Fiction,Just because you are a character doesn't mean you have character.,placeholder text,http://image.tmdb.org/t/p/w185/dM2w364MScsjFf8pfMbaWUcWrR.jpg,154,R
130,Cloud Atlas,Everything is Connected,placeholder text,http://image.tmdb.org/t/p/w185/k9gWDjfXM80iXQLuMvPlZgSFJgR.jpg,172,R
457,The Shawshank Redemption,Fear can hold you prisoner. Hope can set you free.,placeholder text,http://image.tmdb.org/t/p/w185/9O7gLzmreU0nGkIB6K3BsJbzvNv.jpg,142,R
471,The Godfather,An offer you can't refuse.,placeholder text,http://image.tmdb.org/t/p/w185/d4KNaTrltq6bpkFS01pYtyXa09m.jpg,175,R
496,The Godfather: Part II,I don't feel I have to wipe everybody out\ Tom. Just my enemies.,placeholder text,http://image.tmdb.org/t/p/w185/tHbMIIF51rguMNSastqoQwR0sBs.jpg,200,R
517,The Good\ the Bad and the Ugly,For three men the Civil War wasn't hell. It was practice.,placeholder text,http://image.tmdb.org/t/p/w185/8PD1dgf0kQHtRawoSxp1jFemI1q.jpg,161,R
532,The Dark Knight,Why So Serious?,placeholder text,http://image.tmdb.org/t/p/w185/1hRoyzDtpgMU7Dz4JF22RANzQO7.jpg,152,PG-13
564,The Dark Knight Rises,The Legend Ends,placeholder text,http://image.tmdb.org/t/p/w185/dEYnvnUfXrqvqeRSqvIEtmzhoA8.jpg,165,PG-13
*/
        String moiveData = "82,Pulp Fiction,Just because you are a character doesn't mean you have character.,placeholder text,http://image.tmdb.org/t/p/w185/dM2w364MScsjFf8pfMbaWUcWrR.jpg,154,R\n" +
                "130,Cloud Atlas,Everything is Connected,placeholder text,http://image.tmdb.org/t/p/w185/k9gWDjfXM80iXQLuMvPlZgSFJgR.jpg,172,R\n" +
                "457,The Shawshank Redemption,Fear can hold you prisoner. Hope can set you free.,placeholder text,http://image.tmdb.org/t/p/w185/9O7gLzmreU0nGkIB6K3BsJbzvNv.jpg,142,R\n" +
                "471,The Godfather,An offer you can't refuse.,placeholder text,http://image.tmdb.org/t/p/w185/d4KNaTrltq6bpkFS01pYtyXa09m.jpg,175,R\n" +
                "496,The Godfather: Part II,I don't feel I have to wipe everybody out\\ Tom. Just my enemies.,placeholder text,http://image.tmdb.org/t/p/w185/tHbMIIF51rguMNSastqoQwR0sBs.jpg,200,R\n" +
                "517,The Good\\ the Bad and the Ugly,For three men the Civil War wasn't hell. It was practice.,placeholder text,http://image.tmdb.org/t/p/w185/8PD1dgf0kQHtRawoSxp1jFemI1q.jpg,161,R\n" +
                "532,The Dark Knight,Why So Serious?,placeholder text,http://image.tmdb.org/t/p/w185/1hRoyzDtpgMU7Dz4JF22RANzQO7.jpg,152,PG-13\n" +
                "564,The Dark Knight Rises,The Legend Ends,placeholder text,http://image.tmdb.org/t/p/w185/dEYnvnUfXrqvqeRSqvIEtmzhoA8.jpg,165,PG-13";

        // import vertex data of 'moive' label
        // moiveData : data block in csv format
        // moiveDesc : describe the detailed format of the data block
        client.importDataFromContent(moiveDesc, moiveData, ",", true, 4, graphName, 10000);



/*
{
    "files": [
        {
            "format":"CSV",
            "label":"acted_in",
            "SRC_ID":"person",
            "DST_ID":"movie",
            "columns": ["SRC_ID", "DST_ID", "role"]
        }
    ]
}
*/
        String actedInDesc = "{\n" +
                "    \"files\": [\n" +
                "        {\n" +
                "            \"format\":\"CSV\",\n" +
                "            \"label\":\"acted_in\",\n" +
                "            \"SRC_ID\":\"person\",\n" +
                "            \"DST_ID\":\"movie\",\n" +
                "            \"columns\": [\"SRC_ID\", \"DST_ID\", \"role\"]\n" +
                "        }\n" +
                "    ]\n" +
                "}";

/*
2,82,Morpheus
2,130,Morpheus
2,457,Morpheus
3,496,Trinity
3,517,Trinity
3,564,Trinity
*/

        String actedInData = "2,82,Morpheus\n" +
                "2,130,Morpheus\n" +
                "2,457,Morpheus\n" +
                "3,496,Trinity\n" +
                "3,517,Trinity\n" +
                "3,564,Trinity";

        // import edge data of 'acted_in' label
        // actedInData : data block in csv format
        // actedInDesc : describe the detailed format of the data block
        client.importDataFromContent(actedInDesc, actedInData, ",", true, 4, graphName, 10000);

        // get all vertex labels
        String res = client.callCypher("CALL db.vertexLabels()", graphName, 1000);
        log.info("CALL db.vertexLabels() : " + res);
        // get all edge labels
        res = client.callCypher("CALL db.edgeLabels()", graphName, 1000);
        log.info("CALL db.edgeLabels() : " + res);

        // count all vertexs
        String vertexCount = client.callCypher("MATCH (n) RETURN count(n)", graphName, 1000);
        log.info(vertexCount);
        // count all edges
        String edgeCount = client.callCypher("MATCH (n)-[r]->(m) RETURN count(r)", graphName, 1000);
        log.info(edgeCount);
    }

    @Test
    // create vertex and edge labels by cypher statements
    // create vertex and edge data by cypher statements
    void demo3() throws IOException {
        String graphName = "demo3";
        deleteAndCreate(graphName);
        // create vertex `person` label
        client.callCypher("CALL db.createVertexLabel(" +
                "'person'," +  // vertex name
                "'id'," +      // primary property
                "'id', int32, false," +
                "'name', string, false," +
                "'born', int32, true," +
                "'poster_image', string, true" +
                ")", graphName, 1000);

        // create vertex `movie` label
        client.callCypher("CALL db.createVertexLabel(" +
                "'movie'," +  // vertex name
                "'id'," +     // primary property
                "'id',int32, false," +
                "'title', string, false," +
                "'tagline', string, false," +
                "'summary', string, true," +
                "'poster_image', string, true," +
                "'duration', int32, false," +
                "'rated', string, true" +
                ")", graphName, 1000);

        // create edge `acted_in` label
        client.callCypher(("CALL db.createEdgeLabel(" +
                "'acted_in'," + // edge name
                "'[]'," +      // edge constraints. empty array means no constraints;
                "'role', string, false)"), graphName, 1000);

        // doc-zh/3.developer-document/2.cypher.md

        // 2,Laurence Fishburne,1961,https://image.tmdb.org/t/p/w185/mh0lZ1XsT84FayMNiT6Erh91mVu.jpg
        // create vertex by cypher
        client.callCypher("CREATE (n:person {" +
                "id: 2," +
                "name: 'Laurence Fishburne'," +
                "born: 1961," +
                "poster_image: 'https://image.tmdb.org/t/p/w185/mh0lZ1XsT84FayMNiT6Erh91mVu.jpg'})", graphName, 1000);

        // 130,Cloud Atlas,Everything is Connected,placeholder text,http://image.tmdb.org/t/p/w185/k9gWDjfXM80iXQLuMvPlZgSFJgR.jpg,172,R
        // create vertex by cypher
        client.callCypher("CREATE (n:movie {" +
                "id: 130," +
                "title: 'Cloud Atlas'," +
                "tagline:'Everything is Connected'," +
                "summary: 'placeholder text'," +
                "poster_image: 'http://image.tmdb.org/t/p/w185/k9gWDjfXM80iXQLuMvPlZgSFJgR.jpg'," +
                "duration: 172," +
                "rated: 'R'})", graphName, 1000);

        // create edge by cypher
        client.callCypher("MATCH (a:person), (b:movie) WHERE a.id = 2 AND b.id = 130 CREATE (a)-[r:acted_in {role: 'Morpheus'}]->(b)", graphName, 1000);

        // get all vertex labels
        String res = client.callCypher("CALL db.vertexLabels()", graphName, 1000);
        log.info("CALL db.vertexLabels() : " + res);
        // get all edge labels
        res = client.callCypher("CALL db.edgeLabels()", graphName, 1000);
        log.info("CALL db.edgeLabels() : " + res);

        // count all vertexs
        String vertexCount = client.callCypher("MATCH (n) RETURN count(n)", graphName, 1000);
        log.info(vertexCount);
        // count all edges
        String edgeCount = client.callCypher("MATCH (n)-[r]->(m) RETURN count(r)", graphName, 1000);
        log.info(edgeCount);
    }
}

pom文件

<!--
  ~ Copyright (c) 2022 AntGroup, Inc. All Rights Reserved.
  -->

<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>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.sonatype.oss</groupId>
        <artifactId>oss-parent</artifactId>
        <version>7</version>
    </parent>

    <groupId>com.antgroup.tugraph</groupId>
    <artifactId>tugraph-rpc-client-demo</artifactId>
    <version>3.1.0</version>
    <!--    <packaging>pom</packaging>-->

    <licenses>
        <license>
            <name>Apache 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
            <distribution>repo</distribution>
        </license>
    </licenses>

    <profiles>
        <profile>
            <id>release</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-gpg-plugin</artifactId>
                        <version>1.6</version>
                        <executions>
                            <execution>
                                <id>sign-artifacts</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <version>3.1.0</version>
                        <executions>
                            <execution>
                                <id>attach-javadoc</id>
                                <phase>compile</phase>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <doclint>none</doclint>
                            <charset>UTF-8</charset>
                            <encoding>UTF-8</encoding>
                            <docencoding>UTF-8</docencoding>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
            <distributionManagement>
                <snapshotRepository>
                    <id>oss</id>
                    <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
                </snapshotRepository>
                <repository>
                    <id>oss</id>
                    <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
                </repository>
            </distributionManagement>
        </profile>
    </profiles>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.tugraph.TuGraphRpcClientDemo</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.tugraph.TuGraphRpcClientDemo</mainClass>
                        </manifest>jav
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>assemble-all</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


    <dependencies>
        <dependency>
            <groupId>com.antgroup.tugraph</groupId>
            <artifactId>tugraph-java-rpc-client</artifactId>
            <version>0.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.83</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.13.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.17.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

配置log4j.properties

log4j.rootLogger = info,stdout,D,E

log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS}ms, [%t]:%m%n

官方代码地址包含测试使用的文件:https://gitee.com/tugraph/tugraph-db/tree/master/demo/JavaClientDemo



版权声明:本文为qq_43259670原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。