这是讲数据库的数据导入到es里  所有用到了mysql!

1.依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>cn.thislx</groupId>
<artifactId>springboot-es</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>springboot-es</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--ES6.4.2 start-->
<!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/transport -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.4.2</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--ES6.4.2 end --> <!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.39</version>
</dependency> <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency> <!--mybatis的依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency> </dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

2.配置文件

# Elasticsearch
# 9200端口是用来让HTTP REST API来访问ElasticSearch,而9300端口是传输层监听的默认端口
#es地址
elasticsearch.ip=127.0.0.1
#es端口
elasticsearch.port=9300
#连接池数量
elasticsearch.pool=5
#集群名称
elasticsearch.cluster.name=elasticsearch #server.port=8181
#mysql 连接地址与数据库名字
spring.datasource.url=jdbc:mysql:///es
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#帐号
spring.datasource.username=root
#密码
spring.datasource.password=root
#扫描mapper.xml文件
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
#扫描实体类包
mybatis.type-aliases-package=com.xiaoteng.entity

3.ESConfig

package cn.thislx.springbootes.config;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import java.net.InetAddress; /**
* @Configuration用于定义配置类,可替换xml配置文件
*/
@Configuration
public class ElasticsearchConfig { private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchConfig.class); /**
* elk集群地址
*/
@Value("${elasticsearch.ip}")
private String hostName; /**
* 端口
*/
@Value("${elasticsearch.port}")
private String port; /**
* 集群名称
*/
@Value("${elasticsearch.cluster.name}")
private String clusterName; /**
* 连接池
*/
@Value("${elasticsearch.pool}")
private String poolSize; /**
* Bean name default 函数名字
*
* @return
*/
@Bean(name = "transportClient")
public TransportClient transportClient() {
LOGGER.info("Elasticsearch初始化开始。。。。。");
TransportClient transportClient = null;
try {
// 配置信息
Settings esSetting = Settings.builder()
.put("cluster.name", clusterName) //集群名字
.put("client.transport.sniff", true)//增加嗅探机制,找到ES集群
.put("thread_pool.search.size", Integer.parseInt(poolSize))//增加线程池个数,暂时设为5
.build();
//配置信息Settings自定义
transportClient = new PreBuiltTransportClient(esSetting);
TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(hostName), Integer.valueOf(port));
transportClient.addTransportAddresses(transportAddress);
} catch (Exception e) {
LOGGER.error("elasticsearch TransportClient create error!!", e);
}
LOGGER.info("Elasticsearch初始化结束。。。。。");
return transportClient;
} }

4.查看集群信息

class test{
@Autowired
private TransportClient client;
@Test
public void test7(){
List<DiscoveryNode> discoveryNodes = client.connectedNodes();
for (DiscoveryNode discoveryNode : discoveryNodes) {
System.out.println("discoveryNode = " + discoveryNode);
}
}
}

5.判断索引是否存在

@Test
public void test8(){
//判断索引是否存在
String index="goods";
IndicesExistsResponse indicesExistsResponse = client.admin().indices()
.exists(new IndicesExistsRequest(index)).actionGet();
boolean exists = indicesExistsResponse.isExists();
System.out.println(exists); }

6.创建索引

	@Test
public void test8(){
//判断索引是否存在
String index="goods";
IndicesExistsResponse indicesExistsResponse = client.admin().indices()
.exists(new IndicesExistsRequest(index)).actionGet();
boolean exists = indicesExistsResponse.isExists();
//如果不存在就创建
if (!exists){
CreateIndexResponse createIndexResponse = client.admin().indices().prepareCreate(index)
.execute().actionGet();
//是否创建成功
boolean acknowledged = createIndexResponse.isAcknowledged();
}
}

7.删除索引

@Test
public void test8(){
//判断索引是否存在
String index="users";
IndicesExistsResponse indicesExistsResponse = client.admin().indices()
.exists(new IndicesExistsRequest(index)).actionGet();
boolean exists = indicesExistsResponse.isExists();
//如果存在就删除
if (exists){
DeleteIndexResponse deleteIndexResponse = client.admin().indices()
.prepareDelete(index).execute().actionGet();
boolean acknowledged = deleteIndexResponse.isAcknowledged();
//true 删除成功
System.out.println("acknowledged = " + acknowledged);
}
}

8.判断索引下的类型是否存在

@Test
public void test8(){
//有此索引的前提下 不然会报错
String index="goods";
//类型
String type="_doc";
//判断索引下type是否存在
boolean exists = client.admin().indices().prepareTypesExists(index).setTypes(type).
execute().actionGet().isExists();
System.out.println(exists);
}

9.往索引添加数据

	@Test //index =users  type=doc
public void test8(){
String index="users";
String type="doc";
//1.对象
EsModel model = new EsModel();
model.setId("2");
model.setName("小明");
model.setAge(20);
model.setDate(new Date());
//讲对象转为json
JSONObject jsonObject= (JSONObject) JSONObject.toJSON(model);
//返回id
IndexResponse indexResponse = client.prepareIndex(index, type,
jsonObject.getString("id"))
.setSource(jsonObject).get();
//拿到id
String id1 = indexResponse.getId();
System.out.println(id1);
//2.jsonObject key:value 形式
JSONObject object=new JSONObject();
object.put("id",UUID.randomUUID().toString().replaceAll("-","").
toUpperCase());
object.put("name","小红");
object.put("age",25);
object.put("date",new Date());
IndexResponse response = client.prepareIndex(index, type,
object.getString("id")).setSource(object).get();
String id = response.getId();
System.out.println("id = " + id);
}

10.通过id获取数据   显示的字段 以及排除字段(不显示)

	@Test //index =users  type=doc
public void test8(){
String index="users";
String type="doc";
//通过id获取数据
GetRequestBuilder getRequestBuilder = client.prepareGet(index, type,
"0DC8ADD7F0184608BB7356441AC8B2A4");
String[] files={"name","age","id"};
//显示的字段和排除的字段 显示可数组可单个字符出
GetResponse documentFields = getRequestBuilder.setFetchSource(files,null).execute().actionGet();
String sourceAsString = documentFields.getSourceAsString();
System.out.println(sourceAsString);
Map<String, Object> source = documentFields.getSource();
System.out.println(source);
}

11.通过id更新数据

 @Test //index =users  type=doc
public void test8() {
String index = "users";
String type = "doc";
String id = "0DC8ADD7F0184608BB7356441AC8B2A4";
JSONObject object = new JSONObject();
object.put("id", id);
object.put("name", "小庄");
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index(index).type(type).id(id).doc(object);
client.update(updateRequest);
}

12.通过id删除数据

 @Test //index =users  type=doc
public void test8() {
String index = "users";
String type = "doc";
String id = "0DC8ADD7F0184608BB7356441AC8B2A4";
DeleteResponse deleteResponse = client.prepareDelete(index, type, id).execute().actionGet();
int status = deleteResponse.status().getStatus();
//200 successful
System.out.println("status = " + status);
}

13.分页查询  显示想显示的字段  排序   没有条件

 @Test
public void test8() {
String index = "goods"; //索引
String type = "_doc"; //类型
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index).setTypes(type);
//要显示的字段
String fields="brandName,price,saleNum,categoryName";
String[] split = fields.split(",");
// 需要显示的字段,逗号分隔(缺省为全部字段)
if (StringUtils.isNotEmpty(fields)) {
//第一个参数 可以数一个字符串 可以是一个数组 第二个参数是排除
searchRequestBuilder.setFetchSource(split, null);
}
//根据 price排序 倒叙
searchRequestBuilder.addSort("price", SortOrder.DESC);
// 分页应用 pageNum=(n-1)*m; pageSize=m; n为当前页,m为显示多少条
searchRequestBuilder.setFrom(1).setSize(11);
SearchResponse searchResponse = searchRequestBuilder.
execute().actionGet();
SearchHits hits = searchResponse.getHits();
//总共查多少条数据
long totalHits = hits.totalHits;
//处理了几条 展示的size 我这里是11条 对应上面11
int length = hits.getHits().length;
System.out.println("totalHits = " + totalHits);
System.out.println("length = " + length);
for (SearchHit hit : hits) {
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
System.out.println(sourceAsMap);
}
}

14.再加+++

es6.4.2api的更多相关文章

  1. ES6模块import细节

    写在前面,目前浏览器对ES6的import支持还不是很好,需要用bable转译. ES6引入外部模块分两种情况: 1.导入外部的变量或函数等: import {firstName, lastName, ...

  2. webpack+react+redux+es6开发模式

    一.预备知识 node, npm, react, redux, es6, webpack 二.学习资源 ECMAScript 6入门 React和Redux的连接react-redux Redux 入 ...

  3. ES6的一些常用特性

    由于公司的前端业务全部基于ES6开发,于是给自己开个小灶补补ES6的一些常用特性.原来打算花两天学习ES6的,结果花了3天才勉强过了一遍阮老师的ES6标准入门(水好深,ES6没学好ES7又来了...) ...

  4. ES6(块级作用域)

    我们都知道在javascript里是没有块级作用域的,而ES6添加了块级作用域,块级作用域能带来什么好处呢?为什么会添加这个功能呢?那就得了解ES5没有块级作用域时出现了哪些问题. ES5在没有块级作 ...

  5. es6小白学习笔记(一)

    1.let和const命令 1.es6新增了let和const命令,与var用法类似,但它声明的变量只在let所在的代码块内有效(块级作用域,es5只有全局和函数作用域) { let a = 1; v ...

  6. ES6之变量常量字符串数值

    ECMAScript 6 是 JavaScript 语言的最新一代标准,当前标准已于 2015 年 6 月正式发布,故又称 ECMAScript 2015. ES6对数据类型进行了一些扩展 在js中使 ...

  7. ES6之let命令详解

    let与块级作用域 { var foo='foo'; let bar='bar'; } console.log(foo,'var'); //foo varconsole.log(bar ,'bar') ...

  8. ES6 箭头函数中的 this?你可能想多了(翻译)

    箭头函数=>无疑是ES6中最受关注的一个新特性了,通过它可以简写 function 函数表达式,你也可以在各种提及箭头函数的地方看到这样的观点——“=> 就是一个新的 function”. ...

  9. ES6+ 现在就用系列(二):let 命令

    系列目录 ES6+ 现在就用系列(一):为什么使用ES6+ ES6+ 现在就用系列(二):let 命令 ES6+ 现在就用系列(三):const 命令 ES6+ 现在就用系列(四):箭头函数 => ...

随机推荐

  1. 带UI的小初高数学学习软件—艰难地用C++(QT库)实现的过程

    从互相了解对方的代码思路然后确定用C++编写,到用win32写界面时变得摇摆不定的考虑着要不要改变语言,再到用QT写完界面后发现短信接口一般都不提供C++,最后到QT打包出来的可执行文件在别的设备上无 ...

  2. 圆桌killer

    [问题描述] 圆桌上围坐着2n个人.其中n个人是好人,另外n个人是坏人.如果从第m(m<=2n)个人开始数数,数到第k个人,则立即处死该人:然后从被处死的人之后重新开始数数,再将数到的第k个人处 ...

  3. 创建可以运行宿主机GPU的容器

    1.安装NVIDIA Container Runtime apt-get参考https://blog.csdn.net/li_ellin/article/details/107180516 yum参考 ...

  4. python版本共存与语法的注释

    python的多种版本共存 首先还是先下载python解释器除最高版本的另外两个版本 个人推荐的是 3.6.8和2.7.14 首先我电脑是win7系统 在计算机属性右键点开高级设置点击环境变量 将下载 ...

  5. 7-19(排序) 寻找大富翁 (25 分)(归并排序)(C语言实现)

    7-19(排序) 寻找大富翁 (25 分) 胡润研究院的调查显示,截至2017年底,中国个人资产超过1亿元的高净值人群达15万人.假设给出N个人的个人资产值,请快速找出资产排前M位的大富翁. 输入格式 ...

  6. java 竞赛常用方法

    一,基本数据类型 1.int,float.double等转换为字符串用 String.valueOf方法 eg:double转换为String Double dtDouble=12.31354; St ...

  7. 关于物理机没有VMnet1和VMnet8网卡的问题

    当我们在用虚拟机做实验需要与物理机进行连接时,发现无法连接上,这时候可能是没有Vmnet1或者Vmnet8网卡,又或者是Vmnet1和Vmnet8网卡都没有. 之前试过很多方法,重装.重启虚拟网络编辑 ...

  8. JavaWeb和WebGIS学习笔记(五)——使用OpenLayers显示地图

    系列链接: Java web与web gis学习笔记(一)--Tomcat环境搭建 Java web与web gis学习笔记(二)--百度地图API调用 JavaWeb和WebGIS学习笔记(三)-- ...

  9. Linux下安装并使用jumpserver跳板机

    堡垒机介绍: 官方网站:https://www.jumpserver.org/ 在一个特定网络环境下,为了保障网络和数据不受外界入侵和破坏,而运用各种技术手段实时收集和监控网络环境中每一个组成部分的系 ...

  10. FreeRTOS --(3)内存管理 heap2

    在<FreeRTOS --(2)内存管理 heap1>知道 heap 1 的内存管理其实只是简单的实现了内存对齐的分配策略,heap 2 的实现策略相比 heap 1 稍微复杂一点,不仅仅 ...