Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
S
streamingUserPortrait
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
赵威
streamingUserPortrait
Commits
233f109e
Commit
233f109e
authored
Feb 04, 2021
by
赵威
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
try es search
parent
eb5ecc8a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
85 additions
and
20 deletions
+85
-20
build.sbt
build.sbt
+2
-2
ES.scala
src/main/scala/com.gmei.up/utils/ES.scala
+83
-18
No files found.
build.sbt
View file @
233f109e
...
...
@@ -29,9 +29,9 @@ lazy val root = (project in file("."))
name
:=
"streamingUserPortrait"
,
libraryDependencies
+=
"com.alibaba"
%
"fastjson"
%
"1.2.75"
,
// libraryDependencies += esCore,
// libraryDependencies += esJava,
libraryDependencies
+=
"com.sksamuel.elastic4s"
%%
"elastic4s-http"
%
"6.7.8"
,
libraryDependencies
+=
esCore
,
libraryDependencies
+=
esJava
,
// libraryDependencies += es,
// libraryDependencies += esCore,
...
...
src/main/scala/com.gmei.up/utils/ES.scala
View file @
233f109e
package
com.gmei.up.utils
import
scala.concurrent._
import
scala.concurrent.duration._
import
ExecutionContext.Implicits.global
import
com.sksamuel.elastic4s.http.
{
ElasticClient
,
ElasticProperties
,
Response
}
import
com.sksamuel.elastic4s.http.search.SearchResponse
import
com.sksamuel.elastic4s.http.ElasticDsl._
import
com.sksamuel.elastic4s.ElasticClient
import
com.sksamuel.elastic4s.http.JavaClient
import
org.apache.http.HttpHost
import
org.apache.http.auth.
{
AuthScope
,
UsernamePasswordCredentials
}
import
org.apache.http.impl.client.BasicCredentialsProvider
import
org.apache.http.impl.nio.client.HttpAsyncClientBuilder
import
org.elasticsearch.client.RestClient
import
org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback
object
ES
{
// TODO read config from file
val
host
=
"172.16.52.33"
val
port
=
9200
val
username
=
"elastic"
val
password
=
"gengmei!@#"
val
diaryIndex
=
"gm-dbmw-diary-read"
val
nodes
=
ElasticProperties
(
"http://172.16.52.33:9200,http://172.16.52.19:9200,http://172.16.52.27:9200,http://172.16.52.34:9200,http://172.16.52.48:9200"
)
val
client
=
ElasticClient
(
nodes
)
// import scala.concurrent._
// import scala.concurrent.duration._
// import ExecutionContext.Implicits.global
// import com.sksamuel.elastic4s.ElasticsearchClientUri
// import com.sksamuel.elastic4s.http.{ ElasticClient, ElasticProperties, Response }
// import com.sksamuel.elastic4s.http.search.SearchResponse
import
com.sksamuel.elastic4s.ElasticDsl._
// import org.apache.http.client.config.RequestConfig
// import org.apache.http.impl.client.BasicCredentialsProvider
// import org.apache.http.auth.{ AuthScope, UsernamePasswordCredentials }
// import org.apache.http.impl.nio.client.HttpAsyncClientBuilder
// import org.elasticsearch.client.RestClientBuilder.{ HttpClientConfigCallback, RequestConfigCallback }
// // object ES {
// // // TODO read config from file
// // val host = "172.16.52.33"
// // val port = 9200
// // val username = "elastic"
// // val password = "gengmei!@#"
// // val diaryIndex = "gm-dbmw-diary-read"
// // // val nodes = ElasticProperties(
// // // "http://172.16.52.33:9200,http://172.16.52.19:9200,http://172.16.52.27:9200,http://172.16.52.34:9200,http://172.16.52.48:9200"
// // // )
// // // val client = ElasticClient(nodes)
// // def test() {
// // val json =
// // """
// // |{
// // | "query" : {
// // | "match" : {"tags_v3" : ["光子嫩肤", "水光针"]}
// // | }
// // |}
// // |""".stripMargin
// // val resp = client.execute {
// // search("gm-dbmw-diary-read").source(json)
// // }.await
// // println(resp)
// // resp
// // }
// // }
object
ESClient
{
private
class
ElasticCredentials
(
username
:
String
,
password
:
String
)
extends
HttpClientConfigCallback
{
val
credentials
=
new
UsernamePasswordCredentials
(
username
,
password
)
val
credentialsProvider
=
new
BasicCredentialsProvider
()
credentialsProvider
.
setCredentials
(
AuthScope
.
ANY
,
credentials
)
override
def
customizeHttpClient
(
httpClientBuilder
:
HttpAsyncClientBuilder
)
:
HttpAsyncClientBuilder
=
httpClientBuilder
.
setDefaultCredentialsProvider
(
credentialsProvider
)
}
def
create
(
hostname
:
String
,
port
:
Int
,
username
:
String
,
password
:
String
)
:
ElasticClient
=
{
val
restClient
=
RestClient
.
builder
(
new
HttpHost
(
hostname
,
port
,
"http"
))
.
setHttpClientConfigCallback
(
new
ElasticCredentials
(
username
,
password
))
.
build
()
ElasticClient
(
JavaClient
.
fromRestClient
(
restClient
))
}
}
object
ES
{
def
test
()
{
val
client
=
ESClient
.
create
(
"172.16.52.33"
,
9200
,
"elastic"
,
"gengmei!@#"
)
val
json
=
"""
|{
...
...
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