分布式是写出来的(三)

添加元数据服务

元数据服务就是对元数据提供存取功能的服务。元数据就是系统定义的基本信息,比如一张相片的名字,版本,拍摄时间,散列值等。客户端和接口服务之间根据对象的名字来引用一个对象,一个对象可以有多个版本,除了删除标志外,每个版本指向数据服务的一个实际的数据存储

ES的基本使用

ES或者任意的一个分布式数据库都可以作为元数据服务
启动(windows)elasticsearch-7.6.2-windows-x86_64\elasticsearch-7.6.2\bin\elasticsearch.bat
成功http://192.168.7.6:9200/people

链接服务器的golang操作

全文搜索引擎 Elasticsearch 入门教程 作者: 阮一峰
ES官方中文手册
添加索引

package main
 
import (
	"context"
	"fmt"
	"github.com/olivere/elastic"
)
 
func main(){
	Client, err := elastic.NewClient(elastic.SetURL("http://192.168.7.6:9200"))
	fmt.Println(Client, err)
	name := "people2"
	Client.CreateIndex(name).Do(context.Background())
}

插入和查找

func main(){
	Client, err := elastic.NewClient(elastic.SetURL("http://192.168.7.6:9200"))
	fmt.Println(Client, err)
	name := "people2"
	data := `{
	"name": "wali",
	"country": "Chian",
	"age": 30,
	"date": "1987-03-07"
	}`
	_, err = Client.Index().Index(name).Type("man1").Id("1").BodyJson(data).Do(context.Background())

	get, err := Client.Get().Index(name).Type("man1").Id("1").Do(context.Background())
	fmt.Println(get, err)

ES的API访问(curl)

常用操作

浏览ES服务器

$ curl -XGET http://localhost:9200/
{
  "name" : "DESKTOP-PVBHUQ5",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "79x294HmR3iDFIQ2M3-_Kg",
  "version" : {
    "number" : "7.6.2",
    "build_flavor" : "default",
    "build_type" : "zip",
    "build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
    "build_date" : "2020-03-26T06:34:37.794943Z",
    "build_snapshot" : false,
    "lucene_version" : "8.4.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

插入索引

$curl -XPUT http://localhost:9200/people

{"acknowledged":true,"shards_acknowledged":true,"index":"people"}

往索引库中新增数据

curl  http://localhost:9200/people/ -XPOST -d '{"author" : "a", "name":"a"}' 

查看集群健康状况

curl -XGET http://localhost:9200/_cluster/health?format=yaml
---
cluster_name: "elasticsearch"
status: "yellow"
timed_out: false
number_of_nodes: 1
number_of_data_nodes: 1
active_primary_shards: 3
active_shards: 3
relocating_shards: 0
initializing_shards: 0
unassigned_shards: 3
delayed_unassigned_shards: 0
number_of_pending_tasks: 0
number_of_in_flight_fetch: 0
task_max_waiting_in_queue_millis: 0
active_shards_percent_as_number: 50.0

获取ES所有索引

$~curl -XGET  http://localhost:9200/_cat/indices
yellow open people2   OIyNXAzwSvCXdRTM1OCcug 1 1 1 0 10.2kb 10.2kb
yellow open bigdata_p GlbTA7_xSVK26L_oU_pVKw 1 1 0 0   283b   283b
yellow open people    AwsSuR6VS_uCfkNvqDxc6Q 1 1 0 0   283b   283b

获取索引字段

C:\Users\HodgeKou> curl -XGET http://localhost:9200/people2/_mapping?format=yaml
---
people2:
  mappings:
    properties:
      age:
        type: "long"
      country:
        type: "text"
        fields:
          keyword:
            type: "keyword"
            ignore_above: 256
      date:
        type: "date"
      name:
        type: "text"
        fields:
          keyword:
            type: "keyword"
            ignore_above: 256

新建索引

curl -XPUT localhost:9200/people2

删除索引

curl -XDELETE localhost:9200/people?format=yaml

插入单条数据

curl -XPUT localhost:9200/people/external/1?format=yaml' -d 
quote> { "name":"paxi"}'

查询单条数据

curl -XGET  localhost:9200/people2/_search?pretty

删除单条数据

curl -XDELETE localhost:9200/people2/external/3?format=yaml

ES进行更新PUT请求时,会重发数据

第一次PUT

$ curl -X PUT 'localhost:9200/accounts/person/1' -d '
{
  "user": "张三",
  "title": "工程师",
  "desc": "数据库管理"
}' 
---------------------------------------------------
{
  "_index":"accounts",
  "_type":"person",
  "_id":"1",
  "_version":1,
  "result":"created",
  "_shards":{"total":2,"successful":1,"failed":0},
  "created":true
}

第二次PUT,更改了数据,banben_version加1

$ curl -X PUT 'localhost:9200/accounts/person/1' -d '
{
    "user" : "张三",
    "title" : "工程师",
    "desc" : "数据库管理,软件开发"
}' 

----------------------------------------------------
{----
  "_index":"accounts",
  "_type":"person",
  "_id":"1",
  "_version":2,
  "result":"updated",
  "_shards":{"total":2,"successful":1,"failed":0},
  "created":false
}
上面代码中,我们将原始数据从"数据库管理"改成"数据库管理,软件开发"。 返回结果里面,有几个字段发生了变化。


"_version" : 2,
"result" : "updated",
"created" : false
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值