Neo4j建立自動索引

1、建立Neo4j的Legacy indexinghtml

  1.爲節點建立索引node

  官方API的建立示例爲:數據庫

將一節點添加至索引:api

public static void AddNodeIndex(String nid)
    {
        String txUri=SERVER_ROOT_URI+"index/node/favorites";
        WebResource resource = Client.create().resource(txUri);
        String entity="{\"value\" : \"n204\",\"uri\" : \"http://192.168.209.128:7474/db/data/node/"+nid+"\",\"key\" : \"n201\"}";
        ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
                .type(MediaType.APPLICATION_JSON).entity(entity)
                .post(ClientResponse.class);

        System.out.println(response.getStatus());
        System.out.println(response.getEntity(String.class));
        response.close();
    }

ps:nid是要添加索引的節點ID  value是索引值  key是索引名稱app

2.經過屬性查找節點post

public static void GetNodeByIndex()
    {
        String txUri=SERVER_ROOT_URI+"index/node/favorites/n201/n201";
        WebResource resource = Client.create().resource(txUri);
        
        ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
                .type(MediaType.APPLICATION_JSON)
                .get(ClientResponse.class);

        System.out.println(response.getStatus());
        System.out.println(response.getEntity(String.class));
        response.close();
    }

txUri路徑中:favorites爲剛建立的索引名稱,第一個n201是節點索引key,第二個n201是節點索引值測試

2、自動建立索引(Legacy Automatic Indexes)ui

What default configuration means depends on how you have configured your database. If you haven’t
changed any indexing configuration, it means the indexes will be using a Lucene-based backend.this

數據庫配置以後,就能夠自動建立索引編碼

1.配置文件配置

Auto-indexing must be enabled through configuration before we can create or configure them. Firstly
ensure that you’ve added some config like this into your server’s conf/neo4j.properties file:

打開conf/neo4j.properties文件如圖

配置下面的節點

# Enable auto-indexing for nodes, default is false.
node_auto_indexing=true

# The node property keys to be auto-indexed, if enabled.
node_keys_indexable=name,ki

# Enable auto-indexing for relationships, default is false.
relationship_auto_indexing=true

# The relationship property keys to be auto-indexed, if enabled.
relationship_keys_indexable=name,ki

Node_keys_indexable、relationship_keys_indexable對應節點、關係的屬性

配置完成以後重啓服務

重啓三個節點的集羣

2。測試索引

插入一個節點和關係

    // 建立節點
    @Test
    public void test2() {
        URI uri = CreateSimpleGraph.createNode();
        CreateSimpleGraph.addProperty(uri, "name", "張三");
        URI uri1 = CreateSimpleGraph.createNode();
        CreateSimpleGraph.addProperty(uri1, "name", "李四");
    }
// 爲節點設置關係
    @Test
    public void test6() {
        for (int i = 1; i < 2; i++) {
            try {
                URI suri = new URI("http://192.168.209.128:7474/db/data/node/171391");
                String uri1="http://192.168.209.128:7474/db/data/node/";
                URI euri = new URI("http://192.168.209.128:7474/db/data/node/171392");
                URI reluri= CreateSimpleGraph.addRelationship(suri, euri, "家人","{\"ki\" : \"1234567890\", \"name\" : \"無\" }");
                System.out.println(reluri);
            } catch (URISyntaxException e) {
                // 異常信息輸出該內容
                e.printStackTrace();
            }
        }
    }

3.經過屬性查找節點

public static void GetNodeByAutoIndex(String ki)
    {
//        String txUri=SERVER_ROOT_URI+"index/node/node_auto_index/name/"+ki;
        String txUri=SERVER_ROOT_URI+"index/auto/node/ki/"+ki;
        WebResource resource = Client.create().resource(txUri);
        
        ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
                .type(MediaType.APPLICATION_JSON)
                .get(ClientResponse.class);

        System.out.println(response.getStatus());
        System.out.println(response.getEntity(String.class));
        response.close();
    }
    
    public static void GetRelationshipByAutoIndex(String ki)
    {
//        String txUri=SERVER_ROOT_URI+"index/node/node_auto_index/name/"+ki;
        String txUri=SERVER_ROOT_URI+"index/auto/relationship/ki/"+ki;
        WebResource resource = Client.create().resource(txUri);
        
        ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
                .type(MediaType.APPLICATION_JSON)
                .get(ClientResponse.class);

        System.out.println(response.getStatus());
        System.out.println(response.getEntity(String.class));
        response.close();
    }

關係的輸出結果爲:

[ {
  "extensions" : { },
  "metadata" : {
    "id" : 337,
    "type" : "家人"
  },
  "data" : {
    "name" : "",
    "ki" : "1234567890"
  },
  "property" : "http://192.168.209.128:7474/db/data/relationship/337/properties/{key}",
  "start" : "http://192.168.209.128:7474/db/data/node/171391",
  "self" : "http://192.168.209.128:7474/db/data/relationship/337",
  "end" : "http://192.168.209.128:7474/db/data/node/171392",
  "type" : "家人",
  "properties" : "http://192.168.209.128:7474/db/data/relationship/337/properties"
} ]

這裏說明一下,傳值爲中文的時候,查詢不出來,可能須要編碼,由於工做暫時沒有用到,就沒有再研究了

3、NEO4J批量插入和用戶密碼問題

1.批量操做

批量操做的官方文檔地址:http://neo4j.com/docs/milestone/rest-api-batch-ops.html

public static int BatchInserterNode(String body) {
        // String
        // body="[{\"method\":\"POST\",\"to\":\"/node\",\"body\":{\"name\":\"aaa\",\"lead\":\"aaa1\"},\"id\":0},{\"method\":\"POST\",\"to\":\"/node\",\"body\":{\"name\":\"bbb\",\"lead\":\"bbb1\"},\"id\":1}]";
        // POST {} to the node entry point URI
        ClientResponse response = GetResourceInstance("batch")
                .accept(MediaType.APPLICATION_JSON)
                .type(MediaType.APPLICATION_JSON).entity(body)
                .post(ClientResponse.class);

        // System.out.println(String.format(
        // "POST to [%s], status code [%d], location header [%s]",
        // nodeEntryPointUri, response.getStatus(), location.toString()));
        String entity = response.getEntity(String.class);
        response.close();

        int status = response.getStatus();

        return status;
    }

//調用 
//批量插入速度測試
    @Test
    public void test10()
    {
    
        StringBuilder sbody=new StringBuilder();
        sbody.append("[");
        for (int i = 161337; i < 171337; i++) {
            if(i>161337)
            {
                sbody.append(",");
            }
            sbody.append("{");
            sbody.append("\"method\":\"POST\",\"to\":\"/node\",\"body\":{\"name\":\"n"+i+"\",\"lead\":\"a"+i+"\"},\"id\":"+i+"");
            sbody.append("}");
        }
        
        
        sbody.append("]");
        long st = System.currentTimeMillis();
        int status= CreateSimpleGraph.BatchInserterNode(sbody.toString());
        System.out.println("插入狀態:"+status);
        long et = System.currentTimeMillis();
        System.out.println("插入10000條數據總共耗時:" + (et - st) + "毫秒");        
    }

批量插入的速度10000條大概在6秒左右吧

2.NEO4J密碼操做

本次用的版本是:neo4j-enterprise-2.2.0-unix,鏈接數據庫的時候須要輸入用戶名稱密碼。上個版本好像是不用的。

使用密碼時:

public void checkDatabaseIsRunning() {
        // START SNIPPET: checkServer
        WebResource resource = Client.create().resource(SERVER_ROOT_URI);
        ClientResponse response = resource.get(ClientResponse.class);
        resource.addFilter(new HTTPBasicAuthFilter("neo4j", "123456"));
        if (response.getStatus() == 200) {
            System.out.println("鏈接成功!");
        } else {
            System.out.println("鏈接失敗!");
        }
        // System.out.println(String.format("GET on [%s], status code [%d]",
        // SERVER_ROOT_URI, response.getStatus()));
        response.close();
        // END SNIPPET: checkServer
    }

如不須要使用密碼,能夠在配置文件進行配置

vi /conf/neo4j-server.properties這個文件

這裏改爲false便可,默認是True

 

參考文檔

http://neo4j.com/docs/milestone/rest-api-batch-ops.html

http://neo4j.com/docs/milestone/rest-api-auto-indexes.html

http://neo4j.com/docs/milestone/rest-api-security.html

相關文章
相關標籤/搜索