?一、初始化MYSQL數(shù)據(jù)
public boolean initMysql() throws Exception { log.info("initMysql.start"); //獲取所連接的數(shù)據(jù)庫(kù)名稱(chēng) ? String database = systemMapper.getDatabase(); if (StringUtils.isBlank(database)) { throw new BusinessException("連接數(shù)據(jù)庫(kù)失敗,數(shù)據(jù)庫(kù)不存在"); } //當(dāng)庫(kù)中沒(méi)有表、則執(zhí)行sql腳本 if (systemMapper.countTable(database) == 0) { SqlSession sqlSession = sqlSessionFactory.openSession(); Connection conn = sqlSession.getConnection(); ? String mysqlInitPath="config/mysql/init.sql"; ClassPathResource rc = new ClassPathResource(mysqlInitPath); EncodedResource er = new EncodedResource(rc, "utf-8"); ScriptUtils.executeSqlScript(conn, er); log.info("initMysql.db:" + database + ".end"); } log.info("initMysql.end"); return true; }
其中SystemMapper為
@Mapper public interface SystemMapper { ? //獲得當(dāng)前數(shù)據(jù)庫(kù)表的數(shù)量 @Select("select count(*) from information_schema.TABLES where TABLE_SCHEMA= #{schema}") int countTable(@Param("schema")String schema); ? //===獲得當(dāng)前連接的數(shù)據(jù)庫(kù)名稱(chēng) @Select("select database()") String getDatabase(); }
二、初始化ES數(shù)據(jù)
public boolean initEs() throws Exception { log.info("initEs.start"); // 讀取配置 String artInfoMappingPath =config/es/art_info_index_mapping.json String artInfoMapping = ResourceUtil.readStr(artInfoMappingPath, StandardCharsets.UTF_8); // 創(chuàng)建博文索引 String esIndex="wechat" createIndexIfNotExist(esIndex, artInfoMapping); // 創(chuàng)建別名 ? ?String?alias="art_info"; ? addAlias(alias, esIndex); ? return true; }
?如果索引不存在就創(chuàng)建es索引
private void createIndexIfNotExist(String index, String mapping) { // 判斷索引存不存在 if (StrUtil.isBlank(index) || esAggregateService.indexExist(index)) { return; } log.info("initEs --> index: {}", index); // 創(chuàng)建索引 CreateIndexRequest request = new CreateIndexRequest(index); Settings.Builder settings = Settings.builder() .put("max_result_window", 100000); request.settings(settings); request.mapping(mapping, XContentType.JSON); createIndex(request); ?????}
?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-653921.html
public void createIndex(CreateIndexRequest request) { try { CreateIndexResponse response = elasticSearchClient.indices().create(request, RequestOptions.DEFAULT); log.info("create index: {}, isAcknowledged: {}", response.index(), response.isAcknowledged()); } catch (IOException e) { log.error("ES 索引創(chuàng)建失敗 --> ", e); throw new BusinessException(ResultCode.INTERNAL_SERVER_ERROR); } }
?根據(jù)需要、添加es別名文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-653921.html
@Override public void addAlias(String alias, String... index) { // 構(gòu)建請(qǐng)求參數(shù) IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest(); IndicesAliasesRequest.AliasActions aliasActions = new IndicesAliasesRequest.AliasActions(IndicesAliasesRequest.AliasActions.Type.ADD) .indices(index) .alias(alias); indicesAliasesRequest.addAliasAction(aliasActions); try { AcknowledgedResponse response = elasticSearchClient.indices().updateAliases(indicesAliasesRequest, RequestOptions.DEFAULT); log.info("add alias --> index: {}, alias: {}, isAcknowledged: {}", index, alias, response.isAcknowledged()); } catch (IOException e) { log.error("ES 別名創(chuàng)建失敗 --> ", e); throw new BusinessException(ResultCode.INTERNAL_SERVER_ERROR); } }
到了這里,關(guān)于Java項(xiàng)目初始化ES、MYSQL表結(jié)構(gòu)及表數(shù)據(jù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!