You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by GitBox <gi...@apache.org> on 2021/04/01 08:29:51 UTC
[GitHub] [unomi] sergehuber commented on a change in pull request #274: UNOMI-435 Migrate existing events scope to source
sergehuber commented on a change in pull request #274:
URL: https://github.com/apache/unomi/pull/274#discussion_r605470522
##########
File path: tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo200.java
##########
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.unomi.shell.migration.impl;
+
+import org.apache.http.HttpStatus;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.util.EntityUtils;
+import org.apache.karaf.shell.api.console.Session;
+import org.apache.unomi.shell.migration.Migration;
+import org.json.JSONObject;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Version;
+import org.osgi.service.component.annotations.Component;
+
+import java.io.IOException;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+@Component
+public class MigrationTo200 implements Migration {
+
+ private CloseableHttpClient httpClient;
+ private Session session;
+ private String esAddress;
+
+ @Override
+ public Version getFromVersion() {
+ return new Version("1.5.0");
+ }
+
+ @Override
+ public Version getToVersion() {
+ return new Version("2.0.0");
+ }
+
+ @Override
+ public String getDescription() {
+ return "Updates mapping for an index with prefix \"context-event\". Adds the \"sourceId\" field and copies value from the \"scope\" field to it.";
+ }
+
+ @Override
+ public void execute(Session session, CloseableHttpClient httpClient, String esAddress, BundleContext bundleContext) throws IOException {
+ this.httpClient = httpClient;
+ this.session = session;
+ this.esAddress = esAddress;
+
+ doExecute();
+ }
+
+ private void doExecute() throws IOException {
+ try (CloseableHttpResponse response = httpClient.execute(new HttpGet(esAddress + "/_aliases"))) {
+
+ if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
+ JSONObject indicesAsJson = new JSONObject(EntityUtils.toString(response.getEntity()));
+
+ final Set<String> indices = indicesAsJson.keySet().stream().
+ filter(alias -> alias.startsWith("context-event")).
+ collect(Collectors.toSet());
+
+ for (String indexName : indices) {
+ updateMapping(indexName, httpClient);
+ }
+ }
+ }
+ }
+
+ private void updateMapping(final String indexName, final CloseableHttpClient httpClient) throws IOException {
+ HttpPut httpPut = new HttpPut(esAddress + "/" + indexName + "/_mapping");
+
+ httpPut.addHeader("Accept", "application/json");
+ httpPut.addHeader("Content-Type", "application/json");
+
+ String request = "{\n" +
+ "\"properties\": {\n" +
+ " \"sourceId\": {\n" +
Review comment:
Seems like the analyzer is missing here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org