-1

我有建议说将元素存储在 arraylist 中然后进行比较,我也听到有人建议使用 HashMap。但是,我对 Java 非常陌生,我不知道什么是 arraylist,什么是 hashmap。有没有好心人来引导我?

我的 json 文件:

{"rel": "IsA", "weight": 0.5, "dataset": "/d/dbpedia/en", "sources": ["/s/dbpedia/3.7"], "id": "914e6775fd79d660bacf22ec699568e6694da3e8", "features": ["america_beautiful IsA -", "america_beautiful - national_anthem", "- IsA national_anthem"], "end": "national_anthem", "license": "/l/CC/By-SA", "uri": "/a/[IsA/,america_beautiful/,national_anthem/]", "start": "america_beautiful", "context": "/ctx/all", "surfaceText": null}
{"rel": "PartOf", "weight": 0.5, "dataset": "/d/dbpedia/en", "sources": ["/s/dbpedia/3.7"], "id": "a2c3ddf6b6237156dbeac84eea2801d35a9562d4", "features": ["ankara PartOf -", "ankara - central_anatolia_region", "- PartOf central_anatolia_region"], "end": "central_anatolia_region", "license": "/l/CC/By-SA", "uri": "/a/[PartOf/,ankara/,central_anatolia_region/]", "start": "ankara", "context": "/ctx/all", "surfaceText": null}
{"rel": "AtLocation", "weight": 0.5, "dataset": "/d/dbpedia/en", "sources": ["/s/dbpedia/3.7"], "id": "ec1914bfb0606c36376fbbcd316e5666022e2469", "features": ["apple_inc AtLocation -", "apple_inc - unite_state", "- AtLocation unite_state"], "end": "unite_state", "license": "/l/CC/By-SA", "uri": "/a/[AtLocation/,apple_inc/,unite_state/]", "start": "apple_inc", "context": "/ctx/all", "surfaceText": null}
{"rel": "AtLocation", "weight": 0.5, "dataset": "/d/dbpedia/en", "sources": ["/s/dbpedia/3.7"], "id": "0209765bf7185615ef242513f61ca0f61efe2a04", "features": ["audi AtLocation -", "audi - curitiba", "- AtLocation curitiba"], "end": "curitiba", "license": "/l/CC/By-SA", "uri": "/a/[AtLocation/,audi/,curitiba/]", "start": "audi", "context": "/ctx/all", "surfaceText": null}
{"rel": "AtLocation", "weight": 0.5, "dataset": "/d/dbpedia/en", "sources": ["/s/dbpedia/3.7"], "id": "5158d1cfce728efb3e42840d166ec06153a3d77d", "features": ["audi AtLocation -", "audi - ingolstadt", "- AtLocation ingolstadt"], "end": "unite_state", "license": "/l/CC/By-SA", "uri": "/a/[AtLocation/,audi/,ingolstadt/]", "start": "audi", "context": "/ctx/all", "surfaceText": null}

我的java文件:

package com.infinitegraph.samples.hellograph;

// Import all InfiniteGraph packages
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import com.infinitegraph.*;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
// Import SLF4J logging packages
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.infinitegraph.BaseVertex;
import java.util.HashMap;
import java.util.ArrayList;

public class HelloGraph
{

    public static void main(String[] args)
    {
                BufferedReader br = null;
                JSONParser parser = new JSONParser();

        // Set up logging for the HelloGraph class
        Logger logger = LoggerFactory.getLogger(HelloGraph.class);

        // Create null transaction, null graph database instance
        Transaction tx = null;
        GraphDatabase helloGraphDB = null;

        // Name for graph database and property file
        String graphDbName = "HelloGraph";
        String propertiesFileName = "HelloGraph.properties";    

        try
        {
            try
            {
                // Delete graph database if it already exists
                GraphFactory.delete(graphDbName, propertiesFileName);
            }
            catch (StorageException sE)
            {
                logger.info(sE.getMessage());
            }

            // HINT: Add code to create graph database and its contents
            // Create graph database
            logger.info("> Creating graph database ...");
            GraphFactory.create(graphDbName, propertiesFileName);

            // Open graph database
            logger.info("> Opening graph database ...");
            helloGraphDB = GraphFactory.open(graphDbName, propertiesFileName);

            // Begin transaction
            logger.info("> Starting a read/write transaction ...");
            tx = helloGraphDB.beginTransaction(AccessMode.READ_WRITE);

            //create head vertex
            HeaderVertex head = new HeaderVertex("JSON File");
            helloGraphDB.addVertex(head);

            try {

                String sCurrentLine;

                br = new BufferedReader(new FileReader("C:/Users/ji/Desktop/example.json"));

                while ((sCurrentLine = br.readLine()) != null) {

                    Object obj;

                    try {
                        obj = parser.parse(sCurrentLine);
                        JSONObject jsonObject = (JSONObject) obj;

                        //code to create vertex
                        logger.info("> Creating Start vertices ...");           
                        String start = (String) jsonObject.get("start");
                        StartVertex startV = new StartVertex(start);
                        helloGraphDB.addVertex(startV); 

                        String end = (String) jsonObject.get("end");
                        //System.out.println(end);
                        EndVertex endV = new EndVertex(end);
                        helloGraphDB.addVertex(endV);

                        String rel = (String) jsonObject.get("rel");
                        //System.out.println(rel);
                        logger.info("> Creating Relationship edge ...");
                        Relationship relationship1 = new Relationship("");
                        Relationship relationship2 = new Relationship(rel);

                        // Connect edges
                        logger.info("> Connecting vertices ...");
                        startV.addEdge(relationship1, head, EdgeKind.BIDIRECTIONAL, (short) 0);
                        endV.addEdge(relationship2, startV, EdgeKind.BIDIRECTIONAL, (short) 0);

                    } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (br != null)br.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }

            // Specify a named root for traversal
            logger.info("> Naming a root vertex ...");
            helloGraphDB.nameVertex("JSON File", head);

            // Commit to save your changes to the graph database
            logger.info("> Committing changes ...");
            tx.commit();
        }

        catch (ConfigurationException cE)
        {
            logger.warn("> Configuration Exception was thrown ... ");
            logger.error(cE.getMessage());
        }

        finally
        {
            // If the transaction was not committed, complete
            // will roll it back
            if (tx != null)
                tx.complete();
            if (helloGraphDB != null)
            {
                helloGraphDB.close();
                logger.info("> On Exit: Closed graph database");
            }
        }
    }

}

// HINT: Add class definitions for Person and Meeting classes here.
class HeaderVertex extends BaseVertex
{
    private String header;

    public HeaderVertex (String header)
    {
        setStartName(header);
    }

    public void setStartName (String header)
    {
        markModified();
        this.header = header;
    }

    public String toString()
    {
        fetch();
        return this.header;
    }
}

class StartVertex extends BaseVertex
{
    private String start;

    public StartVertex (String start)
    {
        setStartName(start);
    }

    public void setStartName (String start)
    {
        markModified();
        this.start = start;
    }

    public String toString()
    {
        fetch();
        return this.start;
    }
}

class EndVertex extends BaseVertex
{
    private String end;

    public EndVertex (String end)
    {
        setEndVertex(end);
    }

    public void setEndVertex (String end)
    {
        markModified();
        this.end = end;
    }

    public String toString()
    {
        fetch();
        return this.end;
    }
}

class Relationship extends BaseEdge
{
    private String rel;

    public Relationship (String rel)
    {
        setRelationship(rel);
    }

    public void setRelationship (String rel)
    {
        markModified();
        this.rel = rel;
    }

    public String toString()
    {
        fetch();
        return this.rel;
    }
}

感谢所有帮助!请帮我这个编码菜鸟!:( 谢谢!

4

1 回答 1

0

你可以在那里看到这两个类的官方文档。这应该让您知道他们有哪些可用的方法以及如何使用它们:)

如果您需要更多帮助,请随时询问:)

于 2012-09-28T03:25:38.050 回答