/
index_example.go
142 lines (126 loc) · 4.01 KB
/
index_example.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// Copyright (c) 2019, 2023 Oracle and/or its affiliates. All rights reserved.
//
// Licensed under the Universal Permissive License v 1.0 as shown at
// https://oss.oracle.com/licenses/upl/
//
// The index example shows how to create and use the index on a table.
// For example:
//
// Create a table in Oracle NoSQL database
// Create an index on the table
// Put rows into the table
// Get the rows using the index
// Drop the table
//
// See https://godoc.org/github.com/oracle/nosql-go-sdk/examples for more details
// on how to build and run the examples.
//
package main
import (
"fmt"
"time"
"github.com/oracle/nosql-go-sdk/examples"
"github.com/oracle/nosql-go-sdk/nosqldb"
"github.com/oracle/nosql-go-sdk/nosqldb/jsonutil"
"github.com/oracle/nosql-go-sdk/nosqldb/types"
)
func main() {
client, err := examples.CreateClient()
if err != nil {
fmt.Println(err)
return
}
defer client.Close()
err = runExample(client)
if err != nil {
fmt.Println(err)
}
}
func runExample(client *nosqldb.Client) error {
// Creates a table.
tableName := "exampleProfiles"
stmt := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s "+
"(id INTEGER, userInfo JSON, primary key(id))", tableName)
tableReq := &nosqldb.TableRequest{
Statement: stmt,
TableLimits: &nosqldb.TableLimits{
ReadUnits: 50,
WriteUnits: 50,
StorageGB: 5,
},
}
tableRes, err := client.DoTableRequest(tableReq)
if err != nil {
return fmt.Errorf("failed to create table %s: %v", tableName, err)
}
fmt.Printf("Creating table %s ...\n", tableName)
// The create table request is asynchronous, wait for table creation to complete.
_, err = tableRes.WaitForCompletion(client, 60*time.Second, time.Second)
if err != nil {
return fmt.Errorf("failed to create table %s: %v", tableName, err)
}
fmt.Println("Created table", tableName)
// Create an index on the firstName field inside the userInfo JSON object.
idxName := "idx1"
stmt = fmt.Sprintf("CREATE INDEX IF NOT EXISTS %s ON %s "+
"(userInfo.firstName AS STRING)", idxName, tableName)
idxReq := &nosqldb.TableRequest{
Statement: stmt,
}
tableRes, err = client.DoTableRequest(idxReq)
if err != nil {
return fmt.Errorf("failed to create index %s: %v", idxName, err)
}
fmt.Printf("Creating index %s ...\n", idxName)
_, err = tableRes.WaitForCompletion(client, 60*time.Second, time.Second)
if err != nil {
return fmt.Errorf("failed to create index %s: %v", idxName, err)
}
fmt.Println("Created index", idxName)
// Put with json
jsonStrings := []string{
`{"firstName": "Taylor", "lastName": "Smith", "age": 33}`,
`{"firstName": "Xiao", "lastName": "Zhu", "age": 17}`,
`{"firstName": "Supriya", "lastName": "Jain", "age": 65}`,
`{"firstName": "Taylor", "lastName": "Rodriguez", "age": 43}`,
}
for i, s := range jsonStrings {
v, err := types.NewMapValueFromJSON(s)
if err != nil {
return fmt.Errorf("failed to create value from JSON: %v", err)
}
value := &types.MapValue{}
value.Put("id", i+1).Put("userInfo", v)
putReq := &nosqldb.PutRequest{
TableName: tableName,
Value: value,
}
putRes, err := client.Put(putReq)
if err != nil {
return fmt.Errorf("failed to put a row: %v", err)
}
fmt.Printf("Put row: %v,\nresult: %s\n", jsonutil.AsPrettyJSON(putReq.Value.Map()),
jsonutil.AsPrettyJSON(putRes))
}
// Query from the indexed field
query := "SELECT * FROM " + tableName + " p WHERE p.userInfo.firstName=\"Taylor\""
results, err := examples.RunQuery(client, query)
if err != nil {
return fmt.Errorf("failed to execute statement %q: %v", query, err)
}
fmt.Printf("Number of query results for %q: %d\n", query, len(results))
for i, r := range results {
fmt.Printf("\t%d: %s\n", i+1, jsonutil.AsJSON(r.Map()))
}
// Drop the table
dropReq := &nosqldb.TableRequest{
Statement: "DROP TABLE IF EXISTS " + tableName,
}
tableRes, err = client.DoTableRequestAndWait(dropReq, 60*time.Second, time.Second)
if err != nil {
return fmt.Errorf("failed to drop table %s: %v", tableName, err)
}
fmt.Println("Dropped table", tableName)
return nil
}