1.引入相關依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
//提供getset 讓代碼更加簡潔
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
//mybatis 提供分頁
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.3.0</version>
</dependency>
2.yml配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/nsbdcms?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8
data:
elasticsearch:
cluster-name: es-Cluster
cluster-nodes: 192.168.126.128:9200
repositories:
enabled: true
es:
url: 192.168.126.128:9200
index: sougoulog
#mybatis plus 設定
mybatis-plus:
type-aliases-package: com.example.jsoupdome.domin
mapper-locations: classpath:mapper/*.xml
configuration:
jdbc-type-for-null: null
global-config:
# 關閉 mybatis-plus的 banner
banner: false
2.1創建配置類 Client
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;
/**
* @author zhang
* @version 1.0
* @date 2021/10/27 15:11
*/
@Configuration
public class Client {
@Value("${es.url}")
private String esUrl;
@Bean
RestHighLevelClient configRestHighLevelClient() throws Exception {
String[] esUrlArr = esUrl.split(",");
List<HttpHost> httpHosts = new ArrayList<>();
for(String es : esUrlArr){
String[] esUrlPort = es.split(":");
httpHosts.add(new HttpHost(esUrlPort[0], Integer.parseInt(esUrlPort[1]), "http"));
}
return new RestHighLevelClient(
RestClient.builder(httpHosts.toArray(new HttpHost[0]))
);
}
3.創建物體類對應于資料庫表 一一對應串列資訊
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.data.elasticsearch.annotations.Document;
import java.util.Date;
@Data
@TableName(value = "t_cms_content_news")
@Document(indexName = "content")
public class TCmsContentNews {
@TableId(value = "content_id",type = IdType.INPUT)
private Long contentId;
@ApiModelProperty(value="內容")
private String content;
@ApiModelProperty(value="附件")
private String fujian;
@ApiModelProperty(value="來源")
private String laiyuan;
private String val;
@TableField("create_time")
private Date createTime;
@TableField("update_time")
private Date updateTime;
}
4.ContentEsRepository介面 繼承 ElasticsearchRepository 這個類,提供操作es api 直接用就可以
import com.example.jsoupdome.domin.TCmsContentNews;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
/**
* @author zhang
* @version 1.0
* @date 2021/10/28 16:18
*/
public interface ContentEsRepository extends ElasticsearchRepository<TCmsContentNews,Long> {
}
5.然后就三層架構了
controller
import com.example.jsoupdome.service.ContentEsServce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* 向es匯入內容表
* @author zhang
* @version 1.0
* @date 2021/10/28 14:50
*/
@RestController
@RequestMapping("/update")
public class ContentEsController {
@Autowired
private ContentEsServce contentEsServce;
/**
* 創建索引并存入內容
* */
@RequestMapping(value = "/importEs",method = RequestMethod.POST)
public String importEsByContent(){
this.contentEsServce.saveEsByContent();
return "success" ;
}
}
ContentEsServce介面
/**
* @author zhang
* @version 1.0
* @date 2021/10/28 15:52
*/
public interface ContentEsServce {
void saveEsByContent();
}
ContentEsServceImpl實作類
import com.example.jsoupdome.dao.ContentMapper;
import com.example.jsoupdome.repository.ContentEsRepository;
import com.example.jsoupdome.domin.TCmsContentNews;
import com.example.jsoupdome.service.ContentEsServce;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @author zhang
* @version 1.0
* @date 2021/10/28 15:53
*/
@Service
public class ContentEsServceImpl implements ContentEsServce {
@Autowired
private ContentEsRepository contentEsRepository;
@Autowired
private ContentMapper contentMapper;
public static ThreadPoolExecutor es = new ThreadPoolExecutor(50, 100, 0L, TimeUnit.SECONDS,new LinkedBlockingQueue());
@Override
public void saveEsByContent() {
//查詢全部資料
List<TCmsContentNews> content = this.contentMapper.selectList(null);
//獲取總數
int total = content.size();
//定義每頁個數 --前臺傳來
final int pagesize = 100;
//求出當每頁為pagesize 時候,可以分幾頁,就回圈幾次 ,分頁插入
int pagenum = (int) Math.ceil(total / pagesize);
System.out.println("一共有"+pagenum+"頁");
//分頁匯入
for (int i = 1; i <= pagenum; i++) {
//分頁物件 mybatis 提供的
PageHelper.startPage(i,pagesize);
PageInfo<TCmsContentNews> tcmFilePageInfo = new PageInfo(content);
System.out.println("正在匯入第" + i + "頁資料");
if(tcmFilePageInfo.getSize()>0){
es.submit(()->{
contentEsRepository.saveAll(tcmFilePageInfo.getList());
});
}
}
System.out.println("匯入完畢!");
}
}
dao層
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.jsoupdome.domin.TCmsContentNews;
/**
* @author zhang
* @version 1.0
* @date 2021/10/28 17:01
*/
public interface ContentMapper extends BaseMapper<TCmsContentNews> {
}
然后 post 請求一下 http://localhost:8888/update/importEs
我們打開head 插件查詢資料
說明插入成功 了
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/340678.html
標籤:其他
上一篇:Flink集群部署centos7
