Elasticsearch 7.3 SQL新特性在Java API中的应用解析?

2026-05-05 17:341阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计381个文字,预计阅读时间需要2分钟。

Elasticsearch 7.3 SQL新特性在Java API中的应用解析?

1. ES7 SQL新特性:数据准备、创建索引及映射、建立价格、颜色、品牌、销售日期等四个字段

2.PUT /tvs

3.PUT /tvs/_mapping

{ properties: { price: { type: long }, color: { type: keyword }, brand: { type: text }, sale_date: { type: date } } }

一、ES7 sql新特性 1.1 数据准备

创建索引及映射

建立价格、颜色、品牌、售卖日期 四个字段

PUT /tvs

PUT /tvs/_mapping { "properties": { "price": { "type": "long" }, "color": { "type": "keyword" }, "brand": { "type": "keyword" }, "sold_date": { "type": "date" } } }

插入数据

Elasticsearch 7.3 SQL新特性在Java API中的应用解析?

POST /tvs/_bulk {"index":{}} {"price":1000,"color":"红色","brand":"长虹","sold_date":"2019-10-28"} {"index":{}} {"price":2000,"color":"红色","brand":"长虹","sold_date":"2019-11-05"} {"index":{}} {"price":3000,"color":"绿色","brand":"小米","sold_date":"2019-05-18"} {"index":{}} {"price":1500,"color":"蓝色","brand":"TCL","sold_date":"2019-07-02"} {"index":{}} {"price":1200,"color":"绿色","brand":"TCL","sold_date":"2019-08-19"} {"index":{}} {"price":2000,"color":"红色","brand":"长虹","sold_date":"2019-11-05"} {"index":{}} {"price":8000,"color":"红色","brand":"三星","sold_date":"2020-01-01"} {"index":{}} {"price":2500,"color":"蓝色","brand":"小米","sold_date":"2020-02-12"} 1.2 简单示例

POST /_sql?format=txt { "query": "SELECT * FROM tvs " }

返回类似于数据库的界面

二、启动方式 2.1 artifacts.elastic.co/maven</url> </repository> </repositories> 6.3 Java代码

public static void main(String[] args) { //1创建连接 try { Connection connection = DriverManager.getConnection("jdbc:es://localhost:9200"); //2创建statement Statement statement = connection.createStatement(); //3执行sql语句 ResultSet resultSet = statement.executeQuery("select * from tvs"); //4获取结果 while (resultSet.next()) { String str = resultSet.getString(1)+"," +resultSet.getString(2)+"," +resultSet.getString(3)+"," +resultSet.getString(4); System.out.println(str); System.out.println("======================================"); } } catch (SQLException e) { e.printStackTrace(); } }

返回结果

本文共计381个文字,预计阅读时间需要2分钟。

Elasticsearch 7.3 SQL新特性在Java API中的应用解析?

1. ES7 SQL新特性:数据准备、创建索引及映射、建立价格、颜色、品牌、销售日期等四个字段

2.PUT /tvs

3.PUT /tvs/_mapping

{ properties: { price: { type: long }, color: { type: keyword }, brand: { type: text }, sale_date: { type: date } } }

一、ES7 sql新特性 1.1 数据准备

创建索引及映射

建立价格、颜色、品牌、售卖日期 四个字段

PUT /tvs

PUT /tvs/_mapping { "properties": { "price": { "type": "long" }, "color": { "type": "keyword" }, "brand": { "type": "keyword" }, "sold_date": { "type": "date" } } }

插入数据

Elasticsearch 7.3 SQL新特性在Java API中的应用解析?

POST /tvs/_bulk {"index":{}} {"price":1000,"color":"红色","brand":"长虹","sold_date":"2019-10-28"} {"index":{}} {"price":2000,"color":"红色","brand":"长虹","sold_date":"2019-11-05"} {"index":{}} {"price":3000,"color":"绿色","brand":"小米","sold_date":"2019-05-18"} {"index":{}} {"price":1500,"color":"蓝色","brand":"TCL","sold_date":"2019-07-02"} {"index":{}} {"price":1200,"color":"绿色","brand":"TCL","sold_date":"2019-08-19"} {"index":{}} {"price":2000,"color":"红色","brand":"长虹","sold_date":"2019-11-05"} {"index":{}} {"price":8000,"color":"红色","brand":"三星","sold_date":"2020-01-01"} {"index":{}} {"price":2500,"color":"蓝色","brand":"小米","sold_date":"2020-02-12"} 1.2 简单示例

POST /_sql?format=txt { "query": "SELECT * FROM tvs " }

返回类似于数据库的界面

二、启动方式 2.1 artifacts.elastic.co/maven</url> </repository> </repositories> 6.3 Java代码

public static void main(String[] args) { //1创建连接 try { Connection connection = DriverManager.getConnection("jdbc:es://localhost:9200"); //2创建statement Statement statement = connection.createStatement(); //3执行sql语句 ResultSet resultSet = statement.executeQuery("select * from tvs"); //4获取结果 while (resultSet.next()) { String str = resultSet.getString(1)+"," +resultSet.getString(2)+"," +resultSet.getString(3)+"," +resultSet.getString(4); System.out.println(str); System.out.println("======================================"); } } catch (SQLException e) { e.printStackTrace(); } }

返回结果