【elasticsearch】PUT 指定ID的方式批量插入数据

1.准备数据 

export LANG=en_US.UTF8
vi sspuuser.json
{"index":{"_index":"sspuuser","_id":"1"}}
{"id": 1, "name": "sspu001", "city": "广州", "course":"oracle", "teacher": "王麻子", "birthdate": "20200829"}
{"index":{"_index":"sspuuser","_id":"2"}}
{"id": 2, "name": "sspu002", "city": "深圳", "course":"mysql", "teacher": "王麻子 2 号", "birthdate": "20200826"}
{"index":{"_index":"sspuuser","_id":"3"}}
{"id": 3, "name": "sspu003", "city": "北京", "course":"sqlserver", "teacher": "王麻子 1号", "birthdate": "20200825"}
{"index":{"_index":"sspuuser","_id":"4"}}
{"id": 4, "name": "sspu004", "city": "上海", "course":"postgrestsql", "teacher": "王麻子 3 号", "birthdate": "20200828"}
{"index":{"_index":"sspuuser","_id":"5"}}
{"id": 5, "name": "sspu005", "city": "杭州", "course":"开源数据库", "teacher": "王麻子", "birthdate": "20200827"}
{"index":{"_index":"sspuuser","_id":"6"}}
{"id": 6, "name": "sspu006", "city": "成都", "course":"greenplum", "teacher": "王麻子", "birthdate": "20200729"}
{"index":{"_index":"sspuuser","_id":"7"}}
{"id": 7, "name": "sspu007", "city": "武汉", "course":"redis", "teacher": "王麻子", "birthdate": "20200210"}
{"index":{"_index":"sspuuser","_id":"8"}}
{"id": 8, "name": "sspu008", "city": "北京", "course":"mongodb", "teacher": "王麻子", "birthdate": "20190529"}
{"index":{"_index":"sspuuser","_id":"9"}}
{"id": 9, "name": "sspu009", "city": "上海", "course":"db2", "teacher": "王麻子", "birthdate": "20180229"}
{"index":{"_index":"sspuuser","_id":"10"}}
{"id": 10, "name": "sspu010", "city": "广州", "course":"国产数据库", "teacher": "王麻子 1 号", "birthdate": "20200302"}
{"index":{"_index":"sspuuser","_id":"11"}}
{"id": 11, "name": "sspu011", "city": "上海", "course":"国产数据库", "teacher": "王麻子 2 号", "birthdate": "20200402"}

2.批量插入数据。

curl -XPUT '192.168.1.7:9201/_bulk?pretty' -H "Content-Type: application/json" --data-binary @sspuuser.json

--查询所有数据。
GET sspuuser/_search
{
	"query": {
	"match_all": {}
	}
}

{
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 11,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "id" : 5,
          "name" : "sspu005",
          "city" : "杭州",
          "course" : "开源数据库",
          "teacher" : "王麻子",
          "birthdate" : "20200827"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "7",
        "_score" : 1.0,
        "_source" : {
          "id" : 7,
          "name" : "sspu007",
          "city" : "武汉",
          "course" : "redis",
          "teacher" : "王麻子",
          "birthdate" : "20200210"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "id" : 2,
          "name" : "sspu002",
          "city" : "深圳",
          "course" : "mysql",
          "teacher" : "王麻子 2 号",
          "birthdate" : "20200826"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "id" : 3,
          "name" : "sspu003",
          "city" : "北京",
          "course" : "sqlserver",
          "teacher" : "王麻子 1号",
          "birthdate" : "20200825"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "id" : 4,
          "name" : "sspu004",
          "city" : "上海",
          "course" : "postgrestsql",
          "teacher" : "王麻子 3 号",
          "birthdate" : "20200828"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "10",
        "_score" : 1.0,
        "_source" : {
          "id" : 10,
          "name" : "sspu010",
          "city" : "广州",
          "course" : "国产数据库",
          "teacher" : "王麻子 1 号",
          "birthdate" : "20200302"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "id" : 1,
          "name" : "sspu001",
          "city" : "广州",
          "course" : "oracle",
          "teacher" : "王麻子",
          "birthdate" : "20200829"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : 1.0,
        "_source" : {
          "id" : 6,
          "name" : "sspu006",
          "city" : "成都",
          "course" : "greenplum",
          "teacher" : "王麻子",
          "birthdate" : "20200729"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "8",
        "_score" : 1.0,
        "_source" : {
          "id" : 8,
          "name" : "sspu008",
          "city" : "北京",
          "course" : "mongodb",
          "teacher" : "王麻子",
          "birthdate" : "20190529"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "9",
        "_score" : 1.0,
        "_source" : {
          "id" : 9,
          "name" : "sspu009",
          "city" : "上海",
          "course" : "db2",
          "teacher" : "王麻子",
          "birthdate" : "20180229"
        }
      }
    ]
  }
}

3.按照一个字段顺序排序

GET sspuuser/_search
{
	"query": {
	"match_all": {}
	},
	"_source": ["birthdate"],
	"sort": [
		{
		"birthdate": {
		"order": "asc"
		}
		}
	]
}
--无法直接排序,需要设置:在字段:birthdate上设置:
--fielddata=true
"failed_shards" : [
      {
        "shard" : 0,
        "index" : "sspuuser",
        "node" : "rVIh8-d2SmKscxM5zlgqfA",
        "reason" : {
          "type" : "illegal_argument_exception",
          "reason" : "Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [birthdate] in order to load field data by uninverting the inverted index. Note that this can use significant memory."
        }
      }
    ]

--设置:字段的:"fielddata": true属性。
PUT sspuuser/_mapping
{
	"properties": {
		"birthdate": {
		"type": "text",
		"fielddata": true
		}
	}
}
--按生日升序。
GET sspuuser/_search
{
	"query": {
	"match_all": {}
	},
	"_source": ["birthdate"],
	"sort": [
		{
		"birthdate": {
		"order": "asc"
		}
		}
	]
}

{
  "took" : 71,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 11,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "9",
        "_score" : null,
        "_source" : {
          "birthdate" : "20180229"
        },
        "sort" : [
          "20180229"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "8",
        "_score" : null,
        "_source" : {
          "birthdate" : "20190529"
        },
        "sort" : [
          "20190529"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "7",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200210"
        },
        "sort" : [
          "20200210"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "10",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200302"
        },
        "sort" : [
          "20200302"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "11",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200402"
        },
        "sort" : [
          "20200402"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200729"
        },
        "sort" : [
          "20200729"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200825"
        },
        "sort" : [
          "20200825"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200826"
        },
        "sort" : [
          "20200826"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200827"
        },
        "sort" : [
          "20200827"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200828"
        },
        "sort" : [
          "20200828"
        ]
      }
    ]
  }
}

4.按生日降序

GET sspuuser/_search
{
	"query": {
	"match_all": {}
	},
	"_source": ["birthdate"],
	"sort": [
		{
		"birthdate": {
		"order": "desc"
		}
		}
	]
}
{
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 11,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200829"
        },
        "sort" : [
          "20200829"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200828"
        },
        "sort" : [
          "20200828"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200827"
        },
        "sort" : [
          "20200827"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200826"
        },
        "sort" : [
          "20200826"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200825"
        },
        "sort" : [
          "20200825"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200729"
        },
        "sort" : [
          "20200729"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "11",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200402"
        },
        "sort" : [
          "20200402"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "10",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200302"
        },
        "sort" : [
          "20200302"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "7",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200210"
        },
        "sort" : [
          "20200210"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "8",
        "_score" : null,
        "_source" : {
          "birthdate" : "20190529"
        },
        "sort" : [
          "20190529"
        ]
      }
    ]
  }
}

--筛选出多个字段。
GET sspuuser/_search
{
	"query": {
	"match_all": {}
	},
	"_source": ["birthdate","city","course"],
	"sort": [
		{
		"birthdate": {
		"order": "desc"
		}
		}
	]
}
{
  "took" : 8,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 11,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200829",
          "city" : "广州",
          "course" : "oracle"
        },
        "sort" : [
          "20200829"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200828",
          "city" : "上海",
          "course" : "postgrestsql"
        },
        "sort" : [
          "20200828"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200827",
          "city" : "杭州",
          "course" : "开源数据库"
        },
        "sort" : [
          "20200827"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200826",
          "city" : "深圳",
          "course" : "mysql"
        },
        "sort" : [
          "20200826"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200825",
          "city" : "北京",
          "course" : "sqlserver"
        },
        "sort" : [
          "20200825"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200729",
          "city" : "成都",
          "course" : "greenplum"
        },
        "sort" : [
          "20200729"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "11",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200402",
          "city" : "上海",
          "course" : "国产数据库"
        },
        "sort" : [
          "20200402"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "10",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200302",
          "city" : "广州",
          "course" : "国产数据库"
        },
        "sort" : [
          "20200302"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "7",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200210",
          "city" : "武汉",
          "course" : "redis"
        },
        "sort" : [
          "20200210"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "8",
        "_score" : null,
        "_source" : {
          "birthdate" : "20190529",
          "city" : "北京",
          "course" : "mongodb"
        },
        "sort" : [
          "20190529"
        ]
      }
    ]
  }
}

5.筛选指定字段排序

GET sspuuser/_search?q=teacher:"王麻子 2 号"&sort=birthdate:desc
{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "id" : 2,
          "name" : "sspu002",
          "city" : "深圳",
          "course" : "mysql",
          "teacher" : "王麻子 2 号",
          "birthdate" : "20200826"
        },
        "sort" : [
          "20200826"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "11",
        "_score" : null,
        "_source" : {
          "id" : 11,
          "name" : "sspu011",
          "city" : "上海",
          "course" : "国产数据库",
          "teacher" : "王麻子 2 号",
          "birthdate" : "20200402"
        },
        "sort" : [
          "20200402"
        ]
      }
    ]
  }
}

6.全文排序。

GET sspuuser/_search?sort=birthdate:asc  --按照生日升序。
GET sspuuser/_search?sort=birthdate:desc --按照生日降序

7.分页查询 

--按照生日升序,从第3条开始取两条。
GET sspuuser/_search
{
	"query": {
	"match_all": {}
	},
	"_source": ["birthdate","city","name"],
	"sort": [
		{
		"birthdate": {
		"order": "asc"
		}
		}
	],
	"from": 3,
	"size": 2
}

{
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 11,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "10",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200302",
          "city" : "广州",
          "name" : "sspu010"
        },
        "sort" : [
          "20200302"
        ]
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "11",
        "_score" : null,
        "_source" : {
          "birthdate" : "20200402",
          "city" : "上海",
          "name" : "sspu011"
        },
        "sort" : [
          "20200402"
        ]
      }
    ]
  }
}

8.全文搜索取前两行 

GET sspuuser/_search
{
"query": {
"match_all": {}
},
"from": 0,
"size": 2
}
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 11,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "id" : 5,
          "name" : "sspu005",
          "city" : "杭州",
          "course" : "开源数据库",
          "teacher" : "王麻子",
          "birthdate" : "20200827"
        }
      },
      {
        "_index" : "sspuuser",
        "_type" : "_doc",
        "_id" : "7",
        "_score" : 1.0,
        "_source" : {
          "id" : 7,
          "name" : "sspu007",
          "city" : "武汉",
          "course" : "redis",
          "teacher" : "王麻子",
          "birthdate" : "20200210"
        }
      }
    ]
  }
}

使用elasticsearch-sql框架,可以通过以下步骤将SQL数据插入ES: 1. 创建一个ES索引,定义字段映射 2. 使用ES-SQL框架连接ES,执行SQL查询获取数据 3. 遍历查询结果,使用ES-SQL框架提供的API将数据插入ES 下面是一个示例代码,演示如何使用ES-SQL框架将SQL数据插入ES: ```java import io.github.iamazy.elasticsearch.dsl.sql.parser.SqlParser; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.nlpcn.es4sql.exception.SqlParseException; import org.nlpcn.es4sql.query.QueryAction; import org.nlpcn.es4sql.query.SqlElasticRequestBuilder; import java.io.IOException; import java.sql.*; public class SqlToEs { // 定义ES索引名称 private static final String INDEX_NAME = "my_index"; // 定义ES连接客户端 private RestHighLevelClient client; // 定义SQL查询语句 private String sql = "SELECT * FROM my_table WHERE id > 100"; public SqlToEs() { // 初始化ES连接客户端 client = new RestHighLevelClient(); } public void insert() throws SQLException, IOException, SqlParseException { // 解析SQL查询语句 SqlParser sqlParser = new SqlParser(); QueryAction queryAction = sqlParser.parseSelect(sql); // 构建ES查询请求 SqlElasticRequestBuilder requestBuilder = queryAction.explain(); SearchSourceBuilder sourceBuilder = requestBuilder.getSourceBuilder(); sourceBuilder.query(QueryBuilders.matchAllQuery()); String query = sourceBuilder.toString(); // 执行SQL查询获取数据 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/my_database", "user", "password"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(query); // 遍历查询结果,将数据插入ES BulkRequest request = new BulkRequest(); while (rs.next()) { // 创建一个ES文档 Map<String, Object> document = new HashMap<>(); document.put("id", rs.getInt("id")); document.put("name", rs.getString("name")); document.put("age", rs.getInt("age")); // 添加到批量请求中 request.add(new IndexRequest(INDEX_NAME).source(document)); } // 执行批量请求 BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT); if (bulkResponse.status() == RestStatus.OK) { System.out.println("数据插入成功!"); } } } ``` 这里使用ES-SQL框架解析SQL查询语句,并构建ES查询请求。然后执行SQL查询获取数据,并遍历查询结果,将数据插入ES。最后使用ES客户端执行批量请求,将数据插入ES
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值