先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7
深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新大数据全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
如果你需要这些资料,可以添加V获取:vip204888 (备注大数据)
正文
我这边使用es主要是涉及了创建索引(index名称),删除索引(index名称),判断索引是否存在(index名称),查询全部索引,新增数据(索引名称,数据对象),删除数据(index名称,数据id),获取某一条数据(index名称,数据id),分页查询数据(index名称,数据id,分页参数)
配置文件
# es
spring.elasticsearch.rest.uris=ip:9200
spring.elasticsearch.rest.connection-timeout=10s
spring.elasticsearch.rest.read-timeout=30s
我使用的ES依赖
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>8.7.1</version>
</dependency>
创建索引
@Resource
public RestHighLevelClient restHighLevelClient;
public CommonResult CreateIndex(@RequestBody EsCreateVo esCreateVo) throws IOException {
//判断索引是否存在
GetIndexRequest requests = new GetIndexRequest(esCreateVo.getIndex());
XContentBuilder mappingBuilder = XContentFactory.jsonBuilder();
mappingBuilder.startObject();
{
mappingBuilder.startObject("properties");
{
mappingBuilder.startObject("id");
{
mappingBuilder.field("type", "text");
mappingBuilder.field("analyzer", "standard");
}
mappingBuilder.endObject();
mappingBuilder.startObject("name");
{
mappingBuilder.field("type", "text");
mappingBuilder.field("analyzer", "standard");
}
mappingBuilder.endObject();
mappingBuilder.startObject("time");
{
mappingBuilder.field("type", "text");
mappingBuilder.field("analyzer", "standard");
}
mappingBuilder.endObject();
mappingBuilder.startObject("content");
{
mappingBuilder.field("type", "text");
mappingBuilder.field("analyzer", "standard");
}
mappingBuilder.endObject();
}
mappingBuilder.endObject();
}
boolean exists = restHighLevelClient.indices().exists(requests, RequestOptions.DEFAULT);
if (!exists) {
CreateIndexRequest request = new CreateIndexRequest(esCreateVo.getIndex());
request.mapping(mappingBuilder);
CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
//判断是否创建成功
boolean acknowledged = response.isAcknowledged();
return acknowledged ? new CommonResult().success() : new CommonResult().failed("创建分类失败!");
}
return new CommonResult().failed("分类已存在!");
}
mappingBuilder.startObject() 是在创建索引的时候把索引中字段映射关系提前新建,不这样做的话,如果你查询的时候用了一些特定的条件的话,会报错(当新建索引,而且索引中没有添加过数据的时候)
删除索引
public CommonResult DeleteIndex(@RequestBody EsCreateVo esCreateVo) throws IOException {
//判断索引是否存在
GetIndexRequest requests = new GetIndexRequest(esCreateVo.getIndex());
boolean exists = restHighLevelClient.indices().exists(requests, RequestOptions.DEFAULT);
if (exists) {
DeleteIndexRequest request = new DeleteIndexRequest(esCreateVo.getIndex());
AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
return delete.isAcknowledged() ? new CommonResult().success() : new CommonResult().failed("创建分类失败!");
}
return new CommonResult().failed("分类已删除或不存在!");
}
判断索引是否存在
public Boolean IndexIsExists(@RequestBody EsCreateVo esCreateVo) throws IOException {
GetIndexRequest request = new GetIndexRequest(esCreateVo.getIndex());
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
return exists ? true : false;
}
查询所有索引
public CommonResult selectIndexAll() throws IOException {
GetAliasesRequest request = new GetAliasesRequest();
GetAliasesResponse alias = restHighLevelClient.indices().getAlias(request, RequestOptions.DEFAULT);
Map<String, Set<AliasMetaData>> aliases = alias.getAliases();
Set<String> indices = aliases.keySet();
List<EsCreateVo> esCreateVoList = new ArrayList<>();
for (String s : indices) {
EsCreateVo esCreateVo = new EsCreateVo();
esCreateVo.setIndex(s);
esCreateVoList.add(esCreateVo);
}
return new CommonResult().success(esCreateVoList);
}
创建文档
public CommonResult AddDocument(EsReqVO esReqVO) throws Exception {
System.out.println("》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》执行创建所有文档"+ DateTime.now());
//上传文件,获取文件基本信息
MultipartFile file = esReqVO.getFile();
String content = wordService.importDatas(file);
// DecimalFormat decimalFormat = new DecimalFormat("#0.000");
// 判断文件大小来转换并拼接 M单位
long size = file.getSize();
Double fileSizes =file.getSize()/1024/1024.;
BigDecimal bigDecimal = BigDecimal.valueOf(fileSizes).setScale(2, BigDecimal.ROUND_HALF_UP);
// 创建一个DocumentVO对象
DocumentVO documentVO = new DocumentVO(file.getOriginalFilename(), String.valueOf(bigDecimal) + " M", DateTime.now().toString(), content);
// 创建请求
IndexRequest request = new IndexRequest(esReqVO.getIndex());
// 制定规则 PUT /liuyou_index/_doc/1
// request.id("13");// 设置文档ID,不设置自动生成
request.timeout(TimeValue.timeValueMillis(1000));// request.timeout("1s")
// 将我们的数据放入请求中
request.source(JSON.toJSONString(documentVO), XContentType.JSON);
// 客户端发送请求,获取响应的结果
try {
IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
System.out.println("======" + response.status());// 获取建立索引的状态信息 CREATED
System.out.println("[[[[[[[[[" + response);// 查看返回内容
} catch (Exception e) {
String msg = e.getMessage();
if (!msg.contains("201 Created") && !msg.contains("200 OK")) {
throw e;
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return new CommonResult().success();
}
创建文档的时候 需要在return之前让线程休眠至少一秒,删除也一样,因为ES有一个机制是你创建或删除文档的时候,他不会立即刷新,而是把数据放到一个类似于暂存区的地方1秒,如果不执行现成休眠的话,前端操作完创建/删除的时候,调用查询接口会查询不到你刚新增的数据,或是删除的数据还在存在列表,这是个坑,一定要注意!!!
修改文档
public CommonResult UpdateDocument(@RequestBody DocumentsVO documentsVO) throws IOException {
UpdateRequest request = new UpdateRequest(documentsVO.getIndex(), documentsVO.getId());
DocumentVO documentVO = new DocumentVO();
BeanUtil.copyProperties(documentsVO, documentVO);
request.doc(JSON.toJSONString(documentVO), XContentType.JSON);
try {
UpdateResponse response = restHighLevelClient.update(request, RequestOptions.DEFAULT);
System.out.println(response.status()); // OK
restHighLevelClient.close();
} catch (Exception e) {
String msg = e.getMessage();
if (!msg.contains("201 Created") && !msg.contains("200 OK")) {
throw e;
}
}
return new CommonResult().success();
}
查询某条文档信息的获取
public CommonResult GetDocument(String index, String id) throws IOException {
GetRequest request = new GetRequest(index, id);
GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());// 打印文档内容
System.out.println(request);// 返回的全部内容和命令是一样的
return new CommonResult().success(response.getSourceAsString());
}
文档的获取,并判断其是否存在
public CommonResult DocumentIsExists(String index, String id) throws IOException {
GetRequest request = new GetRequest(index, id);
// 不获取返回的 _source的上下文了
request.fetchSourceContext(new FetchSourceContext(false));
request.storedFields("_none_");
boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
return exists ? new CommonResult().success(exists) : new CommonResult().failed("文档不存在!");
}
删除文档
public CommonResult DeleteDocument(@RequestParam String index, @RequestParam String id) throws IOException {
System.out.println("》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》执行删除文档"+ DateTime.now());
DeleteRequest request = new DeleteRequest(index, id);
request.timeout("1s");
try {
restHighLevelClient.delete(request, RequestOptions.DEFAULT);
} catch (Exception e) {
String msg = e.getMessage();
if (!msg.contains("201 Created") && !msg.contains("200 OK")) {
throw e;
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return new CommonResult().success();
}
根据索引查询所有文档
public CommonResult SearchsDocument(@RequestParam(value = "fileName", required = false) String fileName,
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注大数据)**
![img](https://img-blog.csdnimg.cn/img_convert/c57bbcb03fd29402e32e1ea39178a09f.png)
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
(value = "fileName", required = false) String fileName,
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注大数据)**
[外链图片转存中...(img-XkPQnIiL-1713702348214)]
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**