Gremlin 基础知识

Gremlin 基础知识

数据创建

1. 创建属性类型(PropertyKey)

	graph.schema().propertyKey("name").asText().create() // 创建姓名属性,文本类型
	graph.schema().propertyKey("age").asInt().create()   // 创建年龄属性,整数类型
	graph.schema().propertyKey("addr").asText().create() // 创建地址属性,文本类型
	graph.schema().propertyKey("lang").asText().create() // 创建语言属性,文本类型
	graph.schema().propertyKey("tag").asText().create()  // 创建标签属性,文本类型
	graph.schema().propertyKey("weight").asFloat().create() // 创建权重属性,浮点类型

2. 创建顶点类型(VertexLabel)

// 创建顶点类型:人"person",包含姓名、年龄、地址等属性,使用自定义的字符串作为ID
graph.schema().vertexLabel("person")
              .properties("name", "age", "addr", "weight")
              .useCustomizeStringId()
              .create()
// 创建顶点类型:软件"software",包含名称、使用语言、标签等属性,使用名称作为主键
graph.schema().vertexLabel("software")
              .properties("name", "lang", "tag", "weight")
              .primaryKeys("name")
              .create()
// 创建顶点类型:语言"language",包含名称、使用语言等属性,使用名称作为主键
graph.schema().vertexLabel("language")
              .properties("name", "lang", "weight")
              .primaryKeys("name")
              .create()

3. 创建边类型(EdgeLabel)

// 创建边类型:人认识人"knows",此类边由"person"指向"person"
	graph.schema().edgeLabel("knows")
	              .sourceLabel("person").targetLabel("person")
	              .properties("weight")
	              .create()
	// 创建边类型:人创建软件"created",此类边由"person"指向"software"
	graph.schema().edgeLabel("created")
	              .sourceLabel("person").targetLabel("software")
	              .properties("weight")
	              .create()
	// 创建边类型:软件包含软件"contains",此类边由"software"指向"software"
	graph.schema().edgeLabel("contains")
	              .sourceLabel("software").targetLabel("software")
	              .properties("weight")
	              .create()
	// 创建边类型:软件定义语言"define",此类边由"software"指向"language"
	graph.schema().edgeLabel("define")
	              .sourceLabel("software").targetLabel("language")
	              .properties("weight")
	              .create()
	// 创建边类型:软件实现软件"implements",此类边由"software"指向"software"
	graph.schema().edgeLabel("implements")
	              .sourceLabel("software").targetLabel("software")
	              .properties("weight")
	              .create()
	// 创建边类型:软件支持语言"supports",此类边由"software"指向"language"
	graph.schema().edgeLabel("supports")
	              .sourceLabel("software").targetLabel("language")
	              .properties("weight")
	              .create()
	 ### 4. 创建index类型(IndexLabel)
	graph.schema().indexLabel("personByAge").onV("person").by("age").range().ifNotExist().create()
	graph.schema().indexLabel("personByAge").onV("person").by("age").range().ifNotExist().create()

5. 添加顶点与边

// 调用Gremlin的addVertex方法添加顶点,参数由顶点label、id、properties构成
	// T.label表示顶点类型、T.id表示顶点id
	// 后面接着的"name"、"age"等是顶点属性,每个属性由一对键值组成
	
	// 添加2个作者顶点
	okram = graph.addVertex(T.label, "person", T.id, "okram",
	                        "name", "Marko A. Rodriguez", "age", 29,
	                        "addr", "Santa Fe, New Mexico", "weight", 1)
	spmallette = graph.addVertex(T.label, "person", T.id, "spmallette",
	                             "name", "Stephen Mallette", "age", 0,
	                             "addr", "", "weight", 1)
	
	// 添加TinkerPop顶点
	tinkerpop = graph.addVertex(T.label, "software", "name", "TinkerPop",
	                            "lang", "java", "tag", "Graph computing framework",
	                            "weight", 1)
	// 添加TinkerGraph顶点
	tinkergraph = graph.addVertex(T.label, "software", "name", "TinkerGraph",
	                              "lang", "java", "tag", "In-memory property graph",
	                              "weight", 1)
	// 添加Gremlin顶点
	gremlin = graph.addVertex(T.label, "language", "name", "Gremlin",
	                          "lang", "groovy/python/javascript", "weight", 1)
	
	// 调用Gremlin的addEdge方法添加边
	// 由源顶点对象发起调用,参数由边类型、目标顶点、属性构成
	// 后面接着的"name"、"age"等是顶点属性,每个属性由一对键值组成
	
	// 添加2位作者创建TinkerPop的边
	okram.addEdge("created", tinkerpop, "weight", 1)
	spmallette.addEdge("created", tinkerpop, "weight", 1)
	
	// 添加2位作者的认识边
	okram.addEdge("knows", spmallette, "weight", 1)
	
	// 添加TinkerPop、TinkerGraph、Gremlin之间的关系边
	tinkerpop.addEdge("define", gremlin, "weight", 1)
	tinkerpop.addEdge("contains", tinkergraph, "weight", 1)
	tinkergraph.addEdge("supports", gremlin, "weight", 1)
	
	// 注意:下面的Gremlin语句在执行时需要紧接着上述第一步的语句
	// 因为这里使用了上面定义的tinkerpop、gremlin等变量
	
	// 添加3个作者顶点
	javeme = graph.addVertex(T.label, "person", T.id, "javeme",
	                         "name", "Jermy Li", "age", 29, "addr",
	                         "Beijing", "weight", 1)
	zhoney = graph.addVertex(T.label, "person", T.id, "zhoney",
	                         "name", "Zhoney Zhang", "age", 29,
	                         "addr", "Beijing", "weight", 1)
	linary = graph.addVertex(T.label, "person", T.id, "linary",
	                         "name", "Linary Li", "age", 28,
	                         "addr", "Wuhan. Hubei", "weight", 1)
	
	// 添加HugeGraph顶点
	hugegraph = graph.addVertex(T.label, "software", "name", "HugeGraph",
	                            "lang", "java", "tag", "Graph Database",
	                            "weight", 1)
	
	// 添加作者创建HugeGraph的边
	javeme.addEdge("created", hugegraph, "weight", 1)
	zhoney.addEdge("created", hugegraph, "weight", 1)
	linary.addEdge("created", hugegraph, "weight", 1)
	
	// 添加作者之间的关系边
	javeme.addEdge("knows", zhoney, "weight", 1)
	javeme.addEdge("knows", linary, "weight", 1)
	
	// 添加HugeGraph实现TinkerPop的边
	hugegraph.addEdge("implements", tinkerpop, "weight", 1)
	// 添加HugeGraph支持Gremlin的边
	hugegraph.addEdge("supports", gremlin, "weight", 1)
	
	// 注意:下面的Gremlin语句在执行时需要紧接着上述第一步的语句
	// 因为这里使用了上面定义的tinkerpop、gremlin等变量
	
	// 添加2个作者顶点
	alaro = graph.addVertex(T.label, "person", T.id, "dalaro",
	                        "name", "Dan LaRocque ", "age", 0,
	                        "addr", "", "weight", 1)
	mbroecheler = graph.addVertex(T.label, "person", T.id, "mbroecheler",
	                              "name", "Matthias Broecheler",
	                              "age", 0, "addr", "San Francisco", "weight", 1)
	
	// 添加Titan顶点
	titan = graph.addVertex(T.label, "software", "name", "Titan",
	                        "lang", "java", "tag", "Graph Database", "weight", 1)
	
	// 添加作者、Titan之间的关系边
	dalaro.addEdge("created", titan, "weight", 1)
	mbroecheler.addEdge("created", titan, "weight", 1)
	okram.
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值