I am using this gocql package.
I am trying to get this example working.
func main() {
// connect to the cluster
cluster := gocql.NewCluster("192.168.1.1", "192.168.1.2", "192.168.1.3")
cluster.ProtoVersion = 3
cluster.Keyspace = "example"
cluster.Consistency = gocql.Quorum
session, err := cluster.CreateSession()
defer session.Close()
if err != nil {
fmt.Printf("%v\n", err)
return
}
uuid := gocql.TimeUUID()
fmt.Printf("UUID : %v", uuid)
query := session.Query(`INSERT INTO tweet (timeline, id, text) VALUES
(?, ?, ?)`, "me", uuid, "hello world")
fmt.Println("About to exec")
err = query.Exec()
// insert a tweet
if err != nil {
log.Fatal(err)
}
var id gocql.UUID
var text string
/* Search for a specific set of records whose 'timeline' column matches
* the value 'me'. The secondary index that we created earlier will be
* used for optimizing the search */
if err := session.Query(`SELECT id, text FROM tweet WHERE timeline = ?
LIMIT 1`,"me").Consistency(gocql.One).Scan(&id, &text); err != nil {
log.Fatal(err)
}
fmt.Println("Tweet:", id, text)
// list all tweets
iter := session.Query(`SELECT id, text FROM tweet WHERE timeline = ?`,
"me").Iter()
for iter.Scan(&id, &text) {
fmt.Println("Tweet:", id, text)
}
if err := iter.Close(); err != nil {
log.Fatal(err)
}
}
Using the Cassandra shell I have created the keyspace "example" and the table "tweet" and they all work fine.
However, when I run the program, it gives me this error:
2017/04/14 20:52:55 gocql: unable to dial control conn 192.168.1.3: dial tcp
192.168.1.3:9042: i/o timeout
gocql: unable to create session: control: unable to connect to initial
hosts: dial tcp 192.168.1.3:9042: i/o timeoutpanic: runtime error: invalid
memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x60236d]
goroutine 1 [running]:
github.com/gocql/gocql.(*Session).Close(0x0)
/home/mda/.local/go/src/github.com/gocql/gocql/session.go:344 +0x2d
For some reason, gocql is unable to dial the localhost connection, and it times out. I am not sure how to fix this and a stackoverflow and google search hasn't helped so far.
Any ideas?
Looks like the ip addresses are not according to the cluster situation on your computer. After giving me the nodetool status I think you should update the cluster := gocql.NewCluster to have just the 127.0.0.1 address and not the addresses from example.
I think in reality you are just running single node cassandra local instance. Which is fine for development.
Basically:
cluster := gocql.NewCluster("127.0.0.1")
and just drop this line all together
cluster.ProtoVersion = 3
Related
Potential bug in gremlin-go when creating new edge and returning next result. Before creating a JIRA bug ticket for the following, I just want to make sure, it is not an user error/ my fault.
Version of Apache Tinkerpop gremlin-go used to connect and interact with JanusGraph (0.6.2): github.com/apache/tinkerpop/gremlin-go/v3 v3.6.1.
Example without transaction:
g := gremlingo.Traversal_().WithRemote(gremlingo.NewDriverRemoteConnection(source))
rv, err := g.AddV("v").Next()
fmt.Println(err)
fmt.Println(rv)
v, err := rv.GetVertex()
fmt.Println(err)
fmt.Println(v)
fmt.Println(v.Id)
re, err := g.AddE("e").Property("some_property", "some-property").From(__.V(v.Id)).To(__.V(v.Id)).Next()
fmt.Println(err)
fmt.Println(re)
Returns:
<nil>
result{object=v[%!s(int64=24776)] class=*gremlingo.Vertex}
<nil>
v[%!s(int64=24776)]
24776
E0903: there are no results left
<nil>
Example with transaction:
g := gremlingo.Traversal_().WithRemote(gremlingo.NewDriverRemoteConnection(source))
tx := g.Tx()
gtx, err := tx.Begin()
if err != nil {
tx.Close()
}
rv, err = gtx.AddV("v").Next()
if err != nil {
fmt.Println(err)
tx.Rollback()
}
fmt.Println(rv)
v, err = rv.GetVertex()
if err != nil {
fmt.Println(err)
tx.Rollback()
}
fmt.Println(v)
fmt.Println(v.Id)
re, err = gtx.AddE("e").Property("some_property", "some-property").From(__.V(v.Id)).To(__.V(v.Id)).Next()
if err != nil {
fmt.Println(err)
tx.Rollback()
}
fmt.Println(re)
err = txx.Commit()
if err != nil {
tx.Rollback()
}
Returns:
Creating Session based connection
Connecting.
result{object=v[%!s(int64=28872)] class=*gremlingo.Vertex}
v[%!s(int64=28872)]
28872
panic: runtime error: slice bounds out of range [:7623] with capacity 512
goroutine 133 [running]:
github.com/apache/tinkerpop/gremlin-go/v3/driver.readString(0xc00000ec60, 0xc0003b2fa0)
/go/pkg/mod/github.com/apache/tinkerpop/gremlin-go/v3#v3.6.1/driver/graphBinary.go:912 +0x145
github.com/apache/tinkerpop/gremlin-go/v3/driver.readUnqualified(0xc00000ec60, 0xc0003b2fa0, 0x3, 0x0?)
/go/pkg/mod/github.com/apache/tinkerpop/gremlin-go/v3#v3.6.1/driver/graphBinary.go:1308 +0x362
github.com/apache/tinkerpop/gremlin-go/v3/driver.edgeReader(0xc4e860?, 0xc0003b2fa0)
/go/pkg/mod/github.com/apache/tinkerpop/gremlin-go/v3#v3.6.1/driver/graphBinary.go:1079 +0x7d
github.com/apache/tinkerpop/gremlin-go/v3/driver.readFullyQualifiedNullable(0xc00000ec60, 0xc0003b2fa0, 0xe0?)
/go/pkg/mod/github.com/apache/tinkerpop/gremlin-go/v3#v3.6.1/driver/graphBinary.go:1327 +0x3f1
github.com/apache/tinkerpop/gremlin-go/v3/driver.traverserReader(0xc00000ec60, 0xc0003b2fa0)
/go/pkg/mod/github.com/apache/tinkerpop/gremlin-go/v3#v3.6.1/driver/graphBinary.go:1159 +0xdc
github.com/apache/tinkerpop/gremlin-go/v3/driver.readFullyQualifiedNullable(0xc00000ec60, 0xc0003b2fa0, 0x0?)
/go/pkg/mod/github.com/apache/tinkerpop/gremlin-go/v3#v3.6.1/driver/graphBinary.go:1327 +0x3f1
github.com/apache/tinkerpop/gremlin-go/v3/driver.readList(0xc00000ec60, 0xc0003b2fa0)
/go/pkg/mod/github.com/apache/tinkerpop/gremlin-go/v3#v3.6.1/driver/graphBinary.go:953 +0x133
github.com/apache/tinkerpop/gremlin-go/v3/driver.readFullyQualifiedNullable(0xc00000ec60, 0xc0003b2fa0, 0xc0?)
/go/pkg/mod/github.com/apache/tinkerpop/gremlin-go/v3#v3.6.1/driver/graphBinary.go:1327 +0x3f1
github.com/apache/tinkerpop/gremlin-go/v3/driver.graphBinarySerializer.deserializeMessage({0xc00051ec00?}, {0xc0005b2200, 0xc5, 0x200})
/go/pkg/mod/github.com/apache/tinkerpop/gremlin-go/v3#v3.6.1/driver/serializer.go:214 +0x3fa
github.com/apache/tinkerpop/gremlin-go/v3/driver.(*gremlinServerWSProtocol).readLoop(0xc00051ec00, 0x5456ea?, 0x1c?)
/go/pkg/mod/github.com/apache/tinkerpop/gremlin-go/v3#v3.6.1/driver/protocol.go:75 +0x1e2
created by github.com/apache/tinkerpop/gremlin-go/v3/driver.newGremlinServerWSProtocol
/go/pkg/mod/github.com/apache/tinkerpop/gremlin-go/v3#v3.6.1/driver/protocol.go:200 +0x250
When adding .ElementMap():
gtx.AddE("e").Property("some_property", "some-property").From(__.V(v.Id)).To(__.V(v.Id)).ElementMap().Next()
Returns:
Creating Session based connection
Connecting.
result{object=v[%!s(int64=16576)] class=*gremlingo.Vertex}
v[%!s(int64=16576)]
16576
2023/01/10 11:49:46 Error occurred during operation gremlinServerWSProtocol.readLoop(): 'E0408: unknown data type to deserialize 0x0'
2023/01/10 11:49:46 Read loop error 'E0408: unknown data type to deserialize 0x0', closing read loop.
2023/01/10 11:49:46 Connection error callback invoked, closing protocol.
2023/01/10 11:49:46 Connecting.
E0903: there are no results left
2023/01/10 11:49:47 Closing DriverRemoteConnection with url 'ws://janusgraph:8182/gremlin' with session '3071a486-6649-4f05-8baa-9603163936a3'
2023/01/10 11:49:47 Closing Client with url 'ws://janusgraph:8182/gremlin'
2023/01/10 11:49:47 Closing the connection.
Expected result via gremlin-console:
gremlin> g.addE("e").property("some_property", "some-property").from(__.V(24776)).to(__.V(24776)).next()
==>e[8bd-j48-q39-j48][24776-e->24776]
gremlin> g.addE("e").property("some_property", "some-property").from(__.V(24776)).to(__.V(24776)).elementMap().next()
==>id=8pl-j48-q39-j48
==>label=e
==>IN={id=24776, label=v}
==>OUT={id=24776, label=v}
==>some_property=some-property
Adding an Edge returns the Edge, and JanusGraph uses a special type RelationIdentifier as the Edge Id, which the Gremlin Go driver does not support. This causes the E0408: unknown data type to deserialize 0x0.
If you just want to create an Edge in the graph, a workaround is to not return it in the driver with g.addE().from([...]).to([...]).none().
More details in https://issues.apache.org/jira/browse/TINKERPOP-2828.
I am trying to run this insert query statement using golang which should return an id
INSERT INTO users (u_email,u_phone,u_password_hash) VALUES (?,?,?) RETURNING u_user_id;
it seems there is no support for that in MonetDB.
Well i tried to get the id by unique email when the insert is done without the "RETURNING u_user_id" part as below
func GetByEmail(db *sql.DB) {
userid := `SELECT u_user_id FROM users WHERE u_email = ?;`
var id UserResponse
email := `baicmr#email.com`
stmt, err := db.Prepare(userid)
if err != nil {
log.Panic(err)
}
defer stmt.Close()
result := stmt.QueryRow(email)
if getErr := result.Scan(&id.ID); getErr != nil {
log.Panic(getErr)
}
fmt.Printf("id.ID: %v\n", id.ID)
}
the result.Scan() line panics
panic: runtime error: index out of range [0] with length 0
What am i doing which is wrong
Can you please open a issue/feature request over at: https://github.com/MonetDB/MonetDB-Go ? This looks like a bug.
Hey I'm getting an error message : conn busy from pgx
I don't know how to solve this. Here is my function :
func (r *proverbRepo) SelectPendingProverbs(table string) (proverbs []domain.Proverb, err error) {
query := fmt.Sprintf("SELECT id, proverb literal FROM %s", table)
rows, err := r.Db.Query(context.Background(), query)
defer rows.Close()
if err != nil {
return
}
for rows.Next() {
var prov domain.Proverb
if err = rows.Scan(&prov.ID, &prov.Literal); err != nil {
return
}
proverbs = append(proverbs, prov)
}
return
}
r.Db is pgx.Connect(context.Background(), os.Getenv("PSQL_URL"))
I'm fetching two different table in a very short interval from two separate front end requests.
The first request goes through, the other one returns the conn busy error message.
I really don't know what to look for, would somebody help me ?
pgx.Connect() returns a pgx.Conn which cannot be used concurrently. This is what the godocs of this type state:
Conn is a PostgreSQL connection handle. It is not safe for concurrent usage. Use a connection pool to manage access to multiple database connections from multiple goroutines.
So if you replace pgx.Connect() with pgxpool.Connect() from github.com/jackc/pgx/pgxpool you should be fine.
The r.Db returned by pgx.Connect(context.Background(), os.Getenv("PSQL_URL")) if of type *pgx.Conn and represents a single connection which is not concurrency safe. Usually, you would like to use a connection pool to handle the concurrency for you and allows reusing open connection.
To use a connection pool replace the import github.com/jackc/pgx/v4 with github.com/jackc/pgx/v4/pgxpool and connect with pgxpool.Connect() instead of pgx.Connect() and the api will be the same:
r.Pool := pgxpool.Connect(context.Background(), os.Getenv("PSQL_URL"))
r.Pool.Query(context.Background(), query)
...
if at any given point you need to use a single connection to access some lower-level feature you can safely acquire a concurrent safe connection as follow:
conn, err := r.Pool.Acquire(context.Background())
if err != nil {
fmt.Fprintln(os.Stderr, "Error acquiring connection:", err)
os.Exit(1)
}
defer conn.Release()
...
I have a sample go application running with tendermint. I would like to access the data Tendermint's blockchain stores in leveldb while both the application and the node are running.
I have tried to run a basic go script that iterates over the tx_index.db which stores the tx.hash and the corresponding result of every transaction. But when I try to run the script the program panics with an error message.
Here's the go script to iterate over the tx_index.db
package main
import (
"fmt"
"github.com/syndtr/goleveldb/leveldb"
)
func main() {
db, err := leveldb.OpenFile("tx_index.db", nil)
if err != nil {
panic(err)
}
iter := db.NewIterator(nil, nil)
for iter.Next() {
key := iter.Key()
h_key := fmt.Sprintf("%X", key)
value := iter.Value()
h_value := fmt.Sprintf("%X", value)
fmt.Printf("Key: %v \nValue: %v \n", h_key, h_value)
}
iter.Release()
err = iter.Error()
defer db.Close()
}
Upon running the above script while the application and the tendermint node are running. I get the following error message!
panic: resource temporarily unavailable
goroutine 1 [running]:
main.main()
$HOME/tendermint/data/read_db.go:11 +0x375
exit status 2
I am interested in accessing the tx_index.db while the application and tendermint node are running to get the transaction hash of the transactions performed.
PS: If it's feasible I'd like to know if there's a way to get tx.hash by accessing the tx_index.db by interacting with the tendermint node.
I have a go program connected to a bigquery table. This is the table's schema:
name STRING NULLABLE
age INTEGER NULLABLE
amount INTEGER NULLABLE
I have succeded at queryng the data of this table and printing all rows on console with this code:
ctx := context.Background()
client, err := bigquery.NewClient(ctx, projectID)
q := client.Query("SELECT * FROM test.test_user LIMIT 1000")
it, err := q.Read(ctx)
if err != nil {
log.Fatal(err)
}
for {
var values []bigquery.Value
err := it.Next(&values)
if err == iterator.Done {
break
}
if err != nil {
// TODO: Handle error.
}
fmt.Println(values)
}
And I also have succeded to insert data on the table from a struct using this code:
type test struct {
Name string
Age int
Amount int
}
u := client.Dataset("testDS").Table("test_user").Uploader()
savers := []*bigquery.StructSaver{
{Struct: test{Name: "Jack", Age: 23, Amount:123}, InsertID: "id1"},
}
if err := u.Put(ctx, savers); err != nil {
log.Fatal(err)
}
fmt.Printf("rows inserted!!")
Now, what I am failing to do is updating rows. What I want to do is selecting all the rows and update all of them with an operation (for example: amount = amount * 2)
How can I achieve this using golang?
Updating rows is not specific to Go, or any other client library. If you want to update data in BigQuery, you need to use DML (Data Manipulation Language) via SQL. So, essentially you already have the main part working (running a query) - you just need to change this SQL to use DML.
But, a word of caution: BigQuery is a OLAP service. Don't use it for OLTP. Also, there are quotas with using DML. Make sure you familiarise yourself with them.