关于es及其大集群规模下的优化

es主要特点:

  1. 分布式的实时文件存储,每个字段都被索引并可被搜索
  2. 分布式的实时分析搜索引擎–做不规则查询
  3. 可以扩展到上百台服务器,处理 PB 级结构化或非结构化数据

es和其他数据存储对比

 

es基本操作不做介绍

远程向es中写数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
object MyESUtil {
    val esUrl = "http://hadoop201:9200"
    val factory = new JestClientFactory
    val conf: HttpClientConfig = new HttpClientConfig.Builder(esUrl)
        .multiThreaded(true)
        .maxTotalConnection(20)
        .connTimeout(10000)
        .readTimeout(10000)
        .build()
    factory.setHttpClientConfig(conf)
     
    // 获取客户端
    def getESClient = factory.getObject
     
    // 插入单条数据
    def insertSingle(indexName: String, source: Any) = {
        val client: JestClient = getESClient
        val index: Index = new Index.Builder(source)
            .`type`("_doc")
            .index(indexName)
            .build()
        client.execute(index)
        client.close()
    }
     
    // 插入多条数据 sources:   Iterable[(id, caseClass)] 或者 Iterable[caseClass]
    def insertBulk(indexName: String, sources: Iterator[Any]): Unit = {
        if (sources.isEmpty) return
         
        val client: JestClient = getESClient
        val bulkBuilder = new Bulk.Builder()
            .defaultIndex(indexName)
            .defaultType("_doc")
        sources.foreach { // 把所有的source变成action添加buck中
            //传入的是值是元组, 第一个表示id
            case (id: String, data) => bulkBuilder.addAction(new Index.Builder(data).id(id).build())
            // 其他类型 没有id, 将来省的数据会自动生成默认id
            case data => bulkBuilder.addAction(new Index.Builder(data).build())
        }
        client.execute(bulkBuilder.build())
        closeClient(client)
    }
     
    def main(args: Array[String]): Unit = {
        //        insertSingle("user", User("a", 20))
        insertBulk("user", Iterator(User("aa", 20), User("bb", 30)))
         
    }
     
    /**
      * 关闭客户端
      *
      * @param client
      */
    def closeClient(client: JestClient) = {
        if (client != null) {
            try {
                client.shutdownClient()
            } catch {
                case e => e.printStackTrace()
            }
        }
    }
     
}
case class User(name: String, age: Int)

  

es千台集群优化方案(转载腾讯)

https://mp.weixin.qq.com/s/FeqdB-rT1vmde7IVpEA1dw

 

posted @   七寸青衫  阅读(486)  评论(0编辑  收藏  举报
编辑推荐:
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
阅读排行:
· 本地部署 DeepSeek:小白也能轻松搞定!
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 从 Windows Forms 到微服务的经验教训
· 李飞飞的50美金比肩DeepSeek把CEO忽悠瘸了,倒霉的却是程序员
· 超详细,DeepSeek 接入PyCharm实现AI编程!(支持本地部署DeepSeek及官方Dee
loading: { rebound: { tension: 16, }, spinner: { id: 'spinner', radius: 90, } }
点击右上角即可分享
微信分享提示