接口幂等性,倒排索引,索引操作,文档基本增删改查,文档查询
0 小需求
虚拟环境,es 集群,节点,分片,副本,全文检索
gil,io模型,bio,nio,aio io多路复用等
-如何写一个短链系统(用djagno写一个短链,给公司其他部门推广用)
-https://www.cnblogs.com/huangxincheng/p/12839160.html
-长链转短链
-短链接:http://suo.im/6sM2AG
-长链接:https://detail.tmall.com/item.htm?id=520828941985&ut_sk=1.XD7VvRDqBU0DAH4ORk9nhaJe_21380790_1588813899709.Copy.1&sourceType=item&price=44&origin_price=51.2&suid=55C94CA2-EDF3-4218-B48B-DA1E5E2440FA&un=2fdfcc84f08e8c0b2040bd82ddc2e1ea&share_crt_v=1&spm=a2159r.13376460.0.0&sp_tk=JEMyb3gxTUZlUjI4JA==&cpp=1&shareurl=true&short_name=h.VQmKBs1&sm=34797a&app=chrome
-http://suo.im/6sM2AG
长地址:短地址(自己生成的)https://m.tb.cn/6sM2AG
mysql:key:value
你访问:https://m.tb.cn/6sM2AG 其实是访问https://m.tb.cn,根据后面的
6sM2AG取数据库查询,重定向到这个地址
1 接口幂等性
# 0 接口幂等性:数学概念,多次请求,相当于一次请求
-get,put,delete都是幂等性的接口
-post 存在幂等性的问题
# 1 前端速度很快,点了两次,会生成两个订单
# 用户在访问新增页面时(提交订单)---》接口返回一个唯一id,提交订单,携带唯一id过来,后端判断这个唯一id是否被用过---》没用过,创建订单
-你在项目中碰到的问题和如何解决(项目收获)
下订单,经常重复订单,点得快,幂等性问题,如何解决的
2 倒排索引
# 1 es介绍10个点
# 2 安装
-jdk :java开发环境
-官网下载es相应的版本,解压,到bin目录下启动elasticsearch.bat
-两个客户端(kibana:官方,发送请求,数据统计,展示,elasticsearch-head:第三方用node写的)
-kibana跟es版本对应,bin路径下,直接启动
-elasticsearch-head需要安装node环境,npm install ---》npm run start
-kibana配置文件配置(copy一下,kibana监听的地址端口,随便名一个名,连接es的地址)
-修改es配置:允许跨域(浏览器的同源策略),cors:跨域资源共享,实现跨域
# es的倒排索引(扩展阅读.md)
-把文章进行分词,对每个词建立索引
3 索引操作
# 索引操作---》数据库操作---》新增,删除,修改,查询
# 1 新增索引:
PUT lqz2 # lqz2就是索引名字
{
"settings": {
"index":{
"number_of_shards":5,
"number_of_replicas":1
}
}
}
# 2 查询索引
GET lqz2/_settings
返回结果
{
"lqz2" : {
"settings" : {
"index" : {
"creation_date" : "1588822389842",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "NBXIeVdHQ26vCuPn8_6uew",
"version" : {
"created" : "7050099"
},
"provided_name" : "lqz2"
}
}
}
}
# 3 更新索引
PUT lqz2/_settings
{
"number_of_replicas": 2
}
#4 删除lqz2索引
DELETE lqz2
4 文档基本增删查改
#1 新增文档
POST lqz2/_doc/1
{
"title":"红楼梦",
"price":12,
"publish_addr":{
"province":"黑龙江",
"city":"鹤岗"
},
"publish_date":"2013-11-11",
"read_num":199,
"tag":["古典","名著"]
}
POST lqz2/_doc/2
{
"title":"西游记",
"price":22,
"publish_addr":{
"province":"上海",
"city":"上海"
},
"publish_date":"2013-11-11",
"read_num":66,
"tag":["古典","小说"]
}
# 2 查询文档(更详细的查找,后面讲)
GET lqz2/_doc/2
# 3 修改文档
-覆盖式(原来的字段就没有了)
PUT lqz2/_doc/1
{
"title":"xxxx",
"price":333,
"publish_addr":{
"province":"黑龙江",
"city":"福州"
}
}
-增量式(只修改某个字段)一定要注意包在doc中
POST lqz2/_doc/1/_update
{
"doc":{
"title":"xx"
}
}
# 4 删除文档
DELETE lqz/_doc/1
# 5 批量获取
GET _mget
{
"docs":[
{
"_index":"lqz2",
"_type":"_doc",
"_id":2
},
{
"_index":"lqz2",
"_type":"_doc",
"_id":1
}
]
}
# 6 批量插入
PUT test/_doc/2/_create
{
"field1" : "value22"
}
POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
5 文档查询
# 测试数据
PUT lqz/doc/1
{
"name":"顾老二",
"age":30,
"from": "gu",
"desc": "皮肤黑、武器长、性格直",
"tags": ["黑", "长", "直"]
}
PUT lqz/doc/2
{
"name":"大娘子",
"age":18,
"from":"sheng",
"desc":"肤白貌美,娇憨可爱",
"tags":["白", "富","美"]
}
PUT lqz/doc/3
{
"name":"龙套偏房",
"age":22,
"from":"gu",
"desc":"mmp,没怎么看,不知道怎么形容",
"tags":["造数据", "真","难"]
}
# 两种方式
-第一种(字符串查询)
GET lqz/doc/_search?q=from:gu
GET lqz/doc/_search?q=age:22
-查询的第二种方式(结构化)
GET lqz/doc/_search
{
"query": {
"match": {
"from": "gu"
}
}
}
GET lqz/doc/_search
{
"query": {
"match": {
"age": 22
}
}
}
GET lqz/doc/_search
{
"query": {
"match": {
"desc": "形容"
}
}
}
GET lqz/doc/_search
{
"query": {
"match": {
"tags": "造数据"
}
}
}
###重点,以后你做项目
mysql,redis,es都有
mysql中:文章表 ,用户表,标签表
文章表
id name content user_id
redis中放缓存
es:只查文章,用户需要查吗?有全文检索需求的东西,放到es中
es的表结构,不一定跟mysql的表结构一致
id name content user_name tag
Only you can control your future
You're not alone. You still have family,peopel who care for you and want to save you.