Skip to content
Snippets Groups Projects
Commit 5cfb25e0 authored by Benoit Lavenier's avatar Benoit Lavenier
Browse files

ES: code refactoring - create a inherited method bulkFromClasspathFile()

ES: update registry categories
parent de4cc003
No related branches found
No related tags found
No related merge requests found
...@@ -27,17 +27,20 @@ import com.fasterxml.jackson.databind.ObjectMapper; ...@@ -27,17 +27,20 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import io.ucoin.ucoinj.core.beans.Bean; import io.ucoin.ucoinj.core.beans.Bean;
import io.ucoin.ucoinj.core.beans.InitializingBean; import io.ucoin.ucoinj.core.beans.InitializingBean;
import io.ucoin.ucoinj.core.exception.TechnicalException; import io.ucoin.ucoinj.core.exception.TechnicalException;
import io.ucoin.ucoinj.core.util.StringUtils;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequestBuilder; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder; import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse; import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.Closeable; import java.io.*;
import java.io.IOException;
/** /**
* Created by Benoit on 08/04/2015. * Created by Benoit on 08/04/2015.
...@@ -105,4 +108,51 @@ public abstract class BaseIndexerService implements Bean, InitializingBean, Clos ...@@ -105,4 +108,51 @@ public abstract class BaseIndexerService implements Bean, InitializingBean, Clos
throw new TechnicalException("Error while preparing default index analyzer: " + e.getMessage(), e); throw new TechnicalException("Error while preparing default index analyzer: " + e.getMessage(), e);
} }
} }
protected void bulkFromClasspathFile(String classpathFile, String indexName, String indexType) {
BulkRequest bulkRequest = Requests.bulkRequest();
InputStream ris = null;
try {
ris = getClass().getClassLoader().getResourceAsStream(classpathFile);
if (ris == null) {
throw new TechnicalException(String.format("Could not retrieve data file [%s] need to fill index [%s]: ", classpathFile, indexName));
}
StringBuilder builder = new StringBuilder();
BufferedReader bf = new BufferedReader(new InputStreamReader(ris));
String line = bf.readLine();
while(line != null) {
if (StringUtils.isNotBlank(line)) {
if (log.isTraceEnabled()) {
log.trace(String.format("[%s] Add to bulk: %s", indexName, line));
}
builder.append(line).append('\n');
}
line = bf.readLine();
}
byte[] data = builder.toString().getBytes();
bulkRequest.add(new BytesArray(data), indexName, indexType, false);
} catch(Exception e) {
throw new TechnicalException(String.format("[%s] Error while inserting rows", indexName), e);
}
finally {
if (ris != null) {
try {
ris.close();
}
catch(IOException e) {
// Silent is gold
}
}
}
try {
getClient().bulk(bulkRequest).actionGet();
} catch(Exception e) {
throw new TechnicalException(String.format("[%s] Error while inserting rows", indexName), e);
}
}
} }
...@@ -147,56 +147,11 @@ public class MarketCategoryIndexerService extends BaseIndexerService { ...@@ -147,56 +147,11 @@ public class MarketCategoryIndexerService extends BaseIndexerService {
public void initCategories() { public void initCategories() {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Initializing all categories"); log.debug("Initializing all market categories");
} }
BulkRequest bulkRequest = Requests.bulkRequest(); // Insert categories
bulkFromClasspathFile(CATEGORIES_BULK_CLASSPATH_FILE, INDEX_NAME, INDEX_TYPE);
InputStream ris = null;
try {
ris = getClass().getClassLoader().getResourceAsStream(CATEGORIES_BULK_CLASSPATH_FILE);
if (ris == null) {
throw new TechnicalException(String.format("Could not retrieve data file [%s] need to fill index [%s]: ", CATEGORIES_BULK_CLASSPATH_FILE, INDEX_NAME));
}
StringBuilder builder = new StringBuilder();
BufferedReader bf = new BufferedReader(new InputStreamReader(ris));
String line = bf.readLine();
while(line != null) {
if (StringUtils.isNotBlank(line)) {
if (log.isTraceEnabled()) {
log.trace("Add to category bulk: " + line);
}
builder.append(line).append('\n');
}
line = bf.readLine();
}
byte[] data = builder.toString().getBytes();
bulkRequest.add(new BytesArray(data), INDEX_NAME, INDEX_TYPE, false);
/*
InputStreamStreamInput is = new InputStreamStreamInput(ris);
bulkRequest.readFrom(is);*/
} catch(Exception e) {
throw new TechnicalException(String.format("Error while initializing data [%s]", INDEX_NAME), e);
}
finally {
if (ris != null) {
try {
ris.close();
}
catch(IOException e) {
// Silent is gold
}
}
}
try {
getClient().bulk(bulkRequest).actionGet();
} catch(Exception e) {
throw new TechnicalException(String.format("Error while initializing data [%s]", INDEX_NAME), e);
}
} }
/* -- Internal methods -- */ /* -- Internal methods -- */
......
...@@ -55,6 +55,7 @@ public class RegistryCategoryIndexerService extends BaseIndexerService { ...@@ -55,6 +55,7 @@ public class RegistryCategoryIndexerService extends BaseIndexerService {
private static final Logger log = LoggerFactory.getLogger(RegistryCategoryIndexerService.class); private static final Logger log = LoggerFactory.getLogger(RegistryCategoryIndexerService.class);
private static final String CATEGORIES_BULK_CLASSPATH_FILE = "registry-categories-bulk-insert.json"; private static final String CATEGORIES_BULK_CLASSPATH_FILE = "registry-categories-bulk-insert.json";
private static final String SUB_CATEGORIES_BULK_CLASSPATH_FILE = "registry-sub-categories-bulk-insert.json";
public static final String INDEX_NAME = "registry"; public static final String INDEX_NAME = "registry";
public static final String INDEX_TYPE = "category"; public static final String INDEX_TYPE = "category";
...@@ -151,53 +152,11 @@ public class RegistryCategoryIndexerService extends BaseIndexerService { ...@@ -151,53 +152,11 @@ public class RegistryCategoryIndexerService extends BaseIndexerService {
public void initCategories() { public void initCategories() {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Initializing all categories"); log.debug("Initializing all registry categories");
} }
BulkRequest bulkRequest = Requests.bulkRequest(); // Insert categories
bulkFromClasspathFile(CATEGORIES_BULK_CLASSPATH_FILE, INDEX_NAME, INDEX_TYPE);
InputStream ris = null;
try {
ris = getClass().getClassLoader().getResourceAsStream(CATEGORIES_BULK_CLASSPATH_FILE);
if (ris == null) {
throw new TechnicalException(String.format("Could not retrieve data file [%s] need to fill index [%s]: ", CATEGORIES_BULK_CLASSPATH_FILE, INDEX_NAME));
}
StringBuilder builder = new StringBuilder();
BufferedReader bf = new BufferedReader(new InputStreamReader(ris));
String line = bf.readLine();
while(line != null) {
if (StringUtils.isNotBlank(line)) {
if (log.isTraceEnabled()) {
log.trace("Add to category bulk: " + line);
}
builder.append(line).append('\n');
}
line = bf.readLine();
}
byte[] data = builder.toString().getBytes();
bulkRequest.add(new BytesArray(data), INDEX_NAME, INDEX_TYPE, false);
} catch(Exception e) {
throw new TechnicalException(String.format("Error while initializing data [%s]", INDEX_NAME), e);
}
finally {
if (ris != null) {
try {
ris.close();
}
catch(IOException e) {
// Silent is gold
}
}
}
try {
getClient().bulk(bulkRequest).actionGet();
} catch(Exception e) {
throw new TechnicalException(String.format("Error while initializing data [%s]", INDEX_NAME), e);
}
} }
/* -- Internal methods -- */ /* -- Internal methods -- */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment