forked from opensearch-project/opensearch-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindexing-data_stream.go
More file actions
124 lines (109 loc) · 3.25 KB
/
Copy pathindexing-data_stream.go
File metadata and controls
124 lines (109 loc) · 3.25 KB
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
// SPDX-License-Identifier: Apache-2.0
//
// The OpenSearch Contributors require contributions made to
// this file be licensed under the Apache-2.0 license or a
// compatible open source license.
// This sample demonstrates creating and managing a data stream.
//
// Learn more:
// - Guide: https://github.com/opensearch-project/opensearch-go/blob/main/guides/indexing-data_streams.md
// - API reference: https://pkg.go.dev/github.com/opensearch-project/opensearch-go/v5/opensearchapi
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/opensearch-project/opensearch-go/v5"
"github.com/opensearch-project/opensearch-go/v5/opensearchapi"
)
func main() {
if err := example(); err != nil {
fmt.Println(fmt.Sprintf("Error: %s", err))
os.Exit(1)
}
}
func example() error {
// Initialize the client with SSL/TLS enabled.
client, err := opensearchapi.NewClient(
opensearchapi.Config{
Client: opensearch.Config{
InsecureSkipVerify: true, // For testing only. Use certificate for validation.
Addresses: []string{"https://localhost:9200"},
Username: "admin", // For testing only. Don't store credentials in code.
Password: "myStrongPassword123!",
},
},
)
if err != nil {
return err
}
ctx := context.Background()
tempCreateResp, err := client.Indices.PutIndexTemplate(
ctx,
opensearchapi.IndicesPutIndexTemplateReq{
Name: "books",
BodyReader: strings.NewReader(`{
"index_patterns": ["books-nonfiction"],
"template": {
"settings": {
"index": {
"number_of_shards": 3,
"number_of_replicas": 0
}
}
},
"data_stream": {},
"priority": 50
}`),
},
)
if err != nil {
return err
}
fmt.Printf("Index Tempalte created: %t\n", tempCreateResp.Acknowledged)
createResp, err := client.Indices.CreateDataStream(ctx, opensearchapi.IndicesCreateDataStreamReq{Name: "books-nonfiction"})
if err != nil {
return err
}
fmt.Printf("Created: %t\n", createResp.Acknowledged)
getResp, err := client.Indices.GetDataStream(ctx, nil)
if err != nil {
return err
}
respAsJson, err := json.MarshalIndent(getResp, "", " ")
if err != nil {
return err
}
fmt.Printf("Get DataStream:\n%s\n", string(respAsJson))
getResp, err = client.Indices.GetDataStream(ctx, &opensearchapi.IndicesGetDataStreamReq{Name: []string{"books-nonfiction"}})
if err != nil {
return err
}
respAsJson, err = json.MarshalIndent(getResp, "", " ")
if err != nil {
return err
}
fmt.Printf("Get DataStream:\n%s\n", string(respAsJson))
statsResp, err := client.Indices.DataStreamsStats(ctx, nil)
if err != nil {
return err
}
respAsJson, err = json.MarshalIndent(statsResp, "", " ")
if err != nil {
return err
}
fmt.Printf("Stats DataStream:\n%s\n", string(respAsJson))
delResp, err := client.Indices.DeleteDataStream(ctx, &opensearchapi.IndicesDeleteDataStreamReq{Name: []string{"books-nonfiction"}})
if err != nil {
return err
}
fmt.Printf("DataStream deleted: %t\n", delResp.Acknowledged)
delTempResp, err := client.Indices.DeleteIndexTemplate(ctx, opensearchapi.IndicesDeleteIndexTemplateReq{Name: "books"})
if err != nil {
return err
}
fmt.Printf("Deleted templates: %t\n", delTempResp.Acknowledged)
return nil
}