Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
S
shop
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
曲欣亮
shop
Commits
1cec9af7
Commit
1cec9af7
authored
Sep 17, 2019
by
Quxl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
x
parent
7017c98f
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
173 additions
and
0 deletions
+173
-0
create-index-query.sql
sql/goods/create-index-query.sql
+0
-0
delete-index-query.sql
sql/goods/delete-index-query.sql
+0
-0
update-index-query.sql
sql/goods/update-index-query.sql
+0
-0
AbstractSolrApi.java
src/main/java/com/egolm/shop/schedules/AbstractSolrApi.java
+81
-0
SolrApi.java
src/main/java/com/egolm/shop/schedules/SolrApi.java
+19
-0
GoodsCollectTask.java
...java/com/egolm/shop/schedules/goods/GoodsCollectTask.java
+73
-0
No files found.
sql/goods/create-index-query.sql
0 → 100644
View file @
1cec9af7
sql/goods/delete-index-query.sql
0 → 100644
View file @
1cec9af7
sql/goods/update-index-query.sql
0 → 100644
View file @
1cec9af7
src/main/java/com/egolm/shop/schedules/AbstractSolrApi.java
0 → 100644
View file @
1cec9af7
package
com
.
egolm
.
shop
.
schedules
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
import
org.apache.solr.client.solrj.impl.HttpSolrClient
;
import
org.apache.solr.common.SolrInputDocument
;
import
com.egolm.common.StringUtil
;
import
com.egolm.shop.config.XException
;
import
com.google.common.collect.Lists
;
public
abstract
class
AbstractSolrApi
implements
SolrApi
{
public
abstract
HttpSolrClient
getSolrClient
();
public
void
update
(
List
<
Map
<
String
,
Object
>>
list
)
{
HttpSolrClient
solrClient
=
getSolrClient
();
try
{
List
<
SolrInputDocument
>
docs
=
new
ArrayList
<
SolrInputDocument
>();
for
(
Map
<
String
,
Object
>
map
:
list
)
{
SolrInputDocument
solrInputDocument
=
new
SolrInputDocument
();
for
(
String
key
:
map
.
keySet
())
{
if
(
key
!=
null
&&
key
.
trim
().
length
()
>
0
)
{
solrInputDocument
.
addField
(
key
,
StringUtil
.
toString
(
map
.
get
(
key
)));
}
}
docs
.
add
(
solrInputDocument
);
}
List
<
List
<
SolrInputDocument
>>
doclist
=
Lists
.
partition
(
docs
,
1000
);
for
(
List
<
SolrInputDocument
>
tmp
:
doclist
)
{
solrClient
.
add
(
tmp
);
}
solrClient
.
commit
();
}
catch
(
Exception
e
)
{
try
{
solrClient
.
rollback
();
}
catch
(
Exception
ex
)
{
logger
.
error
(
"重建索引异常回滚失败"
,
ex
);
}
throw
new
XException
(
"重建索引失败"
,
e
);
}
}
@Override
public
void
deleteById
(
List
<
String
>
documentIds
)
{
HttpSolrClient
solrClient
=
getSolrClient
();
try
{
if
(
documentIds
!=
null
&&
documentIds
.
size
()
>
0
)
{
solrClient
.
deleteById
(
documentIds
);
solrClient
.
commit
();
}
}
catch
(
Exception
e
)
{
try
{
solrClient
.
rollback
();
}
catch
(
Exception
ex
)
{
logger
.
error
(
"删除索引异常回滚失败"
,
ex
);
}
throw
new
XException
(
"删除索引失败"
,
e
);
}
}
@Override
public
void
clear
()
{
HttpSolrClient
solrClient
=
getSolrClient
();
try
{
solrClient
.
deleteByQuery
(
"*"
);
solrClient
.
commit
();
}
catch
(
Exception
e
)
{
try
{
solrClient
.
rollback
();
}
catch
(
Exception
ex
)
{
logger
.
error
(
"删除全部索引异常回滚失败"
,
ex
);
}
throw
new
XException
(
"删除全部索引失败"
,
e
);
}
}
}
src/main/java/com/egolm/shop/schedules/SolrApi.java
0 → 100644
View file @
1cec9af7
package
com
.
egolm
.
shop
.
schedules
;
import
java.util.List
;
import
java.util.Map
;
import
org.apache.log4j.Logger
;
public
interface
SolrApi
{
Logger
logger
=
Logger
.
getLogger
(
SolrApi
.
class
);
public
void
update
(
List
<
Map
<
String
,
Object
>>
datas
);
public
void
deleteById
(
List
<
String
>
idList
);
public
void
clear
();
}
src/main/java/com/egolm/shop/schedules/goods/GoodsCollectTask.java
0 → 100644
View file @
1cec9af7
package
com
.
egolm
.
shop
.
schedules
.
goods
;
import
java.util.Date
;
import
java.util.stream.Collectors
;
import
org.apache.solr.client.solrj.impl.HttpSolrClient
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Qualifier
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.scheduling.annotation.Scheduled
;
import
org.springframework.stereotype.Component
;
import
com.egolm.common.jdbc.JdbcTemplate
;
import
com.egolm.shop.schedules.AbstractSolrApi
;
@Component
public
class
GoodsCollectTask
extends
AbstractSolrApi
{
@Autowired
JdbcTemplate
jdbcTemplate
;
@Autowired
@Qualifier
(
"solrClient"
)
HttpSolrClient
solrClient
;
@Value
(
"${solr.goods.offset-minute-for-day}"
)
private
Long
offsetMinuteForDay
;
@Value
(
"${solr.goods.offset-minute-for-minute}"
)
private
Long
offsetMinuteForMinute
;
@Value
(
"${sql.goods.create-index-query}"
)
private
String
createIndexQueryFilePath
;
@Value
(
"${sql.goods.delete-index-query}"
)
private
String
deleteIndexQueryFilePath
;
@Value
(
"${sql.goods.update-index-query}"
)
private
String
updateIndexQueryFilePath
;
private
String
createIndexQuerySql
;
private
String
updateIndexQuerySql
;
private
String
deleteIndexQuerySql
;
@Scheduled
(
cron
=
"${solr.goods.cron-for-day}"
)
public
void
dayExecute
()
{
Date
date
=
new
Date
(
System
.
currentTimeMillis
()
-
(
offsetMinuteForDay
*
60L
*
1000L
));
this
.
updateIndex
(
date
);
}
@Scheduled
(
cron
=
"${solr.goods.cron-for-minute}"
)
public
void
minuteExecute
()
{
Date
date
=
new
Date
(
System
.
currentTimeMillis
()
-
(
offsetMinuteForMinute
*
60L
*
1000L
));
this
.
updateIndex
(
date
);
}
public
void
createIndex
()
{
this
.
clear
();
this
.
update
(
jdbcTemplate
.
queryForList
(
createIndexQuerySql
));
}
public
void
updateIndex
(
Date
date
)
{
this
.
deleteById
(
jdbcTemplate
.
queryForList
(
deleteIndexQuerySql
,
date
).
stream
().
map
(
o
->
String
.
valueOf
(
o
.
get
(
"id"
))).
collect
(
Collectors
.
toList
()));
this
.
update
(
jdbcTemplate
.
queryForList
(
updateIndexQuerySql
,
date
));
}
@Override
public
HttpSolrClient
getSolrClient
()
{
return
solrClient
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment