github.com/opensearch-project/opensearch-go/v2@v2.3.0/guides/data_streams.md (about) 1 ## Data Streams API 2 3 ### Create Data Streams 4 5 - Create new client 6 7 ``` 8 client, err := opensearch.NewDefaultClient() 9 if err != nil { 10 panic(err) 11 } 12 ``` 13 14 - Create template index 15 16 ``` 17 iPut := opensearchapi.IndicesPutIndexTemplateRequest{ 18 Name: "demo-data-template", 19 Pretty: true, 20 Human: true, 21 ErrorTrace: true, 22 Body: strings.NewReader(`{"index_patterns": ["demo-*"], "data_stream": {}, "priority": 100} }`), 23 } 24 iPutResponse, err := iPut.Do(context.Background(), client) 25 ``` 26 27 - Prepare request object 28 29 ``` 30 es := opensearchapi.IndicesCreateDataStreamRequest{ 31 Name: "demo-name", 32 Human: true, 33 Pretty: true, 34 ErrorTrace: true, 35 Header: map[string][]string{ 36 "Content-Type": {"application/json"}, 37 }, 38 } 39 ``` 40 41 - Execute request 42 43 ``` 44 res, err := es.Do(context.TODO(), client) 45 if err != nil { 46 // do not panic in production code 47 panic(err) 48 } 49 ``` 50 51 - Try to read response 52 53 ``` 54 defer res.Body.Close() 55 body, err := ioutil.ReadAll(res.Body) 56 if err != nil { 57 // do not panic in production code 58 panic(err) 59 } 60 61 fmt.Println("Response Status Code: ", res.StatusCode) 62 fmt.Println("Response Headers: ", res.Header) 63 fmt.Println("Response Body: ", string(body)) 64 ``` 65 66 - Successfully created data stream 67 68 ``` 69 Response Status Code: 200 70 Response Headers: map[Content-Length:[28] Content-Type:[application/json; charset=UTF-8]] 71 Response Body: {"acknowledged" : true} 72 ``` 73 74 ### Delete Data Streams 75 76 - Create new client as previous example 77 - Prepare request object 78 79 ``` 80 opensearchapi.IndicesDeleteDataStreamRequest{ 81 Name: "demo-name", 82 Pretty: true, 83 Human: true, 84 ErrorTrace: true, 85 Header: map[string][]string{ 86 "Content-Type": {"application/json"}, 87 }, 88 } 89 ``` 90 91 - Execute request as previous example 92 - Try to read response as previous example 93 - Successfully deleted data stream 94 95 ``` 96 Response Status Code: 200 97 Response Headers: map[Content-Length:[28] Content-Type:[application/json; charset=UTF-8]] 98 Response Body: {"acknowledged" : true} 99 ``` 100 101 ### Get All Data Streams 102 103 - Create new client as previous example 104 - Prepare request object 105 106 ``` 107 r := opensearchapi.IndicesGetDataStreamRequest{ 108 Pretty: true, 109 Human: true, 110 ErrorTrace: true, 111 Header: map[string][]string{ 112 "Content-Type": {"application/json"}, 113 }, 114 } 115 ``` 116 117 - Execute request as previous example 118 - Try to read response as previous example 119 - Successfully retrieved data streams 120 121 ``` 122 Response Status Code: 200 123 Response Headers: map[Content-Length:[28] Content-Type:[application/json; charset=UTF-8]] 124 Response Body: {"data_streams":[{"name":"demo-name","timestamp_field":{"name":"@timestamp"},"indices":[{"index_name":".ds-demo-2023-03-21-23-33-46-000001","index_uuid":"NnzgqnP0ThS7LOMHJuZ-VQ"}],"generation":1,"status":"YELLOW","template":"demo-data-template"}]} 125 ``` 126 127 ### Get Specific Data Stream 128 129 - Create new client as previous example 130 - Prepare request object 131 132 ``` 133 r := opensearchapi.IndicesGetDataStreamRequest{ 134 Name: "demo-name", 135 Pretty: true, 136 Human: true, 137 ErrorTrace: true, 138 Header: map[string][]string{ 139 "Content-Type": {"application/json"}, 140 }, 141 } 142 ``` 143 144 - Execute request as previous example 145 - Try to read response as previous example 146 - Successfully retrieved data stream 147 148 ``` 149 Response Status Code: 200 150 Response Headers: map[Content-Length:[28] Content-Type:[application/json; charset=UTF-8]] 151 Response Body: {"data_streams":[{"name":"demo-name","timestamp_field":{"name":"@timestamp"},"indices":[{"index_name":".ds-demo-2023-03-21-23-31-50-000001","index_uuid":"vhsowqdeRFCmr1GgQ7mIsQ"}],"generation":1,"status":"YELLOW","template":"demo-data-template"}]} 152 ``` 153 154 ### Get Specific Data Stream Stats 155 156 - Create new client as as previous example 157 - Prepare request object 158 159 ``` 160 r := opensearchapi.IndicesGetDataStreamStatsRequest{ 161 Name: "demo-name", 162 Pretty: true, 163 Human: true, 164 ErrorTrace: true, 165 Header: map[string][]string{ 166 "Content-Type": {"application/json"}, 167 }, 168 } 169 ``` 170 171 - Execute request as previous example 172 - Try to read response as previous example 173 - Successfully retrieved data stream stats 174 175 ``` 176 Response Status Code: 200 177 Response Headers: map[Content-Length:[28] Content-Type:[application/json; charset=UTF-8]] 178 Response Body: {"_shards":{"total":2,"successful":1,"failed":0},"data_stream_count":1,"backing_indices":1,"total_store_size":"208b","total_store_size_bytes":208,"data_streams":[{"data_stream":"demo-name","backing_indices":1,"store_size":"208b","store_size_bytes":208,"maximum_timestamp":0}]} 179 ```