From 44ecafd5faafd955062161157bd418296d819efe Mon Sep 17 00:00:00 2001 From: fanxb Date: Mon, 29 Jul 2019 17:55:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Ees=20demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3.排序算法/src/sort/exchange/QuickSort.java | 27 +- 3.排序算法/src/util/NumberUtil.java | 17 ++ 3.排序算法/src/util/StringUtil.java | 15 ++ es-demo/.gitignore | 31 +++ es-demo/pom.xml | 79 ++++++ .../com/fanxb/esdemo/EsDemoApplication.java | 13 + .../java/com/fanxb/esdemo/entity/Book.java | 56 +++++ .../com/fanxb/esdemo/entity/EsEntity.java | 39 +++ .../com/fanxb/esdemo/service/BookService.java | 115 +++++++++ .../java/com/fanxb/esdemo/util/EsUtil.java | 235 ++++++++++++++++++ .../src/main/resources/application.properties | 3 + .../fanxb/esdemo/EsDemoApplicationTests.java | 60 +++++ 12 files changed, 681 insertions(+), 9 deletions(-) create mode 100644 3.排序算法/src/util/NumberUtil.java create mode 100644 3.排序算法/src/util/StringUtil.java create mode 100644 es-demo/.gitignore create mode 100644 es-demo/pom.xml create mode 100644 es-demo/src/main/java/com/fanxb/esdemo/EsDemoApplication.java create mode 100644 es-demo/src/main/java/com/fanxb/esdemo/entity/Book.java create mode 100644 es-demo/src/main/java/com/fanxb/esdemo/entity/EsEntity.java create mode 100644 es-demo/src/main/java/com/fanxb/esdemo/service/BookService.java create mode 100644 es-demo/src/main/java/com/fanxb/esdemo/util/EsUtil.java create mode 100644 es-demo/src/main/resources/application.properties create mode 100644 es-demo/src/test/java/com/fanxb/esdemo/EsDemoApplicationTests.java diff --git a/3.排序算法/src/sort/exchange/QuickSort.java b/3.排序算法/src/sort/exchange/QuickSort.java index 09f9fd1..fb17526 100644 --- a/3.排序算法/src/sort/exchange/QuickSort.java +++ b/3.排序算法/src/sort/exchange/QuickSort.java @@ -1,6 +1,7 @@ package sort.exchange; import util.ArrayUtil; +import util.NumberUtil; import java.util.Arrays; @@ -18,25 +19,33 @@ public class QuickSort { if (start >= end) { return; } - int base = arr[start], i = start, j = end; + int baseIndex = NumberUtil.getRandom(start, end); + int base = arr[baseIndex], i = start, j = end; while (i < j) { - while (arr[i] <= base && i < end) { - i++; - } - while (arr[j] >= base && j > start) { + while (arr[j] >= base && j > i) { j--; } + while (arr[i] <= base && i < j) { + i++; + } + + if (i < j) { - ArrayUtil.swap(arr, i, j); + ArrayUtil.swap(arr, i++, j--); + System.out.println(Arrays.toString(arr)); } } + boolean isSwap = (baseIndex > j && arr[baseIndex] < arr[j]) || (baseIndex < j && arr[baseIndex] > arr[j]); + if (isSwap) { + ArrayUtil.swap(arr, baseIndex, i); + } System.out.println(Arrays.toString(arr)); - deal(arr, start, i); - deal(arr, j, end); + deal(arr, start, i - 1); + deal(arr, j + 1, end); } public static void main(String[] args) { - Integer[] arr = {4, 3, 1, 89, 5}; + Integer[] arr = {1, 43, 2, 3, 4, 5, 6, 7, 5, 6, 555, 12, 31, 5, 5}; deal(arr, 0, arr.length - 1); } } diff --git a/3.排序算法/src/util/NumberUtil.java b/3.排序算法/src/util/NumberUtil.java new file mode 100644 index 0000000..66e027d --- /dev/null +++ b/3.排序算法/src/util/NumberUtil.java @@ -0,0 +1,17 @@ +package util; + +/** + * 类功能简述: + * 类功能详述: + * + * @author fanxb + * @date 2019/5/17 17:15 + */ +public class NumberUtil { + + public static int getRandom(int min, int max) { + return (int) Math.ceil(min + Math.random() * (max - min)); + } + + +} diff --git a/3.排序算法/src/util/StringUtil.java b/3.排序算法/src/util/StringUtil.java new file mode 100644 index 0000000..26d9ef0 --- /dev/null +++ b/3.排序算法/src/util/StringUtil.java @@ -0,0 +1,15 @@ +package util; + +/** + * 类功能简述: + * 类功能详述: + * + * @author fanxb + * @date 2019/4/4 16:17 + */ +public class StringUtil { + + public static boolean isEmpty(String str) { + return str == null || str.trim().length() == 0; + } +} diff --git a/es-demo/.gitignore b/es-demo/.gitignore new file mode 100644 index 0000000..a2a3040 --- /dev/null +++ b/es-demo/.gitignore @@ -0,0 +1,31 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/** +!**/src/test/** + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ + +### VS Code ### +.vscode/ diff --git a/es-demo/pom.xml b/es-demo/pom.xml new file mode 100644 index 0000000..0e82b04 --- /dev/null +++ b/es-demo/pom.xml @@ -0,0 +1,79 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.6.RELEASE + + + com.fanxb + es-demo + 0.0.1-SNAPSHOT + es-demo + Elasticsearch Demo project for Spring Boot + + + 1.8 + + + + + + + org.elasticsearch.client + elasticsearch-rest-high-level-client + 7.2.0 + + + + org.elasticsearch + elasticsearch + 7.2.0 + + + + org.elasticsearch.client + elasticsearch-rest-client + 7.2.0 + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.elasticsearch.client + elasticsearch-rest-high-level-client + 7.2.0 + + + + com.alibaba + fastjson + 1.2.56 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/es-demo/src/main/java/com/fanxb/esdemo/EsDemoApplication.java b/es-demo/src/main/java/com/fanxb/esdemo/EsDemoApplication.java new file mode 100644 index 0000000..8e3a92e --- /dev/null +++ b/es-demo/src/main/java/com/fanxb/esdemo/EsDemoApplication.java @@ -0,0 +1,13 @@ +package com.fanxb.esdemo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class EsDemoApplication { + + public static void main(String[] args) { + SpringApplication.run(EsDemoApplication.class, args); + } + +} diff --git a/es-demo/src/main/java/com/fanxb/esdemo/entity/Book.java b/es-demo/src/main/java/com/fanxb/esdemo/entity/Book.java new file mode 100644 index 0000000..9a47a0c --- /dev/null +++ b/es-demo/src/main/java/com/fanxb/esdemo/entity/Book.java @@ -0,0 +1,56 @@ +package com.fanxb.esdemo.entity; + +/** + * 类功能简述: 插入es的数据 + * 类功能详述: + * + * @author fanxb + * @date 2019/7/29 11:33 + */ +public class Book { + private Integer id; + private Integer userId; + private String name; + + public Book() { + } + + public Book(Integer id, Integer userId, String name) { + this.id = id; + this.userId = userId; + this.name = name; + } + + @Override + public String toString() { + return "Book{" + + "id=" + id + + ", userId=" + userId + + ", name='" + name + '\'' + + '}'; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getUserId() { + return userId; + } + + public void setUserId(Integer userId) { + this.userId = userId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/es-demo/src/main/java/com/fanxb/esdemo/entity/EsEntity.java b/es-demo/src/main/java/com/fanxb/esdemo/entity/EsEntity.java new file mode 100644 index 0000000..715cd78 --- /dev/null +++ b/es-demo/src/main/java/com/fanxb/esdemo/entity/EsEntity.java @@ -0,0 +1,39 @@ +package com.fanxb.esdemo.entity; + +/** + * 类功能简述: + * 类功能详述: + * + * @author fanxb + * @date 2019/7/29 11:29 + */ +public final class EsEntity { + + private String id; + private T data; + + public EsEntity() { + } + + public EsEntity(String id, T data) { + this.data = data; + this.id = id; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } +} + diff --git a/es-demo/src/main/java/com/fanxb/esdemo/service/BookService.java b/es-demo/src/main/java/com/fanxb/esdemo/service/BookService.java new file mode 100644 index 0000000..65ccc48 --- /dev/null +++ b/es-demo/src/main/java/com/fanxb/esdemo/service/BookService.java @@ -0,0 +1,115 @@ +package com.fanxb.esdemo.service; + +import com.fanxb.esdemo.entity.Book; +import com.fanxb.esdemo.entity.EsEntity; +import com.fanxb.esdemo.util.EsUtil; +import org.elasticsearch.index.query.BoolQueryBuilder; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.index.query.TermQueryBuilder; +import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.List; + +/** + * 类功能简述: + * 类功能详述: + * + * @author fanxb + * @date 2019/7/29 14:31 + */ +@RestController +@RequestMapping("/book") +@Service +public class BookService { + + @Autowired + private EsUtil esUtil; + + /** + * @param id 获取某一个 + */ + @GetMapping("/{id}") + public Book getById(@PathVariable("id") int id) { + SearchSourceBuilder builder = new SearchSourceBuilder(); + builder.query(new TermQueryBuilder("id", id)); + List res = esUtil.search(EsUtil.INDEX_NAME, builder, Book.class); + if (res.size() > 0) { + return res.get(0); + } else { + return null; + } + } + + /** + * 获取全部 + */ + @GetMapping("/") + public List getAll() { + return esUtil.search(EsUtil.INDEX_NAME, new SearchSourceBuilder(), Book.class); + } + + /** + * 根据关键词搜索某用户下的书 + * + * @param content 关键词 + */ + @GetMapping("/search") + public List searchByUserIdAndName(int userId, String content) { + BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder(); + boolQueryBuilder.must(QueryBuilders.termQuery("userId", userId)); + boolQueryBuilder.must(QueryBuilders.matchQuery("name", content)); + SearchSourceBuilder builder = new SearchSourceBuilder(); + builder.size(10).query(boolQueryBuilder); + return esUtil.search(EsUtil.INDEX_NAME, builder, Book.class); + + } + + /** + * 单个插入 + * + * @param book book + */ + @PutMapping("/") + public void putOne(@RequestBody Book book) { + EsEntity entity = new EsEntity<>(book.getId().toString(), book); + esUtil.insertOrUpdateOne(EsUtil.INDEX_NAME, entity); + } + + /** + * 批量插入 + * + * @param books books + */ + @PutMapping("/many") + public void putList(@RequestBody List books) { + List list = new ArrayList<>(); + books.forEach(item -> list.add(new EsEntity<>(item.getId().toString(), item))); + esUtil.insertBatch(EsUtil.INDEX_NAME, list); + } + + /** + * 批量删除 + * + * @param list list + */ + @DeleteMapping("/deleteBatch") + public void deleteBatch(List list) { + esUtil.deleteBatch(EsUtil.INDEX_NAME, list); + } + + /** + * delete by query 根据用户id删除数据 + * + * @param userId userId + */ + @DeleteMapping("/userId/{userId}") + public void deleteByUserId(@PathVariable("userId") int userId) { + esUtil.deleteByQuery(EsUtil.INDEX_NAME, new TermQueryBuilder("userId", userId)); + } + + +} diff --git a/es-demo/src/main/java/com/fanxb/esdemo/util/EsUtil.java b/es-demo/src/main/java/com/fanxb/esdemo/util/EsUtil.java new file mode 100644 index 0000000..7f9fd02 --- /dev/null +++ b/es-demo/src/main/java/com/fanxb/esdemo/util/EsUtil.java @@ -0,0 +1,235 @@ +package com.fanxb.esdemo.util; + +import com.alibaba.fastjson.JSON; +import com.fanxb.esdemo.entity.EsEntity; +import org.apache.http.HttpHost; +import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; +import org.elasticsearch.action.bulk.BulkRequest; +import org.elasticsearch.action.delete.DeleteRequest; +import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.action.search.SearchRequest; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.client.indices.CreateIndexRequest; +import org.elasticsearch.client.indices.CreateIndexResponse; +import org.elasticsearch.client.indices.GetIndexRequest; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.index.reindex.DeleteByQueryRequest; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +/** + * 类功能简述: + * 类功能详述: + * + * @author fanxb + * @date 2019/7/29 11:24 + */ +@Component +public class EsUtil { + + + @Value("${es.host}") + public String host; + @Value("${es.port}") + public int port; + @Value("${es.scheme}") + public String scheme; + + public static final String INDEX_NAME = "book-index"; + + public static final String CREATE_INDEX = "{\n" + + " \"properties\": {\n" + + " \"id\":{\n" + + " \"type\":\"integer\"\n" + + " },\n" + + " \"userId\":{\n" + + " \"type\":\"integer\"\n" + + " },\n" + + " \"name\":{\n" + + " \"type\":\"text\",\n" + + " \"analyzer\": \"ik_max_word\",\n" + + " \"search_analyzer\": \"ik_smart\"\n" + + " },\n" + + " \"url\":{\n" + + " \"type\":\"text\",\n" + + " \"index\": true,\n" + + " \"analyzer\": \"ik_max_word\",\n" + + " \"search_analyzer\": \"ik_smart\"\n" + + " }\n" + + " }\n" + + " }"; + + public static RestHighLevelClient client = null; + + + @PostConstruct + public void init() { + try { + if (client != null) { + client.close(); + } + client = new RestHighLevelClient(RestClient.builder(new HttpHost(host, port, scheme))); + if (this.indexExist(INDEX_NAME)) { + return; + } + CreateIndexRequest request = new CreateIndexRequest(INDEX_NAME); + request.settings(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 2)); + request.mapping(CREATE_INDEX, XContentType.JSON); + CreateIndexResponse res = client.indices().create(request, RequestOptions.DEFAULT); + if (!res.isAcknowledged()) { + throw new RuntimeException("初始化失败"); + } + } catch (Exception e) { + e.printStackTrace(); + System.exit(0); + } + } + + /** + * Description: 判断某个index是否存在 + * + * @param index index名 + * @return boolean + * @author fanxb + * @date 2019/7/24 14:57 + */ + public boolean indexExist(String index) throws Exception { + GetIndexRequest request = new GetIndexRequest(index); + request.local(false); + request.humanReadable(true); + request.includeDefaults(false); + return client.indices().exists(request, RequestOptions.DEFAULT); + } + + /** + * Description: 插入/更新一条记录 + * + * @param index index + * @param entity 对象 + * @author fanxb + * @date 2019/7/24 15:02 + */ + public void insertOrUpdateOne(String index, EsEntity entity) { + IndexRequest request = new IndexRequest(index); + request.id(entity.getId()); + request.source(JSON.toJSONString(entity.getData()), XContentType.JSON); + try { + client.index(request, RequestOptions.DEFAULT); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * Description: 批量插入数据 + * + * @param index index + * @param list 带插入列表 + * @author fanxb + * @date 2019/7/24 17:38 + */ + public void insertBatch(String index, List list) { + BulkRequest request = new BulkRequest(); + list.forEach(item -> request.add(new IndexRequest(index).id(item.getId()) + .source(JSON.toJSONString(item.getData()), XContentType.JSON))); + try { + client.bulk(request, RequestOptions.DEFAULT); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * Description: 批量删除 + * + * @param index index + * @param idList 待删除列表 + * @author fanxb + * @date 2019/7/25 14:24 + */ + public void deleteBatch(String index, Collection idList) { + BulkRequest request = new BulkRequest(); + idList.forEach(item -> request.add(new DeleteRequest(index, item.toString()))); + try { + client.bulk(request, RequestOptions.DEFAULT); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * Description: 搜索 + * + * @param index index + * @param builder 查询参数 + * @param c 结果类对象 + * @return java.util.ArrayList + * @author fanxb + * @date 2019/7/25 13:46 + */ + public List search(String index, SearchSourceBuilder builder, Class c) { + SearchRequest request = new SearchRequest(index); + request.source(builder); + try { + SearchResponse response = client.search(request, RequestOptions.DEFAULT); + SearchHit[] hits = response.getHits().getHits(); + List res = new ArrayList<>(hits.length); + for (SearchHit hit : hits) { + res.add(JSON.parseObject(hit.getSourceAsString(), c)); + } + return res; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * Description: 删除index + * + * @param index index + * @return void + * @author fanxb + * @date 2019/7/26 11:30 + */ + public void deleteIndex(String index) { + try { + client.indices().delete(new DeleteIndexRequest(index), RequestOptions.DEFAULT); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * Description: delete by query + * + * @param index index + * @param builder builder + * @author fanxb + * @date 2019/7/26 15:16 + */ + public void deleteByQuery(String index, QueryBuilder builder) { + DeleteByQueryRequest request = new DeleteByQueryRequest(index); + request.setQuery(builder); + //设置批量操作数量,最大为10000 + request.setBatchSize(10000); + request.setConflicts("proceed"); + try { + client.deleteByQuery(request, RequestOptions.DEFAULT); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + +} diff --git a/es-demo/src/main/resources/application.properties b/es-demo/src/main/resources/application.properties new file mode 100644 index 0000000..ad589aa --- /dev/null +++ b/es-demo/src/main/resources/application.properties @@ -0,0 +1,3 @@ +es.host=192.168.64.129 +es.port=9200 +es.scheme=http \ No newline at end of file diff --git a/es-demo/src/test/java/com/fanxb/esdemo/EsDemoApplicationTests.java b/es-demo/src/test/java/com/fanxb/esdemo/EsDemoApplicationTests.java new file mode 100644 index 0000000..9aa9fda --- /dev/null +++ b/es-demo/src/test/java/com/fanxb/esdemo/EsDemoApplicationTests.java @@ -0,0 +1,60 @@ +package com.fanxb.esdemo; + +import com.fanxb.esdemo.service.BookService; +import com.fanxb.esdemo.entity.Book; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.ArrayList; +import java.util.List; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class EsDemoApplicationTests { + + @Autowired + private BookService bookService; + + @Test + public void getOne() { + System.out.println(bookService.getById(1).toString()); + } + + @Test + public void getAll() { + List res = bookService.getAll(); + res.forEach(System.out::println); + } + + @Test + public void addOneTest() { + bookService.putOne(new Book(1, 1, "格林童话")); + bookService.putOne(new Book(2, 1, "美人鱼")); + } + + @Test + public void addBatchTest() { + List list = new ArrayList<>(); + list.add(new Book(3, 1, "第一本书")); + list.add(new Book(4, 1, "第二本书")); + bookService.putList(list); + } + + @Test + public void deleteBatch() { + List list = new ArrayList<>(); + list.add(1); + list.add(3); + bookService.deleteBatch(list); + } + + @Test + public void deleteByQuery(){ + bookService.deleteByUserId(1); + } + + +}