example_kv_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // Copyright 2016 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package clientv3_test
  15. import (
  16. "fmt"
  17. "log"
  18. "github.com/coreos/etcd/clientv3"
  19. "golang.org/x/net/context"
  20. )
  21. func ExampleKV_put() {
  22. cli, err := clientv3.New(clientv3.Config{
  23. Endpoints: endpoints,
  24. DialTimeout: dialTimeout,
  25. })
  26. if err != nil {
  27. log.Fatal(err)
  28. }
  29. defer cli.Close()
  30. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  31. resp, err := cli.Put(ctx, "sample_key", "sample_value")
  32. cancel()
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. fmt.Println("current revision:", resp.Header.Revision) // revision start at 1
  37. // current revision: 2
  38. }
  39. func ExampleKV_get() {
  40. cli, err := clientv3.New(clientv3.Config{
  41. Endpoints: endpoints,
  42. DialTimeout: dialTimeout,
  43. })
  44. if err != nil {
  45. log.Fatal(err)
  46. }
  47. defer cli.Close()
  48. _, err = cli.Put(context.TODO(), "foo", "bar")
  49. if err != nil {
  50. log.Fatal(err)
  51. }
  52. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  53. resp, err := cli.Get(ctx, "foo")
  54. cancel()
  55. if err != nil {
  56. log.Fatal(err)
  57. }
  58. for _, ev := range resp.Kvs {
  59. fmt.Printf("%s : %s\n", ev.Key, ev.Value)
  60. }
  61. // foo : bar
  62. }
  63. func ExampleKV_getWithRev() {
  64. cli, err := clientv3.New(clientv3.Config{
  65. Endpoints: endpoints,
  66. DialTimeout: dialTimeout,
  67. })
  68. if err != nil {
  69. log.Fatal(err)
  70. }
  71. defer cli.Close()
  72. _, err = cli.Put(context.TODO(), "foo", "bar1")
  73. if err != nil {
  74. log.Fatal(err)
  75. }
  76. _, err = cli.Put(context.TODO(), "foo", "bar2")
  77. if err != nil {
  78. log.Fatal(err)
  79. }
  80. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  81. resp, err := cli.Get(ctx, "foo", clientv3.WithRev(2))
  82. cancel()
  83. if err != nil {
  84. log.Fatal(err)
  85. }
  86. for _, ev := range resp.Kvs {
  87. fmt.Printf("%s : %s\n", ev.Key, ev.Value)
  88. }
  89. // foo : bar1
  90. }
  91. func ExampleKV_getSortedPrefix() {
  92. cli, err := clientv3.New(clientv3.Config{
  93. Endpoints: endpoints,
  94. DialTimeout: dialTimeout,
  95. })
  96. if err != nil {
  97. log.Fatal(err)
  98. }
  99. defer cli.Close()
  100. for i := range make([]int, 3) {
  101. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  102. _, err = cli.Put(ctx, fmt.Sprintf("key_%d", i), "value")
  103. cancel()
  104. if err != nil {
  105. log.Fatal(err)
  106. }
  107. }
  108. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  109. resp, err := cli.Get(ctx, "key", clientv3.WithPrefix(), clientv3.WithSort(clientv3.SortByKey, clientv3.SortDescend))
  110. cancel()
  111. if err != nil {
  112. log.Fatal(err)
  113. }
  114. for _, ev := range resp.Kvs {
  115. fmt.Printf("%s : %s\n", ev.Key, ev.Value)
  116. }
  117. // key_2 : value
  118. // key_1 : value
  119. // key_0 : value
  120. }
  121. func ExampleKV_delete() {
  122. cli, err := clientv3.New(clientv3.Config{
  123. Endpoints: endpoints,
  124. DialTimeout: dialTimeout,
  125. })
  126. if err != nil {
  127. log.Fatal(err)
  128. }
  129. defer cli.Close()
  130. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  131. resp, err := cli.Delete(ctx, "key", clientv3.WithPrefix())
  132. cancel()
  133. if err != nil {
  134. log.Fatal(err)
  135. }
  136. fmt.Println("Deleted", resp.Deleted, "keys")
  137. // Deleted n keys
  138. }
  139. func ExampleKV_compact() {
  140. cli, err := clientv3.New(clientv3.Config{
  141. Endpoints: endpoints,
  142. DialTimeout: dialTimeout,
  143. })
  144. if err != nil {
  145. log.Fatal(err)
  146. }
  147. defer cli.Close()
  148. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  149. resp, err := cli.Get(ctx, "foo")
  150. cancel()
  151. if err != nil {
  152. log.Fatal(err)
  153. }
  154. compRev := resp.Header.Revision // specify compact revision of your choice
  155. ctx, cancel = context.WithTimeout(context.Background(), requestTimeout)
  156. err = cli.Compact(ctx, compRev)
  157. cancel()
  158. if err != nil {
  159. log.Fatal(err)
  160. }
  161. }
  162. func ExampleKV_txn() {
  163. cli, err := clientv3.New(clientv3.Config{
  164. Endpoints: endpoints,
  165. DialTimeout: dialTimeout,
  166. })
  167. if err != nil {
  168. log.Fatal(err)
  169. }
  170. defer cli.Close()
  171. kvc := clientv3.NewKV(cli)
  172. _, err = kvc.Put(context.TODO(), "key", "xyz")
  173. if err != nil {
  174. log.Fatal(err)
  175. }
  176. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  177. _, err = kvc.Txn(ctx).
  178. If(clientv3.Compare(clientv3.Value("key"), ">", "abc")). // txn value comparisons are lexical
  179. Then(clientv3.OpPut("key", "XYZ")). // this runs, since 'xyz' > 'abc'
  180. Else(clientv3.OpPut("key", "ABC")).
  181. Commit()
  182. cancel()
  183. if err != nil {
  184. log.Fatal(err)
  185. }
  186. gresp, err := kvc.Get(context.TODO(), "key")
  187. cancel()
  188. if err != nil {
  189. log.Fatal(err)
  190. }
  191. for _, ev := range gresp.Kvs {
  192. fmt.Printf("%s : %s\n", ev.Key, ev.Value)
  193. }
  194. // key : XYZ
  195. }
  196. func ExampleKV_do() {
  197. cli, err := clientv3.New(clientv3.Config{
  198. Endpoints: endpoints,
  199. DialTimeout: dialTimeout,
  200. })
  201. if err != nil {
  202. log.Fatal(err)
  203. }
  204. defer cli.Close()
  205. ops := []clientv3.Op{
  206. clientv3.OpPut("put-key", "123"),
  207. clientv3.OpGet("put-key"),
  208. clientv3.OpPut("put-key", "456")}
  209. for _, op := range ops {
  210. if _, err := cli.Do(context.TODO(), op); err != nil {
  211. log.Fatal(err)
  212. }
  213. }
  214. }