Skip to content

interhui/ndb-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#ndb-java#

ndb(node database) 是一种轻量级嵌入式的文档型数据库, ndb-java是ndb数据库的java实现

  • 可扩展的数据格式(Flexible Data Model)
  • 使用表达式查询语言(Expressive Query Language)
  • 采用程序嵌入的方式(Embedded Program)

下载路径:ndb-1.4.zip

安装ndb-java##

Maven

<dependency>
	<groupId>org.pinae</groupId>
	<artifactId>ndb</artifactId>
	<version>1.4</version>
</dependency>

ndb语法规则

ndb 语法格式:

command:path->query:value

command是操作指令,包括select、one、update、delete、insert、delete
path是检索路径,格式为:node->node,通过层级关系进行指定检索路径
item是检索项,value是检索值

查询列表(select)

select:path->item1:value1 && item2:value2

检索路径(path)和检索值(value) 支持正则表达式,数值区域,startwith 和 endwith:

  • 正则表达式使用 / 进行分割,例如 /[aA].*/
  • 数值区域仅对整数区域有效,使用 [min, max] 描述,例如 [12,17]
  • startwith使用^开头,例如^hello
  • endwith使用$结尾,例如hello$

select的返回值是一个列表,其中包括全部符合查询路径和条件的节点

查询单个(one)

one:path->item1:value1 && item2:value2

one的使用方法与select一致,当出现多个结果时,仅返回第一个结果

修改(update)

update:node->node->item:value !! item1=value1,item2=value2

item : value用于检索对应的node,根据后面的值对节点中的数据进行修改

删除(delete)

delete:path->item : value !! [item1, item2] / block

如果使用item 标注,则仅删除item条目,多个item使用","分割 如果使用block标注,则删除item的父node项目

新增(insert)

insert:path && item1=new value1,item2=new value

在节点中新建item,如果父路径不存在,则自动建立父路径

ndb使用样例

举例说明(数据样例):

root {
	parent {
		name : green
		child [{
				name: jim
				age: 20
				sex: male
				}, {
				name: lily
				age: 17
				sex: female
				}, {
				name: tom
				age: 28
				sex: male
			}]
		nephew {
			name: lucy
			age: 12
			sex: female
		}
	}
	
}

one

获得sex为male的第一个数据

one:root->parent->child->sex:male

select

获得name为jim和tom的child

select:root->parent->child->name:/.*m/

获得age在15-25之间的child

select:root->parent->child->age:[15,25]  

获得sex以fe开头的child

select:root->parent->child->sex:^fe

获得name为m结尾的child

select:root->parent->child->name:m$  

获得sex为male,age在15-25之间的child

select:root->parent->child->sex:male && age:[15,25] 

获得root->parent下所有的child

select:root->parent->child   

获得root->parent中sex为female的child和nephew

select:root->parent->:/child|nephew/->sex:female 

delete

删除name为jim的sex和age属性:

delete:root->parent->child->name:jim !! [sex, age]  

删除name为jim的所有属性,其中block为可选参数:

delete:root->parent->child->name:jim !! block 

update

修改name为jim的age值,并增加address值

update:root->parent->child->name:jim !! age=21, address=China 

insert

增加name为bill的child

insert:root->parent->child !! name=bill, sex=male, age=31

在java中使用ndb

package org.pinae.ndb.demo;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.pinae.ndb.Statement;

public class NdbDemo {

	public static void main(String arg[]) {
		Statement statment = new Statement();
		try {
			Map<String, Object> ndb = statment.read("d:/example.ndb");
			Object result = statment.execute(ndb, "select:root->parent->child->name:/.*m/");
			if (result instanceof List) {
				List resultList = (List)result;
				System.out.println(((Map) resultList.get(0)).get("name"));
				System.out.println(((Map) resultList.get(1)).get("name"));
			}
		} catch (IOException e) {

		}
	}
}

About

Node database for Java

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages