Skip to content

Commit

Permalink
ATLAS-1825: updated import to support optional transformation of attr…
Browse files Browse the repository at this point in the history
…ibute values

Signed-off-by: Madhan Neethiraj <[email protected]>
  • Loading branch information
ashutoshm authored and mneethiraj committed Jun 2, 2017
1 parent d2198bb commit 4c9c3bb
Show file tree
Hide file tree
Showing 15 changed files with 828 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,18 @@
@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class AtlasImportRequest implements Serializable {
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;
public static final String TRANSFORMS_KEY = "transforms";

private Map<String, Object> options;
private Map<String, String> options;

public AtlasImportRequest() {
this.options = new HashMap<>();
}

public AtlasImportRequest(Map<String, Object> options) {
this.options = options;
}

public Map<String, Object> getOptions() { return options; }
public Map<String, String> getOptions() { return options; }

public void setOptions(Map<String, Object> options) { this.options = options; }
public void setOptions(Map<String, String> options) { this.options = options; }

public StringBuilder toString(StringBuilder sb) {
if (sb == null) {
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,12 @@
<version>${jersey.version}</version>
</dependency>

<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>${jersey.version}</version>
</dependency>

<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.atlas.repository.store.graph.AtlasEntityStore;
import org.apache.atlas.store.AtlasTypeDefStore;
import org.apache.atlas.type.AtlasTypeRegistry;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
Expand All @@ -44,7 +45,7 @@ public class ImportService {
private static final Logger LOG = LoggerFactory.getLogger(ImportService.class);

private final AtlasTypeDefStore typeDefStore;
private final AtlasEntityStore entityStore;
private final AtlasEntityStore entityStore;
private final AtlasTypeRegistry typeRegistry;

private long startTimestamp;
Expand All @@ -53,7 +54,7 @@ public class ImportService {

public ImportService(final AtlasTypeDefStore typeDefStore, final AtlasEntityStore entityStore, AtlasTypeRegistry typeRegistry) {
this.typeDefStore = typeDefStore;
this.entityStore = entityStore;
this.entityStore = entityStore;
this.typeRegistry = typeRegistry;
}

Expand All @@ -62,8 +63,12 @@ public AtlasImportResult run(ZipSource source, AtlasImportRequest request, Strin
AtlasImportResult result = new AtlasImportResult(request, userName, requestingIP, hostName, System.currentTimeMillis());

try {

LOG.info("==> import(user={}, from={})", userName, requestingIP);

String transforms = MapUtils.isNotEmpty(request.getOptions()) ? request.getOptions().get(AtlasImportRequest.TRANSFORMS_KEY) : null;

source.setImportTransform(ImportTransforms.fromJson(transforms));
startTimestamp = System.currentTimeMillis();
processTypes(source.getTypesDef(), result);
processEntities(source, result);
Expand All @@ -86,8 +91,8 @@ public AtlasImportResult run(ZipSource source, AtlasImportRequest request, Strin
}

public AtlasImportResult run(AtlasImportRequest request, String userName, String hostName, String requestingIP)
throws AtlasBaseException {
String fileName = (String)request.getOptions().get("FILENAME");
throws AtlasBaseException {
String fileName = (String) request.getOptions().get("FILENAME");

if (StringUtils.isBlank(fileName)) {
throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, "FILENAME parameter not found");
Expand All @@ -98,8 +103,9 @@ public AtlasImportResult run(AtlasImportRequest request, String userName, String
try {
LOG.info("==> import(user={}, from={}, fileName={})", userName, requestingIP, fileName);

File file = new File(fileName);
ZipSource source = new ZipSource(new ByteArrayInputStream(FileUtils.readFileToByteArray(file)));
String transforms = MapUtils.isNotEmpty(request.getOptions()) ? request.getOptions().get(AtlasImportRequest.TRANSFORMS_KEY) : null;
File file = new File(fileName);
ZipSource source = new ZipSource(new ByteArrayInputStream(FileUtils.readFileToByteArray(file)), ImportTransforms.fromJson(transforms));

result = run(source, request, userName, hostName, requestingIP);
} catch (AtlasBaseException excp) {
Expand All @@ -116,7 +122,7 @@ public AtlasImportResult run(AtlasImportRequest request, String userName, String
throw new AtlasBaseException(excp);
} finally {
LOG.info("<== import(user={}, from={}, fileName={}): status={}", userName, requestingIP, fileName,
(result == null ? AtlasImportResult.OperationStatus.FAIL : result.getOperationStatus()));
(result == null ? AtlasImportResult.OperationStatus.FAIL : result.getOperationStatus()));
}

return result;
Expand All @@ -142,19 +148,19 @@ private void updateMetricsForTypesDef(AtlasTypesDef typeDefinitionMap, AtlasImpo
}

private void setGuidToEmpty(AtlasTypesDef typesDef) {
for (AtlasEntityDef def: typesDef.getEntityDefs()) {
for (AtlasEntityDef def : typesDef.getEntityDefs()) {
def.setGuid(null);
}

for (AtlasClassificationDef def: typesDef.getClassificationDefs()) {
for (AtlasClassificationDef def : typesDef.getClassificationDefs()) {
def.setGuid(null);
}

for (AtlasEnumDef def: typesDef.getEnumDefs()) {
for (AtlasEnumDef def : typesDef.getEnumDefs()) {
def.setGuid(null);
}

for (AtlasStructDef def: typesDef.getStructDefs()) {
for (AtlasStructDef def : typesDef.getStructDefs()) {
def.setGuid(null);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* 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.atlas.repository.impexp;

import org.apache.atlas.AtlasErrorCode;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.commons.lang.StringUtils;


public abstract class ImportTransformer {
private static final String TRANSFORMER_PARAMETER_SEPARATOR = "\\:";

private final String transformType;


public static ImportTransformer getTransformer(String transformerSpec) throws AtlasBaseException {
String[] params = StringUtils.split(transformerSpec, TRANSFORMER_PARAMETER_SEPARATOR);
String key = (params == null || params.length < 1) ? transformerSpec : params[0];

final ImportTransformer ret;

if (StringUtils.isEmpty(key)) {
throw new AtlasBaseException(AtlasErrorCode.INVALID_VALUE, "Error creating ImportTransformer. Invalid transformer-specification: {}.", transformerSpec);
} else if (key.equals("replace")) {
String toFindStr = (params == null || params.length < 2) ? "" : params[1];
String replaceStr = (params == null || params.length < 3) ? "" : params[2];

ret = new Replace(toFindStr, replaceStr);
} else if (key.equals("lowercase")) {
ret = new Lowercase();
} else if (key.equals("uppercase")) {
ret = new Uppercase();
} else {
throw new AtlasBaseException(AtlasErrorCode.INVALID_VALUE, "Error creating ImportTransformer. Unknown transformer: {}.", transformerSpec);
}

return ret;
}

public String getTransformType() { return transformType; }

public abstract Object apply(Object o) throws AtlasBaseException;


protected ImportTransformer(String transformType) {
this.transformType = transformType;
}

static class Replace extends ImportTransformer {
private final String toFindStr;
private final String replaceStr;

public Replace(String toFindStr, String replaceStr) {
super("replace");

this.toFindStr = toFindStr;
this.replaceStr = replaceStr;
}

public String getToFindStr() { return toFindStr; }

public String getReplaceStr() { return replaceStr; }

@Override
public Object apply(Object o) throws AtlasBaseException {
Object ret = o;

if(o instanceof String) {
ret = StringUtils.replace((String) o, toFindStr, replaceStr);
}

return ret;
}
}

static class Lowercase extends ImportTransformer {
public Lowercase() {
super("lowercase");
}

@Override
public Object apply(Object o) {
Object ret = o;

if(o instanceof String) {
ret = StringUtils.lowerCase((String) o);
}

return ret;
}
}

static class Uppercase extends ImportTransformer {
public Uppercase() {
super("uppercase");
}

@Override
public Object apply(Object o) {
Object ret = o;

if(o instanceof String) {
ret = StringUtils.upperCase((String) o);
}

return ret;
}
}
}
Loading

0 comments on commit 4c9c3bb

Please sign in to comment.