天天看點

ELK學習筆記

ElasticSearch

安裝搭建步驟見部落格:Linux安裝部署elasticsearch

代碼配置

pom.xml

<dependency>
	<groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>6.0.0</version>
</dependency>

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>transport</artifactId>
    <version>6.0.0</version>
</dependency>
           

application-default.yml

es:
  clusterName: my-application
  client:
    transport:
      sniff: true
  host: 127.0.0.1 // 部署es的IP位址
  port: 9300
  shards: 5
  replicas: 1
  nodeName: node-1
           

為什麼ElasticSearch.yml是9200端口,但是項目中連結要配置9300?

9200作為Http協定,主要用于外部通訊
9300作為Tcp協定,jar之間就是通過tcp協定通訊
ES叢集之間是通過9300進行通訊
kibana連結elasticSearch配置9200
           

es主鍵?

es中預設将id作為主鍵,資料同步至_id
但是可以在資料插入時設定某一字段為主鍵,new IndexRequest().id(property)
           

代碼操作

/**
 * ElasticSearch用戶端
 */
@Autowired
private TransportClient client;

/**
 * 判斷索引是否存在
 * @param index 索引名稱
 */
public boolean isIndexExist(String index) {
    return client.admin().indices().prepareExists(index).execute().actionGet().isExists();
}
	

/**
 * 删除索引
 * @param index 索引名稱
 */
public boolean deleteIndex(String index) {
    return isIndexExist(index)
        ? client.admin().indices().prepareDelete(index).execute().actionGet().isAcknowledged() 
        : false;

/**
 * 新增索引
 * @param index 索引名稱
 */
public boolean addIndex(String index) {
    return isIndexExist(index) 
        ? false
        : client.admin().indices().prepareCreate(index).execute().actionGet().isAcknowledged();
}
           

Kibana

安裝部署

wget https://artifacts.elastic.co/downloads/kibana/kibana-6.0.0-linux-x86_64.tar.gz

tar -zxvf kibana-6.4.3-linux-x86_64.tar.gz 

cd kibana-6.4.3-linux-x86_64

vim config/kibana.yml 

修改配置檔案中的三個地方(注釋掉的删除注釋)

server.port: 5601

server.host: "172.23.4.218"  (此處改成伺服器的IP)

elasticsearch.url: "http://172.23.4.218:9200"  (此處改成es的通路位址)

cd /usr/local/kibana-6.4.3-linux-x86_64/bin

./kibana

浏覽器通路:http://172.23.4.218:5601
           

建立索引

PUT test   //test:索引名稱
{
  "mappings": {
    "product": {   //product:type名稱  ps:es版本6.0以下是多type,6.0是單type,6.0之後沒有type
      "properties": {
        "cId": {"type": "text"},
        "cName": {"type": "text"}
      }
    }
  }
}
           

删除索引

未完待續