先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7
深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新大数据全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
如果你需要这些资料,可以添加V获取:vip204888 (备注大数据)
正文
springboot集成es
1、新建springboot项目,引入相关版本依赖
这里用的elasticsearch 6.8 版本 导入
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<!--springboot帮我们自动集成了es,所以只需要引入下面这一个依赖即可-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2.配置es.ym
server:
port: 9007
spring:
data:
elasticsearch:
cluster-name: my-application
cluster-nodes: 127.0.0.1:9300
3. 在你的数据库实体类里每个字段加上相应的注解即可
@Document(indexName = "shop")
@Data
public class Goods {
/**
* 商品id
*/
@Field(type = FieldType.Integer)//type表示存到es当中的数据类型
private Integer id;
/**
* 商品名字
*/
@Field(type = FieldType.Text,analyzer = "ik-max-word")
private String productName;
/**
* 商品描述
*/
@Field(type = FieldType.Text,analyzer = "ik-max-word")
private String productDescription;
/**
* 商品价格
*/
@Field(type = FieldType.Double)
private Double productPrice;
/**
* 商品分类id
*/
@Field(type = FieldType.Integer)
private Integer categoryId;
/**
* 商品图片
*/
@Field(type = FieldType.Text)
private String productImage;
/**
* 商品创建时间
*/
@Field(type = FieldType.Date,format = DateFormat.basic_date_time)
private Date productCreateTime;
/**
* 商品修改时间
*/
@Field(type = FieldType.Date,format = DateFormat.basic_date_time)
private Date productUpdateTime;
/**
* 商品状态(0:在销售,1:售空,2:下架)
*/
@Field(type = FieldType.Date,format = DateFormat.basic_date_time)
private String productStatus;
}
4. 操作es
由于spring官方对es的高度封装,我们已经可以做到像操作数据库一样操作es了
package org.springframework.data.elasticsearch.repository;
import org.elasticsearch.index.query.QueryBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.lang.Nullable;
@NoRepositoryBean
public interface ElasticsearchRepository<T, ID> extends PagingAndSortingRepository<T, ID> {
/** @deprecated */
@Deprecated
default <S extends T> S index(S entity) {
return this.save(entity);
}
/** @deprecated */
@Deprecated
<S extends T> S indexWithoutRefresh(S entity);
/** @deprecated */
@Deprecated
Iterable<T> search(QueryBuilder query);
/** @deprecated */
@Deprecated
Page<T> search(QueryBuilder query, Pageable pageable);
/** @deprecated */
Page<T> search(Query searchQuery);
Page<T> searchSimilar(T entity, @Nullable String[] fields, Pageable pageable);
/** @deprecated */
@Deprecated
void refresh();
}
以上面的Goods对象举例,创建接口EsGoodsRepository,继承Spring封装的ElasticsearchRepository,ElasticsearchRepository提供了一些简单的操作es方法
创建EsGoodsRepository接口:
package com.buba.service;
import com.buba.pojo.Goods;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface EsGoodsRepository extends ElasticsearchRepository<Goods,Integer> {
}
保存或者更新时,先存db后操作es(增删改查+批量删除)。.
@RequestMapping("/goods")
@RestController
public class GoodsControoler{
@Autowired
private GoodService goodService;
@Autowired
private EsGoodsRepository esGoodsRepository;
@Autowired
private EsGoodsService esGoodsService;
@PostMapping("/add")
@ApiOperation(value = "添加商品")
public Result<Goods> save(@RequestBody Goods goods) {
goods.setProductCreateTime(new Date());
int i = goodService.insert(goods);
if (i==1){
// 调用EsGoodsRepository save 方法
Goods save=esGoodsRepository.save(goods);
return new Result(true, StatusCode.OK,"添加成功");
}else {
return new Result(true, StatusCode.ERROR,"添加失败");
}
}
// 引用pagehelper写分页查询
@GetMapping("/list")
@ApiOperation(value = "查询商品列表")
public Result list(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
PageInfo<Goods> page = goodService.findAllUserByPageS(pageNum,pageSize);
return new Result(true, StatusCode.OK, "查询成功", page);
}
@GetMapping("/searchGoods")
@ApiOperation(value = "查询检索功能")
public Result searchProduct(@RequestParam("productName") String productName,@RequestParam(value = "pageNum", defaultValue = "0") int pageNum, @RequestParam(value = "pageSize", defaultValue = "1") int pageSize){
QueryBuilder queryBuilder = QueryBuilders.matchQuery("productName", productName);
PageRequest pageRequest = PageRequest.of(pageNum, pageSize);
Page<Goods> search = esGoodsRepository.search(queryBuilder, pageRequest);
if (search != null){
return new Result(true,StatusCode.OK,"商品展示成功",search);
}
return new Result(false,StatusCode.ERROR,"查询失败");
}
//根据id 删除商品
@DeleteMapping("/delete")
@ApiOperation(value = "删除商品")
public Result delete(@RequestParam( "list") List<Integer> ids) {
int i = goodService.deleteByPrimaryKey(ids);
if (i >= 1) {
esGoodsService.deleteByIds(ids);
return new Result(true, StatusCode.OK, "删除成功");
} else {
return new Result(true, StatusCode.ERROR, "删除失败");
}
}
//根据id修改商品
@PutMapping("/update")
@ApiOperation(value = "修改商品")
public Result update(@RequestBody Goods goods) {
int i = goodService.updateByPrimaryKeySelective(goods);
if (i == 1) {
esGoodsRepository.save(goods);
return new Result(true, StatusCode.OK, "修改成功");
}
return new Result(true, StatusCode.ERROR, "修改失败");
}
5.swagger +postman 测试
这里只测试了添加 其余自己测
总结
提示:这里对文章遇到报错总结:
1. NodeAvailableException[None of the configured nodes are availabl
原因是我们application.yaml配置文件出现了问题:
配置文件正确写法:
spring:
data:
elasticsearch:
cluster-name: my-application
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注大数据)**
![img](https://img-blog.csdnimg.cn/img_convert/edf1b0205893ee2066dd80516068ca82.png)
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
spring:
data:
elasticsearch:
cluster-name: my-application
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注大数据)**
[外链图片转存中...(img-ZutMPeIV-1713155139716)]
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**