Frage

I have search and read related things for whole night, but i can't get any example which can run.

public class TestNeo4j {
public enum RelTypes implements RelationshipType {
    HASFOLLOW
}

private static final String MATRIX_DB = "target/matrix-db";
private GraphDatabaseService graphDb;
private long matrixNodeId;

public static void main(String[] args) throws IOException {
    TestNeo4j matrix = new TestNeo4j();
    matrix.setUp();
    matrix.shutdown();
}

public void setUp() throws IOException {
    deleteRecursively(new File(MATRIX_DB));
    graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(MATRIX_DB);
    registerShutdownHook();
    createNodespace();
}

public void shutdown() {
    graphDb.shutdown();
}

public void createNodespace() throws IOException {

    try (Transaction tx = graphDb.beginTx()) {

        FileReader fr = new FileReader("twitter.txt");
        BufferedReader br = new BufferedReader(fr);
        String line = "";


        while ((line = br.readLine()) != null) {
            //read data like "1 1" 
                            //               "2 3"
            String s = line;
            String[] sa = s.split(" ");
            //build relation twitter and his follow each line  
            //In my program, for every line, it will create a new
                            //For example, I will get 1-2, 1-3, 1-4, but I want it to be
                            //     / 4
                            //   1 - 3                                                          
                            //     \ 2
                            //!!!!i want to use unique node how to change it!!!!
            Node node1 = graphDb.createNode();
            Node node2 = graphDb.createNode();
            node1.setProperty("id", sa[0]);
            node2.setProperty("id", sa[1]);

            node1.createRelationshipTo(node2, RelTypes.HASFOLLOW);
        }

        tx.success();       }

}

Could you help me to implement it? In addition, I run JavaQuery.javaA example about query always get a empty iteration.

War es hilfreich?

Lösung

Are you sure that twitter.txt is on your classpath and you're not getting a FileNotFoundException? You might want to check that first. If the FileReader can't find your file, no nodes and relationships will be created.

Although I'm not really sure why your example does not work, I do have a couple of suggestions. First of all, I'd get rid of the "id" property as it's very confusing. Every node already has an id so you'd want to change the property name to something else. Second of all, since you are clearly using java 7, you can make use of the AutoClosable interface of FileReader and BufferedReader as well.

When I run the JavaQuery class, I do get the expected results. It's again really strange that your experiencing different behavior. Why don't you try the simplest example of them all? See what output you get from the following script:

public class TestNeo4j {

    public static void main(String[] args) throws Exception {
        final GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase("db");
        final Transaction tx = graphDb.beginTx();
        final Node node = graphDb.createNode();
        node.setProperty("test", "test");
        tx.success();
        tx.close();
        final ExecutionEngine engine = new ExecutionEngine(graphDb);
        System.out.println(engine.execute("START n=node(*) RETURN id(n), n.test").dumpToString());
        graphDb.shutdown();
    }

}

Your output should be similar to:

+----------------+
| id(n) | n.test |
+----------------+
| 0     | "test" |
+----------------+
1 row

Let me know your results.

EDIT:

If you want to create unique nodes (or paths), use Cyphers MERGE command. It can create unique nodes and/or full paths. An example:

MERGE (tweep1:Tweep{user: "twitter_account"})
MERGE (tweep1)-[:FOLLOWS]->(tweep2:Tweep{user: "another_account"})

This will create or use node "tweep1" if there is already a matching node. That node will be used to create a relationship to node "tweep2". Does this more or less answer your question?

Andere Tipps

I have dealt with the unique node question by building hashmap thought this is a pristine method.

public void createNodespace() throws IOException {

    try (Transaction tx = graphDb.beginTx()) {

        FileReader fr = new FileReader("twitter.txt");
        BufferedReader br = new BufferedReader(fr);
        String line = "";

        Map<String, Node> check = new HashMap<String, Node>();
        while ((line = br.readLine()) != null) {

            String s = line;
            String[] sa = s.split(" ");



            if (!check.containsKey(sa[0])) {
                Node node1 = graphDb.createNode();
                node1.setProperty("id", sa[0]);
                check.put(sa[0], node1);
            }
            if (!check.containsKey(sa[1])) {
                Node node2 = graphDb.createNode();
                node2.setProperty("id", sa[1]);
                check.put(sa[1], node2);
            }

            check.get(sa[0]).createRelationshipTo(check.get(sa[1]), RelTypes.HASFOLLOW);


        }

        tx.success();
    }

}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top