go - Gremlin addV() 返回“无效的请求参数”
问题描述
以下测试失败,带有Invalid Request Arguments
从 JanusGraph 0.4 返回的错误,后端为 cassandra 3.11:
const url = "ws://localhost:8182/gremlin"
var target *DocumentGraph
func TestMain(m *testing.M) {
var err error
target, err = NewDocumentGraph(url)
if err != nil {
fmt.Printf("failed to initialize gremlin cluster: %v\n", err)
os.Exit(1)
}
os.Exit(m.Run())
}
func TestDocumentGraph_CreateFromJSON(t *testing.T) {
data := `{
"label":"inline",
"tagName":"img",
"key":"inline_VMJc8GgHqqJjmvb",
"attributes":{
"alt":"",
"border":"0",
"height":"1",
"src":"http://links.transactions.overstock.com/wf/open?upn=lvH1F6JqxvnN0PAh3-2B8DWHCc6xqkDFpMVqX-2F-2F5xEHnOuk56mqNJgyKTEH4WKg1ov-2FNuwliFWi7L8-2B268R5AHIsziCbIAIfk3yJkj4St6VLbMb7KSBbacUm0cOuk-2Bbc8s1AU42HQ0qXvV5RjQoP1kHZvbQTNtpLRgsHqp4qBfCUyNvIkyTuQYk-2BrGwE-2BuIFLR37x2H8QgDhDb6BRqPqRa1sBoBktHbPkPzToRS0450idiTYDCBw-2BGuYcmjsxiioABXbhiD83nLxak5QKz3fEGpcWDtV0FIJ6791hoRnbV-2FDSWPdtvCNUVXFFF0N4zhsYS-2Bgj4kXAin-2FZvHdfc7WwtGH17ZrHT4euNwpxBgoVHQZ9Q-2BrLvhMS35ktJFtOV5YwVf3-2FEopcH1ZLJrRxDRfLqjUktPLU9b5mUtmRYky-2F38wzbEWn-2BR7OcG2XZAcIsr-2BjCvduEW04bS1Gpt-2F9fRL59vzEBUnPidS2e9CNPxu8ew4m-2BHLxUScvqISAO9cIsrxZh-2FxctpugiqU2Cof1i9IZ2eyO9CXG4Mm00qaWzUaW4cofLSb-2FswC6fmM0IEaI9WcRTFBhiHmHbotLQquw4K0vXLyi6pbHn-2Bo6NaKDM6-2BjeaaGULUqLmDdnGGw6D0Txe4v8bDYxjtbkPvCs9VWHzPYcXQ-3D-3D",
"style@border-width":"0 !important",
"style@height":"1px !important",
"style@margin-bottom":"0 !important",
"style@margin-left":"0 !important",
"style@margin-right":"0 !important",
"style@margin-top":"0 !important",
"style@padding-bottom":"0 !important",
"style@padding-left":"0 !important",
"style@padding-right":"0 !important",
"style@padding-top":"0 !important",
"style@width":"1px !important",
"width":"1"
}
}`
v := &vertex{
children: make([]*vertex, 0),
}
if err := JSON.Unmarshal([]byte(data), v); err != nil {
t.Fatalf("failed to unmarshal test json data: %v", err)
}
if err := target.createVertex(v); err != nil {
t.Errorf("failed to create vertex: %v", err)
}
}
奇怪的是,如果我删除 ANY 2 attributes
,它可以正常工作。我知道 gremlin 不限于 14 个property
语句,因为我从控制台运行了一个超过 17 个语句的语句,那么发生了什么?
编辑:如果我从属性值中删除参数并且只发送不带任何参数的查询它可以工作。那么查询的参数数量是否有限制?
以下是相关代码:
type vertex struct {
ID int64 `json:"id,omitempty"`
parent *vertex
children []*vertex
Label string `json:"label,omitempty"`
TagName string `json:"tagName,omitempty"`
Key string `json:"key,omitempty"`
Attributes map[string]string `json:"attributes"`
}
type DocumentGraph struct {
graph *gremlin.Client
}
func NewDocumentGraph(url string) (*DocumentGraph, error) {
g, err := gremlin.NewClient(url)
if err != nil {
return nil, fmt.Errorf("failed to create gremlin client: %v", err)
}
return &DocumentGraph{
graph: g,
}, nil
}
func (d *DocumentGraph) createVertex(v *vertex) error {
var (
labelParam = "labelName"
keyParam = "docKey"
)
var q strings.Builder
p := gremlin.Bind{
labelParam: v.Label,
keyParam: v.Key,
}
q.WriteString(fmt.Sprintf(`g.addV(%s).as(%s).property("document_key", %s)`, labelParam, keyParam, keyParam))
for k, v := range v.Attributes {
pname := strings.ReplaceAll(strings.ReplaceAll(k, `-`, `_`), `@`, `_at_`)
q.WriteString(fmt.Sprintf(`.property(%q, %s)`, k, pname))
p[pname] = v
}
for i, c := range v.children {
q.WriteString(fmt.Sprintf(`.V(id_%d).addE("parent").to(%s)`, i, keyParam))
p[fmt.Sprintf("id_%d", i)] = c.ID
}
// terminal step will trigger the batch to write everything
q.WriteString(fmt.Sprintf(".select(%s).valueMap(true)", keyParam))
// read result for id
req := gremlin.Query(q.String()).Bindings(p)
b, _ := gremlin.GraphSONSerializer(req)
data, err := d.graph.Exec(req)
if err != nil {
return fmt.Errorf("failed to create vertex for query : %s: %v", b, err)
}
fmt.Printf("successfully created vertex: %s\n", b)
return nil
}
解决方案
推荐阅读
- python-3.x - serial.serialutil.SerialException: 无法打开端口 'COM4': PermissionError(13, 'Acceso denegado.', None, 5)?
- flask - 缓存会在多大程度上破坏我的 Flask 应用程序?
- python-3.x - 如何以月(字符串)日(整数)年(整数)的形式获取日期?
- javascript - react-native-maps MapView WMS 没有平铺
- plot - 在mathematica中绘制两个变量的方程
- php - 尝试使用 php 和密钥使用 aes-256-gcm 解密
- python - 如何让 django 将用户重定向到我需要的页面?
- python - Python discord.py/Image:如何使用 Image 编辑 ctx.author 个人资料图片并将其打磨回不和谐?
- javascript - 打字稿日期问题(VueJS)
- rest - Pentaho 数据集成 - REST 客户端步骤