zoukankan      html  css  js  c++  java
  • cacheed 限制 4节点 3000万 es 批量删除 shell脚本练习 elasticsearch_action

    文件分割

    "www.laiwunews.cn/xinxi/25324717.html"
    "www.zznews.cn/xinxi/10411214.html"
    "tongren.qd8.com.cn/daikuan/xinxi2_41773448.html"
    "www.ailaba.org/sell/2804817.html"
    "bbs.28tui.com/xinxi/3632846.html"
    "www.ailaba.org/sell/1777560.html"
    "bbs.28tui.com/xinxi/6626601.html"
    "info.b2b168.com/s168-58071410.html"
    "www.laiwunews.cn/xinxi/21446979.html"
    "www.ailaba.org/sell/473886.html"
    "info.b2b168.com/s168-43564514.html"
    "info.b2b168.com/s168-56905740.html"
    "info.b2b168.com/s168-45450164.html"
    "info.b2b168.com/s168-45284506.html"
    "info.b2b168.com/s168-15929619.html"
    4877978

    [root@iZ2uZ xiaole_chk_url]#

    执行对分割后的文件执行脚本

    Dload Upload Total Spent Left Speed
    100 2411k 100 1835k 100 575k 2131k 668k --:--:-- --:--:-- --:--:-- 2129k
    /root/xiaole_chk_url/splitfile/4360000bulk.index.del.splitfile.json
    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed
    100 2412k 100 1836k 100 576k 3823k 1199k --:--:-- --:--:-- --:--:-- 3825k
    /root/xiaole_chk_url/splitfile/4370000bulk.index.del.splitfile.json
    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed
    100 2410k 100 1835k 100 575k 4999k 1566k --:--:-- --:--:-- --:--:-- 5000k
    /root/xiaole_chk_url/splitfile/4380000bulk.index.del.splitfile.json
    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed
    100 2413k 100 1836k 100 576k 4647k 1459k --:--:-- --:--:-- --:--:-- 4650k
    /root/xiaole_chk_url/splitfile/4390000bulk.index.del.splitfile.json
    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed
    100 2411k 100 1835k 100 575k 4162k 1305k --:--:-- --:--:-- --:--:-- 4162k
    /root/xiaole_chk_url/splitfile/4400000bulk.index.del.splitfile.json
    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed
    100 576k 100 391 100 576k 23277 33.5M --:--:-- --:--:-- --:--:-- 35.1M
    /root/xiaole_chk_url/splitfile/440000bulk.index.del.splitfile.json
    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed
    100 2415k 100 1837k 100 577k 3168k 996k --:--:-- --:--:-- --:--:-- 3168k
    /root/xiaole_chk_url/splitfile/4410000bulk.index.del.splitfile.json
    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed
    100 2413k 100 1837k 100 576k 4504k 1414k --:--:-- --:--:-- --:--:-- 4502k
    /root/xiaole_chk_url/splitfile/4420000bulk.index.del.splitfile.json
    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed
    100 2414k 100 1837k 100 577k 5531k 1738k --:--:-- --:--:-- --:--:-- 5534k
    /root/xiaole_chk_url/splitfile/4430000bulk.index.del.splitfile.json
    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed
    100 2414k 100 1837k 100 577k 5305k 1667k --:--:-- --:--:-- --:--:-- 5310k
    /root/xiaole_chk_url/splitfile/4440000bulk.index.del.splitfile.json
    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed
    100 2413k 100 1836k 100 576k 4914k 1543k --:--:-- --:--:-- --:--:-- 4911k

    [root@12 xiaole_chk_url]# cat looh.index.splitfile.es.sh 
    split_file_dir='/root/xiaole_chk_url/splitfile/*'
    log_file=bulk.index.del.es.json.log
    for fl in $split_file_dir
    do 
     if test -f $fl
     then 
      echo $fl
      curl -XPOST 10.11.17.14:9200/direct_vote/kwaddress/_bulk --data-binary @$fl >> $log_file
     fi
    done
    echo
    exit 0
    
    [root@12 xiaole_chk_url]# cat looh.index.splitfile.es.sh 
    split_file_dir='/root/xiaole_chk_url/splitfile/*'
    log_file=bulk.index.del.es.json.log
    for fl in $split_file_dir
    do 
     if test -f $fl
     then 
      echo $fl
      curl -XPOST 10.11.17.14:9200/direct_vote/kwaddress/_bulk --data-binary @$fl >> $log_file
     fi
    done
    echo
    exit 0
    
    [root@12 xiaole_chk_url]# 
    
    
    [root@12 xiaole_chk_url]# cat looh.index.sh 
    loop_c=0
    loop_step=10000
    loop_tag=0
    #str_head='{"delete":{"_index":"direct_vote","_type":"kwaddress","_id":"'
    #str_foot='"}}'
    #str_head='{"delete":{"_id":"'
    #str_foot='"}}'
    str_head='{"delete":{"_id":'
    str_foot='}}'
    bulk_file=bulk.del.index.es.json
    log_file=bulk.del.index.es.json.log
    
    echo '' >  $bulk_file
    #for LINE in `cat /root/xiaole_chk_url/chk_url_404_pure_url_4877954.txt`
    for LINE in `cat /root/xiaole_chk_url/chk_url_404_pure_url3.txt`
    do 
     echo $LINE
     loop_c=$((loop_c+1))
     loop_tag=$((loop_c%loop_step))
     echo ${str_head}$LINE${str_foot} >> $bulk_file
     echo $loop_c
     if [ $loop_tag -eq 0 ]
      then
       echo $loop_c
    #   curl -XPOST 10.11.17.14:9200/_bulk --data-binary @$bulk_file >> $log_file
       curl -XPOST 10.11.17.14:9200/direct_vote/kwaddress/_bulk --data-binary @$bulk_file >> $log_file
       sleep 180s
       echo '' >  $bulk_file
     else
       continue
     fi
    done
    
    echo $loop_c
    
    #curl -XPOST 10.11.17.14:9200/_bulk --data-binary @$bulk_file >> $log_file
    curl -XPOST 10.11.17.14:9200/direct_vote/kwaddress/_bulk --data-binary @$bulk_file >> $log_file
    
    echo
    exit 0
    
    [root@12 xiaole_chk_url]# 
    

      

    查询验证
    
    es查询

    查询验证
    
    es查询
    
    curl '10.11.17.14:9200/direct_vote/kwaddress/_search?pretty=true'  -d '{"from": 1,"size": 2}'
    
    curl '10.11.17.14:9200/direct_vote/kwaddress/_search'  -d '
    {
      "query": {
        "bool": {
          "must": [
            { "match": { "_id": "www.ailaba.org/sell/3788942.html" } }
          ]
        }
      }
    }'
    
    curl '10.11.17.14:9200/direct_vote/kwaddress/_search'  -d '
    {
      "query": {
        "bool": {
          "must": [
            { "match": { "_id": "www.fltbearing.com/sell/show-22694236.html" } }
          ]
        }
      }
    }'
    

      

    组合过滤器 | Elasticsearch: 权威指南 | Elastic https://www.elastic.co/guide/cn/elasticsearch/guide/current/combining-filters.html

    这种情况下,我们需要 bool (布尔)过滤器。 这是个 复合过滤器(compound filter) ,它可以接受多个其他过滤器作为参数,并将这些过滤器结合成各式各样的布尔(逻辑)组合。

    布尔过滤器

    一个 bool 过滤器由三部分组成:

    {
       "bool" : {
          "must" :     [],
          "should" :   [],
          "must_not" : [],
       }
    }
    must
    所有的语句都 必须(must) 匹配,与 AND 等价。
    must_not
    所有的语句都 不能(must not) 匹配,与 NOT 等价。
    should
    至少有一个语句要匹配,与 OR 等价。

    集群内的原理 | Elasticsearch: 权威指南 | Elastic https://www.elastic.co/guide/cn/elasticsearch/guide/current/distributed-cluster.html

    ElasticSearch 的主旨是随时可用和按需扩容。 而扩容可以通过购买性能更强大( 垂直扩容 ,或 纵向扩容) 或者数量更多的服务器( 水平扩容 ,或 横向扩容 )来实现。

    虽然 Elasticsearch 可以获益于更强大的硬件设备,但是垂直扩容是有极限的。 真正的扩容能力是来自于水平扩容--为集群添加更多的节点,并且将负载压力和稳定性分散到这些节点中。

    对于大多数的数据库而言,通常需要对应用程序进行非常大的改动,才能利用上横向扩容的新增资源。 与之相反的是,ElastiSearch天生就是 分布式的 ,它知道如何通过管理多节点来提高扩容性和可用性。 这也意味着你的应用无需关注这个问题。

    空集群 | Elasticsearch: 权威指南 | Elastic https://www.elastic.co/guide/cn/elasticsearch/guide/current/_an-empty-cluster.html

     【个节点都知道任意文档所处的位置,并且能够将我们的请求直接转发到存储我们所需文档的节点。】

    一个运行中的 Elasticsearch 实例称为一个 节点,而集群是由一个或者多个拥有相同 cluster.name 配置的节点组成, 它们共同承担数据和负载的压力。当有节点加入集群中或者从集群中移除节点时,集群将会重新平均分布所有的数据。

    当一个节点被选举成为  节点时, 它将负责管理集群范围内的所有变更,例如增加、删除索引,或者增加、删除节点等。 而主节点并不需要涉及到文档级别的变更和搜索等操作,所以当集群只拥有一个主节点的情况下,即使流量的增加它也不会成为瓶颈。 任何节点都可以成为主节点。我们的示例集群就只有一个节点,所以它同时也成为了主节点。

    作为用户,我们可以将请求发送到 集群中的任何节点 ,包括主节点。 每个节点都知道任意文档所处的位置,并且能够将我们的请求直接转发到存储我们所需文档的节点。 无论我们将请求发送到哪个节点,它都能负责从各个包含我们所需文档的节点收集回数据,并将最终结果返回給客户端。 Elasticsearch 对这一切的管理都是透明的。

    https://www.elastic.co/guide/en/elasticsearch/guide/current/_add_an_index.html

    Add an Index

    To add data to Elasticsearch, we need an index—a place to store related data. In reality, an index is just a logical namespace that points to one or more physical shards.

    shard is a low-level worker unit that holds just a slice of all the data in the index. In Inside a Shard, we explain in detail how a shard works, but for now it is enough to know that a shard is a single instance of Lucene, and is a complete search engine in its own right. Our documents are stored and indexed in shards, but our applications don’t talk to them directly. Instead, they talk to an index.

    Shards are how Elasticsearch distributes data around your cluster. Think of shards as containers for data. Documents are stored in shards, and shards are allocated to nodes in your cluster. As your cluster grows or shrinks, Elasticsearch will automatically migrate shards between nodes so that the cluster remains balanced.

    A shard can be either a primary shard or a replica shard. Each document in your index belongs to a single primary shard, so the number of primary shards that you have determines the maximum amount of data that your index can hold.

    我们往 Elasticsearch 添加数据时需要用到 索引 —— 保存相关数据的地方。 索引实际上是指向一个或者多个物理 分片 的 逻辑命名空间 。

    一个 分片 是一个底层的 工作单元 ,它仅保存了 全部数据中的一部分。 在分片内部机制中,我们将详细介绍分片是如何工作的,而现在我们只需知道一个分片是一个 Lucene 的实例,以及它本身就是一个完整的搜索引擎。 我们的文档被存储和索引到分片内,但是应用程序是直接与索引而不是与分片进行交互。

    Elasticsearch 是利用分片将数据分发到集群内各处的。分片是数据的容器,文档保存在分片内,分片又被分配到集群内的各个节点里。 当你的集群规模扩大或者缩小时, Elasticsearch 会自动的在各节点中迁移分片,使得数据仍然均匀分布在集群里。

    一个分片可以是  分片或者 副本 分片。 索引内任意一个文档都归属于一个主分片,所以主分片的数目决定着索引能够保存的最大数据量。

    技术上来说,一个主分片最大能够存储 Integer.MAX_VALUE - 128 个文档,但是实际最大值还需要参考你的使用场景:包括你使用的硬件, 文档的大小和复杂程度,索引和查询文档的方式以及你期望的响应时长。

    While a primary shard can technically contain up to Integer.MAX_VALUE - 128 documents, the practical limit depends on your use case: the hardware you have, the size and complexity of your documents, how you index and query your documents, and your expected response times.

    一个副本分片只是一个主分片的拷贝。 副本分片作为硬件故障时保护数据不丢失的冗余备份,并为搜索和返回文档等读操作提供服务。

    在索引建立的时候就已经确定了主分片数,但是副本分片数可以随时修改。

    让我们在包含一个空节点的集群内创建名为 blogs 的索引。 索引在默认情况下会被分配5个主分片, 但是为了演示目的,我们将分配3个主分片和一份副本(每个主分片拥有一个副本分片):

    PUT /blogs
    {
       "settings" : {
          "number_of_shards" : 3,
          "number_of_replicas" : 1
       }
    }


    水平扩容 | Elasticsearch: 权威指南 | Elastic https://www.elastic.co/guide/cn/elasticsearch/guide/current/_scale_horizontally.html


    当然,如果只是在相同节点数目的集群上增加更多的副本分片并不能提高性能,因为每个分片从节点上获得的资源会变少。 你需要增加更多的硬件资源来提升吞吐量。

    但是更多的副本分片数提高了数据冗余量:按照上面的节点配置,我们可以在失去2个节点的情况下不丢失任何数据。


    文档元数据 | Elasticsearch: 权威指南 | Elastic https://www.elastic.co/guide/cn/elasticsearch/guide/current/_Document_Metadata.html

    文档元数据

    一个文档不仅仅包含它的数据 ,也包含 元数据 —— 有关 文档的信息。 三个必须的元数据元素如下:

    _index
    文档在哪存放
    _type
    文档表示的对象类别
    _id
    文档唯一标识

    _index

    一个 索引 应该是因共同的特性被分组到一起的文档集合。 例如,你可能存储所有的产品在索引 products中,而存储所有销售的交易到索引 sales 中。 虽然也允许存储不相关的数据到一个索引中,但这通常看作是一个反模式的做法。

     

    实际上,在 Elasticsearch 中,我们的数据是被存储和索引在 分片 中,而一个索引仅仅是逻辑上的命名空间, 这个命名空间由一个或者多个分片组合在一起。 然而,这是一个内部细节,我们的应用程序根本不应该关心分片,对于应用程序而言,只需知道文档位于一个 索引内。 Elasticsearch 会处理所有的细节。

    我们将在 索引管理 介绍如何自行创建和管理索引,但现在我们将让 Elasticsearch 帮我们创建索引。 所有需要我们做的就是选择一个索引名,这个名字必须小写,不能以下划线开头,不能包含逗号。我们用website 作为索引名举例。

    _type

    数据可能在索引中只是松散的组合在一起,但是通常明确定义一些数据中的子分区是很有用的。 例如,所有的产品都放在一个索引中,但是你有许多不同的产品类别,比如 "electronics" 、 "kitchen" 和 "lawn-care"。

    这些文档共享一种相同的(或非常相似)的模式:他们有一个标题、描述、产品代码和价格。他们只是正好属于“产品”下的一些子类。

    Elasticsearch 公开了一个称为 types (类型)的特性,它允许您在索引中对数据进行逻辑分区。不同 types 的文档可能有不同的字段,但最好能够非常相似。 我们将在 类型和映射 中更多的讨论关于 types 的一些应用和限制。

    一个 _type 命名可以是大写或者小写,但是不能以下划线或者句号开头,不应该包含逗号, 并且长度限制为256个字符. 我们使用 blog 作为类型名举例。

    _id

    ID 是一个字符串, 当它和 _index 以及 _type 组合就可以唯一确定 Elasticsearch 中的一个文档。 当你创建一个新的文档,要么提供自己的 _id ,要么让 Elasticsearch 帮你生成。

     

    还有一些其他的元数据元素,他们在 类型和映射 进行了介绍。通过前面已经列出的元数据元素, 我们已经能存储文档到 Elasticsearch 中并通过 ID 检索它--换句话说,使用 Elasticsearch 作为文档的存储介质。

    路由一个文档到一个分片中 | Elasticsearch: 权威指南 | Elastic https://www.elastic.co/guide/cn/elasticsearch/guide/current/routing-value.html

    为什么我们要在创建索引的时候就确定好主分片的数量 并且永远不会改变这个数量 

    当索引一个文档的时候,文档会被存储到一个主分片中。 Elasticsearch 如何知道一个文档应该存放到哪个分片中呢?当我们创建文档时,它如何决定这个文档应当被存储在分片 1 还是分片 2 中呢?

    首先这肯定不会是随机的,否则将来要获取文档的时候我们就不知道从何处寻找了。实际上,这个过程是根据下面这个公式决定的:

    shard = hash(routing) % number_of_primary_shards

    routing 是一个可变值,默认是文档的 _id ,也可以设置成一个自定义的值。 routing 通过 hash 函数生成一个数字,然后这个数字再除以 number_of_primary_shards (主分片的数量)后得到 余数 。这个分布在 0 到 number_of_primary_shards-1 之间的余数,就是我们所寻求的文档所在分片的位置。

    这就解释了为什么我们要在创建索引的时候就确定好主分片的数量 并且永远不会改变这个数量:因为如果数量变化了,那么所有之前路由的值都会无效,文档也再也找不到了。

    集群健康度

    [root@a2 xiaole_chk_url]# curl 104.217.217.101:9200/_cluster/health
    {"cluster_name":"es_app","status":"green","timed_out":false,"number_of_nodes":4,"number_of_data_nodes":3,"active_primary_shards":169,"active_shards":338,"relocating_shards":0,"initializing_shards":0,"unassigned_shards":0,"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":100.0}
    [root@a2 xiaole_chk_url]#

    status 字段是我们最关心的。

    status 字段指示着当前集群在总体上是否工作正常。它的三种颜色含义如下:

    green
    所有的主分片和副本分片都正常运行。
    yellow
    所有的主分片都正常运行,但不是所有的副本分片都正常运行。
    red
    有主分片没能正常运行。

    删除操作

    curl '101.217.217.110:9200/direct_vote/kwaddress/_bulk' -d '
    {"delete" : {"_index":"direct_vote","_type":"kwaddress","_id":"www.fltbearing.com/sell/show-22694236.html"}}
    '


    检测集群健康与刷新



    [root@a2 ~]# curl 10.27.217.10:9200/_cluster/health
    {"cluster_name":"es_app","status":"green","timed_out":false,"number_of_nodes":4,"number_of_data_nodes":3,"active_primary_shards":169,"active_shards":338,"relocating_shards":0,"initializing_shards":0,"unassigned_shards":0,"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":100.0}

    [root@a2 ~]# curl 10.27.217.10:9200/_refresh
    {"_shards":{"total":338,"successful":338,"failed":0}}[root@a2 ~]#

    检验存在性
    curl '130.22.217.10:9200/direct_vote/kwaddress/_search?pretty=true' -d '
    {
    "query": {
    "bool": {
    "should": [
    {
    "match": {
    "_id": "site.leshou.com/s/3477475.html"
    }
    }
    ]
    }
    }
    }
    '

    今日完成工作: 2018年3月22日 18:20:26
    检测es删除url的质量;
    解决2个bug:对源自windows的txt文档进行dos2unix处理,shell删除操作过快,导致部分命令没有执行;

    延长批处理的间隔提交时间,由2秒变为10秒,浏览器端实时检测效果,发现执行结果正确,达到删除目的。

    #split_file_dir='/data/xiaole_chk_url/splitfile01/*'
    split_file_dir='/data/xiaole_chk_url/splitfile01/*'
    log_file=${BASH_SOURCE}.log
    #log_file=bulk.index.del.es.json.domaindel.log
    for fl in $split_file_dir
    do
    if test -f $fl
    then
    echo $fl
    curl -XPOST 10.27.217.10:9200/direct_vote/kwaddress/_bulk --data-binary @$fl >> $log_file
    sleep 10s
    fi
    done
    echo
    exit 0

     选出若干


    curl 'hadoop:9200/direct_vote/kwaddress/_search?pretty=true' -d '
    {
    "from": 1,
    "size": 10
    }'
    curl hadoop2:9200/direct_vote/kwaddress/_search?pretty=true -d "{"from": 1,"size": 10,"_source": false}"




     shell 数组遍历 获取系统当前时间

    elasticsearch 查询过滤

    [root@hadoop3 xiaole_chk_url]# cat looh.the.site.del.sh 
    arr=(8 11 16 27 68 74 75 77 81)
    cur="`date +%Y%m%d%H%m%s`"
    res_file=res.${BASH_SOURCE}.${cur}.json.txt
    log_file=${BASH_SOURCE}.log
    es_str=''
    for v in ${arr[@]}
     do 
     es_str='curl hadoop4:9200/direct_vote/kwaddress/_search?pretty=true  -d "{"_source": false,"query": {"match": {"site": "'$v'"}},"from": 1,"size": 9999}"'
     echo $es_str 
     eval  $es_str >> $res_file
    done
    
    exit 0
    
    [root@hadoop3 xiaole_chk_url]# l l-as
    

      




  • 相关阅读:
    Oracle 执行计划(Explain Plan) 说明
    RMAN backup recovery area 命令
    Linux 终端访问 FTP 及 上传下载 文件
    Putty 工具 保存配置的 小技巧
    Oracle 补丁体系 及 opatch 工具 介绍
    闪回恢复区 (Flash Recovery Area)
    Oracle 10g OCP 043 题库 141185题 共185题
    Oracle 补丁体系 及 opatch 工具 介绍
    ORA16401 archivelog rejected by RFS 解决方法
    多表连接的三种方式详解 HASH JOIN MERGE JOIN NESTED LOOP
  • 原文地址:https://www.cnblogs.com/rsapaper/p/8574390.html
Copyright © 2011-2022 走看看