当前位置: 代码迷 >> 综合 >> Elastic Stack核心技术实战03--Elasticsearch中的JavaAPI详解
  详细解决方案

Elastic Stack核心技术实战03--Elasticsearch中的JavaAPI详解

热度:42   发布时间:2023-12-25 18:51:36.0

   今天我们来学习一下怎么使用Java语言去操作Elasticsearch,好了,首先我们打开idea,新建一个项目,

接下来我们引入相关的坐标:

   <dependencies><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.5</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpmime</artifactId><version>4.3.6</version></dependency><dependency><groupId>io.searchbox</groupId><artifactId>jest</artifactId><version>5.3.3</version></dependency><dependency><groupId>net.java.dev.jna</groupId><artifactId>jna</artifactId><version>4.5.2</version></dependency><dependency><groupId>org.codehaus.janino</groupId><artifactId>commons-compiler</artifactId><version>2.7.8</version></dependency><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>6.3.1</version></dependency></dependencies>

 也没啥好说的,下面我们来写个demo:

public class Demo {public static void main(String[] args) {//1.创建ES客户端连接池JestClientFactory factory = new JestClientFactory();//2.创建ES客户端连接地址HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://192.168.137.93:9200").build();//3.设置ES连接地址factory.setHttpClientConfig(httpClientConfig);//4.获取ES客户端连接JestClient jestClient = factory.getObject();//5.构建ES插入数据对象Index index = new Index.Builder("{\n" +"  \"name\":\"李莫愁\",\n" +"  \"sex\":0,\n" +"  \"age\":35,\n" +"  \"force\":8.5,\n" +"  \"girlfriend\":\"杨过\",\n" +"  \"novel\":\"倚天屠龙记\",\n" +"  \"hobby\":\"玉女剑法、少林罗汉拳、黯然销魂拳\",\n" +"  \"home\":\"昆仑山\"\n" +"}").index("martial_arts").type("_doc").id("00012").build();//6.执行插入数据操作try {jestClient.execute(index);} catch (IOException e) {e.printStackTrace();}//7.关闭连接jestClient.shutdownClient();}
}

好了,我们运行之后就可以回到我们的Kibana中查询一下, 

说明数据已经插入进去了,这里我们使用Java代码将一个json字符串通过HTTP协议发送到elasticsearch中,其实我们仔细看一下Index对象的Builder方法,接受的类型是一个对象,如下图所示:

好吧,既然是个对象的话,那我们是不是就可以创建一个对象,然后将数据封装在对象中传到es中去呢,这样我们就不需要拼接字符串了。下面我们来试试,先创建一个对象:

public class MartialPojo {private String name;private String sex;private String age;private String force;private String girlfriend;private String novel;private String hobby;private String home;public MartialPojo(String name, String sex, String age, String force, String girlfriend, String novel, String hobby, String home) {this.name = name;this.sex = sex;this.age = age;this.force = force;this.girlfriend = girlfriend;this.novel = novel;this.hobby = hobby;this.home = home;}
}public class Demo {public static void main(String[] args) {//1.创建ES客户端连接池JestClientFactory factory = new JestClientFactory();//2.创建ES客户端连接地址HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://192.168.137.93:9200").build();//3.设置ES连接地址factory.setHttpClientConfig(httpClientConfig);//4.获取ES客户端连接JestClient jestClient = factory.getObject();//创建对象MartialPojo martialPojo = new MartialPojo("小龙女","0", "0", "8.0", "杨过", "倚天屠龙记","玉女心经、玉女剑法,弹指神功","古墓,襄阳");//5.构建ES插入数据对象
//        Index index = new Index.Builder(
//                "{\n" +
//                "  \"name\":\"李莫愁\",\n" +
//                "  \"sex\":0,\n" +
//                "  \"age\":35,\n" +
//                "  \"force\":8.5,\n" +
//                "  \"girlfriend\":\"杨过\",\n" +
//                "  \"novel\":\"倚天屠龙记\",\n" +
//                "  \"hobby\":\"玉女剑法、少林罗汉拳、黯然销魂拳\",\n" +
//                "  \"home\":\"昆仑山\"\n" +
//                "}").index("martial_arts").type("_doc").id("00012").build();Index index = new Index.Builder(martialPojo).index("martial_arts").type("_doc").id("00011").build();//6.执行插入数据操作try {jestClient.execute(index);} catch (IOException e) {e.printStackTrace();}//7.关闭连接jestClient.shutdownClient();}
}

 然后我们再回到kibana查询一下:

发现小龙女这条数据已经更新进去了。好了上面就是一个简单的数据导入进去的demo。下面我们来学习一怎么进行批量导入。

看下面这段代码:

public class BatchImportDemo {public static void main(String[] args) throws IOException {JestClientFactory factory = new JestClientFactory();HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://192.168.137.93:9200").build();factory.setHttpClientConfig(httpClientConfig);JestClient jestClient = factory.getObject();MartialPojo s1 = new MartialPojo("殷素素","0", "0", "8.0", "张翠山", "倚天屠龙记","弹指神功","昆仑山");MartialPojo s2 = new MartialPojo("张翠山","1", "0", "8.0", "殷素素", "倚天屠龙记","武当剑法、太极剑法,弹指神功","武当");MartialPojo s3 = new MartialPojo("灭绝师太","0", "0", "8.0", "成昆", "倚天屠龙记","峨嵋剑法","峨眉");MartialPojo s4 = new MartialPojo("张三丰","1", "0", "9.9", "郭襄", "倚天屠龙记","太极","武当");MartialPojo s5 = new MartialPojo("郭襄","0", "0", "8.0", "杨过", "倚天屠龙记","九阴真经,弹指神功","襄阳");//4.创建Bulk对象Bulk bulk = new Bulk.Builder().defaultIndex("martial_arts").defaultType("_doc").addAction(new Index.Builder(s1).id("1014").build()).addAction(new Index.Builder(s2).id("1015").build()).addAction(new Index.Builder(s3).id("1016").build()).addAction(new Index.Builder(s4).id("1017").build()).addAction(new Index.Builder(s5).id("1018").build()).build();//5.批量写入jestClient.execute(bulk);//6.关闭连接jestClient.shutdownClient();}
}

执行以下就会发先,此时5条数据已经导入到es中了,好了数据的写入我们已经学习的差不多了,下面我们来看看怎么使用JavaAPI 去实现查询的功能。

public class ReadDemo {public static void main(String[] args) throws IOException {//1.创建ES客户端连接池JestClientFactory factory = new JestClientFactory();//2.创建ES客户端连接地址HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://192.168.137.93:9200").build();//3.设置ES连接地址factory.setHttpClientConfig(httpClientConfig);//4.获取ES客户端连接JestClient jestClient = factory.getObject();//5.构建查询数据对象Search search = new Search.Builder("{\n" +"  \"query\":{\n" +"    \"bool\":{\n" +"      \"filter\":{\n" +"        \"term\":{\n" +"          \"novel\" : \"倚天屠龙记\"\n" +"        }\n" +"        \n" +"      }\n" +"    }\n" +"  }\n" +"}").addIndex("martial_arts").addType("_doc").build();//6.执行查询操作SearchResult searchResult = jestClient.execute(search);//7.解析查询结果System.out.println(searchResult.getTotal());List<SearchResult.Hit<MartialPojo, Void>> hits = searchResult.getHits(MartialPojo.class);for (SearchResult.Hit<MartialPojo, Void> hit : hits) {System.out.println(hit.source);}//8.关闭连接jestClient.shutdownClient();}
}

运行上述的代码,结果如下图所示:

  

可以看到查出来了倚天屠龙记里面的的角色。这就是一个简单的查询,通过Java程序向es发送查询语句来查询。大家也许发现了,再程序里拼接json字符串实在是很难受,而且也不优雅,好了,看下面的代码:

  public static void main(String[] args) throws IOException {//1.创建ES客户端连接池JestClientFactory factory = new JestClientFactory();//2.创建ES客户端连接地址HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://192.168.137.93:9200").build();//3.设置ES连接地址factory.setHttpClientConfig(httpClientConfig);//4.获取ES客户端连接JestClient jestClient = factory.getObject();SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();//创建QueryBuilder对象BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();boolQueryBuilder.filter(new TermQueryBuilder("novel", "倚天屠龙记"));searchSourceBuilder.query(boolQueryBuilder);searchSourceBuilder.from(2);searchSourceBuilder.size(4);//3.创建Search对象Search search = new Search.Builder(searchSourceBuilder.toString()).addIndex("martial_arts").addType("_doc").build();//6.执行查询操作SearchResult searchResult = jestClient.execute(search);//7.解析查询结果System.out.println(searchResult.getTotal());List<SearchResult.Hit<MartialPojo, Void>> hits = searchResult.getHits(MartialPojo.class);for (SearchResult.Hit<MartialPojo, Void> hit : hits) {System.out.println(hit.source);}//8.关闭连接jestClient.shutdownClient();}
}

执行之后发先也是可以的,而且还有一个分页的功能。,如下图所示:

好了,关于es的JavaApi读写操作就先给大家介绍到这里了。

 

 

 

 

  相关解决方案