基于搜索引擎的大数据分析技术_第1页
基于搜索引擎的大数据分析技术_第2页
基于搜索引擎的大数据分析技术_第3页
基于搜索引擎的大数据分析技术_第4页
基于搜索引擎的大数据分析技术_第5页
已阅读5页,还剩34页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、技术创新,变革未来基于搜索引擎的大数据分析技术 数据统计与展示4 ES基础知识2 数据搜索3 ELK日志分析1ELK日志分析架构ELK是什么?E=ElasticSearch ,一款基于的Lucene的分布式搜索引擎,我们熟悉的github,就是由ElastiSearch提供的搜索,据传已经有10TB+的数据量。 L=LogStash , 一款分布式日志收集系统,支持多输入源,并内置一些过滤操作,支持多元输出K=Kibana , 一款配合ElasticSearch的web可视化界面,内置非常各种查询,聚合操作,并拥有漂亮的图形化展示功能 数据统计与展示4 ES基础知识2 数据搜索3 ELK日志分

2、析1Elasticsearch简介ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是第二流行的企业搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。Elasticsearch基本概念Elasticsearch中的一些基本概念:Cluster 集群多个相同集群名的节点的集合Node 节点存储数据、参与集群的索引和搜索的服务器Shards & replicas 分片和副本索引数据分多个块存储,

3、并可配置数据的备份具体的含义和内容请参考百度百科- elasticsearchElasticsearch基本概念Elasticsearch与关系型数据库一些概念对比:Elasticsearch的安装非常简单:1. 安装java1.7以上的版本2. 下载elasticsearch:https:/www.elastic.co/downloads/elasticsearch3. 解压进入bin目录:运行elasticsearch.bat,在linux中运行./elasticsearch4. 在浏览器输入http:/localhost:9200,返回节点信息。Elasticsearch的入门请参考:E

4、lasticSearch 简单入门Elasticsearch的安装Elasticsearch提供多种的客户端,包括java、js、perl等。除了客户端之外也可以通过http的方式访问其提供的restful接口做数据的CURD操作。一、插入数据 curl -XPUT http:/localhost:9201/twitter/tweet/1 -d user : kimchy, post_date : 2009-11-15T14:12:12, message : trying out Elasticsearchnote:index =twitter,type =tweet,id =1 curl -

5、XPOST http:/localhost:9201/twitter/tweet -d user : kimchy, post_date : 2009-11-15T14:12:12, message : trying out Elasticsearch note:不指定idElasticsearch数据的CURD二、查询数据 curl -XGET http:/localhost:9201/twitter/tweet/1 返回结果: _index : twitter, _type : tweet, _id : 1, _version : 1, found: true, _source : use

6、r : kimchy, postDate : 2009-11-15T14:12:12, message : trying out Elasticsearch Elasticsearch数据的CURD三、删除数据 curl -XDELETE http:/localhost:9201/twitter/tweet/1返回结果: found : true, _index : twitter, _type : tweet, _id : 1, _version : 2 这里指定了index、type、id,删除单条数据,也可以不指定id 、type等。除此之外,还可以增加查询条件,将符合条件的数据删除。E

7、lasticsearch数据的CURD四、更新数据ES中更新数据可以整个文档更新,也可以部分更新(本质上也是全文档更新),还可以通过脚本方式修改。 curl -XPOST localhost:9200/test/type1/1/_update -d doc : name : new_name curl -XPOST localhost:9200/test/type1/1/_update -d script : ctx._source.counter += count, params : count : 4 Elasticsearch数据的CURD五、批量操作ES中提供批量操作的接口,如下例子批

8、量插入数据:curl -XPOST localhost:9201/cars/transactions/_bulk -d index: price : 10000, color : red, make : honda, sold : 2014-10-28 index: price : 20000, color : red, make : honda, sold : 2014-11-05 index: price : 30000, color : green, make : ford, sold : 2014-05-18 Elasticsearch数据的CURD六、mapping操作 在前面插入数

9、据的例子中并没有对inex的字段做mapping操作,ES会自动的设置字段类型。此外我们还可以通过mapping接口设置索引字段的类型:curl -XPUT http:/localhost:9201/twitter/_mapping/tweet -d tweet : properties : message : type : string, store : true Elasticsearch数据的CURD 数据统计与展示4 ES基础知识2 数据搜索3 ELK日志分析1数据搜索Structured Search 索引中的数据有很多是结构化的,如dates、times、numbers。结构化的数

10、据不用关心相关性、得分情况等,只需要查询确切的值,所以会使用filter做查询。filter查询数据速度快,不用进行相关性打分等操作,同时还有缓存功能。如果某一字段不需要分词解析请更改mapping设置: productID : type : string, index : not_analyzed 2022/8/2317数据搜索Structured Search 在关系型数据库中,确切值的查询:SELECT document FROM products WHERE price = 202022/8/2318批量插入数据:curl -XPOST localhost:9201/my_store/

11、products/_bulk -d index: _id: 1 price : 10, productID : XHDK-A-1293-#fJ3 index: _id: 2 price : 20, productID : KDKE-B-9947-#kL5 index: _id: 3 price : 30, productID : JODL-X-1937-#pV7 index: _id: 4 price : 30, productID : QQPX-R-3956-#aD8 查询price=20的数据:curl -XGET localhost:9201/my_store/products/_sea

12、rch -d query : filtered : query : match_all : , filter : term : price : 20 返回结果:hits : _index : my_store, _type : products, _id : 2, _score : 1.0, _source : price : 20, productID : KDKE-B-9947-#kL5 数据搜索Structured Search 上面查询的是数值类型的字段,还可以查询字符串类型的字段。关系型数据库查询语句:SELECT product FROM products WHERE produc

13、tID = XHDK-A-1293-#fJ32022/8/2319curl -XGET localhost:9201/my_store/products/_search -d query : filtered : filter : term : productID : XHDK-A-1293-#fJ3 返回的结果,productID被分词器解析: tokens : token : xhdk, start_offset : 0, end_offset : 4, type : , position : 1 , token : a, start_offset : 5, end_offset : 6,

14、 type : , position : 2 , token : 1293, start_offset : 7, end_offset : 11, type : , position : 3 , token : fj3, start_offset : 13, end_offset : 16, type : , position : 4 基于上面的结果,我们需要设置productID为not_analyzed:curl -XPUT localhost:9201/my_store -d mappings : products : properties : productID : type : st

15、ring, index : not_analyzed 数据搜索Structured Search 多个查询条件组合可以Bool Filter。 bool : must : , should : , must_not : 关系型数据库查询语句:SELECT product FROM products WHERE (price = 20 OR productID = XHDK-A-1293-#fJ3) AND (price != 30)2022/8/2320curl -XGET localhost:9201/my_store/products/_search -d query : filtered

16、 : filter : bool : should : term : price : 20, term : productID : XHDK-A-1293-#fJ3 , must_not : term : price : 30 返回的结果,productID被分词器解析:hits : _id : 2, _score : 1.0, _source : price : 20, productID : KDKE-B-9947-#kL5 , _id : 3, _score : 1.0, _source : price : 30, productID : JODL-X-1937-#pV7 数据搜索Str

17、uctured Search 除了以上的数据查询,我们还可以使用terms filter以及range filter等。query : filtered : filter : terms : price : 20, 30 query : filtered : filter : range : price : gte : 20, lt : 40 2022/8/2321数据搜索Full-Text Search 结构化的数据字段查询使用filter,但是全文检索在ES中非常重要,我们需要去文档中查询最相关的文档。2022/8/2322批量插入数据:curl -XPOST localhost:9201

18、/my_index/my_type/_bulk d index: _id: 1 title: The quick brown fox index: _id: 2 title: The quick brown fox jumps over the lazy dog index: _id: 3 title: The quick brown fox jumps over the quick dog index: _id: 4 title: Brown fox brown dog 查询语句:curl -XGET localhost:9201/my_index/my_type/_search -d qu

19、ery: match: title: QUICK! 返回结果:_score字段返回该文档对于查询语句的相关度。hits: _id: 1, _score: 0.5, _source: title: The quick brown fox , _id: 3, _score: 0.44194174, _source: title: The quick brown fox jumps over the quick dog , _id: 2, _score: 0.3125, _source: title: The quick brown fox jumps over the lazy dog 数据搜索F

20、ull-Text Search 全文检索的内容非常多,除了简单的match query外还有许多的query类型,如range query、bool query、nested query等,具体的内容请参考ES的官方文档:https:/www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html对于全文检索相关度计算的理论请参考:https:/www.elastic.co/guide/en/elasticsearch/guide/current/controlling-relevance.html2022/8/23

21、23数据搜索Full-Text Search 自然语言的处理,根据不同的分词器会有不同的结果。对于自然语言的处理以及查询请阅读: https:/www.elastic.co/guide/en/elasticsearch/guide/current/languages.html2022/8/2324相同的文档根据分词器的不同会有不同的结果,根据需要可以对相关字段设置其分词器:curl -XPUT localhost:9201/my_index -d mappings: blog: properties: title: type: string, fields: english: type: st

22、ring, analyzer: english 数据统计与展示4 ES基础知识2 数据搜索3 ELK日志分析1聚合查询- Aggregations在关系型数据库中,会有分组查询的语句:SELECT COUNT(color) FROM table GROUP BY color相应的ES中会有聚合查询来做统计,有两个概念要理解:BucketsCollections of documents that meet a criterionMetricsStatistics calculated on the documents in a bucket2022/8/2326参考ES的官方文档,我们来看看E

23、S中聚合统计实例的操作:2022/8/23271、初始化数据:curl -XPOST localhost:9201/cars/transactions/_bulk -d index: price : 10000, color : red, make : honda, sold : 2014-10-28 index: price : 20000, color : red, make : honda, sold : 2014-11-05 index: price : 30000, color : green, make : ford, sold : 2014-05-18 index: price

24、: 15000, color : blue, make : toyota, sold : 2014-07-02 index: price : 12000, color : green, make : toyota, sold : 2014-08-19 index: price : 20000, color : red, make : honda, sold : 2014-11-05 index: price : 80000, color : red, make : bmw, sold : 2014-01-01 index: price : 25000, color : blue, make :

25、 ford, sold : 2014-02-12 2、根据颜色统计销售情况:curl -XGET localhost:9201/cars/transactions/_search?search_type=count -d aggs : colors : terms : field : color 返回结果:took: 65,timed_out: false,_shards: total: 5,successful: 5,failed: 0,hits: total: 8,max_score: 0.0,hits: ,aggregations: colors: doc_count_error_upp

26、er_bound: 0,sum_other_doc_count: 0,buckets: key: red,doc_count: 4, key: blue,doc_count: 2, key: green,doc_count: 2聚合查询- Aggregations上面我们根据统计了不同颜色汽车销售的数据,现在我们在此基础上再统计出不同颜色销售价格的平均值:2022/8/2328curl -XGET localhost:9201/cars/transactions/_search?search_type=count -d aggs: colors: terms: field: color , a

27、ggs: avg_price: avg: field: price took: 34,timed_out: false,_shards: total: 5,successful: 5,failed: 0,hits: total: 8,max_score: 0.0,hits: ,aggregations: colors: doc_count_error_upper_bound: 0,sum_other_doc_count: 0,buckets: key: red,doc_count: 4,avg_price: value: 32500.0,value_as_string: 32500.0, ke

28、y: blue,doc_count: 2,avg_price: value: 20000.0,value_as_string: 20000.0, key: green,doc_count: 2,avg_price: value: 21000.0,value_as_string: 21000.0做完了对平均价值的查询之后我们还可以在此基础上统计不同颜色下不同品牌汽车的销售数据:curl -XGET localhost:9201/cars/transactions/_search?search_type=count -d aggs: colors: terms: field: color , ag

29、gs: avg_price: avg: field: price , make: terms: field: make took: 2,timed_out: false,_shards: total: 5,successful: 5,failed: 0,hits: total: 8,max_score: 0.0,hits: ,aggregations: colors: doc_count_error_upper_bound: 0,sum_other_doc_count: 0,buckets: key: red,doc_count: 4,avg_price: value: 32500.0,val

30、ue_as_string: 32500.0,make: doc_count_error_upper_bound: 0,sum_other_doc_count: 0,buckets: key: honda,doc_count: 3, key: bmw,doc_count: 1, key: blue,doc_count: 2,avg_price: value: 20000.0,value_as_string: 20000.0,make: doc_count_error_upper_bound: 0,sum_other_doc_count: 0,buckets: key: ford,doc_coun

31、t: 1, key: toyota,doc_count: 1, key: green,doc_count: 2,avg_price: value: 21000.0,value_as_string: 21000.0,make: doc_count_error_upper_bound: 0,sum_other_doc_count: 0,buckets: key: ford,doc_count: 1, key: toyota,doc_count: 1聚合查询- Aggregations前面的统计都是对全数据的统计,ES中还可以增加query或者filter的查询条件,对符合条件的数据做聚合统计:20

32、22/8/2329curl -XGET localhost:9201/cars/transactions/_search?search_type=count -d query : filtered: filter: range: price: gte: 10000 , aggs : single_avg_price: avg : field : price 聚合查询- AggregationsAggregations操作其他的相关内容,包括相关性、排序、按时间聚合以及Aggregations操作正确性等内容请参考相关的文档: https:/www.elastic.co/guide/en/ela

33、sticsearch/guide/current/aggregations.html2022/8/2330聚合查询- Aggregations数据组织方式在现实的世界中,数据的相关性非常重要。Relational databases are specifically designedand this will not come as a surprise to youto managerelationships:Each entity (orrow, in the relational world) can be uniquely identified by aprimary key.Enti

34、ties arenormalized. The data for a unique entity is stored only once, and related entities store just its primary key. Changing the data of an entity has to happen in only one place.Entities can be joined at query time, allowing for cross-entity search.Changes to a single entity areatomic,consistent

35、,isolated, anddurable. (SeeACID Transactionsfor more on this subject.)Most relational databases support ACID transactions across multiple entities.2022/8/2331数据组织方式ES的数据存储属于对象存储,所有的数据都是平的。 This FlatWorld has its advantages:Indexing is fast and lock-free.Searching is fast and lock-free.Massive amount

36、s of data can be spread across multiple nodes, because each document is independent of the others.2022/8/2332数据组织方式ES中的FlatWorld虽然有其优点,但是数据间的关系也非常重要。ES通过四种方式来建立数据间的关系:Application-side joinsData denormalizationNested objectsParent/child relationships2022/8/2333数据组织方式 Application-side Joins像关系型数据库一样,在

37、数据中保存相关数据的唯一标识。2022/8/2334初始化数据:curl -XPUT localhost:9201/my_index/user/1 -d name: John Smith, email: john, dob: 1970/10/24 curl XPUT localhost:9201/my_index/blogpost/2 d title: Relationships, body: Its complicated., user: 1 查询语句:curl -XGET localhost:9201/my_index/blogpost/_search -d query: filtered

38、: filter: term: user: 1 数据组织方式Denormalizing Your Data将相关数据中重要的内容冗余的保存在对象中,这样就不用去查询相关的其他数据索引。2022/8/2335初始化数据:curl -XPUT localhost:9201/my_index/user/1 -d name: John Smith, email: john, dob: 1970/10/24curl -XPUT localhost:9201/my_index/blogpost/2 -d title: Relationships, body: Its complicated., user:

39、 id: 1, name: John Smith 查询语句:curl -XGET localhost:9201/my_index/blogpost/_search -d query: bool: must: match: title: relationships , match: : John 数据组织方式Nested Objects将有关系的数据保存在同一个对象中,但是标注为nested对象。Nested方式的优缺点:Nested objectsare useful when there is one main entity, like ourblogpost, with a limited

40、 number of closely related but less important entities, such as comments. It is useful to be able to find blog posts based on the content of the comments, and thenestedquery and filter provide for fast query-time joins.The disadvantages of the nested model are as follows:To add, change, or delete a

41、nested document, the whole document must be reindexed. This becomes more costly the more nested documents there are.Search requests return the whole document, not just the matching nested documents. Although there are plans afoot to support returning the best -matching nested documents with the root

42、 document, this is not yet supported.2022/8/2336设置mapping:curl -XPUT localhost:9201/my_index -d mappings: blogpost: properties: comments: type: nested, properties: name: type: string , comment: type: string , age: type: short , stars: type: short , date: type: date 查询语句:curl -XGET localhost:9201/my_

43、index/blogpost/_search -d query: bool: must: match: title: eggs , nested: path: comments, query: bool: must: match: : john , match: comments.age: 28 数据组织方式Parent-Child Relationship最后一种给数据建立父子关系,这个和nested很像,但是父子关系文档完全独立,nested中子文档虽然是独立的文档,但是存储在相同的对象中。The parent-child functionality allows you to associate one document type with another, in aone-to-manyrelati

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论