一、创建Neo4j的Legacy indexing

  1.为节点创建索引

  官方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是索引名称

2.通过属性查找节点

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是节点索引值

二、自动创建索引(Legacy Automatic Indexes)

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.

数据库配置之后,就可以自动创建索引

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 = ; i < ; 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" : ,
"type" : "家人"
},
"data" : {
"name" : "无",
"ki" : ""
},
"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"
} ]

这里说明一下,传值为中文的时候,查询不出来,可能需要编码,因为工作暂时没有用到,就没有再研究了

三、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 = ; i < ; i++) {
if(i>)
{
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", ""));
if (response.getStatus() == ) {
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

Neo4j创建自动索引的更多相关文章

  1. Eclipse下maven使用嵌入式(Embedded)Neo4j创建Hello World项目

    Eclipse下maven使用嵌入式(Embedded)Neo4j创建Hello World项目 新建一个maven工程,这里不赘述如何新建maven工程. 添加Neo4j jar到你的工程 有两种方 ...

  2. MongoDB 创建基础索引、组合索引、唯一索引以及优化

    一.索引 MongoDB 提供了多样性的索引支持,索引信息被保存在system.indexes 中,且默认总是为_id创建索引,它的索引使用基本和MySQL 等关系型数据库一样.其实可以这样说说,索引 ...

  3. 《高性能MySQL》——第五章创建高性能索引

    1.创建索引基本语法格 在MySQL中,在已经存在的表上,可以通过ALTER TABLE语句直接为表上的一个或几个字段创建索引.基本语法格式如下: ALTER TABLE 表名 ADD [UNIQUE ...

  4. LabVIEW中数组的自动索引

    我们在LabVIEW里面使用While或者是For循环结构的时候,就会发现每一个循环中在它们的循环结构的边界都可以自动完成一个数组元素的索引或累积.LabVIEW中循环结构的这种能力就叫做自动索引(A ...

  5. windows系统中 利用kibana创建elasticsearch索引等操作

    elasticsearch之借用kibana平台创建索引 1.安装好kibana平台 确保kibana以及elasticsearch正常运行 2.打开kibana平台在Dev Tools 3.创建一个 ...

  6. 利用Phoenix为HBase创建二级索引

    为什么需要Secondary Index 对于Hbase而言,如果想精确地定位到某行记录,唯一的办法是通过rowkey来查询.如果不通过rowkey来查找数据,就必须逐行地比较每一列的值,即全表扫瞄. ...

  7. mysql创建唯一索引UNIQUE INDEX,以及报错“#失败原因: [Execute: Duplicate entry '733186700' for key 'uniq_video_id_index']”

    要给t_video_prods表的video_id字段创建唯一所以,可以使用下面这条语句: alter table t_video_prods add UNIQUE INDEX `uniq_video ...

  8. MYSQ创建联合索引,字段的先后顺序,对查询的影响分析

    MYSQ创建联合索引,字段的先后顺序,对查询的影响分析 前言 最左匹配原则 为什么会有最左前缀呢? 联合索引的存储结构 联合索引字段的先后顺序 b+树可以存储的数据条数 总结 参考 MYSQ创建联合索 ...

  9. es创建普通索引以及各种查询

    创建索引 创建普通索引: PUT /my_index { "settings": { "index": { "number_of_shards&quo ...

随机推荐

  1. android之animation

    Android  Animation一共有四种 Alpha: 淡入淡出效果 Scale: 缩放效果 Rotate: 旋转效果 Translate:移动效果 使用Tweened Animations的步 ...

  2. CSS中的class与id区别及用法

    转自http://www.divcss5.com/rumen/r3.shtml及http://www.jb51.net/css/35927.html 我们平常在用DIV CSS制作Xhtml网页页面时 ...

  3. apache不断占内存过大,导致虚拟机内存不足,处理方法。

    我用512M的vps,访问量不大,但内存占用很大,甚至宕机. 我用top,然后shitf+m发现,httpd占用内存极大.经过网上找资料设置后,用过一段时间终于没再出现内存问题了. 首先查找配置文件的 ...

  4. php7 编译安装 apache

    http://blog.csdn.net/21aspnet/article/details/47708763 根据此教程的步骤但是碰到了若干问题 1.  执行./configure的时候报错 大部分可 ...

  5. maven 私服搭建

    1,下载 https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.14.2-01-bundle.zip 2,解压 3,安装 ...

  6. 05:统计单词数【NOIP2011复赛普及组第二题】

    05:统计单词数 总时间限制:  1000ms 内存限制:  65536kB 描述 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次 ...

  7. Javascript定义类(class)的三种方法

    将近20年前,Javascript诞生的时候,只是一种简单的网页脚本语言.如果你忘了填写用户名,它就跳出一个警告. 如今,它变得几乎无所不能,从前端到后端,有着各种匪夷所思的用途.程序员用它完成越来越 ...

  8. 应用C#和SQLCLR编写SQL Server用户定义函数

    摘要: 文档阐述使用C#和SQLCLR为SQL Server编写用户定义函数,并演示用户定义函数在T-SQL中的应用.文档中实现的 Base64 编码解码函数和正则表达式函数属于标量值函数,字符串分割 ...

  9. AngularJS---表达式

    AngularJS的表达式是放在{{}}里面,用{{ }}符号将一个变量绑定到$scope上. angularJS中的表达式有如下特点: 1.只能在其所属作用域内部 所有的表达式都在其所属的作用域内部 ...

  10. [经验交流] 为 mesos framework 分配资源

    前段时间我在办公网搭建了一套mesos平台,用于docker 集群相关的调研和测试,mesos + marathon + docker 架构运行正常.但是在启用了chronos后,marathon无法 ...