Elasticsearch基本使用初体验02

news2024/9/25 5:22:56

1.Java API操作ES

1.1 创建项目

创建spring Boot工程,添加相关的依赖。

<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>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.79</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
		</dependency>	
	</dependencies>

1.2 创建配置类

创建一个配置类,专门用来获取es客户端对象;创建config包,在该包下创建EsConfig.java类。

@Configuration
public class EsConfig {

    // 该对象可以对我们的ES进行相关的操作
    @Bean
    public RestHighLevelClient restHighLevelClient() {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1",9200,"http")));
        
        return client;
    }
}

1.3 操作索引

1.3.1 创建索引
@SpringBootTest
class SpringbootEsApplicationTests {

	@Autowired
	private RestHighLevelClient client;


	@Test
	public void testCreate() throws IOException{
		// 创建索引 - 请求对象
		CreateIndexRequest request = new CreateIndexRequest("user");
		// 发送请求,获取响应
		CreateIndexResponse response = client.indices().create(request,
				RequestOptions.DEFAULT);
		boolean acknowledged = response.isAcknowledged();
		// 响应状态
		System.out.println("操作状态 = " + acknowledged);

		// 关闭客户端连接
		client.close();

	}
}

在这里插入图片描述
可以看到操作成功了,使用Postman,发送请求可以看到es中成功添加了新的user索引。
在这里插入图片描述

1.3.2 查询索引
	@Test
	public void testSelect() throws IOException{
		// 查询索引 - 请求对象
		GetIndexRequest request = new GetIndexRequest("user");
		// 发送请求,获取响应
		GetIndexResponse response = client.indices().get(request,
				RequestOptions.DEFAULT);

		System.out.println("aliases:"+response.getAliases());
		System.out.println("mappings:"+response.getMappings());
		System.out.println("settings:"+response.getSettings());

		client.close();
	}

在这里插入图片描述

1.3.3 删除索引
	@Test
	public void testDelete() throws IOException{
		// 删除索引 - 请求对象
		DeleteIndexRequest request = new DeleteIndexRequest("user");
		// 发送请求,获取响应
		AcknowledgedResponse response = client.indices().delete(request,RequestOptions.DEFAULT);
		// 操作结果
		System.out.println("操作结果 : " + response.isAcknowledged());
		client.close();
	}

在这里插入图片描述
使用Postman,发送请求可以查看一下es中user索引已经不存在了。
在这里插入图片描述

1.4 操作文档

1.4.1 新增文档

创建User数据对象,以便后续使用。

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {

    private Integer id;
    private String name;
    private Integer age;
    private String sex;
}
    @Test
	public void testInsert() throws Exception{
		// 新增文档 - 请求对象
		IndexRequest request = new IndexRequest();
		// 设置索引及唯一性标识
		request.index("user").id("1001");

		// 创建数据对象
		User user = new User();
		user.setName("picacho");
		user.setAge(18);
		user.setSex("男");

		ObjectMapper objectMapper = new ObjectMapper();
		String productJson = objectMapper.writeValueAsString(user);
		// 添加文档数据,数据格式为 JSON 格式
		request.source(productJson, XContentType.JSON);
		// 客户端发送请求,获取响应对象
		IndexResponse response = client.index(request, RequestOptions.DEFAULT);

		System.out.println("_index:" + response.getIndex());
		System.out.println("_id:" + response.getId());
		System.out.println("_result:" + response.getResult());
	}

在这里插入图片描述
可以看到操作成功了,使用Postman,发送请求可以看到es中成功添加了新的文档内容。
在这里插入图片描述

1.4.2 查询文档
    @Test
	public void testSelectDoc() throws Exception{
		//1.创建请求对象
		GetRequest request = new GetRequest().index("user").id("1001");
		//2.客户端发送请求,获取响应对象
		GetResponse response = client.get(request, RequestOptions.DEFAULT);

		System.out.println("_index:" + response.getIndex());
		System.out.println("_type:" + response.getType());
		System.out.println("_id:" + response.getId());
		System.out.println("source:" + response.getSourceAsString());
	}

在这里插入图片描述

1.4.3 修改文档
    @Test
	public void testUpdateDoc() throws Exception{
		// 修改文档 - 请求对象
		UpdateRequest request = new UpdateRequest();
		// 配置修改参数
		request.index("user").id("1001");
		// 设置请求体,对数据进行修改
		request.doc(XContentType.JSON, "age", 19);
		// 客户端发送请求,获取响应对象
		UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
		System.out.println("_index:" + response.getIndex());
		System.out.println("_id:" + response.getId());
		System.out.println("_result:" + response.getResult());
	}

在这里插入图片描述
可以看到操作成功了,使用Postman,发送请求可以看到es中成功修改了文档的内容。
在这里插入图片描述

1.4.5 删除文档
@Test
	public void testDeleteDoc()throws Exception{
		//创建请求对象
		DeleteRequest request = new DeleteRequest().index("user").id("1001");
		//客户端发送请求,获取响应对象
		DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
		//打印信息
		System.out.println(response.toString());
	}

在这里插入图片描述
可以看到操作成功了,使用Postman,发送请求可以看到es中已经没有该文档的内容了。
在这里插入图片描述

1.4.5 批量新增文档
    @Test
	public void testBatchInsertDoc() throws Exception{
		//创建批量新增请求对象
		BulkRequest request = new BulkRequest();
		request.add(new
				IndexRequest().index("user").id("1001").source(XContentType.JSON, "name",
				"picacho"));
		request.add(new
				IndexRequest().index("user").id("1002").source(XContentType.JSON, "name",
				"picacho1"));
		request.add(new
				IndexRequest().index("user").id("1003").source(XContentType.JSON, "name",
				"picacho2"));
		//客户端发送请求,获取响应对象
		BulkResponse responses = client.bulk(request, RequestOptions.DEFAULT);
		//打印结果信息
		System.out.println("took:" + responses.getTook());
		System.out.println("items:" + responses.getItems());
	}

在这里插入图片描述
可以看到操作成功了,使用Postman,发送请求可以看到es中新增文档的内容了。
在这里插入图片描述

1.4.6 批量删除文档
@Test
	public void testBatchDeleteDoc() throws Exception{
		//创建批量删除请求对象
		BulkRequest request = new BulkRequest();
		request.add(new DeleteRequest().index("user").id("1001"));
		request.add(new DeleteRequest().index("user").id("1002"));
		request.add(new DeleteRequest().index("user").id("1003"));
		//客户端发送请求,获取响应对象
		BulkResponse responses = client.bulk(request, RequestOptions.DEFAULT);
		//打印结果信息
		System.out.println("took:" + responses.getTook());
		System.out.println("items:" + responses.getItems());

	}

在这里插入图片描述
可以看到操作成功了,使用Postman,发送请求可以看到es中新增文档的内容已经不见了。
在这里插入图片描述

1.5 复杂文档操作

首先需要插入多条文档数据,方便后续操作展示所用。

@Test
    public void testBatchInsertDoc1() throws Exception{
		//创建批量新增请求对象
		BulkRequest request = new BulkRequest();
		request.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON, "name", "picacho", "age", "18", "sex","男"));
		request.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON, "name", "picacho1", "age", "19", "sex","女"));
		request.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON, "name", "picacho2", "age", "20", "sex","男"));
		request.add(new IndexRequest().index("user").id("1004").source(XContentType.JSON, "name", "picacho3", "age", "20", "sex","女"));
		request.add(new IndexRequest().index("user").id("1005").source(XContentType.JSON, "name", "picacho4", "age", "22", "sex","男"));
		request.add(new IndexRequest().index("user").id("1006").source(XContentType.JSON, "name", "picacho5", "age", "27", "sex","男"));
		//客户端发送请求,获取响应对象
		BulkResponse responses = client.bulk(request, RequestOptions.DEFAULT);
		//打印结果信息
		System.out.println("took:" + responses.getTook());
		System.out.println("items:" + responses.getItems());
	}
1.5.1 查询所有索引数据
@Test
	public void testQueryDoc() throws Exception{
		// 创建搜索请求对象
		SearchRequest request = new SearchRequest();
		request.indices("user");
		// 构建查询的请求体
		SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
		// 查询所有数据
		sourceBuilder.query(QueryBuilders.matchAllQuery());
		request.source(sourceBuilder);
		SearchResponse response = client.search(request, RequestOptions.DEFAULT);
		// 查询匹配
	    SearchHits hits = response.getHits();
		System.out.println("took:" + response.getTook());
		System.out.println("timeout:" + response.isTimedOut());
		System.out.println("total:" + hits.getTotalHits());
		System.out.println("MaxScore:" + hits.getMaxScore());
		System.out.println("hits========>>");
		for (SearchHit hit : hits) {
			//输出每条查询的结果信息
			System.out.println(hit.getSourceAsString());
		}
	}

在这里插入图片描述

1.5.2 条件查询
@Test
	public void testQueryDocByAge() throws Exception{
		// 创建搜索请求对象
		SearchRequest request = new SearchRequest();
		request.indices("user");
		// 构建查询的请求体
		SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
		sourceBuilder.query(QueryBuilders.termQuery("age", "22"));
		request.source(sourceBuilder);
		SearchResponse response = client.search(request, RequestOptions.DEFAULT);
		// 查询匹配
		SearchHits hits = response.getHits();
		System.out.println("took:" + response.getTook());
		System.out.println("timeout:" + response.isTimedOut());
		System.out.println("total:" + hits.getTotalHits());
		System.out.println("MaxScore:" + hits.getMaxScore());
		System.out.println("hits========>>");
		for (SearchHit hit : hits) {
			//输出每条查询的结果信息
			System.out.println(hit.getSourceAsString());
		}
		System.out.println("<<========");
	}

在这里插入图片描述

1.5.3 分页查询
@Test
	public void testQueryPage() throws Exception{
		// 创建搜索请求对象
		SearchRequest request = new SearchRequest();
		request.indices("user");
		// 构建查询的请求体
		SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
		sourceBuilder.query(QueryBuilders.matchAllQuery());
		// 分页查询
		// 当前页其实索引(第一条数据的顺序号), from
		sourceBuilder.from(0);
		// 每页显示多少条 size
		sourceBuilder.size(2);
		
		request.source(sourceBuilder);
		SearchResponse response = client.search(request, RequestOptions.DEFAULT);
		// 查询匹配
		SearchHits hits = response.getHits();
		System.out.println("took:" + response.getTook());
		System.out.println("timeout:" + response.isTimedOut());
		System.out.println("total:" + hits.getTotalHits());
		System.out.println("MaxScore:" + hits.getMaxScore());
		System.out.println("hits========>>");
		for (SearchHit hit : hits) {
			//输出每条查询的结果信息
			System.out.println(hit.getSourceAsString());
		}
		System.out.println("<<========");
	}

在这里插入图片描述

1.5.4 查询后排序
    @Test
	public void testQueryOrder() throws Exception{
		// 创建搜索请求对象
		SearchRequest request = new SearchRequest();
		request.indices("user");

		// 构建查询的请求体
		SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
		sourceBuilder.query(QueryBuilders.matchAllQuery());
		// 排序
		sourceBuilder.sort("age", SortOrder.ASC);
		request.source(sourceBuilder);
		SearchResponse response = client.search(request, RequestOptions.DEFAULT);
		// 查询匹配
		SearchHits hits = response.getHits();
		System.out.println("took:" + response.getTook());
		System.out.println("timeout:" + response.isTimedOut());
		System.out.println("total:" + hits.getTotalHits());
		System.out.println("MaxScore:" + hits.getMaxScore());
		System.out.println("hits========>>");
		for (SearchHit hit : hits) {
			//输出每条查询的结果信息
			System.out.println(hit.getSourceAsString());
		}
		System.out.println("<<========");
	}

在这里插入图片描述

1.5.5 多条件组合查询
@Test
	public void testQuery() throws Exception{
		// 创建搜索请求对象
		SearchRequest request = new SearchRequest();
		request.indices("user");
		// 构建查询的请求体
		SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
		BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
		// 必须包含
		boolQueryBuilder.must(QueryBuilders.matchQuery("age", "22"));
		// 一定不含
		boolQueryBuilder.mustNot(QueryBuilders.matchQuery("name", "picacho5"));
		// 可能包含
		boolQueryBuilder.should(QueryBuilders.matchQuery("sex", "男"));
		sourceBuilder.query(boolQueryBuilder);
		request.source(sourceBuilder);
		SearchResponse response = client.search(request, RequestOptions.DEFAULT);
		// 查询匹配
		SearchHits hits = response.getHits();
		System.out.println("took:" + response.getTook());
		System.out.println("timeout:" + response.isTimedOut());
		System.out.println("total:" + hits.getTotalHits());
		System.out.println("MaxScore:" + hits.getMaxScore());
		System.out.println("hits========>>");
		for (SearchHit hit : hits) {
			//输出每条查询的结果信息
			System.out.println(hit.getSourceAsString());
		}
		System.out.println("<<========");
	}

在这里插入图片描述

1.5.6 范围查询
@Test
	public void testQuery1() throws Exception{
		// 创建搜索请求对象
		SearchRequest request = new SearchRequest();
		request.indices("user");
		// 构建查询的请求体
		SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
		RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
		// 大于等于
		//rangeQuery.gte("25");
		// 小于等于
		rangeQuery.lte("22");
		sourceBuilder.query(rangeQuery);
		request.source(sourceBuilder);
		SearchResponse response = client.search(request, RequestOptions.DEFAULT);
		// 查询匹配
		SearchHits hits = response.getHits();
		System.out.println("took:" + response.getTook());
		System.out.println("timeout:" + response.isTimedOut());
		System.out.println("total:" + hits.getTotalHits());
		System.out.println("MaxScore:" + hits.getMaxScore());
		System.out.println("hits========>>");
		for (SearchHit hit : hits) {
			//输出每条查询的结果信息
			System.out.println(hit.getSourceAsString());
		}
		System.out.println("<<========");
	}

在这里插入图片描述

1.5.7 模糊查询
@Test
	public void testQuery2() throws Exception{
		// 创建搜索请求对象
		SearchRequest request = new SearchRequest();
		request.indices("user");
		// 构建查询的请求体
		SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
		sourceBuilder.query(QueryBuilders.fuzzyQuery("name","picacho").fuzziness(Fuzziness.ONE));
		request.source(sourceBuilder);
		SearchResponse response = client.search(request, RequestOptions.DEFAULT);
		// 查询匹配
		SearchHits hits = response.getHits();
		System.out.println("took:" + response.getTook());
		System.out.println("timeout:" + response.isTimedOut());
		System.out.println("total:" + hits.getTotalHits());
		System.out.println("MaxScore:" + hits.getMaxScore());
		System.out.println("hits========>>");
		for (SearchHit hit : hits) {
			//输出每条查询的结果信息
			System.out.println(hit.getSourceAsString());
		}
		System.out.println("<<========");
	}

在这里插入图片描述

1.5.8 分组查询
@Test
	public void testQuery3() throws Exception{
		SearchRequest request = new SearchRequest().indices("user");
		SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
		sourceBuilder.aggregation(AggregationBuilders.terms("ageGroupBy").field("age"));
		//设置请求体
		request.source(sourceBuilder);
		//3.客户端发送请求,获取响应对象
		SearchResponse response = client.search(request, RequestOptions.DEFAULT);
		//4.打印响应结果
		SearchHits hits = response.getHits();
		System.out.println(response);
	}

在这里插入图片描述

2.Spring Boot项目集成ES

Spring Data Elasticsearch基于Spring Data API简化 Elasticsearch 操作,将原始操作Elasticsearch的客户端API进行封装。Spring Data为Elasticsearch项目提供集成搜索引擎。

2.1 添加主配置

# es 服务地址
elasticsearch.host=127.0.0.1
# es 服务端口
elasticsearch.port=9200
# 配置日志级别,开启 debug 日志
logging.level.com.picahco.springbootes=debug

2.2 创建实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Document(indexName = "shopping", shards = 3, replicas = 1)
public class Product {
    //必须有 id,这里的 id 是全局唯一的标识,等同于 es 中的"_id"
    @Id
    private Long id;

    /**
     * type : 字段数据类型
     * analyzer : 分词器类型
     * index : 是否索引(默认:true)
     * Keyword : 短语,不进行分词
     */
    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String title;//商品名称

    @Field(type = FieldType.Keyword)
    private String category;//分类名称

    @Field(type = FieldType.Double)
    private Double price;//商品价格

    @Field(type = FieldType.Keyword, index = false)
    private String images;//图片地址
}

2.3 添加配置类

和其他spring项目中的 template类似,ElasticsearchRestTemplate是spring-data-elasticsearch项目中的一个类,用来与ES进行交互。

ElasticsearchRestTemplate基于RestHighLevelClient客户端的。需要自定义配置类,继承AbstractElasticsearchConfiguration,并实现elasticsearchClient()抽象方法,创建RestHighLevelClient对象。

@ConfigurationProperties(prefix = "elasticsearch")
@Configuration
@Data
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration{

    private String host;
    private Integer port;

    //重写父类方法
    @Override
    public RestHighLevelClient elasticsearchClient() {
        RestClientBuilder builder = RestClient.builder(new HttpHost(host, port));
        RestHighLevelClient restHighLevelClient = new
                RestHighLevelClient(builder);
        return restHighLevelClient;
    }
}

2.4 创建DAO数据访问对象

@Repository
public interface ProductDao extends ElasticsearchRepository<Product, Long>{

}   

2.4 测试索引

2.4.1 创建索引
@SpringBootTest
public class SpringDataESIndexTest {

    //注入 ElasticsearchRestTemplate
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Test
    public void createIndex(){
        //创建索引,系统初始化会自动创建索引
        System.out.println("创建索引");
    }
}

在这里插入图片描述
在这里插入图片描述

2.4.2 删除索引
@Test
    public void deleteIndex(){
        //创建索引,系统初始化会自动创建索引
        boolean flg = elasticsearchRestTemplate.indexOps(Product.class).delete();
        System.out.println("删除索引 = " + flg);
    }

在这里插入图片描述

2.5 测试文档

2.5.1 新增文档
@SpringBootTest
public class SpringDataESProductDaoTest {

    @Autowired
    private ProductDao productDao;
    /**
     * 新增
     */
    @Test
    public void save(){
        Product product = new Product();
        product.setId(2L);
        product.setTitle("华为手机");
        product.setCategory("手机");
        product.setPrice(2999.0);
        product.setImages("http://www.picacho/1.jpg");
        productDao.save(product);
    }
}

在这里插入图片描述

2.5.2 查询文档
    @Test
    public void findById(){
        Product product = productDao.findById(2L).get();
        System.out.println(product);
    }

在这里插入图片描述

2.5.3 修改文档
@Test
    public void update(){
        Product product = new Product();
        product.setId(2L);
        product.setTitle("小米12");
        product.setCategory("手机");
        product.setPrice(4999.0);
        product.setImages("http://www.picacho/2.jpg");
        productDao.save(product);
    }

在这里插入图片描述

2.5.4 删除文档
@Test
    public void delete(){
        Product product = new Product();
        product.setId(2L);
        productDao.delete(product);
    }

在这里插入图片描述

2.5.5 批量新增
@Test
    public void saveAll(){
        List<Product> productList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Product product = new Product();
            product.setId(Long.valueOf(i));
            product.setTitle("["+i+"]小米手机");
            product.setCategory("手机");
            product.setPrice(4999.0 + i);
            product.setImages("http://www.picacho/1.jpg");
            productList.add(product);
        }
        productDao.saveAll(productList);
    }

在这里插入图片描述

2.5.6 分页查询
@Test
    public void findByPageable(){
        //设置排序(排序方式,正序还是倒序,排序的 id)
        Sort sort = Sort.by(Sort.Direction.DESC,"id");
        int currentPage=0;//当前页,第一页从 0 开始, 1 表示第二页
        int pageSize = 5;//每页显示多少条
        //设置查询分页
        PageRequest pageRequest = PageRequest.of(currentPage, pageSize,sort);
        //分页查询
        Page<Product> productPage = productDao.findAll(pageRequest);
        for (Product Product : productPage.getContent()) {
            System.out.println(Product);
        }
    }

在这里插入图片描述
在Spring Boot项目中集成使用ES时,有一个很重要的点需要注意,不同版本的ES提供的方法可能存在差异,所以需要知道自己使用的那个版本后,再对应使用方法。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/176032.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

基于ssm校园学生协会管理系统jsp校园社团管理系统源码和论文

1、开发环境 &#xff08;1&#xff09;操作系统: Windows10 &#xff08;2&#xff09;数据库与数据库管理工具: MySQL 5.7.19、Navicat for MySQL &#xff08;3&#xff09;Web服务器: Tomcat8.5.38 &#xff08;4&#xff09;开发工具与技术: Eclipse IDEA、SSM框架、Ajax …

2023寒假算法集训营3

&#xff08;数学场真折磨人&#xff09; A. 不断减损的时间&#xff08;贪心&#xff09; 题意&#xff1a; 给定一个数组&#xff0c;任意次操作&#xff0c;每次操作可以 选择一个偶数除以 222 。 求最终数组所有元素之和的最小值。 思路&#xff1a; 要使得所有元素之…

三十四、Kubernetes1.25中Ingress介绍、安装

1、介绍 在前面文章中已经提到&#xff0c;Service对集群之外暴露服务的主要方式有两种&#xff1a;NotePort和LoadBalancer&#xff0c;但是这两种方式&#xff0c;都有一定的缺点&#xff1a; NodePort方式的缺点是会占用很多集群机器的端口&#xff0c;那么当集群服务变多的…

【JavaSE专栏2】JDK、JRE和JVM

作者主页&#xff1a;Designer 小郑 作者简介&#xff1a;Java全栈软件工程师一枚&#xff0c;来自浙江宁波&#xff0c;负责开发管理公司OA项目&#xff0c;专注软件前后端开发&#xff08;Vue、SpringBoot和微信小程序&#xff09;、系统定制、远程技术指导。CSDN学院、蓝桥云…

Nacos-统一配置中心

Nacos-统一配置中心统一配置管理1.Nacos编写配置文件2.微服务拉取配置&#xff08;1&#xff09;引入pom依赖&#xff08;2&#xff09;添加bootstrap.yaml&#xff08;3&#xff09;读取nacos配置3.配置热更新&#xff08;1&#xff09;方式一&#xff08;2&#xff09;方式二…

Ubuntu显示优化 动画

之前从win转到了ubuntu。老大哥问我为啥不直接用Mac。我笑笑没说话。其实就一个字&#xff0c;穷。 使用Ubuntu的过程中有一点小问题&#xff0c;不过平时我主要用来编程&#xff0c;对壁纸&#xff0c;过渡动画这些东西其实并不是很在乎。直到我审美感爆棚的妻子告诉我&#…

设计模式学习(十一):Builder建造者模式

一、什么是Builder模式大都市中林立着许多高楼大厦&#xff0c;这些高楼大厦都是具有建筑结构的大型建筑。通常&#xff0c;建造和构建这种具有建筑结构的大型物体在英文中称为Build。在建造大楼时&#xff0c;需要先打牢地基&#xff0c;搭建框架&#xff0c;然后自下而上地一…

AcWing 278. 数字组合

AcWing 278. 数字组合一、问题二、思路1、状态表示2、状态转移3、循环设计4、初末状态三、代码一、问题 二、思路 这道题其实看上去和我们的01背包问题是非常相似的。如果这道题我们转化为01背包问题的话&#xff0c;描述如下&#xff1a; 给很多个物品和体积&#xff0c;然后…

深入理解Mysql底层数据结构

一. 索引的本质 索引是帮助MySQL高效获取数据的排好序的数据结构。 二. 索引的数据结构 二叉树红黑树Hash表BTreeBTree mysql的索引采用的是B树的结构 mysql为什么不用二叉树&#xff0c;因为对于单边增长的数据列&#xff0c;二叉树和全表扫描差不多&#xff0c;效率没有什…

pytorch 神经网络笔记-RNN和LSTM

文章目录时间序列表示方法一般过程RNNRNN原理1RNN原理2RNN layer使用pytorch实现nn.RNN__init__forwardSingle layer RNN2 layer RNNpytorch实现nn.RNNCell时间序列波形预测例子LSTMnn.LSTMnn.LSTMCellSingle layerTwo Layersb站课程链接课时自己找一下 时间序列表示方法 卷积神…

自注意力(Self-Attention)机制浅析

一、自注意力机制概述循环神经网络由于信息传递的容量以及梯度消失问题&#xff0c;实际上也只能建立短距离依赖关系。为了建立长距离的依赖关系&#xff0c;可以增加网络的层数或者使用全连接网络。但是全连接网络无法处理变长的输入序列&#xff0c;另外&#xff0c;不同的输…

字节青训前端笔记 | Web安全

在网络时代下&#xff0c;Web 安全随处可见并且危害极大&#xff0c;Web 安全问题也越来越受到重视。本节课将讲述Web中的攻击和防御 XSS 利用开发者盲目信任客户提交的内容来实现的工具&#xff0c;恶意攻击者往 Web 页面里插入恶意 Script 代码&#xff0c;当用户浏览该页面…

机器视觉(十二):二维条码识别

目录&#xff1a; 机器视觉&#xff08;一&#xff09;&#xff1a;概述 机器视觉&#xff08;二&#xff09;&#xff1a;机器视觉硬件技术 机器视觉&#xff08;三&#xff09;&#xff1a;摄像机标定技术 机器视觉&#xff08;四&#xff09;&#xff1a;空域图像增强 …

【16】C语言 | 初识函数递归 | 几个练习

目录 什么是递归? 练习1: 练习2: 练习3、 练习4 什么是递归? 程序调用自身的编程技巧称为递归( recursion )。递归做为一种算法在程序设计语言中广泛应用。一个过程或函数在其定义或说明中有直接或间接调用自身的一种方法&#xff0c;它通常把一个大型复杂的问题层层转化…

生物化学 电阻抗成像OpenEIT,Dbar算法,数据集等(暂记)

Electrical Impedance Tomography,EIT 电阻抗成像&#xff08;Electrical Impedance Tomography,EIT&#xff09;是一种无创的以人体内部的电阻率分布为目标的重建体内组织图像的技术。人体是一个大的生物电导体&#xff0c;各组织、器官均有一定的阻抗&#xff0c;当人体的局部…

SLF4J基本使用

文章目录1. 日志门面2. SLF4J的使用2.1 SLF4J入门2.2 为什么要使用SLF4J作为日志门面&#xff1f;2.3 绑定日志的实现&#xff08;Binding&#xff09;2.4 logback 整合 slf4j2.5 log4j 整合 slf4j2.6 jul 整合 slf4j3. 桥接旧的日志框架&#xff08;Bridging&#xff09;3.1 S…

完全二叉树的权值

题目描述 给定一棵包含 N 个节点的完全二叉树,树上每个节点都有一个权值,按从 上到下、从左到右的顺序依次是 A1​,A2​,⋅⋅⋅AN​,如下图所示: 现在小明要把相同深度的节点的权值加在一起,他想知道哪个深度的节点 权值之和最大?如果有多个深度的权值和同为最大,请你输…

react用高阶组件优化文件结构 帮助建立高阶组件应用思路

其实高阶组件是一个将组件写的更灵活的方式&#xff0c;他的应用场景在业务开发中会非常多样 这里 我们演示一种 主要还是解决问题的思想最重要 或者是 这个不叫解决问题 而是设计组件结构的思路 我们来模拟一个场景 在src下有一个 components 文件夹目录 在 components 下有…

3.开发社区首页

数据库设计如下&#xff1a; 任务&#xff1a;开发社区首页&#xff0c;显示前十个帖子&#xff1b;开发分页组件&#xff0c;分页显示所有帖子。 开发流程&#xff1a;entity->dao->mapper.xml->service->controller 由于关注后端&#xff0c;所以未使用前后分离…

【Potplayer】如何用Potplayer实现对视频的局部缩放播放?如何用potplayer更舒心地播放竖屏视频?

一、问题背景 有时候我们从互联网上下载得到一个竖屏视频&#xff0c;用电脑播放时&#xff0c;左右两边都是黑的&#xff0c;电脑屏幕的高宽度没有得到良好应用。而如果恰好这个竖屏视频其实只有一小部分才是有效区域/重点内容&#xff0c;比如中央区域&#xff0c;上下区域都…