Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Basic use of ElasticSearch

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Basic introduction to 1.rest

REST is called Representational State Transfer. It is an architectural style of software, not a standard, but provides a set of design principles and constraints. It is mainly used for client and server interaction class software. Software designed based on this style can be more concise, more hierarchical, and easier to implement caching and other mechanisms. In fact, to put it bluntly, it is a visit similar to HTTP, which is very similar to HTTP.

The operations related to rest are:

-GET: gets the current state of the object

-PUT: change the state of an object

-POST: create object

-DELETE: delete object

-HEAD: get header information

Example:

Resources

The URI of a set of resources, such as http://zzy.com/res/

URI of a single resource, such as http://z.com/res/123

GET lists the URI and the details of each resource in the resource group

Get the details of the specified resource. You can choose an appropriate network resource media type (json, xml).

PUT replaces the current entire set of resources with a given set of resources replaces / creates the specified resource and appends it to the corresponding resource

POST creates / appends a new resource to this group of resources, which often returns a new URL

Treat the specified resource as a resource group and create / append a new element under it to make it belong to the current resource

DELETE deletes an entire group of resources

Delete the specified element

Rest interface built into ES:

URL

Description

/ index/_search

Search for data under the specified index

/ _ aliases

Gets or manipulates the alias of the index

/ index/

View the details of the specified index

/ index/type/

Create or operate type

/ index/_mapping

Create or manipulate mapping

/ index/_setting

Create or manipulate settings (number_of_shards)

/ index/_open

Opens the specified closed index

/ index/_close

Closes the specified index

/ index/_refresh

Refresh the index (make the new content visible to the search, no guarantee that the data will be written to disk)

/ index/flush

Refresh the index (Lucene commit will be triggered)

two。 Use CURL to manipulate ES

Url is an open source file transfer tool that uses URL syntax to work on the command line. Using curl, you can easily implement common get/post requests. Simply think of it as a tool that can access url under the command line. There is a curl tool in the default library of centos, if you don't have it installed by yum.

The basic operation of curl:

The request method of the specified http is HEAD GET POST PUT DELETE.

ZD-d specifies the data to be transferred

The-H specifies the http request header information

(1) create an index library

Syntax: curl-XPUT: 9200/index_name/ "> http://:9200/index_name/

Example: curl-XPUT 'http://test:9200/zzy'

(2) create an index

Curl-H "Content-Type: application/json"-XPOST 'http://test:9200/zzy/info/1'-d' {"name": "hadoop", "author": "Doug Cutting", "core": ["hdfs", "mr", "yarn"], "last_version": 3.0}'

Here are a few points to add to the beginner editor:

The difference between PUT and POST:

PUT is idempotent, but POST is not. Therefore, it is more appropriate for PUT users to update and POST for new ones. The creation operation can use POST or PUT. The difference is that POST acts on a collection resource (/ articles), while PUT operation acts on a specific resource (/ articles/123). For example, many resources use the database self-increasing primary key as identification information, so you need to use PUT. When the identity information of the created resource can only be provided by the server, POST must be used.

Notes for ES to create index libraries and indexes:

* Index library names must be all lowercase, cannot begin with an underscore, or contain commas.

* if the ID of the index data is not explicitly specified, the es will automatically generate a random ID, which requires the use of the POST parameter.

Example: (do not specify id)

Curl-H "Content-Type: application/json"-XPOST 'http://test:9200/zzy/info/'-d' {"author": "Doug Cutting"}'

Example: (create new data)

Curl-H "Content-Type: application/json"-XPOST 'http://test:9200/zzy/info/2?op_type=create'-d' {"name": "hbase"}'

(3) query operation

Example 1 (query all):

Curl-XGET / / Note? pretty means to make the json appear better.

Example 2: (retrieving a portion of a document to display specific field contents)

Curl-XGET 'http://test:9200/zzy/info/1?_source=name,author&pretty'

Example 3: (query according to conditions)

Curl-XGET 'http://test:9200/zzy/info/_search?q=name=hadoop&pretty'

(4) Update operation

ES can use PUT/GET to update the document, and if the document for the specified ID already exists, the update operation is performed.

When ES performs an update operation, it first marks the old document as deleted, and then adds a new document. The old document does not disappear immediately, but it is also inaccessible. ES will continue to clean up documents that have been marked with deletion status in the background as they continue to add more data.

Example: (partial update)

Curl-H "Content-Type: application/json"-XPOST http://test:9200/zzy/info/1/_update-d'{"doc": {"name": "apache-hadoop"}'/ / Note: "doc": {"name": "apache-hadoop"} can perform updates or inserts.

(5) Delete operation

Example: (delete normally, delete according to id)

Curl-XDELETE http://test:9200/zzy/info/2/

Note:

If the document exists, the value of the es property found:true,successful:1,_version property is + 1.

If the document does not exist, the es attribute found is false, but the version value version will still be + 1, which is part of the internal management, a bit like the svn version number, which ensures that the order of our different operations across multiple nodes is correctly marked.

After a document is deleted, it does not take effect immediately, it is just marked as deleted. ES will delete it in the background when you add more indexes later.

(6) bulk operation

Example:

Curl-H "Content-Type: application/json"-XPOST 'http://test:9200/bank/accout/_bulk?pretty'-- data-binary "@ account.json"

Bulk will load the data to be processed into memory, so the amount of data to be processed in a batch is limited. It is generally recommended that 1000 to 5000 documents are available. If your document is very large, you can reduce the queue appropriately. The recommended size is 5~15MB, which cannot exceed 100m by default. You can modify this value in the configuration file of es: http.max_content_length:100mb.

Version Control of 3.ES

(1) General database and ES control data reading strategy:

A normal relational database is used to lock the row before we read the data, and then make sure that only the thread that reads the data can modify the row. While ES uses (optimistic concurrency control), ES will not block the access of a certain data. if the basic data changes during the interval between reading and writing, the update will fail. At this time, it is up to the program to decide how to deal with the conflict. It can be re-reading the updated data or feedback the failure directly to the user.

(2) ES implements version control:

/ / first get the document that needs to be modified, and obtain the version number curl-XGET http://test:9200/zzy/info/1?pretty

/ / then pass the version to curl-H "Content-Type: application/json"-XPUT http://test:9200/zzy/info/1?version=2-d'{"name": "hadoop", "version": 4}'/ / Note this is an overwrite operation when performing the update operation

Note: if the version number passed does not match the version number of the document to be updated, the update will fail.

(3) external version of ES:

If the database already has a version number, or a timestamp that can represent the version, you can add version_type=external after the query url of ES to use this number. The version number must be an integer greater than 0 and less than 9223372036854775807 (the maximum positive value of long in Java). And when es processes the external version number, it no longer checks whether the _ version is equal to the value specified in the request, but checks whether the current _ version is smaller than the specified value, and if so, the request succeeds.

Example:

Curl-H "Content-Type: application/json"-XPUT 'http://test:9200/zzy/info/3?version=10&version_type=external'-d' {"name": "flink"}'

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report