elasticsearch 口水篇(3)java客户端 - Jest

elasticsearch有丰富的客户端,java客户端有Jest。其原文介绍如下:

Jest is a Java HTTP Rest client for ElasticSearch.It is actively developed and tested by Searchly.

A sample Java application using Jest can be found on GitHub  https://github.com/searchbox-io/java-jest-sample.

[http://www.searchly.com/documentation/developer-api-guide/java-jest/]

 

下面我们做一个很简单的实例,以下几个功能:

1)批量添加1000个user对象;

2)通过id进行查询;

3)通过name进行查询;

 

user对象定义:

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
package  com.fox.c1;
 
import  io.searchbox.annotations.JestId;
 
/**
  * @author huangfox
  * @date 2014年1月22日 下午5:31:52
  *
  */
public  class  User {
     @JestId
     private  Long id;
     private  String name;
     private  int  age;
 
     public  Long getId() {
         return  id;
     }
 
     public  void  setId(Long id) {
         this .id = id;
     }
 
     public  String getName() {
         return  name;
     }
 
     public  void  setName(String name) {
         this .name = name;
     }
 
     public  int  getAge() {
         return  age;
     }
 
     public  void  setAge( int  age) {
         this .age = age;
     }
 
}

  

简单实现一个工厂类,用来获得JestHttpClient,如下:

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
package  com.fox.c1;
 
import  io.searchbox.client.JestClientFactory;
import  io.searchbox.client.config.HttpClientConfig;
import  io.searchbox.client.http.JestHttpClient;
 
/**
  * @author huangfox
  * @date 2014年1月22日 下午5:16:15
  *
  */
public  class  ESFactory {
     private  static  JestHttpClient client;
 
     private  ESFactory() {
 
     }
 
     public  synchronized  static  JestHttpClient getClient() {
         if  (client ==  null ) {
             JestClientFactory factory =  new  JestClientFactory();
             factory.setHttpClientConfig( new  HttpClientConfig.Builder(
                     "http://localhost:9200" ).multiThreaded( true ).build());
             client = (JestHttpClient) factory.getObject();
         }
         return  client;
     }
 
     public  static  void  main(String[] args) {
         JestHttpClient client = ESFactory.getClient();
         System.out.println(client.getAsyncClient());
         System.out.println(client.getServers());
         client.shutdownClient();
     }
}

  

测试类:

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package  com.fox.c1;
 
import  io.searchbox.client.JestResult;
import  io.searchbox.client.http.JestHttpClient;
import  io.searchbox.core.Get;
import  io.searchbox.core.Index;
import  io.searchbox.core.Search;
import  io.searchbox.indices.CreateIndex;
import  io.searchbox.indices.DeleteIndex;
 
import  java.io.IOException;
 
import  org.elasticsearch.index.query.QueryBuilders;
import  org.elasticsearch.search.builder.SearchSourceBuilder;
 
/**
  * @author huangfox
  * @date 2014年1月22日 下午5:32:20
  *
  */
public  class  ESTest {
 
     private  JestHttpClient client = ESFactory.getClient();
 
     /**
      * indexing
      *
      * @param indexName
      */
     public  void  index(String indexName) {
         try  {
             // drop
             DeleteIndex dIndex =  new  DeleteIndex( new  DeleteIndex.Builder(
                     indexName));
             client.execute(dIndex);
             // create
             CreateIndex cIndex =  new  CreateIndex( new  CreateIndex.Builder(
                     indexName));
             client.execute(cIndex);
             // add doc
             for  ( int  i =  0 ; i <  1000 ; i++) {
 
                 User user =  new  User();
                 user.setId( new  Long(i));
                 user.setName( "huang fox "  + i);
                 user.setAge(i %  100 );
                 Index index =  new  Index.Builder(user).index(indexName)
                         .type(indexName).build();
                 client.execute(index);
             }
             //
             client.shutdownClient();
         catch  (IOException e) {
             e.printStackTrace();
         }
     }
 
     /**
      * getting by id
      *
      * @param indexName
      * @param query
      */
     public  void  get(String indexName, String query) {
         Get get =  new  Get.Builder(indexName, query).build();
         try  {
             JestResult rs = client.execute(get);
             System.out.println(rs.getJsonString());
             //
             client.shutdownClient();
         catch  (IOException e) {
             e.printStackTrace();
         }
     }
 
     /**
      * query
      *
      * @param query
      */
     public  void  search(String query) {
         SearchSourceBuilder searchSourceBuilder =  new  SearchSourceBuilder();
         searchSourceBuilder.query(QueryBuilders.queryString(query));
         searchSourceBuilder.field( "name" );
         Search search =  new  Search.Builder(searchSourceBuilder.toString())
                 .build();
         try  {
             JestResult rs = client.execute(search);
             System.out.println(rs.getJsonString());
             //
             client.shutdownClient();
         catch  (IOException e) {
             e.printStackTrace();
         }
     }
 
     public  static  void  main(String[] args) {
         ESTest t =  new  ESTest();
         String indexName =  "user" ;
         // indexing
         t.index(indexName);
         // getting by id
         t.get(indexName,  "2" );
         // query
         String query =  "fox" ;
         t.search(query);
     }
 
}

  

index 批量添加建议使用bulk,效率高(减少通讯次数):

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
public  void  indexBulk(String indexName) {
         try  {
             // drop
             DeleteIndex dIndex =  new  DeleteIndex( new  DeleteIndex.Builder(
                     indexName));
             client.execute(dIndex);
             // create
             CreateIndex cIndex =  new  CreateIndex( new  CreateIndex.Builder(
                     indexName));
             client.execute(cIndex);
             // add doc
             Bulk.Builder bulkBuilder =  new  Bulk.Builder();
             for  ( int  i =  0 ; i <  1000 ; i++) {
 
                 User user =  new  User();
                 user.setId( new  Long(i));
                 user.setName( "huang fox "  + i);
                 user.setAge(i %  100 );
                 Index index =  new  Index.Builder(user).index(indexName)
                         .type(indexName).build();
                 bulkBuilder.addAction(index);
             }
             client.execute(bulkBuilder.build());
             //
             client.shutdownClient();
         catch  (IOException e) {
             e.printStackTrace();
         }
     }

  

 

 原文地址:http://www.cnblogs.com/huangfox/p/3542858.html

 

 

06-22
### 得物技术栈及开发者文档分析 得物作为一家专注于潮流商品的电商平台,其技术栈和开发者文档主要围绕电商平台的核心需求展开。以下是对得物技术栈及相关开发资源的详细解析: #### 1. 技术栈概述 得物的技术栈通常会涵盖前端、后端、移动应用开发以及大数据处理等多个领域。以下是可能涉及的主要技术栈[^3]: - **前端开发**: 前端技术栈可能包括现代框架如 React 或 Vue.js,用于构建高效、响应式的用户界面。此外,还会使用 Webpack 等工具进行模块化打包和优化。 - **后端开发**: 后端技术栈可能采用 Java Spring Boot 或 Node.js,以支持高并发和分布式架构。数据库方面,MySQL 和 Redis 是常见的选择,分别用于关系型数据存储和缓存管理。 - **移动应用开发**: 得物的移动应用开发可能基于原生技术(如 Swift/Kotlin)或跨平台框架(如 Flutter)。这有助于确保移动端应用的性能和用户体验一致性。 - **大数据云计算**: 在大数据处理方面,得物可能会使用 Hadoop 或 Spark 进行数据挖掘和分析。同时,依托云服务提供商(如阿里云或腾讯云),实现弹性扩展和资源优化。 #### 2. 开发者文档分析 类似于引用中提到的 Adobe 开发者文档模板[^2],得物也可能提供一套完整的开发者文档体系,以支持内部团队协作和外部开发者接入。以下是开发者文档可能包含的内容: - **API 文档**: 提供 RESTful API 或 GraphQL 的详细说明,帮助开发者快速集成得物的功能模块,例如商品搜索、订单管理等。 - **SDK 集成指南**: 针对不同平台(如 iOS、Android 或 Web)提供 SDK 下载和集成教程,简化第三方应用的开发流程。 - **技术博客**: 分享得物在技术实践中的经验成果,例如如何优化图片加载速度、提升应用性能等。 - **开源项目**: 得物可能将部分技术成果开源,供社区开发者学习和贡献。这不仅有助于提升品牌形象,还能吸引更多优秀人才加入。 #### 3. 示例代码 以下是一个简单的示例代码,展示如何通过 RESTful API 调用得物的商品搜索功能(假设接口已存在): ```python import requests def search_items(keyword, page=1): url = "https://api.dewu.com/v1/items/search" headers = { "Authorization": "Bearer YOUR_ACCESS_TOKEN", "Content-Type": "application/json" } params = { "keyword": keyword, "page": page, "size": 10 } response = requests.get(url, headers=headers, params=params) if response.status_code == 200: return response.json() else: return {"error": "Failed to fetch data"} # 调用示例 result = search_items("Air Jordan", page=1) print(result) ``` 此代码片段展示了如何通过 Python 请求得物的 API,并获取指定关键词的商品列表。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值