elasticsearch学习(二)——Linux环境安装Elasticsearch6.0

注意:Elasticsearch使用非常简单,深入了解较繁琐而已

一、Linux环境安装Elasticsearch

(1)安装JDK环境变量

export JAVA_HOME=/usr/local/jdk1.8.0_181
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

source /etc/profile

(2)下载elasticsearch安装包

下载elasticsearch安装包

官方文档https://www.elastic.co/downloads/elasticsearch
注意:linux安装内存建议1g内存以上

(3)上传elasticsearch安装包

(4)解压elasticsearch

tar -zxvf elasticsearch-6.4.3.tar.gz

(5)修改elasticsearch.yml

network.host: 192.168.212.151
http.port: 9200

(6)启动elasticsearch报错

Cd /usr/local/elasticsearch-6.4.3/bin
./elasticsearch

can not run elasticsearch as root

解决方案:

因为安全问题elasticsearch 不让用root用户直接运行,所以要创建新用户

第一步:liunx创建新用户  adduser XXX    然后给创建的用户加密码 passwd XXX    输入两次密码。

第二步:切换刚才创建的用户 su XXX  然后执行elasticsearch  会显示Permission denied 权限不足。

第三步:给新建的XXX赋权限,chmod 777 *  这个不行,因为这个用户本身就没有权限,肯定自己不能给自己付权限。所以要用root用户登录付权限。

第四步:root给XXX赋权限,chown -R XXX /你的elasticsearch安装目录。

然后执行成功。

 

创建一个分组

 groupadd XX

useradd Xr-g XX-p 123456

chown -R Xr elasticsearch-6.4.3

su Xr切换用户

 

继续报错

bootstrap checks failed max virtual memory areas vm.max_map_count [65530] is

 

vi /etc/sysctl.conf

vm.max_map_count=655360

sysctl -p

 

max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]

vi /etc/security/limits.conf

* soft nofile 65536

* hard nofile 131072

* soft nproc 2048

* hard nproc 4096

 

重启服务器即可

(7)访问elasticsearch

关闭防火墙 systemctl stop firewalld.service


http://192.168.212.151:9200

(8)9300与9200区别:

9300端口: ES节点之间通讯使用
9200端口: ES节点 和 外部 通讯使用

二、Linux环境安装Kibana

什么是Kibana:

是一个开源的分析和可视化平台,设计用于和Elasticsearch一起工作。用Kibana来搜索,查看,并和存储在Elasticsearch索引中的数据进行交互。以轻松地执行高级数据分析,并且以各种图标、表格和地图的形式可视化数据。Kibana使得理解大量数据变得很容易,实时显示Elasticsearch查询的变化

(1)Kibana环境安装

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


vim config/kibana.yml
# 将默认配置改成如下:
server.port: 5601
server.host: "192.168.212.151"
elasticsearch.url: "http:// 192.168.212.151:9200"

启动Kibana
./bin/kibana

http://192.168.212.179:5601/app/kibana

访问出这个界面说明安装成功了: 

(2)Kibana实现增删改查

###创建索引

PUT /mymayikt


####查询索引
GET /mymayikt

####添加文档 /索引名称/类型/id
PUT /mymayikt/user/1
{
  "name":"XrCheng",
  "sex":0,
  "age":22
}


###查询文档
GET /mymayikt/user/1

###删除索引
DELETE /mymayikt

三、SpringBoot整合Elasticsearch

(1)maven依赖

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
	</dependencies>

 (2)application.yml

spring:
  data:
    elasticsearch:
    ####集群名称
     cluster-name: myes
    ####地址 
     cluster-nodes: 192.168.212.151:9300

(3)实体类层

@Document(indexName = "XX", type = "user")
//@Data注解就不需要get和set方法了
@Data
public class UserEntity {
	@Id
	private String id;
	private String name;
	private int sex;
	private int age;
}

(4)Dao类层

public interface UserReposiory extends CrudRepository<UserEntity, String> {

}

(5)控制器层

@RestController
public class EsController {

	@Autowired
	private UserReposiory userReposiory;
     
    //添加文档
	@RequestMapping("/addUser")
	public UserEntity addUser(@RequestBody UserEntity user) {
		return userReposiory.save(user);
	}

    //查找文档
	@RequestMapping("/findUser")
	public Optional<UserEntity> findUser(String id) {
		return userReposiory.findById(id);
	}

}

(6)启动项目

@SpringBootApplication
@EnableElasticsearchRepositories(basePackages = "com.xxx.repository")
public class AppEs {

	public static void main(String[] args) {
		SpringApplication.run(AppEs.class, args);
	}
}

(7)报错以下错误

None of the configured nodes are available:

解决方案:

Vi /usr/local/elasticsearch-6.4.3/config/elasticsearch.yml
cluster.name: myes

四、windows下完ES整搭建步骤

1.解压好压缩包进入bin目录输入elasticsearch启动,进入浏览器访问localhost:9200看是否成功
2.用PostMan的PUT请求创建索引库http://localhost:9200/articleindex
3.新建文档用POST请求 http://localhost:9200/articleindex/article

{
"title":"SpringBoot2.0",
"content":"发布啦"
}

4.查询全部文档,GET请求 http://127.0.0.1:9200/articleindex/article/_search
5.修改文档PUT请求 http://192.168.184.134:9200/articleindex/article/AWPKrI4pFdLZnId5S_F7(_id)

{
"title":"SpringBoot2.0正式版",
"content":"发布了吗"
}

如果在地址中的ID不存在,则会创建新文档
6.基本匹配查询(模糊查询),比如按标题查询:

http://localhost:9200/articleindex/article/_search?q=title:我是标题1

可以用*代表任意字符:

http://localhost:9200/articleindex/article/_search?q=title:*s*

7.删除文档,根据ID删除文档,比如删除ID为1的文档 DELETE方式提交:

http://localhost:9200/articleindex/article/1

8.安装head插件,然后插件目录下依赖,启动 grunt server,启动后浏览器访问http://localhost:9100/,但是连接没起作用,要解决es跨域问题,修改elasticsearch的配置,让其允许跨域访问,修改elasticsearch配置文件,config目录下:elasticsearch.yml,增加以下两句命令:

http.cors.enabled: true
http.cors.allow‐origin: "*"

重启elasticsearch访问测试连接是否配置成功
9.IK分词器
默认的中文分词是将每个字看成一个词,这是不符合要求的,所以需要安装中文分词器来解决这个问题。安装步骤:

(1)先将其解压,将解压后的elasticsearch文件夹重命名文件夹为ik
(2)将ik文件夹拷贝到elasticsearch/plugins 目录下
(3)重新启动,即可加载IK分词器

IK提供了两个分词算法ik_smart 和 ik_max_word。其中 ik_smart 为最少切分,ik_max_word为最细粒度划分
自定义词库:

(1)进入elasticsearch/plugins/ik/config目录
(2)新建一个my.dic文件,编辑内容
(3)修改IKAnalyzer.cfg.xml(在ik/config目录下)
   编辑为:<entry key="ext_dict">my.dic</entry>

10.一些要记的:

    //Field域,对应列值,
    //是否索引(index= true),就看该域是否能被搜索(想要被搜索就要索引)
    //是否分词,就表示搜索的时候整体匹配还是单词匹配
    //是否存储,就是是否在页面上显示(实体写了几个字段就存几个,对应表里可能还有诶存的)
    @Field(index= true,analyzer="ik_max_word",searchAnalyzer="ik_max_word")
    private String title;//标题
    @Field(index= true,analyzer="ik_max_word",searchAnalyzer="ik_max_word")
    private String content;//文章正文
    private String state;//审核状态

11.Logstash
简介:Logstash是一款轻量级的日志搜集处理框架,可以方便的把分散的、多样化的日志搜集起来,并进行自定义的处理,然后传输到指定的位置,比如某个服务器或者文件,可做数据库同步
安装:解压,进入bin目录,输入:

logstash -e 'input { stdin { } } output { stdout {} }'(直接执行指令可测试安装成功没有,输入啥就输出啥)
  •  

stdin,表示输入流,指从键盘输入stdout,表示输出流,指从显示器输出。
该命令常用,所以可以写到一个文件中,-e改成-f,跟着’文件相对路径’(如果文件在同一级,…-可以省略)
控制台随意输入字符,随后就有日志输出
12.MySQL数据导入Elasticsearch
(1)在logstash-5.6.8安装目录下创建文件夹mysqletc (名称随意)
(2)文件夹下创建mysql.conf (名称随意) ,内容如下:

input {
  jdbc {
	  # 要连接的mysql驱动,一般是微服务里.yml文件找
	  jdbc_connection_string => "jdbc:mysql://192.168.226.128:3306/tensquare_article?characterEncoding=UTF8"
	  # 数据库账号密码
	  jdbc_user => "root"
	  jdbc_password => "123"
	  # jdbc驱动位置  
	  jdbc_driver_library => "D:\My_IT\My_java_aboutDownload\elasticsearch\logstash-5.6.8\mysqletc\mysql-connector-java-5.1.46.jar"
	  # 驱动名称,写死即可
	  jdbc_driver_class => "com.mysql.jdbc.Driver"
                  #是否要分页
	  jdbc_paging_enabled => "true"
                   #要多少条内容
	  jdbc_page_size => "50"
	  #以下对应着要执行的sql的绝对路径。
	  #statement_filepath => ""
                    #在这直接写sql语句
	  statement => "SELECT id,title,content,state FROM tb_article"
	  #定时字段 各字段含义(由左至右)分、时、天、月、年,全部为*默认含义为每分钟都更新(测试结果,不同的话请留言指出)
      schedule => "* * * * *"
  }
}

output {
  elasticsearch {
	  #ESIP地址与端口,就是要把上面数据更新到哪个elasticsearch中
	  hosts => "127.0.0.1:9200" 
	  #ES索引名称(自己定义的,索引库名称)
	  index => "tensquare_article"
	  #自增ID编号,这个id是上面sql的id
	  document_id => "%{id}"
                    #类型
	  document_type => "article"
  }
  stdout {
      #以JSON格式输出
      codec => json_lines
  }
}

(3)将mysql驱动包mysql-connector-java-5.1.46.jar拷贝至D:/logstash-
5.6.8/mysqletc/ 下 。D:/logstash-5.6.8是你的安装目录
(4)在bin目录下命令行下执行

logstash -f ../mysqletc/mysql.conf
  •  

13创建容器
docker run -di --name=tensquare_es -p 9200:9200 ‐p 9300:9300
elasticsearch:5.6.8
14.进入容器elasticsearch所在的目录为/usr/share/elasticsearch ,进入config看到了配置文件elasticsearch.yml,需要以文件挂载的方式创建容器才行,这样我们就可以通过修改宿主机中的某个文件来实现对容器内配置文件的修改,拷贝配置文件到宿主机首先退出容器,然后执行命令:

docker cp
tensquare_elasticsearch:/usr/share/elasticsearch/config/elasticsearch.yml
/usr/share/elasticsearch.yml
  •  

停止和删除原来创建的容器
docker stop tensquare_elasticsearch
docker rm tensquare_elasticsearch
重新执行创建容器命令
修改/usr/share/elasticsearch.yml 将transport.host: 0.0.0.0 前的#去掉后保
存文件退出。其作用是允许任何ip地址访问elasticsearch .开发测试阶段可以这么做,生
产环境下指定具体的IP
最后重启启动,会启动失败,需要调优,修改/etc/security/limits.conf ,追加内容:

* soft nofile 65536
* hard nofile 65536
  •  

nofile是单个进程允许打开的最大文件个数 soft nofile 是软限制 hard nofile是硬限制
修改/etc/sysctl.conf,追加内容:

vm.max_map_count=655360
  •  

限制一个进程可以拥有的VMA(虚拟内存区域)的数量执行下面命令 修改内核参数马上生效:sysctl ‐p
重新启动虚拟机,再次启动容器,发现已经可以启动并远程访问

 

发布了52 篇原创文章 · 获赞 116 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/RuiKe1400360107/article/details/103867016