Commit c0a91c7e by Ashutosh Mestry

ATLAS-2843: AtlasClient updates for exportData and importData.

parent 0c308015
...@@ -59,10 +59,10 @@ import javax.ws.rs.core.MediaType; ...@@ -59,10 +59,10 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriBuilder;
import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.ConnectException; import java.net.ConnectException;
import java.net.URI; import java.net.URI;
...@@ -385,7 +385,7 @@ public abstract class AtlasBaseClient { ...@@ -385,7 +385,7 @@ public abstract class AtlasBaseClient {
} }
try { try {
if(api.getProduces().equals(MediaType.APPLICATION_OCTET_STREAM)) { if(api.getProduces().equals(MediaType.APPLICATION_OCTET_STREAM)) {
return (T) IOUtils.toByteArray(clientResponse.getEntityInputStream()); return (T) clientResponse.getEntityInputStream();
} else if (responseType.getRawClass().equals(ObjectNode.class)) { } else if (responseType.getRawClass().equals(ObjectNode.class)) {
String stringEntity = clientResponse.getEntity(String.class); String stringEntity = clientResponse.getEntity(String.class);
try { try {
...@@ -404,8 +404,6 @@ public abstract class AtlasBaseClient { ...@@ -404,8 +404,6 @@ public abstract class AtlasBaseClient {
} }
} catch (ClientHandlerException e) { } catch (ClientHandlerException e) {
throw new AtlasServiceException(api, e); throw new AtlasServiceException(api, e);
} catch (IOException e) {
throw new AtlasServiceException(api, e);
} }
} else if (clientResponse.getStatus() != ClientResponse.Status.SERVICE_UNAVAILABLE.getStatusCode()) { } else if (clientResponse.getStatus() != ClientResponse.Status.SERVICE_UNAVAILABLE.getStatusCode()) {
break; break;
...@@ -467,9 +465,9 @@ public abstract class AtlasBaseClient { ...@@ -467,9 +465,9 @@ public abstract class AtlasBaseClient {
return configuration.getInt(AtlasBaseClient.ATLAS_CLIENT_HA_RETRIES_KEY, AtlasBaseClient.DEFAULT_NUM_RETRIES); return configuration.getInt(AtlasBaseClient.ATLAS_CLIENT_HA_RETRIES_KEY, AtlasBaseClient.DEFAULT_NUM_RETRIES);
} }
public byte[] exportData(AtlasExportRequest request) throws AtlasServiceException { public InputStream exportData(AtlasExportRequest request) throws AtlasServiceException {
try { try {
return (byte[]) callAPI(EXPORT, Object.class, request); return (InputStream) callAPI(EXPORT, Object.class, request);
} catch (Exception e) { } catch (Exception e) {
LOG.error("error writing to file", e); LOG.error("error writing to file", e);
throw new AtlasServiceException(e); throw new AtlasServiceException(e);
...@@ -479,14 +477,22 @@ public abstract class AtlasBaseClient { ...@@ -479,14 +477,22 @@ public abstract class AtlasBaseClient {
public void exportData(AtlasExportRequest request, String absolutePath) throws AtlasServiceException { public void exportData(AtlasExportRequest request, String absolutePath) throws AtlasServiceException {
OutputStream fileOutputStream = null; OutputStream fileOutputStream = null;
try { try {
byte[] fileBytes = exportData(request); InputStream inputStream = exportData(request);
fileOutputStream = new FileOutputStream(new File(absolutePath)); fileOutputStream = new FileOutputStream(new File(absolutePath));
IOUtils.write(fileBytes, fileOutputStream); byte[] buffer = new byte[8 * 1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, bytesRead);
}
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(fileOutputStream);
} catch (Exception e) { } catch (Exception e) {
LOG.error("error writing to file", e); LOG.error("error writing to file", e);
throw new AtlasServiceException(e); throw new AtlasServiceException(e);
} finally { } finally {
if(fileOutputStream != null) { if (fileOutputStream != null) {
try { try {
fileOutputStream.close(); fileOutputStream.close();
} catch (IOException e) { } catch (IOException e) {
...@@ -502,9 +508,9 @@ public abstract class AtlasBaseClient { ...@@ -502,9 +508,9 @@ public abstract class AtlasBaseClient {
new FileDataBodyPart(IMPORT_DATA_PARAMETER, new File(absoluteFilePath))); new FileDataBodyPart(IMPORT_DATA_PARAMETER, new File(absoluteFilePath)));
} }
public AtlasImportResult importData(AtlasImportRequest request, byte[] fileData) throws AtlasServiceException { public AtlasImportResult importData(AtlasImportRequest request, InputStream stream) throws AtlasServiceException {
return performImportData(getImportRequestBodyPart(request), return performImportData(getImportRequestBodyPart(request),
new StreamDataBodyPart(IMPORT_DATA_PARAMETER, new ByteArrayInputStream(fileData))); new StreamDataBodyPart(IMPORT_DATA_PARAMETER, stream));
} }
private AtlasImportResult performImportData(BodyPart requestPart, BodyPart filePart) throws AtlasServiceException { private AtlasImportResult performImportData(BodyPart requestPart, BodyPart filePart) throws AtlasServiceException {
......
...@@ -35,17 +35,16 @@ import java.util.Set; ...@@ -35,17 +35,16 @@ import java.util.Set;
public class ImportTransforms { public class ImportTransforms {
private static final Logger LOG = LoggerFactory.getLogger(ImportTransforms.class); private static final Logger LOG = LoggerFactory.getLogger(ImportTransforms.class);
private Map<String, Map<String, List<ImportTransformer>>> transforms; private static final String ALL_ATTRIBUTES = "*";
private Map<String, Map<String, List<ImportTransformer>>> transforms;
public static ImportTransforms fromJson(String jsonString) { public static ImportTransforms fromJson(String jsonString) {
ImportTransforms ret = null; if (StringUtils.isEmpty(jsonString)) {
return null;
if (StringUtils.isNotBlank(jsonString)) {
ret = new ImportTransforms(jsonString);
} }
return ret; return new ImportTransforms(jsonString);
} }
public Map<String, Map<String, List<ImportTransformer>>> getTransforms() { public Map<String, Map<String, List<ImportTransformer>>> getTransforms() {
...@@ -72,13 +71,15 @@ public class ImportTransforms { ...@@ -72,13 +71,15 @@ public class ImportTransforms {
} }
public AtlasEntity.AtlasEntityWithExtInfo apply(AtlasEntity.AtlasEntityWithExtInfo entityWithExtInfo) throws AtlasBaseException { public AtlasEntity.AtlasEntityWithExtInfo apply(AtlasEntity.AtlasEntityWithExtInfo entityWithExtInfo) throws AtlasBaseException {
if (entityWithExtInfo != null) { if (entityWithExtInfo == null) {
apply(entityWithExtInfo.getEntity()); return entityWithExtInfo;
}
if(MapUtils.isNotEmpty(entityWithExtInfo.getReferredEntities())) { apply(entityWithExtInfo.getEntity());
for (AtlasEntity e : entityWithExtInfo.getReferredEntities().values()) {
apply(e); if(MapUtils.isNotEmpty(entityWithExtInfo.getReferredEntities())) {
} for (AtlasEntity e : entityWithExtInfo.getReferredEntities().values()) {
apply(e);
} }
} }
...@@ -86,30 +87,46 @@ public class ImportTransforms { ...@@ -86,30 +87,46 @@ public class ImportTransforms {
} }
public AtlasEntity apply(AtlasEntity entity) throws AtlasBaseException { public AtlasEntity apply(AtlasEntity entity) throws AtlasBaseException {
if(entity != null) { if (entity == null) {
Map<String, List<ImportTransformer>> entityTransforms = getTransforms(entity.getTypeName()); return entity;
}
Map<String, List<ImportTransformer>> entityTransforms = getTransforms(entity.getTypeName());
if (MapUtils.isEmpty(entityTransforms)) {
return entity;
}
applyEntitySpecific(entity, entityTransforms);
if (MapUtils.isNotEmpty(entityTransforms)) { applyAttributeSpecific(entity, entityTransforms);
for (Map.Entry<String, List<ImportTransformer>> entry : entityTransforms.entrySet()) {
String attributeName = entry.getKey();
List<ImportTransformer> attrTransforms = entry.getValue();
if (!entity.hasAttribute(attributeName)) { return entity;
continue; }
}
Object transformedValue = entity.getAttribute(attributeName); private void applyAttributeSpecific(AtlasEntity entity, Map<String, List<ImportTransformer>> entityTransforms) throws AtlasBaseException {
for (Map.Entry<String, List<ImportTransformer>> entry : entityTransforms.entrySet()) {
String attributeName = entry.getKey();
List<ImportTransformer> attrTransforms = entry.getValue();
for (ImportTransformer attrTransform : attrTransforms) { if (!entity.hasAttribute(attributeName)) {
transformedValue = attrTransform.apply(transformedValue); continue;
} }
entity.setAttribute(attributeName, transformedValue); Object attributeValue = entity.getAttribute(attributeName);
} for (ImportTransformer attrTransform : attrTransforms) {
attributeValue = attrTransform.apply(attributeValue);
} }
entity.setAttribute(attributeName, attributeValue);
} }
}
return entity; private void applyEntitySpecific(AtlasEntity entity, Map<String, List<ImportTransformer>> entityTransforms) throws AtlasBaseException {
if(entityTransforms.containsKey(ALL_ATTRIBUTES)) {
for (ImportTransformer attrTransform : entityTransforms.get(ALL_ATTRIBUTES)) {
attrTransform.apply(entity);
}
}
} }
private ImportTransforms() { private ImportTransforms() {
...@@ -119,38 +136,58 @@ public class ImportTransforms { ...@@ -119,38 +136,58 @@ public class ImportTransforms {
private ImportTransforms(String jsonString) { private ImportTransforms(String jsonString) {
this(); this();
if(jsonString != null) { if (StringUtils.isEmpty(jsonString)) {
Map typeTransforms = AtlasType.fromJson(jsonString, Map.class); return;
}
if (MapUtils.isNotEmpty(typeTransforms)) {
for (Object key : typeTransforms.keySet()) { Map typeTransforms = AtlasType.fromJson(jsonString, Map.class);
Object value = typeTransforms.get(key); if (MapUtils.isEmpty(typeTransforms)) {
String entityType = (String) key; return;
Map<String, Object> attributeTransforms = (Map<String, Object>)value; }
if (MapUtils.isNotEmpty(attributeTransforms)) { addOuterMap(typeTransforms);
for (Map.Entry<String, Object> e : attributeTransforms.entrySet()) { }
String attributeName = e.getKey();
List<String> transforms = (List<String>)e.getValue(); private void addOuterMap(Map typeTransforms) {
for (Object key : typeTransforms.keySet()) {
if (CollectionUtils.isNotEmpty(transforms)) { Object value = typeTransforms.get(key);
for (String transform : transforms) { String entityType = (String) key;
ImportTransformer transformers = null; Map<String, Object> attributeTransforms = (Map<String, Object>)value;
try { if (MapUtils.isEmpty(attributeTransforms)) {
transformers = ImportTransformer.getTransformer(transform); continue;
} catch (AtlasBaseException ex) { }
LOG.error("Error converting string to ImportTransformer: {}", transform, ex);
} addInnerMap(entityType, attributeTransforms);
}
if (transformers != null) { }
add(entityType, attributeName, transformers);
} private void addInnerMap(String entityType, Map<String, Object> attributeTransforms) {
} for (Map.Entry<String, Object> e : attributeTransforms.entrySet()) {
} String attributeName = e.getKey();
} List<String> transforms = (List<String>)e.getValue();
}
if (CollectionUtils.isEmpty(transforms)) {
continue;
}
addTransforms(entityType, attributeName, transforms);
}
}
private void addTransforms(String entityType, String attributeName, List<String> transforms) {
for (String transform : transforms) {
ImportTransformer transformers = null;
try {
transformers = ImportTransformer.getTransformer(transform);
if (transformers == null) {
continue;
} }
add(entityType, attributeName, transformers);
} catch (AtlasBaseException ex) {
LOG.error("Error converting string to ImportTransformer: {}", transform, ex);
} }
} }
} }
...@@ -158,21 +195,16 @@ public class ImportTransforms { ...@@ -158,21 +195,16 @@ public class ImportTransforms {
private void add(String typeName, String attributeName, ImportTransformer transformer) { private void add(String typeName, String attributeName, ImportTransformer transformer) {
Map<String, List<ImportTransformer>> attrMap; Map<String, List<ImportTransformer>> attrMap;
if(transforms.containsKey(typeName)) { if(!transforms.containsKey(typeName)) {
attrMap = transforms.get(typeName);
} else {
attrMap = new HashMap<>(); attrMap = new HashMap<>();
transforms.put(typeName, attrMap); transforms.put(typeName, attrMap);
} }
List<ImportTransformer> list; attrMap = transforms.get(typeName);
if(attrMap.containsKey(attributeName)) { if(!attrMap.containsKey(attributeName)) {
list = attrMap.get(attributeName); attrMap.put(attributeName, new ArrayList<ImportTransformer>());
} else {
list = new ArrayList<>();
attrMap.put(attributeName, list);
} }
list.add(transformer); attrMap.get(attributeName).add(transformer);
} }
} }
...@@ -128,6 +128,7 @@ public class ImportTransformsTest { ...@@ -128,6 +128,7 @@ public class ImportTransformsTest {
t.apply(entity); t.apply(entity);
assertEquals(entity.getClassifications().size(), 0);
assertNotNull(t); assertNotNull(t);
assertEquals(entity.getAttribute(ATTR_NAME_QUALIFIED_NAME), expected_qualifiedName); assertEquals(entity.getAttribute(ATTR_NAME_QUALIFIED_NAME), expected_qualifiedName);
} }
...@@ -145,6 +146,7 @@ public class ImportTransformsTest { ...@@ -145,6 +146,7 @@ public class ImportTransformsTest {
assertNotNull(t); assertNotNull(t);
assertEquals(entity.getAttribute(ATTR_NAME_QUALIFIED_NAME), expected_qualifiedName); assertEquals(entity.getAttribute(ATTR_NAME_QUALIFIED_NAME), expected_qualifiedName);
assertEquals(entity.getAttribute(HIVE_TABLE_ATTR_SYNC_INFO), new ArrayList<String>() {{ add(expected_syncInfo); }});
} }
...@@ -161,6 +163,8 @@ public class ImportTransformsTest { ...@@ -161,6 +163,8 @@ public class ImportTransformsTest {
t.apply(entity); t.apply(entity);
assertNotNull(t); assertNotNull(t);
assertNull(entity.getAttribute(HIVE_TABLE_ATTR_REPLICATED_FROM));
assertNull(entity.getAttribute(HIVE_TABLE_ATTR_REPLICATED_TO));
} }
@Test @Test
...@@ -173,6 +177,8 @@ public class ImportTransformsTest { ...@@ -173,6 +177,8 @@ public class ImportTransformsTest {
t.apply(entity); t.apply(entity);
assertNotNull(t); assertNotNull(t);
assertNull(entity.getAttribute(HIVE_TABLE_ATTR_REPLICATED_FROM));
assertNull(entity.getAttribute(HIVE_TABLE_ATTR_REPLICATED_TO));
} }
@Test @Test
...@@ -185,6 +191,7 @@ public class ImportTransformsTest { ...@@ -185,6 +191,7 @@ public class ImportTransformsTest {
t.apply(entity); t.apply(entity);
assertNotNull(t); assertNotNull(t);
assertEquals(entity.getStatus(), AtlasEntity.Status.DELETED);
} }
@Test @Test
......
...@@ -32,15 +32,13 @@ import org.testng.SkipException; ...@@ -32,15 +32,13 @@ import org.testng.SkipException;
import org.testng.annotations.AfterClass; import org.testng.annotations.AfterClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.io.ByteArrayInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.io.InputStream;
import java.nio.file.Paths;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
public class AdminExportImportTestIT extends BaseResourceIT { public class AdminExportImportTestIT extends BaseResourceIT {
...@@ -69,10 +67,10 @@ public class AdminExportImportTestIT extends BaseResourceIT { ...@@ -69,10 +67,10 @@ public class AdminExportImportTestIT extends BaseResourceIT {
final int EXPECTED_CREATION_ORDER_SIZE = 10; final int EXPECTED_CREATION_ORDER_SIZE = 10;
AtlasExportRequest request = TestResourceFileUtils.readObjectFromJson(".", EXPORT_REQUEST_FILE, AtlasExportRequest.class); AtlasExportRequest request = TestResourceFileUtils.readObjectFromJson(".", EXPORT_REQUEST_FILE, AtlasExportRequest.class);
byte[] exportedBytes = atlasClientV2.exportData(request); InputStream exportedStream = atlasClientV2.exportData(request);
assertNotNull(exportedBytes); assertNotNull(exportedStream);
ZipSource zs = new ZipSource(new ByteArrayInputStream(exportedBytes)); ZipSource zs = new ZipSource(exportedStream);
assertNotNull(zs.getExportResult()); assertNotNull(zs.getExportResult());
assertTrue(zs.getCreationOrder().size() > EXPECTED_CREATION_ORDER_SIZE); assertTrue(zs.getCreationOrder().size() > EXPECTED_CREATION_ORDER_SIZE);
} }
...@@ -87,14 +85,15 @@ public class AdminExportImportTestIT extends BaseResourceIT { ...@@ -87,14 +85,15 @@ public class AdminExportImportTestIT extends BaseResourceIT {
private void performImport(String fileToImport, AtlasImportRequest request) throws AtlasServiceException { private void performImport(String fileToImport, AtlasImportRequest request) throws AtlasServiceException {
byte[] fileBytes = new byte[0]; FileInputStream fileInputStream = null;
try { try {
fileBytes = Files.readAllBytes(Paths.get(TestResourceFileUtils.getTestFilePath(fileToImport))); fileInputStream = new FileInputStream(TestResourceFileUtils.getTestFilePath(fileToImport));
} catch (IOException e) { } catch (IOException e) {
assertFalse(true, "Exception: " + e.getMessage()); assertFalse(true, "Exception: " + e.getMessage());
} }
AtlasImportResult result = atlasClientV2.importData(request, fileBytes);
AtlasImportResult result = atlasClientV2.importData(request, fileInputStream);
assertNotNull(result); assertNotNull(result);
assertEquals(result.getOperationStatus(), AtlasImportResult.OperationStatus.SUCCESS); assertEquals(result.getOperationStatus(), AtlasImportResult.OperationStatus.SUCCESS);
assertNotNull(result.getMetrics()); assertNotNull(result.getMetrics());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment