Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
jd_goods
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bigdata
jd_goods
Commits
41d6a445
Commit
41d6a445
authored
Jun 11, 2021
by
杨林
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
获取ES客户端
parent
0a654366
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
81 additions
and
0 deletions
+81
-0
src/main/scala/tools/es/ESClient.scala
src/main/scala/tools/es/ESClient.scala
+81
-0
No files found.
src/main/scala/tools/es/ESClient.scala
0 → 100644
View file @
41d6a445
package
tools.es
import
org.apache.http.HttpHost
import
org.elasticsearch.client.sniff.Sniffer
import
org.elasticsearch.client.
{
RestClient
,
RestClientBuilder
,
RestHighLevelClient
}
import
tools.properties.PropertiesTools
import
java.io.IOException
import
java.util.Properties
import
scala.collection.mutable.ArrayBuffer
/**
* @ClassName: ESClient
* @Description: 连接ES客户端
* @Create by: LinYang
* @Date: 2020/12/17 10:34
*/
object
ESClient
{
private
[
this
]
val
properties
:
Properties
=
PropertiesTools
.
getProperties
//主机和端口
private
[
this
]
val
hostsAndPorts
:
Array
[
String
]
=
properties
.
getProperty
(
"es.cluster"
).
split
(
","
)
private
[
this
]
var
highLevelClient
:
RestHighLevelClient
=
_
private
[
this
]
var
sniffer
:
Sniffer
=
_
// 构建客户端
private
[
this
]
def
esClient
:
RestClientBuilder
=
{
val
httpHosts
:
ArrayBuffer
[
HttpHost
]
=
new
ArrayBuffer
[
HttpHost
]()
if
(
0
!=
hostsAndPorts
.
length
)
{
for
(
elem
<-
hostsAndPorts
)
{
val
hostAndPort
:
Array
[
String
]
=
elem
.
split
(
":"
)
val
host
=
hostAndPort
(
0
)
val
port
=
hostAndPort
(
1
).
trim
.
toInt
val
httpHost
=
new
HttpHost
(
host
,
port
)
httpHosts
.+=(
httpHost
)
}
}
val
restClientBuilder
=
RestClient
.
builder
(
httpHosts
.
toArray
:
_
*
)
.
setFailureListener
(
new
RestClient
.
FailureListener
()
{
def
onFailure
(
node
:
Nothing
)
:
Unit
=
{
super
.
onFailure
(
node
)
}
})
// .setRequestConfigCallback((requestConfigBuilder: RequestConfig.Builder) => requestConfigBuilder.setSocketTimeout(10000))
restClientBuilder
}
/**
* 获取客户端
*
* @return 返回ES官方推荐的客户端
*/
def
getClient
:
RestHighLevelClient
=
{
highLevelClient
=
new
RestHighLevelClient
(
esClient
)
val
lowLevelClient
=
highLevelClient
.
getLowLevelClient
sniffer
=
Sniffer
.
builder
(
lowLevelClient
)
.
setSniffAfterFailureDelayMillis
(
10000
)
.
build
()
highLevelClient
}
/**
* 关闭客户端连接
*/
def
closeClient
()
:
Unit
=
{
if
(
null
!=
highLevelClient
)
{
try
{
sniffer
.
close
()
highLevelClient
.
close
()
}
catch
{
case
e
:
IOException
=>
e
.
printStackTrace
()
}
}
}
}
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