-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.go
100 lines (80 loc) · 2.26 KB
/
demo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"crypto/tls"
"fmt"
"os"
"github.com/stargate/stargate-grpc-go-client/stargate/pkg/auth"
"github.com/stargate/stargate-grpc-go-client/stargate/pkg/client"
pb "github.com/stargate/stargate-grpc-go-client/stargate/pkg/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
var stargateClient *client.StargateClient
func main() {
fmt.Println("Begin .....!")
// Astra DB configuration
const astra_uri = "c2a8eb6c-312a-4903-b296-0945a0b50591-us-east-1.apps.astra.datastax.com:443";
const bearer_token = "AstraCS:...";
// Create connection with authentication
// For Astra DB:
config := &tls.Config{
InsecureSkipVerify: false,
}
conn, err := grpc.Dial(astra_uri, grpc.WithTransportCredentials(credentials.NewTLS(config)),
grpc.WithBlock(),
grpc.WithPerRPCCredentials(
auth.NewStaticTokenProvider(bearer_token),
),
)
fmt.Println("Connected .....!")
stargateClient, err = client.NewStargateClientWithConn(conn)
if err != nil {
fmt.Printf("error creating client %v", err)
os.Exit(1)
}
fmt.Printf("made client")
batch := &pb.Batch{
Type: pb.Batch_LOGGED,
Queries: []*pb.BatchQuery{
{
Cql: "INSERT INTO content.zdmdemo (id, first, second) VALUES (100, 'Jane', 'Doe');",
},
{
Cql: "INSERT INTO content.zdmdemo (id, first, second) VALUES (200, 'Serge', 'Provencio');",
},
},
}
_, err = stargateClient.ExecuteBatch(batch)
if err != nil {
fmt.Printf("error creating batch %v", err)
return
}
fmt.Printf("insert executed\n")
// For Astra DB: SELECT the data to read from the table
selectQuery := &pb.Query{
Cql: "SELECT first, second FROM content.zdmdemo;",
}
response, err := stargateClient.ExecuteQuery(selectQuery)
if err != nil {
fmt.Printf("error executing query %v", err)
return
}
fmt.Printf("select executed\n")
result := response.GetResultSet()
rowCount := len(result.Rows)
var i, j int
for i = 0; i < rowCount; i++ {
valueToPrint := ""
columnCount := len(result.Rows[i].Values)
for j = 0; j < columnCount; j++ {
value, err := client.ToString(result.Rows[i].Values[j])
if err != nil {
fmt.Printf("error getting value %v", err)
os.Exit(1)
}
valueToPrint += " "
valueToPrint += value
}
fmt.Printf("%v \n", valueToPrint)
}
}