国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Elasticsearch Java API 的使用-更新索引(update & upset)與 Bulk的批量更新

這篇具有很好參考價(jià)值的文章主要介紹了Elasticsearch Java API 的使用-更新索引(update & upset)與 Bulk的批量更新。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

Java更新索引(update & upset)
update
更新使用UpdateRequest(update類型更新,只能更新)

public class EsUpdate{
    public void updateIndex(TransportClient client){
        Date time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                                .parse("2016-7-21 00:00:01");

        UpdateRequest updateRequest = new UpdateRequest();
        updateRequest.index("pointdata")
				     .type("pointdata")
				     .id("1")
				     .doc(XContentFactory.jsonBuilder()
							             .startObject()
							             .field("pointid","W3.UNIT1.10LBG01CP302")
						                 .field("pointvalue","0.8029")
										 .field("inputtime",
                    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time))
							             .endObject()
                );
		//執(zhí)行修改
       UpdateResponse response1 = client.update(updateRequest).get();

		//查詢修改狀態(tài),返回ok表示成功
		System.out.println(response1.status());
    }
}

upset
要用IndexRequest設(shè)定添加文檔,UpdateRequest設(shè)定更新文檔,設(shè)定upset執(zhí)行有則修改無則更新(upset類型更新,文檔不存在時(shí)創(chuàng)建)

public class EsUpSet{
    public void updateIndex(TransportClient client){
         Date time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                                .parse("2016-7-21 00:00:01");
		//添加文檔
        IndexRequest request1 = new IndexRequest("pointdata", "pointData", "1")
						        .source(
								     XContentFactory.jsonBuilder()
							             .startObject()
							             .field("pointid","W3.UNIT1.10LBG01CP302")
						                 .field("pointvalue","0.8029")
										 .field("inputtime",
                    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time))
							             .endObject()
						        );
		
		//修改文檔		        
        UpdateRequest updateRequest2 = new UpdateRequest();
        updateRequest.index("pointdata")
				     .type("pointdata")
				     .id("1")
				     .doc(XContentFactory.jsonBuilder()
							             .startObject()
							             .field("pointid","W3.UNIT1.10LBG01CP302")
						                 .field("pointvalue","0.8029")
										 .field("inputtime",
                    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time))
							             .endObject()
                ).upset(request1);
       
		UpdateResponce responce = client.update(request2).get();
		
        //查詢修改狀態(tài),返回ok表示成功
	    System.out.println(response2.status());
    }
}

基于Bulk的批量更新(update & upset)
動(dòng)態(tài)的更新一個(gè) documents 中的任意 field(字段),包括原來沒有的 field (字段)。文章來源地址http://www.zghlxwxcb.cn/news/detail-508644.html

public ResultMsg updateIndex(final String index,
			final JSONArray resultList) {
		final ResultMsg resultMsg = new ResultMsg();
		BulkRequestBuilder bulkRequest = client.prepareBulk();
		bulkRequest.setRefreshPolicy(RefreshPolicy.IMMEDIATE);

		int count = 0;
		int len = resultList.size();
		try {
			for (int i = 0; i < len; i++) {
				final XContentBuilder builder = XContentFactory.jsonBuilder()
						.startObject();
				
				JSONObject json = resultList.getJSONObject(i);
				final String id = json.getString("id");
				json.remove("id");
				
				for (String key : json.keySet()) {
					builder.field(key, json.get(key));
				}

				builder.endObject();

				// update更新
				UpdateRequestBuilder requestBuilder = client.prepareUpdate(
						index, index, id).setDoc(builder);
				
				// upset更新
				UpdateRequestBuilder requestBuilder = client.prepareUpdate(
						index, index, id).setDoc(builder).setUpsert();
				// IndexRequestBuilder requestBuilder = client.prepareIndex(
				// "sampling", indexName, id).setSource(builder);
				bulkRequest.add(requestBuilder);
				count++;

				if (count % BULK_SIZE == 0) {
					BulkResponse bulkResponse = bulkRequest.execute()
							.actionGet();
					if (bulkResponse.hasFailures()) {
						LOGGER.error("批量更新索引數(shù)據(jù)失敗: " + indexName);
						LOGGER.error("批量更新索引數(shù)據(jù)失敗: "
								+ bulkResponse.buildFailureMessage());

						resultMsg.setHasFailures(true);

						BulkItemResponse[] responses = bulkResponse.getItems();
						for (int k = 0; k < responses.length; k++) {
							BulkItemResponse response = responses[k];
							if (response.isFailed()) {
								ErrorDetail errorDetail = new ErrorDetail();
								errorDetail.setIndex(k + (i / BULK_SIZE));
								errorDetail.setId(response.getId());
								errorDetail
										.setMsg(response.getFailureMessage());

								resultMsg.addError(errorDetail);
							}
						}
					}

					bulkRequest = client.prepareBulk();
					bulkRequest.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
					count = 0;
				}
			}

			if (count > 0) {
				BulkResponse bulkResponse = bulkRequest.execute().actionGet();
				if (bulkResponse.hasFailures()) {
					LOGGER.error("批量更新索引數(shù)據(jù)失敗: " + indexName);
					LOGGER.error("批量更新索引數(shù)據(jù)失敗: "
							+ bulkResponse.buildFailureMessage());

					resultMsg.setHasFailures(true);

					BulkItemResponse[] responses = bulkResponse.getItems();
					for (int k = 0; k < responses.length; k++) {
						BulkItemResponse response = responses[k];
						if (response.isFailed()) {
							ErrorDetail errorDetail = new ErrorDetail();
							errorDetail.setIndex(k + (len / BULK_SIZE));
							errorDetail.setId(response.getId());
							errorDetail.setMsg(response.getFailureMessage());

							resultMsg.addError(errorDetail);
						}
					}
				}
			}

			return resultMsg;
		} catch (Exception e) {
			LOGGER.error("批量更新索引數(shù)據(jù)失敗: " + indexName);
			throw new RuntimeException("批量更新索引數(shù)據(jù)失敗: " + indexName, e);
		} finally {
			bulkRequest.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
		}

	}

到了這里,關(guān)于Elasticsearch Java API 的使用-更新索引(update & upset)與 Bulk的批量更新的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • ElasticSearch - 批量更新bulk死鎖問題排查 | 京東云技術(shù)團(tuán)隊(duì)

    ElasticSearch - 批量更新bulk死鎖問題排查 | 京東云技術(shù)團(tuán)隊(duì)

    一、問題系統(tǒng)介紹 監(jiān)聽商品變更MQ消息,查詢商品最新的信息,調(diào)用BulkProcessor批量更新ES集群中的商品字段信息; 由于商品數(shù)據(jù)非常多,所以將商品數(shù)據(jù)存儲(chǔ)到ES集群上,整個(gè)ES集群共劃分了256個(gè)分片,并根據(jù)商品的三級類目ID進(jìn)行分片路由。 比如一個(gè)SKU的商品名稱發(fā)生變化,

    2024年02月12日
    瀏覽(27)
  • elasticsearch使用腳本 滾動(dòng)關(guān)閉索引,更新index setting

    elasticsearch使用腳本 滾動(dòng)關(guān)閉索引,更新index setting

    ? ? ?在舊的索引中更新mapping時(shí),新增了分詞器(分詞器已經(jīng)在模板中添加),但是在更新mapping時(shí)報(bào)錯(cuò): 查看elasticsearch官網(wǎng),發(fā)現(xiàn)不允許在已經(jīng)存在的索引中動(dòng)態(tài)更新分詞器,只能先將索引close,更新分詞器,然后再打開 Update index settings API | Elasticsearch Guide [8.3] | Elastic 2.1 由

    2024年02月08日
    瀏覽(20)
  • PHP 如何使用 Elasticsearch 的 索引 API 接口

    PHP 如何使用 Elasticsearch 的 索引 API 接口

    目錄 一、實(shí)戰(zhàn)場景 二、知識(shí)點(diǎn) PHP Elasticsearch 索引 index MySQL 三、菜鳥實(shí)戰(zhàn) 如何在 PHP 中使用 Elasticsearch 的索引 API 接口 PHP Elasticsearch 索引 index MySQL Elasticsearch 本質(zhì)上是一個(gè)數(shù)據(jù)庫,但并不是 MySQL 這種關(guān)系型數(shù)據(jù)庫,查詢語言也不是 SQL,而是 Elasticsearch 自己的一套查詢語言。

    2024年02月13日
    瀏覽(17)
  • Elasticsearch 索引文檔時(shí)create、index、update的區(qū)別【學(xué)習(xí)記錄】

    Elasticsearch 索引文檔時(shí)create、index、update的區(qū)別【學(xué)習(xí)記錄】

    本文基于elasticsearch7.3.0版本。 一、思維導(dǎo)圖 elasticsearch中create、index、update都可以實(shí)現(xiàn)插入功能,但是實(shí)現(xiàn)原理并不相同。 二、驗(yàn)證index和create 由上面思維導(dǎo)圖可以清晰的看出create、index的大致區(qū)別,下面我們來驗(yàn)證下思維導(dǎo)圖中的場景: 1、首先明確一點(diǎn):如何指定是creat

    2024年01月20日
    瀏覽(26)
  • ElasticSearch7.3學(xué)習(xí)(十六)----RestHighLevelClient Java api實(shí)現(xiàn)索引的創(chuàng)建、刪除、是否存在、關(guān)閉、開啟

    注意:導(dǎo)入的包區(qū)別,不同的包創(chuàng)建索引的方式不同。博主親身實(shí)踐,具體體現(xiàn)在createIndexRequest.mapping()里面。讀者可自行試驗(yàn)。 ?由此可以猜想一下: 可以看到上述兩種方式導(dǎo)入的包的子類名是相同的,但是具體對索引的操作方式可能是不同的。具體的區(qū)別博主暫時(shí)還不清

    2024年02月16日
    瀏覽(90)
  • Elasticsearch Document Update API詳解、原理與示例

    Elasticsearch Document Update API詳解、原理與示例

    private int retryOnConflict = 0:更新沖突時(shí)重試次數(shù)。 private RefreshPolicy refreshPolicy = RefreshPolicy.NONE:刷新策略。NONE:代表不重試; private ActiveShardCount waitForActiveShards = ActiveShardCount.DEFAULT:執(zhí)行操作之前需要等待激活的副本數(shù),已在《Elasticsearch Document Get API詳解、原理與示例》中詳

    2024年04月22日
    瀏覽(24)
  • Elasticsearch更新指定字段操作_update_by_query

    MYSQL語句:update index_name set name = ‘wb’ where id = ‘20132112534’; MYSQL語句:update index_name set name = ‘wb’ where (a_time - b_time = 100000) MYSQL語句:update index_name set sort_time = update_time where sort_time is null;

    2024年02月11日
    瀏覽(19)
  • elasticsearch索引操作,索引創(chuàng)建、索引更新、索引刪除

    創(chuàng)建索引 更新索引,添加字段 注意更新索引時(shí)與創(chuàng)建索引大致一樣,只是更新索引時(shí)候的url不同,需要在后面加一個(gè) _mapping 路徑,同時(shí)請求的json里面不需要 mappings 路徑,只需要 properties 即可 更新索引,修改配置 同理在更新setting的時(shí)候和更新maping的時(shí)候一樣 獲取索引結(jié)構(gòu)

    2024年02月11日
    瀏覽(20)
  • 使用kettle同步全量數(shù)據(jù)到Elasticsearch(es)--elasticsearch-bulk-insert-plugin應(yīng)用

    使用kettle同步全量數(shù)據(jù)到Elasticsearch(es)--elasticsearch-bulk-insert-plugin應(yīng)用

    為了前端更快地進(jìn)行數(shù)據(jù)檢索,需要將數(shù)據(jù)存儲(chǔ)到es中是一個(gè)很不錯(cuò)的選擇。由于公司etl主要工具是kettle,這里介紹如何基于kettle的elasticsearch-bulk-insert-plugin插件將數(shù)據(jù)導(dǎo)入es。在實(shí)施過程中會(huì)遇到一些坑,這里記錄解決方案。 可能會(huì)遇到的報(bào)錯(cuò): 1、No elasticSearch nodes found 2、

    2024年02月01日
    瀏覽(28)
  • es elasticsearch 新增更新索引,新增更新文檔

    先新增索引 新增映射 ?或者上述兩步和為一步(創(chuàng)建索引,及創(chuàng)建mapping) 只能增加原有不存在的字段 創(chuàng)建一個(gè)全新的索引,映射包含調(diào)整后的字段或類型 將原有索引的數(shù)據(jù)遷移到新的索引 刪除原有索引 將新的索引的別名設(shè)置為原來索引相同名稱 創(chuàng)建一個(gè) 重建文檔(全量

    2024年02月11日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包