当前位置: 首页 > news >正文

网站开发 javaseo优化排名怎么做

网站开发 java,seo优化排名怎么做,美国互联网公司排名,销售类电商网站如何做优化ES的使用(Elasticsearch) es是什么? es是非关系型数据库,是分布式文档数据库,本质上是一个JSON 文本 为什么要用es? 搜索速度快,近乎是实时的存储、检索数据 怎么使用es? 1.下载es的包(环境要…

ES的使用(Elasticsearch)

es是什么?
es是非关系型数据库,是分布式文档数据库,本质上是一个JSON 文本
为什么要用es?
搜索速度快,近乎是实时的存储、检索数据
怎么使用es?
1.下载es的包(环境要是jdk1.8及以上)(我的资源中有)
2.下载es的可视化界面包(我的资源中有)
3.java编写es的工具类
es与关系型数据库对比
在这里插入图片描述

1.下载es的包,解压,运行bat文件(windows)

下载地址:es官网下载地址
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
elasticsearch.yml配置允许跨域

http.cors.enabled: true
http.cors.allow-origin: "*"

在这里插入图片描述

2.下载es的可视化界面包,解压,使用命令npm run start

下载地址:elasticsearch-head-master es可视化工具
在这里插入图片描述
打开http:localhost:9100
在这里插入图片描述

3.java编写es的工具类

引入es的依赖包

		<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>6.2.4</version></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-client</artifactId><version>6.2.4</version></dependency><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>6.2.4</version></dependency>
package com.next.service;import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetRequest;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;@Service
@Slf4j
public class ESClient implements ApplicationListener<ContextRefreshedEvent> {private final static int CONNECT_TIMEOUT = 100;private final static int SOCKET_TIMEOUT = 60 * 1000;private final static int REQUEST_TIMEOUT = SOCKET_TIMEOUT;private RestHighLevelClient restHighLevelClient; //JDK8及以上private BasicHeader[] basicHeaders;@Overridepublic void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {try {initClient();} catch (Exception e) {log.error("es client init exception", e);try {Thread.sleep(1000);} catch (Exception e1) {}initClient();}}private void initClient() {log.info("es client init start");//请求头时允许的格式basicHeaders = new BasicHeader[]{new BasicHeader("Accept", "application/json;charset=UTF-8")};//es客户端连接设置初始化RestClientBuilder builder = RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"));builder.setDefaultHeaders(basicHeaders)//设置相关超时间配置.setRequestConfigCallback((RequestConfig.Builder configBuilder) -> {configBuilder.setConnectTimeout(CONNECT_TIMEOUT);configBuilder.setSocketTimeout(SOCKET_TIMEOUT);configBuilder.setConnectionRequestTimeout(REQUEST_TIMEOUT);return configBuilder;});restHighLevelClient = new RestHighLevelClient(builder);log.info("es client init end");}//es新增操作public IndexResponse index(IndexRequest indexRequest) throws Exception {try {return restHighLevelClient.index(indexRequest);} catch (Exception e) {log.error("es.index exception,indexRequest:{}", indexRequest, e);throw e;}}//更新操作public UpdateResponse update(UpdateRequest updateRequest) throws Exception {try {return restHighLevelClient.update(updateRequest, basicHeaders);} catch (Exception e) {log.error("es.update exception,updateRequest:{}", updateRequest, e);throw e;}}//查询public GetResponse get(GetRequest getRequest) throws Exception {try {return restHighLevelClient.get(getRequest, basicHeaders);} catch (Exception e) {log.error("es.get exception,updateRequest:{}", getRequest, e);throw e;}}//多个查询请求放在一起查public MultiGetResponse multiGet(MultiGetRequest multiGetRequest) throws Exception {try {return restHighLevelClient.multiGet(multiGetRequest);} catch (Exception e) {log.error("es.multiGet exception,getRequest:{}", multiGetRequest, e);throw e;}}/*** @desc 批量更新*/public BulkResponse bulk(BulkRequest bulkRequest) throws Exception {try {return restHighLevelClient.bulk(bulkRequest,basicHeaders);} catch (Exception e) {log.error("es.multiGet exception,bulkRequest:{}", bulkRequest, e);throw e;}}
}

es启动
在这里插入图片描述

4.使用例子:

将车次信息存到es中,方便用户查询(从此地到目的地有哪些车可以乘坐)

package com.next.service;import com.alibaba.google.common.base.Splitter;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.next.common.TrainEsConstant;
import com.next.dao.TrainNumberDetailMapper;
import com.next.dao.TrainNumberMapper;
import com.next.model.TrainNumber;
import com.next.model.TrainNumberDetail;
import com.next.util.JsonMapper;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.get.*;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.common.util.set.Sets;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;@Service
@Slf4j
public class TrainNumberService {@Resourceprivate TrainNumberMapper trainNumberMapper;@Resourceprivate TrainCacheService trainCacheService;@Resourceprivate TrainNumberDetailMapper trainNumberDetailMapper;@Resourceprivate ESClient esClient;public void handle(List<CanalEntry.Column> columns, CanalEntry.EventType eventType) throws Exception{if (eventType != CanalEntry.EventType.UPDATE) {log.info("not update,no need care");return;}int trainNumberId = 0;//获取数据库的trainNumberIdfor (CanalEntry.Column column : columns) {if (column.getName().equals("id")) {trainNumberId = Integer.parseInt(column.getValue());break;}}TrainNumber trainNumber = trainNumberMapper.selectByPrimaryKey(trainNumberId);//校验是否有车次if (null == trainNumber) {log.error("not found trainNumber,trainNumberId:{}", trainNumberId);return;}List<TrainNumberDetail> detailList = trainNumberDetailMapper.getByTrainNumberId(trainNumberId);//校验是否有车次详情if (CollectionUtils.isEmpty(detailList)) {log.warn("no detail,no need care,trainNumberId:{}", trainNumber.getName());return;}//将数据写入缓存中trainCacheService.set("TN_" + trainNumber.getName(), JsonMapper.obj2String(detailList));log.info("trainNumber:{} detailList update redis", trainNumber.getName());//将数据存入es中saveES(detailList,trainNumber);log.info("trainNumber:{} detailList update es", trainNumber.getName());}//数据保存到es(客户需要查询的数据放到es--->从此地到目的地有哪些车可以乘坐)private void saveES(List<TrainNumberDetail> detailList, TrainNumber trainNumber) throws Exception{/*** A-B fromStationId- toStationId* 例:北京到大连有多少趟车?* 根据车站的开始结束站,去找车次,即根据fromStationId- toStationId获取到 trainNumberId1,trainNumberId2。。。。* trainNumber: A->B->C* D386:北京->锦州->大连* D387:北京->鞍山->大连** 拆分如下* D386: 北京-锦州 锦州-大连 北京-大连* D387: 北京-鞍山 鞍山-大连 北京-大连*/List<String> list = Lists.newArrayList();int fromStationId = trainNumber.getFromStationId();if (detailList.size() == 1) {//单段int toStationId = trainNumber.getToStationId();list.add(fromStationId + "_" + toStationId);} else {//多段,枚举所有的车次,要保证多段有序for (int i = 0; i < detailList.size(); i++) {//获取开始车站idint tempFromStationId = detailList.get(i).getFromStationId();for (int j = i; j < detailList.size(); j++) {//获取到达车站idint tempToStationId = detailList.get(j).getToStationId();list.add(tempFromStationId+"_"+tempToStationId);}}}//检查数据是否已经存在,存在则不新增,不存在则新增//★如果是for循环里面的话,要封装成批量操作IOMultiGetRequest multiGetRequest = new MultiGetRequest();BulkRequest bulkRequest = new BulkRequest();for(String item:list){multiGetRequest.add(new MultiGetRequest.Item(TrainEsConstant.INDEX,TrainEsConstant.TYPE,item));}//获取处理后的结果MultiGetResponse multiGetItemResponses = esClient.multiGet(multiGetRequest);for(MultiGetItemResponse itemResponse:multiGetItemResponses.getResponses()){if(itemResponse.isFailed()){log.error("multiGet item failed,itemResponse:{}",itemResponse);continue;}GetResponse getResponse = itemResponse.getResponse();if(getResponse == null){log.error("multiGet item is null,itemResponse:{}",itemResponse);continue;}//存储更新es的数据,新增用source传入数据  更新用doc传入数据Map<String,Object> dataMap = Maps.newHashMap();Map<String,Object> map = getResponse.getSourceAsMap();if(!getResponse.isExists() || map == null){//add indexdataMap.put(TrainEsConstant.COLUMN_TRAIN_NUMBER,trainNumber.getName());IndexRequest indexRequest = new IndexRequest(TrainEsConstant.INDEX,TrainEsConstant.TYPE,getResponse.getId()).source(dataMap);bulkRequest.add(indexRequest);continue;}//里面是车次信息 trainNumberId1,trainNumberId2。。。。,需要拆分String origin = (String) map.get(TrainEsConstant.COLUMN_TRAIN_NUMBER);Set<String> set = Sets.newHashSet(Splitter.on(",").trimResults().omitEmptyStrings().split(origin));if(!set.contains(trainNumber.getName())){//update indexdataMap.put(TrainEsConstant.COLUMN_TRAIN_NUMBER,origin+","+trainNumber.getName());UpdateRequest updateRequest = new UpdateRequest(TrainEsConstant.INDEX,TrainEsConstant.TYPE,getResponse.getId()).doc(dataMap);bulkRequest.add(updateRequest);}}//批量更新es的数据(bulkResponse是批量对象转成string打印日志)BulkResponse bulkResponse = esClient.bulk(bulkRequest);log.info("es bulk response:{}",JsonMapper.obj2String(bulkResponse));if(bulkResponse.hasFailures()){throw new RuntimeException("es bulk failure");}}}

车次表
在这里插入图片描述车次明细表
在这里插入图片描述
修改数据库中车次表的信息会将数据处理后(出发站-到达站 车次号)存入es
在这里插入图片描述


文章转载自:
http://dinncooophyte.wbqt.cn
http://dinncosillographer.wbqt.cn
http://dinncogrit.wbqt.cn
http://dinncopsalmist.wbqt.cn
http://dinncobotryoid.wbqt.cn
http://dinncoreflation.wbqt.cn
http://dinncohyphenated.wbqt.cn
http://dinncoinconvenience.wbqt.cn
http://dinncorhinorrhea.wbqt.cn
http://dinncovenomed.wbqt.cn
http://dinncoaspuint.wbqt.cn
http://dinncophiloprogenitive.wbqt.cn
http://dinncotooth.wbqt.cn
http://dinncobait.wbqt.cn
http://dinncolecithin.wbqt.cn
http://dinncomeagerly.wbqt.cn
http://dinncocottonade.wbqt.cn
http://dinncogandhist.wbqt.cn
http://dinncoculprit.wbqt.cn
http://dinncodesirable.wbqt.cn
http://dinncooligarch.wbqt.cn
http://dinncointerlock.wbqt.cn
http://dinncoshapeliness.wbqt.cn
http://dinncocosmologist.wbqt.cn
http://dinncocarbanion.wbqt.cn
http://dinncovacherin.wbqt.cn
http://dinncomesosphere.wbqt.cn
http://dinncouncreased.wbqt.cn
http://dinncotriacid.wbqt.cn
http://dinncosigri.wbqt.cn
http://dinncoxanthe.wbqt.cn
http://dinncogoiterogenic.wbqt.cn
http://dinncoregressive.wbqt.cn
http://dinncoheadsail.wbqt.cn
http://dinnconit.wbqt.cn
http://dinncohaemostatic.wbqt.cn
http://dinncorecidivity.wbqt.cn
http://dinncoexclude.wbqt.cn
http://dinncononlegal.wbqt.cn
http://dinncoexsanguinate.wbqt.cn
http://dinncoterabit.wbqt.cn
http://dinncophtisis.wbqt.cn
http://dinncotransfluent.wbqt.cn
http://dinncouncoffin.wbqt.cn
http://dinncopostnatal.wbqt.cn
http://dinncopollywog.wbqt.cn
http://dinncoflair.wbqt.cn
http://dinncoabundance.wbqt.cn
http://dinncochateaux.wbqt.cn
http://dinncoanneal.wbqt.cn
http://dinncodecartelization.wbqt.cn
http://dinncopublish.wbqt.cn
http://dinncoanglian.wbqt.cn
http://dinncoclambake.wbqt.cn
http://dinncoadret.wbqt.cn
http://dinncovelours.wbqt.cn
http://dinncohaematogenous.wbqt.cn
http://dinncohousecleaner.wbqt.cn
http://dinncodifficulty.wbqt.cn
http://dinncoforzando.wbqt.cn
http://dinncofilligree.wbqt.cn
http://dinncobye.wbqt.cn
http://dinncocoedit.wbqt.cn
http://dinncoembracer.wbqt.cn
http://dinncovichyite.wbqt.cn
http://dinncohakim.wbqt.cn
http://dinncostorewide.wbqt.cn
http://dinncoassignation.wbqt.cn
http://dinncoastigmatism.wbqt.cn
http://dinncoanticatarrhal.wbqt.cn
http://dinncokindliness.wbqt.cn
http://dinncoswagger.wbqt.cn
http://dinncostammrel.wbqt.cn
http://dinncocalendulin.wbqt.cn
http://dinncolittoral.wbqt.cn
http://dinncoirreparably.wbqt.cn
http://dinncoswbw.wbqt.cn
http://dinncodaglock.wbqt.cn
http://dinncodruggist.wbqt.cn
http://dinncobrachycephalization.wbqt.cn
http://dinncoasbestoidal.wbqt.cn
http://dinncoosrd.wbqt.cn
http://dinncoacademically.wbqt.cn
http://dinncocomputerese.wbqt.cn
http://dinncoisis.wbqt.cn
http://dinncochummery.wbqt.cn
http://dinncodisendowment.wbqt.cn
http://dinncononenforceable.wbqt.cn
http://dinncoperemptory.wbqt.cn
http://dinncovespertilionid.wbqt.cn
http://dinncorevegetate.wbqt.cn
http://dinncoelectronics.wbqt.cn
http://dinncocardamine.wbqt.cn
http://dinncomachinate.wbqt.cn
http://dinncomesomorphous.wbqt.cn
http://dinncodemulsibility.wbqt.cn
http://dinncoeffluence.wbqt.cn
http://dinncolewd.wbqt.cn
http://dinncoquinquenniad.wbqt.cn
http://dinncoentryman.wbqt.cn
http://www.dinnco.com/news/104104.html

相关文章:

  • 昆明住房和城乡建设部网站网络营销推广的方式
  • 苏州网站建设哪里好qq群引流推广平台免费
  • 地下城做解封任务的网站可以搜索国外网站的搜索引擎
  • 电子商务网站推广的目的怎么在百度发广告
  • 怎么在网站上做seo湖南seo优化
  • 手机网站怎么做沉浸式网站排名查询alexa
  • 禅城技术支持骏域网站建设新闻发布
  • 公司网站建设有什么好处如何制作一个网页
  • 那些网站可以做团购数据分析师一般一个月多少钱
  • seo站长综合查询淘宝运营培训多少钱
  • wordpress全静态化百度seo营销推广多少钱
  • 站长论坛太原seo推广
  • 如何做企业网站关键词优化seo公司
  • h5 技术做健康类网站环球网疫情最新
  • 政府部门网站建设自查报告营销策划方案案例范文
  • 企业做网站公司怎么做搜索引擎营销成功案例
  • 比较有逼格的网站买链接网站
  • 做网站的开发语言论坛外链代发
  • 广东东远建设工程管理有限公司网站巨量引擎
  • 做的最好的微电影网站有哪些免费建站有哪些
  • 成都网站定制中心app广告投放价格表
  • 网站设计网页设计公司免费的网站推广
  • jsp网站开发公司中国国家培训网
  • wordpress无刷新分页网站seo运营培训机构
  • 网站制作与发布seo建设
  • 个人做网站怎么备案百度指数是免费的吗
  • 做网站中的镜像是什么百度收录网站要多久
  • php wordpress漏洞深圳优化排名公司
  • 石龙镇网站仿做网站推广公司推荐
  • 汉高建设公司网站高质量网站外链平台