problem with AWS neptuneDB Golang GremlinGo

huangapple go评论86阅读模式
英文:

problem with AWS neptuneDB Golang GremlinGo

问题

我正在使用neptuneDB AWS进行工作。我想在我的Ubuntu上进行本地测试。我为此编写了一些代码,但它不起作用。尽管我有秘密凭据。我想在本地使用Gremlin编写和测试基本的CRUD操作。但是我无法修复这段代码。这是我的代码示例:

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/config"
	"github.com/aws/aws-sdk-go-v2/service/neptune"
	"github.com/aws/aws-sdk-go-v2/service/neptune/types"
	"github.com/aws/aws-sdk-go-v2/aws/credentials"
)

const (
	ACCESS_KEY = "YOUR_ACCESS_KEY"
	SECRET_KEY = "YOUR_SECRET_KEY"
)

func main() {
	cfg, err := config.LoadDefaultConfig(context.TODO(),
		config.WithRegion("us-west-2"),
		config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(ACCESS_KEY, SECRET_KEY, "")),
	)
	if err != nil {
		log.Fatal("failed to load AWS config", err)
	}

	svc := neptune.NewFromConfig(cfg)

	// Insert data
	err = insertData(svc)
	if err != nil {
		log.Fatal("failed to insert data", err)
	}
	fmt.Println("Data inserted successfully")

	// Update data
	err = updateData(svc)
	if err != nil {
		log.Fatal("failed to update data", err)
	}
	fmt.Println("Data updated successfully")

	// Delete data
	err = deleteData(svc)
	if err != nil {
		log.Fatal("failed to delete data", err)
	}
	fmt.Println("Data deleted successfully")

	// Read data
	_, err = readData(svc)
	if err != nil {
		log.Fatal("failed to read data", err)
	}
	fmt.Println("Data read successfully")
}

// Insert data
func insertData(svc *neptune.Client) error {
	query := "g.addV('Person').property('name', 'John').property('age', 30).property('em', 'eer')"
	_, err := executeGremlinQuery(svc, query)
	return err
}

// Update data
func updateData(svc *neptune.Client) error {
	query := "g.V().has('name', 'John').property('age', 31)"
	_, err := executeGremlinQuery(svc, query)
	return err
}

// Delete data
func deleteData(svc *neptune.Client) error {
	query := "g.V().has('name', 'John').drop()"
	_, err := executeGremlinQuery(svc, query)
	return err
}

// Read data
func readData(svc *neptune.Client) ([]byte, error) {
	query := "g.V().has('name', 'John')"
	result, err := executeGremlinQuery(svc, query)
	if err != nil {
		return nil, err
	}
	fmt.Println("Result:", result)
	return result, nil
}

// Execute a Gremlin query
func executeGremlinQuery(svc *neptune.Client, query string) ([]byte, error) {
	input := &types.ExecuteStatementInput{
		GremlinQueryString: &query,
	}
	output, err := svc.ExecuteStatement(context.TODO(), input)
	if err != nil {
		return nil, err
	}
	return output.Items[0].Properties["value"].Value.([]byte), nil
}

希望这可以帮助到你。如果你有任何问题,请随时问我。

英文:

I am working with neptuneDB AWS. I want to test locally on my Ubuntu. I wrote some code for that but it's not working. Although I've secret credentials. I want to write and test the basic crud with Gremlin locally. I want to write and test the basic crud with Gremlin locally. But I can't and doesn't fix this code. this is my code sample

   package main
import (
"context"
"fmt"
"log"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/neptune"
"github.com/aws/aws-sdk-go-v2/service/neptune/types"
"github.com/aws/aws-sdk-go-v2/aws/credentials"
)
const (
ACCESS_KEY = "YOUR_ACCESS_KEY"
SECRET_KEY = "YOUR_SECRET_KEY"
)
func main() {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion("us-west-2"),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(ACCESS_KEY, SECRET_KEY, "")),
)
if err != nil {
log.Fatal("failed to load AWS config", err)
}
svc := neptune.NewFromConfig(cfg)
// Insert data
err = insertData(svc)
if err != nil {
log.Fatal("failed to insert data", err)
}
fmt.Println("Data inserted successfully")
// Update data
err = updateData(svc)
if err != nil {
log.Fatal("failed to update data", err)
}
fmt.Println("Data updated successfully")
// Delete data
err = deleteData(svc)
if err != nil {
log.Fatal("failed to delete data", err)
}
fmt.Println("Data deleted successfully")
// Read data
_, err = readData(svc)
if err != nil {
log.Fatal("failed to read data", err)
}
fmt.Println("Data read successfully")
}
// Insert data
func insertData(svc *neptune.Client) error {
query := "g.addV('Person').property('name', 'John').property('age', 30).property('em', 'eer')"
_, err := executeGremlinQuery(svc, query)
return err
}
// Update data
func updateData(svc *neptune.Client) error {
query := "g.V().has('name', 'John').property('age', 31)"
_, err := executeGremlinQuery(svc, query)
return err
}
// Delete data
func deleteData(svc *neptune.Client) error {
query := "g.V().has('name', 'John').drop()"
_, err := executeGremlinQuery(svc, query)
return err
}
// Read data
func readData(svc *neptune.Client) ([]byte, error) {
query := "g.V().has('name', 'John')"
result, err := executeGremlinQuery(svc, query)
if err != nil {
return nil, err
}
fmt.Println("Result:", result)
return result, nil
}
// Execute a Gremlin query
func executeGremlinQuery(svc *neptune.Client, query string) ([]byte, error) {
input := &types.ExecuteStatementInput{
GremlinQueryString: &query,
}
output, err := svc.ExecuteStatement(context.TODO(), input)
if err != nil {
return nil, err
}
return output.Items[0].Properties["value"].Value.([]byte), nil
}

答案1

得分: 3

如果您想使用Go连接到Neptune,推荐的方式是使用gremlin-go驱动程序,而不是aws-sdk-go(请参阅Neptune文档这里)。

有关gremlin-go的其他信息,请参阅TinkerPop的参考文档。请注意,gremlin-go具有稍微不同的步骤语法,以匹配Go语言。如果您想使用Groovy语法提交脚本,可以参考这里的说明。

如果您想使用SigV4签名来连接到使用gremlin-go的IAM数据库,您可以按照Kelvin提供的答案进行操作。

希望对您有所帮助。

英文:

If you trying to connect to Neptune using Go, the recommended way is to use the gremlin-go driver instead of the aws-sdk-go (see Neptune docs here).

Additional gremlin-go information can be found in the TinkerPop reference docs. Note that gremlin-go has slightly different step syntax to match the Go language, if you'd like to submit scripts using Groovy syntax instead, see instructions here.

If you are trying to use SigV4 signing to connect to IAM database using gremlin-go, you can follow the answers that Kelvin linked.

Hope this helps.

huangapple
  • 本文由 发表于 2023年5月30日 00:07:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定