• es中文社区
    1. https://github.com/elasticsearch-cn
  • es权威指南
    1. https://es.xiaoleilu.com/
      https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/
  • 搜索类型
      es在查询时,可以指定搜索类型为QUERY_THEN_FETCH,QUERY_AND_FEATCH,DFS_QUERY_THEN_FEATCH和DFS_QUERY_AND_FEATCH
      http://www.cnblogs.com/donlianli/p/3857500.html
      https://www.elastic.co/blog/understanding-query-then-fetch-vs-dfs-query-then-fetch
  • 常用插件:
    1. sql
      如果查询不会写可以用这个 看看他的dsl
      https://github.com/NLPchina/elasticsearch-sql
      header
      https://github.com/mobz/elasticsearch-head
  • 相关资料
    1. exploring elasticsearch http://exploringelasticsearch.com/github_interview.html
      #研发解决方案介绍#基于ES的搜索+筛选+排序解决方案 https://www.cnblogs.com/zhengyun_ustc/p/55solution6.html
  • hadoop集成
    1. https://www.elastic.co/guide/en/elasticsearch/hadoop/current/configuration.html
  • hive集成
    1. https://www.elastic.co/guide/en/elasticsearch/hadoop/current/hive.html
      https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch-hadoop/5.6.11
    常用命令:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    查看索引
    http://127.0.0.1:9200/_cat/indices?v

    curl -i -XGET 'http://127.0.0.1:9200/_count?pretty'

    新增数据
    curl -i -XPOST 'http://127.0.0.1:9200/com/employee/1' -d'{"first_name":"John","last_name":"Smith","age":25,"about":"i love go rock climbing","interests":["sport","music"]}'
    curl -i -XGET 'http://127.0.0.1:9200/com/employee/1?pretty'

    curl -i -XPOST 'http://127.0.0.1:9200/com/employee/2' -d'{"first_name":"weimingwei","last_name":"mingming","age":20,"about":"i love go rock climbing","interests":["music"]}'
    curl -i -XPOST 'http://127.0.0.1:9200/com/employee/3' -d'{"first_name":"yang","last_name":"wenshuai","age":60,"about":"good good study day day up ","interests":["painting"]}'
    curl -i -XPOST 'http://127.0.0.1:9200/com/employee/4' -d'{"first_name":"66777","last_name":"324234324","age":60,"about":"good good study day day up ","interests":["painting"]}'

    简单搜索:
    curl -i -XGET 'http://127.0.0.1:9200/com/employee/_search?pretty'
    curl -i -XGET 'http://127.0.0.1:9200/com/employee/_search?q=last_name=wenshuai&pretty'
    复杂查询
    curl -i -XGET 'http://127.0.0.1:9200/com/employee/_search' -d'
    {
    "query": {
    "filtered": {
    "filter": {
    "range": {
    "age": {
    "gt": 30
    }
    }
    },
    "query": {
    "match": {
    "last_name": "mingwei"
    }
    }
    }
    }
    }'

    全文搜索
    curl -i -XGET 'http://127.0.0.1:9200/com/employee/_search?pretty' -d'
    {

    "query": {
    "match": {
    "about": "love"
    }
    }

    }'

    curl -i -XPUT 'http://127.0.0.1:9200/twitter/_settings?pretty' -d'{"number_of_shards":1,"number_of_replicas":2}'

    任意往里面放数据
    curl -i -XPUT 'http://127.0.0.1:9200/web/nn/123?pretty' -d'{"number_of_shards":1,"number_of_replicas":2}'
    curl -XGET 'http://127.0.0.1:9200/web/nn/123?pretty'
    {
    "_index" : "web",
    "_type" : "nn",
    "_id" : "123",
    "_version" : 1,
    "found" : true,
    "_source" : {
    "number_of_shards" : 1,
    "number_of_replicas" : 2
    }

    过滤字段获取
    curl -XGET 'http://127.0.0.1:9200/web/nn/123?_source=number_of_shards&pretty'
    {
    "_index" : "web",
    "_type" : "nn",
    "_id" : "123",
    "_version" : 1,
    "found" : true,
    "_source" : {
    "number_of_shards" : 1
    }
    只要source
    curl -XGET 'http://127.0.0.1:9200/web/nn/123/_source?pretty'
    {
    "number_of_shards" : 1,
    "number_of_replicas" : 2
    }

    检查是否存在 curl -i -XHEAD 'http://127.0.0.1:9200/web/nn/133' -I

    删除
    curl -XPOST http://10.0.12.108:6335/app_subjoin_renewal_data_di/app_subjoin_renewal_data_di/_delete_by_query -d'
    {"query":{"bool":{"filter":[{"match_phrase":{"date":20180131}}]}}}'

    删除所有:
    curl -XPOST '10.0.9.108:9200/homework/minactivtyfinish/_delete_by_query?conflicts=proceed&pretty' -d'
    {
    "query": {
    "match_all": {}
    }
    }'

    curl -XPOST 'http://10.0.12.110:6335/app_service_tms_teacher_statics_da/app_service_tms_teacher_statics_da/_delete_by_query?conflicts=proceed&pretty' -d'{"query": {"range" : "update_time" : {
    "lt" :
    }
    }
    }
    }'

    中文问题
    curl -i -XPOST 'http://127.0.0.1:9200/com/employee/1' -d'{"first_name":"王伟伟","last_name":"王雪峰","age":25,"about":"i love go rock climbing","interests":["sport","music"]}'

    curl 'http://127.0.0.1:9200/com/employee/_search?pretty' -d'
    {
    "query": {
    "prefix" : { "first_name" : "王伟" }
    }
    }'

    curl 'http://127.0.0.1:9200/com/employee/_search?pretty' -d'
    {
    "query": {
    "prefix" : { "last_name" : "王" }
    }
    }'

    curl -XGET 'http://10.0.12.110:6335/app_es_rpt_kpi_teacher_structure_di/app_es_rpt_kpi_teacher_structure_di/_search?pretty'

    curl 'http://127.0.0.1:9200/com/employee/_search?pretty' -d'
    { "query": {
    "prefix" : { "last_name" : "wen" }
    }
    }

    '

    #scroll
    curl http://10.1.1.246:9200/app_runup_new_teacher_data_di/app_runup_new_teacher_data_di/_search?scroll=1m -d'
    {
    "query": { "match_all": {}},
    "sort" : ["_doc"], //the most efficient sort order
    "size": 10
    }'

    curl http://10.1.1.246:9200/_search/scroll -d'
    {
    "scroll": "1m",
    "scroll_id" : "DnF1ZXJ5VGhlbkZldGNoBQAAAAAACzEGFnI2WFh1RDVJVEt5RERKZ0xhRXZnckEAAAAAAAsxBxZyNlhYdUQ1SVRLeURESmdMYUV2Z3JBAAAAAAALMQgWcjZYWHVENUlUS3lEREpnTGFFdmdyQQAAAAAACzEKFnI2WFh1RDVJVEt5RERKZ0xhRXZnckEAAAAAAAsxCRZyNlhYdUQ1SVRLeURESmdMYUV2Z3JB"
    }'

    删除索引

    curl -XDELETE '10.0.12.108:6335/bi_app_subjoin_renewal_data_flag_di?pretty'
    curl -XDELETE '10.0.12.108:6335/bi_app_subjoin_renewal_data_di?pretty'

    curl -XDELETE '10.0.12.108:6335/app_subjoin_renewal_data_flag_di?pretty'
    curl -XDELETE '10.0.12.108:6335/app_subjoin_renewal_data_di?pretty'

    开源工具地址: https://github.com/taskrabbit/elasticsearch-dump

    查看所有index
    线上 curl ‘10.0.12.100:6335/_cat/indices?v’

    同步mytestindex从线上环境到测试环境

    1
    2
    3
    4
    5
    6
    7
    8
    9
    导出map
    elasticdump \
    --input=http://10.0.12.100:9200/mytestindex \
    --output=./mytestindex.map \
    --type=mapping
    导出数据
    elasticdump \
    --input=http://10.0.12.100:9200/mytestindex \
    --output=./mytestindex.json

    导入map
    elasticdump –input=./mytestindex.map –output=http://10.1.1.246:9200/mytestindex –type=mapping

    导入数据
    elasticdump \
    –input=./mytestindex.json \
    –output=http://10.1.1.246:9200/mytestindex –limit 1000

    1
    2
    3
    4
    5
    6
    7
    sudo -E -u hdfs /home/work/hadoop/bin/hadoop jar /home/work/hadoop/share/hadoop/tools/lib/hadoop-streaming.jar \
        -D mapreduce.job.priority=HIGH \
        -D mapred.reduce.tasks=0 \
        -D stream.non.zero.exit.is.failure=false \
        -input "/user/hive/test/*" \
        -output "/user/hive/testoutput5" \
        -mapper "grep 'job"

    添加 配置stream.non.zero.exit.is.failure=false, grep查找不到 会导致失败

    kudu java-sample: https://github.com/cloudera/kudu-examples/tree/master/java/java-sample
    java -Djava.ext.dirs=./ -DkuduMaster=10.0.2.61:7051 whomm.canel2kudu.App
    运行发现错误

    1) 这个tls报错的问题 https://kudu.apache.org/docs/security.html
    #[29810ms] delaying RPC due to Service unavailable: Master config (10.0.2.61:7051) has no leader. Exceptions received: org.apache.kudu.client.RecoverableException: [Peer master-10.0.2.61:7051] Connection disconnected)
    配置新增:
    –rpc-encryption=disabled
    –rpc_authentication=disabled

    2)副本默认3个的错误(因为单机安装)
    #org.apache.kudu.client.NonRecoverableException: Not enough live tablet servers to create a table with the requested replication factor 3. 1 tablet servers are alive.
    配置新增:
    –unlock_unsafe_flags=true
    –allow_unsafe_replication_factor=true
    –default_num_replicas=1

    最终配置:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    [work@good ~]$ cat /etc/kudu/conf/tserver.gflagfile
    # Do not modify these two lines. If you wish to change these variables,
    # modify them in /etc/default/kudu-tserver.
    --fromenv=rpc_bind_addresses
    --fromenv=log_dir

    --fs_wal_dir=/var/lib/kudu/tserver
    --fs_data_dirs=/var/lib/kudu/tserver

    --rpc-encryption=disabled
    --rpc_authentication=disabled

    --unlock_unsafe_flags=true
    --allow_unsafe_replication_factor=true
    --default_num_replicas=1

    --rpc_negotiation_timeout_ms=9000
    [work@good ~]$ cat /etc/kudu/conf/tserver.gflagfile
    # Do not modify these two lines. If you wish to change these variables,
    # modify them in /etc/default/kudu-tserver.
    --fromenv=rpc_bind_addresses
    --fromenv=log_dir

    --fs_wal_dir=/var/lib/kudu/tserver
    --fs_data_dirs=/var/lib/kudu/tserver

    --rpc-encryption=disabled
    --rpc_authentication=disabled

    --unlock_unsafe_flags=true
    --allow_unsafe_replication_factor=true
    --default_num_replicas=1

    --rpc_negotiation_timeout_ms=9000

    这两个文件
    /etc/kudu/conf/master.gflagfile
    /etc/kudu/conf/tserver.gflagfile
    都需要修改,然后重启

    1
    2
    3
    4
    5
    6
    [root@good work]# service kudu-master restart
    Stopped Kudu Master Server:                                [  OK  ]
    Started Kudu Master Server (kudu-master):                  [  OK  ]
    [root@good work]# service kudu-tserver restart
    Stopped Kudu Tablet Server:                                [  OK  ]
    Started Kudu Tablet Server (kudu-tserver):                 [  OK  ]

    1)100个楼梯,一次只能走 1 、 2、3 步,问有多少种方案?

    动态规划:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    var a = [];
    for ( var i = 1; i <= 100; i++ ){
    if(i == 1){
    a[i] = 1;
    }else if(i == 2){
    a[i] = 2;
    }else if(i == 3){
    a[i] = 4;
    }else{
    a[i] = a[i-1] + a[i-2] + a[i-3];
    }
    }
    console.log(a[100]);

    2)0-40W之间有多少含有1的数,这些数的1的个数是多少?

    含有1的数字:
    1 位数 1
    2 位数 10+9=19
    3 位数 100+9*19 =271
    4 位数 1000+9*271 = 3439
    5 位数 10000+9*3439 = 40951
    6 位数 100000 * 9*40951 = 468559
    40w = 468559 – 6*40950 = 222853
    共有多少个1:
    1 位数 1
    2 位数 11+9=20
    3 位数 100+20 + 9*20 =300
    4 位数 1000+300+ 9* 300 = 4000
    5 位数 50000
    6 位数 600000
    40w = 600000 – 50000*6 = 300000

    3) n个数 从大到小,求每个数之前的所有素数 是多少。

    从2开始,往后一次计算,看哪个数除以i==0,那么把这个数删除掉。
    这样走到那个数的时候就知道,它前面有多少个素数了。

    元字符
    描述
    \
    将下一个字符标记为一个特殊字符、或一个原义字符、或一个向后引用、或一个八进制转义符。例如,“\\n”匹配\n。“\n”匹配换行符。序列“\\”匹配“\”而“\(”则匹配“(”。
    ^
    匹配输入字符串的开始位置。如果设置了RegExp对象的Multiline属性,^也匹配“\n”或“\r”之后的位置。
    $
    匹配输入字符串的结束位置。如果设置了RegExp对象的Multiline属性,$也匹配“\n”或“\r”之前的位置。
    *
    匹配前面的子表达式零次或多次(大于等于0次)。例如,zo*能匹配“z”,“zo”以及“zoo”。*等价于{0,}。
    +
    匹配前面的子表达式一次或多次(大于等于1次)。例如,“zo+”能匹配“zo”以及“zoo”,但不能匹配“z”。+等价于{1,}。
    ?
    匹配前面的子表达式零次或一次。例如,“do(es)?”可以匹配“does”或“does”中的“do”。?等价于{0,1}。
    {n}
    n是一个非负整数。匹配确定的n次。例如,“o{2}”不能匹配“Bob”中的“o”,但是能匹配“food”中的两个o。
    {n,}
    n是一个非负整数。至少匹配n次。例如,“o{2,}”不能匹配“Bob”中的“o”,但能匹配“foooood”中的所有o。“o{1,}”等价于“o+”。“o{0,}”则等价于“o*”。
    {n,m}
    m和n均为非负整数,其中n<=m。最少匹配n次且最多匹配m次。例如,“o{1,3}”将匹配“fooooood”中的前三个o。“o{0,1}”等价于“o?”。请注意在逗号和两个数之间不能有空格。
    ?
    当该字符紧跟在任何一个其他限制符(*,+,?,{n},{n,},{n,m})后面时,匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串,而默认的贪婪模式则尽可能多的匹配所搜索的字符串。例如,对于字符串“oooo”,“o+?”将匹配单个“o”,而“o+”将匹配所有“o”。
    .点
    匹配除“\r\n”之外的任何单个字符。要匹配包括“\r\n”在内的任何字符,请使用像“[\s\S]”的模式。
    (pattern)
    匹配pattern并获取这一匹配。所获取的匹配可以从产生的Matches集合得到,在VBScript中使用SubMatches集合,在JScript中则使用$0…$9属性。要匹配圆括号字符,请使用“\(”或“\)”。
    (?:pattern)
    匹配pattern但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储供以后使用。这在使用或字符“(|)”来组合一个模式的各个部分是很有用。例如“industr(?:y|ies)”就是一个比“industry|industries”更简略的表达式。
    (?=pattern)
    正向肯定预查,在任何匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如,“Windows(?=95|98|NT|2000)”能匹配“Windows2000”中的“Windows”,但不能匹配“Windows3.1”中的“Windows”。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始。
    (?!pattern)
    正向否定预查,在任何不匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如“Windows(?!95|98|NT|2000)”能匹配“Windows3.1”中的“Windows”,但不能匹配“Windows2000”中的“Windows”。
    (?<=pattern)
    反向肯定预查,与正向肯定预查类似,只是方向相反。例如,“(?<=95|98|NT|2000)Windows”能匹配“2000Windows”中的“Windows”,但不能匹配“3.1Windows”中的“Windows”。
    (?<!pattern)
    反向否定预查,与正向否定预查类似,只是方向相反。例如“(?<!95|98|NT|2000)Windows”能匹配“3.1Windows”中的“Windows”,但不能匹配“2000Windows”中的“Windows”。
    x|y
    匹配x或y。例如,“z|food”能匹配“z”或“food”。“(z|f)ood”则匹配“zood”或“food”。
    [xyz]
    字符集合。匹配所包含的任意一个字符。例如,“[abc]”可以匹配“plain”中的“a”。
    [^xyz]
    负值字符集合。匹配未包含的任意字符。例如,“[^abc]”可以匹配“plain”中的“plin”。
    [a-z]
    字符范围。匹配指定范围内的任意字符。例如,“[a-z]”可以匹配“a”到“z”范围内的任意小写字母字符。
    注意:只有连字符在字符组内部时,并且出现在两个字符之间时,才能表示字符的范围; 如果出字符组的开头,则只能表示连字符本身.
    [^a-z]
    负值字符范围。匹配任何不在指定范围内的任意字符。例如,“[^a-z]”可以匹配任何不在“a”到“z”范围内的任意字符。
    \b
    匹配一个单词边界,也就是指单词和空格间的位置。例如,“er\b”可以匹配“never”中的“er”,但不能匹配“verb”中的“er”。
    \B
    匹配非单词边界。“er\B”能匹配“verb”中的“er”,但不能匹配“never”中的“er”。
    \cx
    匹配由x指明的控制字符。例如,\cM匹配一个Control-M或回车符。x的值必须为A-Z或a-z之一。否则,将c视为一个原义的“c”字符。
    \d
    匹配一个数字字符。等价于[0-9]。
    \D
    匹配一个非数字字符。等价于[^0-9]。
    \f
    匹配一个换页符。等价于\x0c和\cL。
    \n
    匹配一个换行符。等价于\x0a和\cJ。
    \r
    匹配一个回车符。等价于\x0d和\cM。
    \s
    匹配任何空白字符,包括空格、制表符、换页符等等。等价于[ \f\n\r\t\v]。
    \S
    匹配任何非空白字符。等价于[^ \f\n\r\t\v]。
    \t
    匹配一个制表符。等价于\x09和\cI。
    \v
    匹配一个垂直制表符。等价于\x0b和\cK。
    \w
    匹配包括下划线的任何单词字符。等价于“[A-Za-z0-9_]”。
    \W
    匹配任何非单词字符。等价于“[^A-Za-z0-9_]”。
    \xn
    匹配n,其中n为十六进制转义值。十六进制转义值必须为确定的两个数字长。例如,“\x41”匹配“A”。“\x041”则等价于“\x04&1”。正则表达式中可以使用ASCII编码。
    \num
    匹配num,其中num是一个正整数。对所获取的匹配的引用。例如,“(.)\1”匹配两个连续的相同字符。
    \n
    标识一个八进制转义值或一个向后引用。如果\n之前至少n个获取的子表达式,则n为向后引用。否则,如果n为八进制数字(0-7),则n为一个八进制转义值。
    \nm
    标识一个八进制转义值或一个向后引用。如果\nm之前至少有nm个获得子表达式,则nm为向后引用。如果\nm之前至少有n个获取,则n为一个后跟文字m的向后引用。如果前面的条件都不满足,若n和m均为八进制数字(0-7),则\nm将匹配八进制转义值nm。
    \nml
    如果n为八进制数字(0-7),且m和l均为八进制数字(0-7),则匹配八进制转义值nml。
    \un
    匹配n,其中n是一个用四个十六进制数字表示的Unicode字符。例如,\u00A9匹配版权符号(&copy;)。
    \< \>
    匹配词(word)的开始(\<)和结束(\>)。例如正则表达式\<the\>能够匹配字符串”for the wise”中的”the”,但是不能匹配字符串”otherwise”中的”the”。注意:这个元字符不是所有的软件都支持的。
    \( \)
    将 \( 和 \) 之间的表达式定义为“组”(group),并且将匹配这个表达式的字符保存到一个临时区域(一个正则表达式中最多可以保存9个),它们可以用 \1 到\9 的符号来引用。
    |
    将两个匹配条件进行逻辑“或”(Or)运算。例如正则表达式(him|her) 匹配”it belongs to him”和”it belongs to her”,但是不能匹配”it belongs to them.”。注意:这个元字符不是所有的软件都支持的。
    +
    匹配1或多个正好在它之前的那个字符。例如正则表达式9+匹配9、99、999等。注意:这个元字符不是所有的软件都支持的。
    ?
    匹配0或1个正好在它之前的那个字符。注意:这个元字符不是所有的软件都支持的。
    {i} {i,j}
    匹配指定数目的字符,这些字符是在它之前的表达式定义的。例如正则表达式A[0-9]{3} 能够匹配字符”A”后面跟着正好3个数字字符的串,例如A123、A348等,但是不匹配A1234。而正则表达式[0-9]{4,6} 匹配连续的任意4个、5个或者6个数字

    一、校验数字的表达式
    1 数字:^[0-9]*$
    2 n位的数字:^\d{n}$
    3 至少n位的数字:^\d{n,}$
    4 m-n位的数字:^\d{m,n}$
    5 零和非零开头的数字:^(0|[1-9][0-9]*)$
    6 非零开头的最多带两位小数的数字:^([1-9][0-9]*)+(.[0-9]{1,2})?$
    7 带1-2位小数的正数或负数:^(\-)?\d+(\.\d{1,2})?$
    8 正数、负数、和小数:^(\-|\+)?\d+(\.\d+)?$
    9 有两位小数的正实数:^[0-9]+(.[0-9]{2})?$
    10 有1~3位小数的正实数:^[0-9]+(.[0-9]{1,3})?$
    11 非零的正整数:^[1-9]\d*$ 或 ^([1-9][0-9]*){1,3}$ 或 ^\+?[1-9][0-9]*$
    12 非零的负整数:^\-[1-9][]0-9″*$ 或 ^-[1-9]\d*$
    13 非负整数:^\d+$ 或 ^[1-9]\d*|0$
    14 非正整数:^-[1-9]\d*|0$ 或 ^((-\d+)|(0+))$
    15 非负浮点数:^\d+(\.\d+)?$ 或 ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$
    16 非正浮点数:^((-\d+(\.\d+)?)|(0+(\.0+)?))$ 或 ^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$
    17 正浮点数:^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ 或 ^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$
    18 负浮点数:^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ 或 ^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$
    19 浮点数:^(-?\d+)(\.\d+)?$ 或 ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$
    二、校验字符的表达式
    1 汉字:^[\u4e00-\u9fa5]{0,}$
    2 英文和数字:^[A-Za-z0-9]+$ 或 ^[A-Za-z0-9]{4,40}$
    3 长度为3-20的所有字符:^.{3,20}$
    4 由26个英文字母组成的字符串:^[A-Za-z]+$
    5 由26个大写英文字母组成的字符串:^[A-Z]+$
    6 由26个小写英文字母组成的字符串:^[a-z]+$
    7 由数字和26个英文字母组成的字符串:^[A-Za-z0-9]+$
    8 由数字、26个英文字母或者下划线组成的字符串:^\w+$ 或 ^\w{3,20}$
    9 中文、英文、数字包括下划线:^[\u4E00-\u9FA5A-Za-z0-9_]+$
    10 中文、英文、数字但不包括下划线等符号:^[\u4E00-\u9FA5A-Za-z0-9]+$ 或 ^[\u4E00-\u9FA5A-Za-z0-9]{2,20}$
    11 可以输入含有^%&’,;=?$\”等字符:[^%&’,;=?$\x22]+
    12 禁止输入含有~的字符:[^~\x22]+
    三、特殊需求表达式
    1 Email地址:^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
    2 域名:[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.?
    3 InternetURL:[a-zA-z]+://[^\s]* 或 ^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$
    4 手机号码:^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$
    5 电话号码(“XXX-XXXXXXX”、”XXXX-XXXXXXXX”、”XXX-XXXXXXX”、”XXX-XXXXXXXX”、”XXXXXXX”和”XXXXXXXX):^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$
    6 国内电话号码(0511-4405222、021-87888822):\d{3}-\d{8}|\d{4}-\d{7}
    7 身份证号(15位、18位数字):^\d{15}|\d{18}$
    8 短身份证号码(数字、字母x结尾):^([0-9]){7,18}(x|X)?$ 或 ^\d{8,18}|[0-9x]{8,18}|[0-9X]{8,18}?$
    9 帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
    10 密码(以字母开头,长度在6~18之间,只能包含字母、数字和下划线):^[a-zA-Z]\w{5,17}$
    11 强密码(必须包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间):^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$
    12 日期格式:^\d{4}-\d{1,2}-\d{1,2}
    13 一年的12个月(01~09和1~12):^(0?[1-9]|1[0-2])$
    14 一个月的31天(01~09和1~31):^((0?[1-9])|((1|2)[0-9])|30|31)$
    15 钱的输入格式:
    16 1.有四种钱的表示形式我们可以接受:”10000.00” 和 “10,000.00”, 和没有 “分” 的 “10000” 和 “10,000”:^[1-9][0-9]*$
    17 2.这表示任意一个不以0开头的数字,但是,这也意味着一个字符”0″不通过,所以我们采用下面的形式:^(0|[1-9][0-9]*)$
    18 3.一个0或者一个不以0开头的数字.我们还可以允许开头有一个负号:^(0|-?[1-9][0-9]*)$
    19 4.这表示一个0或者一个可能为负的开头不为0的数字.让用户以0开头好了.把负号的也去掉,因为钱总不能是负的吧.下面我们要加的是说明可能的小数部分:^[0-9]+(.[0-9]+)?$
    20 5.必须说明的是,小数点后面至少应该有1位数,所以”10.”是不通过的,但是 “10” 和 “10.2” 是通过的:^[0-9]+(.[0-9]{2})?$
    21 6.这样我们规定小数点后面必须有两位,如果你认为太苛刻了,可以这样:^[0-9]+(.[0-9]{1,2})?$
    22 7.这样就允许用户只写一位小数.下面我们该考虑数字中的逗号了,我们可以这样:^[0-9]{1,3}(,[0-9]{3})*(.[0-9]{1,2})?$
    23 8.1到3个数字,后面跟着任意个 逗号+3个数字,逗号成为可选,而不是必须:^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(.[0-9]{1,2})?$
    24 备注:这就是最终结果了,别忘了”+”可以用”*”替代如果你觉得空字符串也可以接受的话(奇怪,为什么?)最后,别忘了在用函数时去掉去掉那个反斜杠,一般的错误都在这里
    25 xml文件:^([a-zA-Z]+-?)+[a-zA-Z0-9]+\\.[x|X][m|M][l|L]$
    26 中文字符的正则表达式:[\u4e00-\u9fa5]
    27 双字节字符:[^\x00-\xff] (包括汉字在内,可以用来计算字符串的长度(一个双字节字符长度计2,ASCII字符计1))
    28 空白行的正则表达式:\n\s*\r (可以用来删除空白行)
    29 HTML标记的正则表达式:<(\S*?)[^>]*>.*?|<.*? /> (网上流传的版本太糟糕,上面这个也仅仅能部分,对于复杂的嵌套标记依旧无能为力)
    30 首尾空白字符的正则表达式:^\s*|\s*$或(^\s*)|(\s*$) (可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式)
    31 腾讯QQ号:[1-9][0-9]{4,} (腾讯QQ号从10000开始)
    32 中国邮政编码:[1-9]\d{5}(?!\d) (中国邮政编码为6位数字)
    33 IP地址:\d+\.\d+\.\d+\.\d+ (提取IP地址时有用)

    与基于接收机(Receiver-based Approach)的方法相比,directstream方法具有以下优点。
    1. 简化并行性:自动创建n个rdd(和分区数目一致)。
    2. 效率:直接读取效率高。
    3. 完全一次的语义:能够很好的避免多次消费。

    ● Simplified Parallelism: No need to create multiple input Kafka streams and union them. With directStream, Spark Streaming will create as many RDD partitions as there are Kafka partitions to consume, which will all read data from Kafka in parallel. So there is a one-to-one mapping between Kafka and RDD partitions, which is easier to understand and tune.
    ● Efficiency: Achieving zero-data loss in the first approach required the data to be stored in a Write Ahead Log, which further replicated the data. This is actually inefficient as the data effectively gets replicated twice – once by Kafka, and a second time by the Write Ahead Log. This second approach eliminates the problem as there is no receiver, and hence no need for Write Ahead Logs. As long as you have sufficient Kafka retention, messages can be recovered from Kafka.
    ● Exactly-once semantics: The first approach uses Kafka’s high level API to store consumed offsets in Zookeeper. This is traditionally the way to consume data from Kafka. While this approach (in combination with write ahead logs) can ensure zero data loss (i.e. at-least once semantics), there is a small chance some records may get consumed twice under some failures. This occurs because of inconsistencies between data reliably received by Spark Streaming and offsets tracked by Zookeeper. Hence, in this second approach, we use simple Kafka API that does not use Zookeeper. Offsets are tracked by Spark

    ● Streaming within its checkpoints. This eliminates inconsistencies between Spark Streaming and Zookeeper/Kafka, and so each record is received by Spark Streaming effectively exactly once despite failures. In order to achieve exactly-once semantics for output of your results, your output operation that saves the data to an external data store must be either idempotent, or an atomic transaction that saves results and offsets (see Semantics of output operations in the main programming guide for further information).

    请注意,这种方法的一个缺点是它不会在Zookeeper中更新偏移量,需要手工自己处理。

    例子中偏移量存储在mysql数据库表格中,方便查阅。

    /home/work/spark-1.6.0-cdh5.8.0/bin/spark-submit
    –jars /home/work/spark-1.6.0-cdh5.8.0/lib/spark-assembly-1.6.0-cdh5.8.0-hadoop2.6.0-cdh5.8.0.jar,/home/work/spark-1.6.0-cdh5.8.0/lib/spark-streaming_2.10-1.6.0-cdh5.8.0.jar –conf spark.streaming.kafka.maxRatePerPartition=40
    ./rr.py 10.0.4.1:9092 nginx_www true
    运行说明:
    ./rr.py brokerlist topic true/false(是否从mysql读取偏移量)
    首次运行的时候mysql表中未存储偏移量所以最后一个参数用false。
    杀死后再次启动用true即可从上次失败位置继续
    数据库的配置在rr.py中设置。数据库表格的创建sql在源码sql中有。

    rr.py

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    #!/usr/bin/env python
    # -*- coding: UTF-8 -*-
    # 存储偏移量到mysql
    from __future__ import print_function




    import sys
    import json
    import traceback
    import logging
    import MySQLdb
    import decimal
    import urllib2
    import time



    from pyspark import SparkContext
    from pyspark.streaming import StreamingContext
    from pyspark.streaming.kafka import KafkaUtils,TopicAndPartition



    FORMAT = '%(asctime)-15s %(message)s'
    logger = logging.getLogger('root')
    logger.setLevel(logging.DEBUG)


    class JSONObject:
        def __init__(self, d):
            self.__dict__ = d


    gdbconf = {

        'ddseconds':10,
        'sparkdbconf' : {
            'host': '20.25.194.93',
            'port': 3306,
            'user': '****',
            'passwd': '*****',
            'db': 'mytest',
            'charset': 'utf8'
        }
    }

    import re




    #处理每一行
    def processrecord(line):
        import sys
        reload(sys)
        sys.setdefaultencoding("utf-8")
        line = line[1].decode('utf-8').encode('utf-8')

        try:

            theone = dict()
            fields = line.split('|')
            if len(fields)>25:
                return fields[25]

            return None



        except ValueError as e:
            #print(e)
            return None
            #return "【line json decode erro】"+line
        except :
            #print(traceback.format_exc())
            raise
        pass

    def getoffset(topic):
        fromOffsets = dict()
        db = MySQLdb.connect(**gdbconf['sparkdbconf'])
        cursor=db.cursor()
        count = cursor.execute("select `partition`,`offset` from sparkstreaming where `topic`='%s' " %(topic))
        if count>=1:
            ofs = cursor.fetchall()
            for o in ofs:
                topicPartion = TopicAndPartition(topic,int(o[0]))
                fromOffsets[topicPartion] = long(o[1])
            return fromOffsets
        else:
            print("no offset found")
            exit(1)
        pass




    def updateoffset(rdd):

        if rdd.isEmpty() is False:

            progress = 'logtime'

            db = MySQLdb.connect(**gdbconf['sparkdbconf'])
            db.autocommit(1)

            cursor=db.cursor()
            for o in rdd.offsetRanges():
                print(o.topic)
                print(o.partition,o.untilOffset,o.untilOffset)
                count = cursor.execute("INSERT INTO sparkstreaming (`topic`,`partition`,`offset`,`progress`) VALUES ('%s',%d,%d,'%s')  ON DUPLICATE KEY UPDATE `offset`=%d,`progress`='%s'" %(o.topic,o.partition,o.untilOffset,progress,o.untilOffset,progress))
                if count>=1:
                    print("update offset success")
                else:
                    print("offset update error")
            pass
            cursor.close()
            db.close()
        else:
            print("rdd is empty no need to update offset")

     #输出数据到数据库
    def get_output(_, rdd):

        newrdd = rdd.map(processrecord).filter(lambda x: x is not None).map(lambda word: (word, 1)).reduceByKey(lambda a, b: a + b)
        if newrdd.isEmpty() is False:

            try:
                updateoffset(rdd)
            except :
                traceback.print_exc()
            else:
                pass

            ##遍历rdd把所有的数据都拿过来
            for jstr in newrdd.collect():
                try:
                    print(jstr)
                except:
                    print(traceback.format_exc())
                    #raise
            pass


    if __name__ == "__main__":
        if len(sys.argv) != 4:
            print("Usage: xxx.py <broker_list> <topic> <fromlast>", file=sys.stderr)
            exit(-1)
        brokers, topic, fromlast  = sys.argv[1:]
        print("Creating new context")
        #create 2 local ddr
        sc = SparkContext("local[2]", "logsdk2")
        ssc = StreamingContext(sc, gdbconf['ddseconds'])

        fromOffsets = None
        if fromlast == "true":
            fromOffsets = getoffset(topic)
            pass

        orderkafkaDstream = KafkaUtils.createDirectStream(ssc, [topic], {"metadata.broker.list": brokers},fromOffsets)
        orderkafkaDstream.foreachRDD(get_output)

        ssc.start()
        ssc.awaitTermination()

    创建mysql相应的表格

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    CREATE TABLE `sparkstreaming` (
      `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
      `topic` VARCHAR(80) NOT NULL DEFAULT '' COMMENT 'kafka topic',
      `partition` INT(11) NOT NULL DEFAULT '0' COMMENT 'kafka partition',
      `offset` BIGINT(20) NOT NULL COMMENT '偏移量',
      `updatetime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
      `progress` VARCHAR(50) DEFAULT NULL COMMENT '日志的时间进度(方便查看)',
      PRIMARY KEY (`id`),
      UNIQUE KEY `idx_topic_partition` (`topic`,`partition`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    参考文档:
    https://spark.apache.org/docs/1.6.1/streaming-kafka-integration.html

    当我们在 shell 的 bash 里操作多行内容的字符串,我们往往会想到 普通的字符串处理办法 例如:

    1
    2
    string="Hello linux"
    echo $string

    其实 bash 提供了一个非常好的解决办法,就是 “Multi-line”
    变量的基本使用
    e.g. 包含变量

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    cat > myfile.txt <<EOF
    this file has $variable $names $inside
    EOF


    # 注入文档到 myfile.txt
    cat myfile.txt
    #输入:
    #this file has

    variable="ONE"
    names="TWO"
    inside="expanded variables"

    cat > myfile.txt <<EOF
    this file has $variable $names $inside
    EOF


    #print out the content of myfile.txt
    cat myfile.txt
    #输入:
    #this file has ONE TWO expanded variables

    无变量

    1
    2
    3
    4
    5
    6
    7
    8
    9
    cat > myfile.txt <<"EOF"
    this file has $variable $dollar $name $inside
    EOF

    cat myfile.txt
    #得到
    #this file has $variable $dollar $name $inside

    #PS:引用符号 "EOF" 决定是否需要输入变量

    无变量 – 例子 2

    1
    2
    3
    4
    5
    6
    7
    8
    9
    cat > myfile.txt <<EOF
    this file has $variable \$dollar \$name \$inside
    EOF


    cat myfile.txt
    # 得到
    # this file has $variable $dollar $name $inside

    #转义 dollar "$" 符号,bash将取消变量的解析

    将一个多行文本赋值到变量里面
    例1:

    1
    2
    3
    4
    5
    6
    7
    8
    read -d '' stringvar <<-"_EOF_"

    all the leading dollars in the $variable $name are $retained

    _EOF_
    # 输入变量
    echo $stringvar;
    # all the leading dollars in the $variable $name are $retained

    例2:

    1
    2
    3
    4
    5
    6
    read -d '' help <<- "_EOF_"
      usage: up [--level <n>| -n <levels>][--help][--version]

      Report bugs to:
      up home page:
    _EOF_

    例3:

    1
    2
    3
    4
    5
    6
    VARIABLE1="<?xml version="1.0" encoding='UTF-8'?>
    <report>
      <img src="a-vs-b.jpg"/>
      <caption>Thus is a future post on Multi Line Strings in bash
      <date>1511</date>-<date>1512</date>.</caption>
    </report>"

    例4:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    VARIABLE2=$(cat <<EOF
    <?xml version="1.0" encoding='UTF-8'?>
    <report>
      <img src="a-vs-b.jpg"/>
      <caption>Thus is a future post on Multi Line Strings in bash
      <date>1511</date>-<date>1512</date>.</caption>
    </report>
    EOF
    )

    例5:

    1
    2
    3
    4
    5
    6
    7
    8
    VARABLE3=`cat <<EOF
    <?xml version="1.0" encoding='UTF-8'?>
    <report>
      <img src="a-vs-b.jpg"/>
      <caption>Thus is a future post on Multi Line Strings in bash
      <date>1511</date>-<date>1512</date>.</caption>
    </report>
    EOF`

    例6 (直接写入文件):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    cat > heredocfile.txt <<_EOF_
    I am line 1
    I am line 2
    I'm the last line
    _EOF_

    # 测试
    cat heredocfile.txt
    # I am line 1
    # I am line 2
    # I'm the last line

    # and then, change your echo statement to include the '-e' option
    # which will turn on escape sequence processing:
    echo -e $USAGE >&2

    例7:

    1
    2
    3
    4
    5
    6
    7
    sudo cat > /aaaa.txt <<_EOF_
    I am line 1
    I am line 2
    I'm the last line
    _EOF_

    # sudo and >>: permission denied

    例8:

    1
    2
    3
    4
    # create
    sudo tee /aaa.txt << EOF
      echo "Hello World 20314"
    EOF

    例9(可向文本文件追加):

    1
    2
    3
    4
    # Append to Sudo
    sudo tee -a  /aaa.txt << EOF
     echo "This Line is appended"
    EOF

    例如10:

    1
    2
    3
    4
    5
    6
    sudo sh -c "cat > /aaa.txt" <<"EOT"
    this text gets saved as sudo - $10 - ten dollars ...
    EOT

    cat /aaa.txt
    #this text gets saved as sudo - $10 - ten dollars ...

    例11:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    cat << "EOF" | sudo tee /aaa.txt
    let's count
    $one
    two
    $three
    four

    EOF

    cat /aaa.txt
    #let's count
    #$one
    #two
    #$three
    #four

    关于 tee
    > tee –help
    Usage: tee [OPTION]… [FILE]…
    Copy standard input to each FILE, and also to standard output.

    -a, –append append to the given FILEs, do not overwrite
    -i, –ignore-interrupts ignore interrupt signals
    –help display this help and exit
    –version output version information and exit

    If a FILE is -, copy again to standard output.

    Report tee bugs to bug-coreutils@gnu.org
    GNU coreutils home page:
    General help using GNU software:
    For complete documentation, run: info coreutils ‘tee invocation’

    参考:
    1. Heredoc Quoting – Credit to Ignacio Vazquez-Abrams: http://serverfault.com/questions/399428/how-do-you-escape-characters-in-heredoc
    2. eredoc Quoting – Credit to Dennis Williamson: http://stackoverflow.com/questions/3731513/how-do-you-type-a-tab-in-a-bash-here-document
    3. http://serverfault.com/questions/72476/clean-way-to-write-complex-multi-line-string-to-a-variable
    4. http://arstechnica.com/civis/viewtopic.php?p=21091503
    5. http://superuser.com/questions/201829/sudo-permission-denied
    6. http://stackoverflow.com/questions/4937792/using-variables-inside-a-bash-heredoc
    7. http://stackoverflow.com/questions/2600783/how-does-the-vim-write-with-sudo-trick-work
    8. http://www.unix.com/shell-programming-scripting/187477-variables-heredoc.html

    来源:http://www.woola.net/detail/2016-09-05-bash-multi-line-text.html

    Encrypting Your File

    tar and gzip the file, then encrypt it using des3 and a secret key.
    tar cvzf – mysql_dump.sql | openssl des3 -salt -k #YOUR PASSWORD# | dd of=encrypted_mysql_dump
    That simple!

    Decrypting Your File

    dd if=encrypted_mysql_dump |openssl des3 -d -k #YOUR PASSWORD# |tar xvzf –