Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
atlas
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
dataplatform
atlas
Commits
be7c4cb6
Commit
be7c4cb6
authored
Jan 19, 2015
by
Venkatesh Seetharam
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ISSUE-38 Map type to graph with type prefixes to enable search. Contributed by Venkatesh Seetharam
parent
1d1531ef
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
254 additions
and
150 deletions
+254
-150
GraphBackedMetadataRepository.java
...adata/repository/graph/GraphBackedMetadataRepository.java
+165
-78
GraphUtils.java
...g/apache/hadoop/metadata/repository/graph/GraphUtils.java
+24
-33
EntityJerseyResourceIT.java
...hadoop/metadata/web/resources/EntityJerseyResourceIT.java
+65
-39
No files found.
repository/src/main/java/org/apache/hadoop/metadata/repository/graph/GraphBackedMetadataRepository.java
View file @
be7c4cb6
...
...
@@ -60,7 +60,6 @@ import java.util.Collections;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.UUID
;
import
java.util.concurrent.atomic.AtomicInteger
;
/**
...
...
@@ -135,6 +134,7 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
throw
new
RepositoryException
(
e
);
}
finally
{
transactionalGraph
.
commit
();
GraphUtils
.
dumpToLog
(
transactionalGraph
);
}
}
...
...
@@ -146,9 +146,11 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
try
{
Vertex
instanceVertex
=
GraphUtils
.
findVertex
(
graph
,
Constants
.
GUID_PROPERTY_KEY
,
guid
);
if
(
instanceVertex
==
null
)
{
LOG
.
debug
(
"Could not find a vertex for guid {}"
,
guid
);
return
null
;
}
LOG
.
debug
(
"Found a vertex {} for guid {}"
,
instanceVertex
,
guid
);
return
graphToInstanceMapper
.
mapGraphToTypedInstance
(
guid
,
instanceVertex
);
}
catch
(
Exception
e
)
{
...
...
@@ -260,37 +262,30 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
public
void
createVerticesForClassTypes
(
TransactionalGraph
transactionalGraph
,
List
<
ITypedReferenceableInstance
>
newInstances
)
{
for
(
ITypedReferenceableInstance
typedInstance
:
newInstances
)
{
final
Vertex
instanceVertex
=
transactionalGraph
.
addVertex
(
null
);
instanceVertex
.
setProperty
(
Constants
.
ENTITY_TYPE_PROPERTY_KEY
,
typedInstance
.
getTypeName
());
// entityVertex.setProperty("entityName", instance.getString("name"));
final
String
guid
=
UUID
.
randomUUID
().
toString
();
instanceVertex
.
setProperty
(
Constants
.
GUID_PROPERTY_KEY
,
guid
);
final
Id
typedInstanceId
=
typedInstance
.
getId
();
instanceVertex
.
setProperty
(
Constants
.
VERSION_PROPERTY_KEY
,
typedInstanceId
.
version
);
idToVertexMap
.
put
(
typedInstanceId
,
instanceVertex
);
final
Vertex
instanceVertex
=
GraphUtils
.
createVertex
(
transactionalGraph
,
typedInstance
);
idToVertexMap
.
put
(
typedInstance
.
getId
(),
instanceVertex
);
}
}
}
private
final
class
TypedInstanceToGraphMapper
{
private
String
mapTypedInstanceToGraph
(
IReferenceableInstance
entity
,
private
String
mapTypedInstanceToGraph
(
IReferenceableInstance
typedInstance
,
TransactionalGraph
transactionalGraph
)
throws
MetadataException
{
EntityProcessor
entityProcessor
=
new
EntityProcessor
();
try
{
new
ObjectGraphWalker
(
typeSystem
,
entityProcessor
,
entity
).
walk
();
LOG
.
debug
(
"Walking the object graph for instance {}"
,
typedInstance
.
getTypeName
());
new
ObjectGraphWalker
(
typeSystem
,
entityProcessor
,
typedInstance
).
walk
();
}
catch
(
MetadataException
me
)
{
throw
new
RepositoryException
(
"TypeSystem error when walking the ObjectGraph"
,
me
);
}
List
<
ITypedReferenceableInstance
>
newTypedInstances
=
discoverInstances
(
entityProcessor
);
entityProcessor
.
createVerticesForClassTypes
(
transactionalGraph
,
newTypedInstances
);
return
addDiscoveredInstances
(
entity
,
entityProcessor
,
newTypedInstances
);
return
addDiscoveredInstances
(
typedInstance
,
entityProcessor
,
newTypedInstances
);
}
/*
...
...
@@ -303,7 +298,7 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
throws
RepositoryException
{
List
<
ITypedReferenceableInstance
>
newTypedInstances
=
new
ArrayList
<>();
for
(
IReferenceableInstance
transientInstance
:
entityProcessor
.
idToInstanceMap
.
values
())
{
LOG
.
debug
(
"
instance {}"
,
transientInstance
);
LOG
.
debug
(
"
Discovered instance {}"
,
transientInstance
.
getTypeName
()
);
try
{
ClassType
cT
=
typeSystem
.
getDataType
(
ClassType
.
class
,
transientInstance
.
getTypeName
());
...
...
@@ -331,13 +326,12 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
EntityProcessor
entityProcessor
,
List
<
ITypedReferenceableInstance
>
newTypedInstances
)
throws
MetadataException
{
String
guid
=
null
;
String
typedInstanceGUID
=
null
;
for
(
ITypedReferenceableInstance
typedInstance
:
newTypedInstances
)
{
// Traverse over newInstances
LOG
.
debug
(
"Adding typed instance {}"
,
typedInstance
);
Id
id
=
typedInstance
.
getId
();
if
(
id
==
null
)
{
// oops
if
(
id
==
null
)
{
// oops
throw
new
RepositoryException
(
"id cannot be null"
);
}
...
...
@@ -346,43 +340,40 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
// add the attributes for the instance
final
Map
<
String
,
AttributeInfo
>
fields
=
typedInstance
.
fieldMapping
().
fields
;
addInstanceToVertex
(
typedInstance
,
instanceVertex
,
fields
,
entityProcessor
.
idToVertexMap
);
mapInstanceToVertex
(
typedInstance
,
instanceVertex
,
fields
,
entityProcessor
.
idToVertexMap
);
for
(
String
traitName
:
typedInstance
.
getTraits
())
{
LOG
.
debug
(
"mapping trait {}"
,
traitName
);
((
TitanVertex
)
instanceVertex
).
addProperty
(
"traits"
,
traitName
);
ITypedStruct
traitInstance
=
(
ITypedStruct
)
typedInstance
.
getTrait
(
traitName
);
// add the attributes for the trait instance
final
String
vertexPropertyName
=
typedInstance
.
getTypeName
()
+
"."
+
traitName
;
instanceVertex
.
setProperty
(
vertexPropertyName
,
traitName
);
addInstanceToVertex
(
traitInstance
,
instanceVertex
,
traitInstance
.
fieldMapping
().
fields
,
entityProcessor
.
idToVertexMap
);
// add the attributes for the trait instance
mapTraitInstanceToVertex
(
traitName
,
traitInstance
,
typedInstance
,
instanceVertex
,
entityProcessor
.
idToVertexMap
);
}
if
(
typedInstance
.
getId
()
==
entity
.
getId
())
{
guid
=
instanceVertex
.
getProperty
(
Constants
.
GUID_PROPERTY_KEY
);
if
(
typedInstance
.
getId
()
==
entity
.
getId
())
{
// save the guid for return
typedInstanceGUID
=
instanceVertex
.
getProperty
(
Constants
.
GUID_PROPERTY_KEY
);
}
}
return
guid
;
return
typedInstanceGUID
;
}
private
void
add
InstanceToVertex
(
ITypedInstance
typedInstance
,
Vertex
instanceVertex
,
private
void
map
InstanceToVertex
(
ITypedInstance
typedInstance
,
Vertex
instanceVertex
,
Map
<
String
,
AttributeInfo
>
fields
,
Map
<
Id
,
Vertex
>
idToVertexMap
)
throws
MetadataException
{
LOG
.
debug
(
"Mapping instance {} to vertex {} for fields {}"
,
typedInstance
,
instanceVertex
,
fields
);
for
(
AttributeInfo
attributeInfo
:
fields
.
values
())
{
System
.
out
.
println
(
"*** attributeInfo = "
+
attributeInfo
);
LOG
.
debug
(
"mapping attributeInfo {}"
,
attributeInfo
);
final
IDataType
dataType
=
attributeInfo
.
dataType
();
final
Object
attributeValue
=
typedInstance
.
get
(
attributeInfo
.
name
);
final
String
vertexPropertyName
=
typedInstance
.
getTypeName
()
+
"."
+
attributeInfo
.
name
;
switch
(
dataType
.
getTypeCategory
())
{
case
PRIMITIVE:
add
PrimitiveToVertex
(
typedInstance
,
instanceVertex
,
attributeInfo
);
map
PrimitiveToVertex
(
typedInstance
,
instanceVertex
,
attributeInfo
);
break
;
case
ENUM:
...
...
@@ -399,23 +390,23 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
break
;
case
STRUCT:
ITypedStruct
structInstance
=
(
ITypedStruct
)
attributeValue
;
addInstanceToVertex
(
structInstance
,
instanceVertex
,
structInstance
.
fieldMapping
().
fields
,
idToVertexMap
);
mapStructInstanceToVertex
(
typedInstance
,
instanceVertex
,
attributeInfo
,
idToVertexMap
);
break
;
case
TRAIT:
ITypedStruct
traitInstance
=
(
ITypedStruct
)
attributeValue
;
addInstanceToVertex
(
traitInstance
,
instanceVertex
,
traitInstance
.
fieldMapping
().
fields
,
idToVertexMap
);
// do NOTHING - this is taken care of earlier
break
;
case
CLASS:
Id
id
=
(
Id
)
typedInstance
.
get
(
attributeInfo
.
name
);
if
(
id
!=
null
)
{
Vertex
referenceVertex
=
idToVertexMap
.
get
(
id
);
GraphUtils
.
addEdge
(
instanceVertex
,
referenceVertex
,
Constants
.
GUID_PROPERTY_KEY
,
id
.
className
);
String
relationshipLabel
=
typedInstance
.
getTypeName
()
+
"."
+
attributeInfo
.
name
;
LOG
.
debug
(
"Adding edge {} -> label {} -> v{}"
,
instanceVertex
,
relationshipLabel
,
referenceVertex
);
instanceVertex
.
addEdge
(
relationshipLabel
,
referenceVertex
);
}
break
;
...
...
@@ -425,9 +416,53 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
}
}
private
void
addPrimitiveToVertex
(
ITypedInstance
typedInstance
,
private
void
mapStructInstanceToVertex
(
ITypedInstance
typedInstance
,
Vertex
parentInstanceVertex
,
AttributeInfo
attributeInfo
,
Map
<
Id
,
Vertex
>
idToVertexMap
)
throws
MetadataException
{
ITypedStruct
structInstance
=
(
ITypedStruct
)
typedInstance
.
get
(
attributeInfo
.
name
);
// add a new vertex for the struct or trait instance
Vertex
structInstanceVertex
=
GraphUtils
.
createVertex
(
graphService
.
getBlueprintsGraph
(),
structInstance
,
((
ITypedReferenceableInstance
)
typedInstance
).
getId
());
LOG
.
debug
(
"created vertex {} for struct {}"
,
structInstanceVertex
,
attributeInfo
.
name
);
// map all the attributes to this newly created vertex
mapInstanceToVertex
(
structInstance
,
structInstanceVertex
,
structInstance
.
fieldMapping
().
fields
,
idToVertexMap
);
// add an edge to the newly created vertex from the parent
String
relationshipLabel
=
attributeInfo
.
dataType
().
getName
()
+
"."
+
attributeInfo
.
name
;
LOG
.
debug
(
"Adding edge for {} -> label {} -> v{}"
,
parentInstanceVertex
,
relationshipLabel
,
structInstanceVertex
);
parentInstanceVertex
.
addEdge
(
relationshipLabel
,
structInstanceVertex
);
}
private
void
mapTraitInstanceToVertex
(
String
traitName
,
ITypedStruct
traitInstance
,
ITypedReferenceableInstance
typedInstance
,
Vertex
parentInstanceVertex
,
Map
<
Id
,
Vertex
>
idToVertexMap
)
throws
MetadataException
{
// add a new vertex for the struct or trait instance
Vertex
traitInstanceVertex
=
GraphUtils
.
createVertex
(
graphService
.
getBlueprintsGraph
(),
traitInstance
,
typedInstance
.
getId
());
LOG
.
debug
(
"created vertex {} for trait {}"
,
traitInstanceVertex
,
traitName
);
// map all the attributes to this newly created vertex
mapInstanceToVertex
(
traitInstance
,
traitInstanceVertex
,
traitInstance
.
fieldMapping
().
fields
,
idToVertexMap
);
// add an edge to the newly created vertex from the parent
String
relationshipLabel
=
typedInstance
.
getTypeName
()
+
"."
+
traitName
;
LOG
.
debug
(
"Adding edge for {} -> label {} -> v{}"
,
parentInstanceVertex
,
relationshipLabel
,
traitInstanceVertex
);
parentInstanceVertex
.
addEdge
(
relationshipLabel
,
traitInstanceVertex
);
}
private
void
mapPrimitiveToVertex
(
ITypedInstance
typedInstance
,
Vertex
instanceVertex
,
AttributeInfo
attributeInfo
)
throws
MetadataException
{
LOG
.
debug
(
"Adding primitive {} to v {}"
,
attributeInfo
,
instanceVertex
);
if
(
typedInstance
.
get
(
attributeInfo
.
name
)
==
null
)
{
// add only if instance has this attribute
return
;
}
...
...
@@ -473,28 +508,44 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
private
ITypedReferenceableInstance
mapGraphToTypedInstance
(
String
guid
,
Vertex
instanceVertex
)
throws
MetadataException
{
LOG
.
debug
(
"Mapping graph root vertex {} to typed instance for guid {}"
,
instanceVertex
,
guid
);
String
typeName
=
instanceVertex
.
getProperty
(
Constants
.
ENTITY_TYPE_PROPERTY_KEY
);
List
<
String
>
traits
=
new
ArrayList
<>();
for
(
TitanProperty
property
:
((
TitanVertex
)
instanceVertex
).
getProperties
(
"traits"
))
{
for
(
TitanProperty
property
:
((
TitanVertex
)
instanceVertex
).
getProperties
(
"traits"
))
{
traits
.
add
((
String
)
property
.
getValue
());
}
Id
id
=
new
Id
(
guid
,
instanceVertex
.<
Integer
>
getProperty
(
"version"
),
typeName
);
LOG
.
debug
(
"Created id {} for instance type {}"
,
id
,
typeName
);
ClassType
classType
=
typeSystem
.
getDataType
(
ClassType
.
class
,
typeName
);
ITypedReferenceableInstance
typedInstance
=
classType
.
createInstance
(
id
,
traits
.
toArray
(
new
String
[
traits
.
size
()]));
graphToInstanceMapper
.
mapVertexToInstance
(
instanceVertex
,
typedInstance
,
classType
.
fieldMapping
().
fields
);
mapVertexToInstance
(
instanceVertex
,
typedInstance
,
classType
.
fieldMapping
().
fields
);
mapVertexToInstanceTraits
(
instanceVertex
,
typedInstance
,
traits
);
return
typedInstance
;
}
private
void
mapVertexToInstanceTraits
(
Vertex
instanceVertex
,
ITypedReferenceableInstance
typedInstance
,
List
<
String
>
traits
)
throws
MetadataException
{
for
(
String
traitName
:
traits
)
{
LOG
.
debug
(
"mapping trait {} to instance"
,
traitName
);
TraitType
traitType
=
typeSystem
.
getDataType
(
TraitType
.
class
,
traitName
);
mapVertexToTraitInstance
(
instanceVertex
,
typedInstance
,
traitName
,
traitType
);
}
}
private
void
mapVertexToInstance
(
Vertex
instanceVertex
,
ITypedInstance
typedInstance
,
Map
<
String
,
AttributeInfo
>
fields
)
throws
MetadataException
{
LOG
.
debug
(
"Mapping vertex {} to instance {} for fields"
,
instanceVertex
,
typedInstance
.
getTypeName
(),
fields
);
for
(
AttributeInfo
attributeInfo
:
fields
.
values
())
{
System
.
out
.
println
(
"***
attributeInfo = "
+
attributeInfo
);
LOG
.
debug
(
"mapping
attributeInfo = "
+
attributeInfo
);
final
IDataType
dataType
=
attributeInfo
.
dataType
();
final
String
vertexPropertyName
=
typedInstance
.
getTypeName
()
+
"."
+
attributeInfo
.
name
;
...
...
@@ -505,9 +556,6 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
break
;
// add only if vertex has this attribute
case
ENUM:
// EnumType enumType = typeSystem.getDataType(
// EnumType.class, attributeInfo.name);
// todo - is this enough
typedInstance
.
setInt
(
attributeInfo
.
name
,
instanceVertex
.<
Integer
>
getProperty
(
vertexPropertyName
));
break
;
...
...
@@ -521,45 +569,83 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
break
;
case
STRUCT:
StructType
structType
=
typeSystem
.
getDataType
(
StructType
.
class
,
attributeInfo
.
name
);
ITypedStruct
structInstance
=
structType
.
createInstance
();
typedInstance
.
set
(
attributeInfo
.
name
,
structInstance
);
mapVertexToInstance
(
instanceVertex
,
structInstance
,
structInstance
.
fieldMapping
().
fields
);
mapVertexToStructInstance
(
instanceVertex
,
typedInstance
,
attributeInfo
);
break
;
case
TRAIT:
TraitType
traitType
=
typeSystem
.
getDataType
(
TraitType
.
class
,
attributeInfo
.
name
);
ITypedStruct
traitInstance
=
(
ITypedStruct
)
((
ITypedReferenceableInstance
)
typedInstance
).
getTrait
(
attributeInfo
.
name
);
typedInstance
.
set
(
attributeInfo
.
name
,
traitInstance
);
mapVertexToInstance
(
instanceVertex
,
traitInstance
,
traitType
.
fieldMapping
().
fields
);
// do NOTHING - handled in class
// mapVertexToCompositeInstance(instanceVertex, typedInstance, attributeInfo);
break
;
case
CLASS:
Id
referenceId
=
null
;
for
(
Edge
edge
:
instanceVertex
.
getEdges
(
Direction
.
IN
))
{
final
Vertex
vertex
=
edge
.
getVertex
(
Direction
.
OUT
);
if
(
vertex
.
getProperty
(
Constants
.
ENTITY_TYPE_PROPERTY_KEY
).
equals
(
attributeInfo
.
name
))
{
referenceId
=
new
Id
(
vertex
.<
String
>
getProperty
(
Constants
.
GUID_PROPERTY_KEY
),
vertex
.<
Integer
>
getProperty
(
Constants
.
VERSION_PROPERTY_KEY
),
String
relationshipLabel
=
typedInstance
.
getTypeName
()
+
"."
+
attributeInfo
.
name
;
LOG
.
debug
(
"Finding edge for {} -> label {} "
,
instanceVertex
,
relationshipLabel
);
for
(
Edge
edge
:
instanceVertex
.
getEdges
(
Direction
.
OUT
,
relationshipLabel
))
{
final
Vertex
referenceVertex
=
edge
.
getVertex
(
Direction
.
IN
);
if
(
referenceVertex
!=
null
)
{
final
String
guid
=
referenceVertex
.
getProperty
(
Constants
.
GUID_PROPERTY_KEY
);
LOG
.
debug
(
"Found vertex {} for label {} with guid {}"
,
referenceVertex
,
relationshipLabel
,
guid
);
if
(
attributeInfo
.
isComposite
)
{
LOG
.
debug
(
"Found composite, mapping vertex to instance"
);
typedInstance
.
set
(
attributeInfo
.
name
,
mapGraphToTypedInstance
(
guid
,
referenceVertex
));
}
else
{
Id
referenceId
=
new
Id
(
guid
,
referenceVertex
.<
Integer
>
getProperty
(
Constants
.
VERSION_PROPERTY_KEY
),
attributeInfo
.
name
);
LOG
.
debug
(
"Found non-composite, adding id {} "
,
referenceId
);
typedInstance
.
set
(
attributeInfo
.
name
,
referenceId
);
}
break
;
}
}
break
;
if
(
referenceId
!=
null
)
{
typedInstance
.
set
(
attributeInfo
.
name
,
referenceId
);
default
:
break
;
}
}
}
private
void
mapVertexToStructInstance
(
Vertex
instanceVertex
,
ITypedInstance
typedInstance
,
AttributeInfo
attributeInfo
)
throws
MetadataException
{
LOG
.
debug
(
"mapping vertex {} to struct {}"
,
typedInstance
,
attributeInfo
.
name
);
StructType
structType
=
typeSystem
.
getDataType
(
StructType
.
class
,
attributeInfo
.
dataType
().
getName
());
ITypedStruct
structInstance
=
structType
.
createInstance
();
typedInstance
.
set
(
attributeInfo
.
name
,
structInstance
);
String
relationshipLabel
=
typedInstance
.
getTypeName
()
+
"."
+
attributeInfo
.
name
;
LOG
.
debug
(
"Finding edge for {} -> label {} "
,
instanceVertex
,
relationshipLabel
);
for
(
Edge
edge
:
instanceVertex
.
getEdges
(
Direction
.
OUT
,
relationshipLabel
))
{
final
Vertex
structInstanceVertex
=
edge
.
getVertex
(
Direction
.
IN
);
if
(
structInstanceVertex
!=
null
)
{
LOG
.
debug
(
"Found struct instance vertex {}, mapping to instance {} "
,
structInstanceVertex
,
structInstance
.
getTypeName
());
mapVertexToInstance
(
structInstanceVertex
,
structInstance
,
structType
.
fieldMapping
().
fields
);
break
;
}
}
}
default
:
private
void
mapVertexToTraitInstance
(
Vertex
instanceVertex
,
ITypedReferenceableInstance
typedInstance
,
String
traitName
,
TraitType
traitType
)
throws
MetadataException
{
ITypedStruct
traitInstance
=
(
ITypedStruct
)
typedInstance
.
getTrait
(
traitName
);
String
relationshipLabel
=
typedInstance
.
getTypeName
()
+
"."
+
traitName
;
LOG
.
debug
(
"Finding edge for {} -> label {} "
,
instanceVertex
,
relationshipLabel
);
for
(
Edge
edge
:
instanceVertex
.
getEdges
(
Direction
.
OUT
,
relationshipLabel
))
{
final
Vertex
traitInstanceVertex
=
edge
.
getVertex
(
Direction
.
IN
);
if
(
traitInstanceVertex
!=
null
)
{
LOG
.
debug
(
"Found trait instance vertex {}, mapping to instance {} "
,
traitInstanceVertex
,
traitInstance
.
getTypeName
());
mapVertexToInstance
(
traitInstanceVertex
,
traitInstance
,
traitType
.
fieldMapping
().
fields
);
break
;
}
}
...
...
@@ -568,6 +654,7 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
private
void
mapVertexToInstance
(
Vertex
instanceVertex
,
ITypedInstance
typedInstance
,
AttributeInfo
attributeInfo
)
throws
MetadataException
{
LOG
.
debug
(
"Adding primitive {} from vertex {}"
,
attributeInfo
,
instanceVertex
);
final
String
vertexPropertyName
=
typedInstance
.
getTypeName
()
+
"."
+
attributeInfo
.
name
;
if
(
instanceVertex
.
getProperty
(
vertexPropertyName
)
==
null
)
{
return
;
...
...
repository/src/main/java/org/apache/hadoop/metadata/repository/graph/GraphUtils.java
View file @
be7c4cb6
...
...
@@ -23,10 +23,14 @@ import com.tinkerpop.blueprints.Edge;
import
com.tinkerpop.blueprints.Graph
;
import
com.tinkerpop.blueprints.GraphQuery
;
import
com.tinkerpop.blueprints.Vertex
;
import
org.apache.hadoop.metadata.ITypedInstance
;
import
org.apache.hadoop.metadata.ITypedReferenceableInstance
;
import
org.apache.hadoop.metadata.storage.Id
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.util.Iterator
;
import
java.util.UUID
;
/**
* Utility class for graph operations.
...
...
@@ -38,40 +42,26 @@ public final class GraphUtils {
private
GraphUtils
()
{
}
public
static
Edge
addEdge
(
Vertex
fromVertex
,
Vertex
toVertex
,
String
vertexPropertyKey
,
String
edgeLabel
)
{
return
addEdge
(
fromVertex
,
toVertex
,
vertexPropertyKey
,
edgeLabel
,
null
);
public
static
Vertex
createVertex
(
Graph
graph
,
ITypedReferenceableInstance
typedInstance
)
{
return
createVertex
(
graph
,
typedInstance
,
typedInstance
.
getId
()
);
}
public
static
Edge
addEdge
(
Vertex
fromVertex
,
Vertex
toVertex
,
String
vertexPropertyKey
,
String
edgeLabel
,
String
timestamp
)
{
Edge
edge
=
findEdge
(
fromVertex
,
toVertex
,
vertexPropertyKey
,
edgeLabel
);
public
static
Vertex
createVertex
(
Graph
graph
,
ITypedInstance
typedInstance
,
Id
typedInstanceId
)
{
final
Vertex
instanceVertex
=
graph
.
addVertex
(
null
);
// type
instanceVertex
.
setProperty
(
Constants
.
ENTITY_TYPE_PROPERTY_KEY
,
typedInstance
.
getTypeName
());
Edge
edgeToVertex
=
edge
!=
null
?
edge
:
fromVertex
.
addEdge
(
edgeLabel
,
toVertex
);
if
(
timestamp
!=
null
)
{
edgeToVertex
.
setProperty
(
Constants
.
TIMESTAMP_PROPERTY_KEY
,
timestamp
);
}
return
edgeToVertex
;
}
// id
final
String
guid
=
UUID
.
randomUUID
().
toString
();
instanceVertex
.
setProperty
(
Constants
.
GUID_PROPERTY_KEY
,
guid
);
public
static
Edge
findEdge
(
Vertex
fromVertex
,
Vertex
toVertex
,
String
vertexPropertyKey
,
String
edgeLabel
)
{
return
findEdge
(
fromVertex
,
toVertex
.
getProperty
(
vertexPropertyKey
),
vertexPropertyKey
,
edgeLabel
);
}
public
static
Edge
findEdge
(
Vertex
fromVertex
,
Object
toVertexName
,
String
vertexPropertyKey
,
String
edgeLabel
)
{
Edge
edgeToFind
=
null
;
for
(
Edge
edge
:
fromVertex
.
getEdges
(
Direction
.
OUT
,
edgeLabel
))
{
if
(
edge
.
getVertex
(
Direction
.
IN
).
getProperty
(
vertexPropertyKey
).
equals
(
toVertexName
))
{
edgeToFind
=
edge
;
break
;
}
}
// version
instanceVertex
.
setProperty
(
Constants
.
VERSION_PROPERTY_KEY
,
typedInstanceId
.
version
);
return
edgeToFind
;
return
instanceVertex
;
}
public
static
Vertex
findVertex
(
Graph
blueprintsGraph
,
...
...
@@ -97,23 +87,23 @@ public final class GraphUtils {
public
static
String
edgeString
(
final
Edge
edge
)
{
return
"e["
+
edge
.
getLabel
()
+
"], ["
+
edge
.
getVertex
(
Direction
.
OUT
)
.
getProperty
(
"name"
)
+
edge
.
getVertex
(
Direction
.
OUT
)
+
" -> "
+
edge
.
getLabel
()
+
" -> "
+
edge
.
getVertex
(
Direction
.
IN
)
.
getProperty
(
"name"
)
+
edge
.
getVertex
(
Direction
.
IN
)
+
"]"
;
}
public
static
void
dumpToLog
(
final
Graph
graph
)
{
LOG
.
debug
(
"*******************Graph Dump****************************"
);
LOG
.
debug
(
"Vertices of {}"
,
graph
);
for
(
Vertex
vertex
:
graph
.
getVertices
())
{
LOG
.
debug
(
vertexString
(
vertex
));
System
.
out
.
println
(
vertexString
(
vertex
));
}
LOG
.
debug
(
"Edges of {}"
,
graph
);
for
(
Edge
edge
:
graph
.
getEdges
())
{
LOG
.
debug
(
edgeString
(
edge
));
System
.
out
.
println
(
edgeString
(
edge
));
}
LOG
.
debug
(
"*******************Graph Dump****************************"
);
}
}
\ No newline at end of file
webapp/src/test/java/org/apache/hadoop/metadata/web/resources/EntityJerseyResourceIT.java
View file @
be7c4cb6
...
...
@@ -34,15 +34,16 @@ import org.apache.hadoop.metadata.types.Multiplicity;
import
org.apache.hadoop.metadata.types.StructTypeDefinition
;
import
org.apache.hadoop.metadata.types.TraitType
;
import
org.codehaus.jettison.json.JSONObject
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.testng.Assert
;
import
org.testng.annotations.BeforeClass
;
import
org.testng.annotations.Test
;
import
scala.actors.threadpool.Arrays
;
import
javax.ws.rs.HttpMethod
;
import
javax.ws.rs.core.MediaType
;
import
javax.ws.rs.core.Response
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.UUID
;
/**
...
...
@@ -50,28 +51,29 @@ import java.util.UUID;
*/
public
class
EntityJerseyResourceIT
extends
BaseResourceIT
{
private
static
final
Logger
LOG
=
LoggerFactory
.
getLogger
(
EntityJerseyResourceIT
.
class
);
private
static
final
String
DATABASE_TYPE
=
"hive_database"
;
private
static
final
String
DATABASE_NAME
=
"foo"
;
private
static
final
String
TABLE_TYPE
=
"hive_table"
;
private
static
final
String
TABLE_NAME
=
"bar"
;
private
static
final
String
TRAIT_TYPE
=
"hive_fetl"
;
private
String
tableInstanceAsJSON
;
private
ITypedReferenceableInstance
tableInstance
;
private
String
guid
;
@BeforeClass
public
void
setUp
()
throws
Exception
{
super
.
setUp
();
List
<
HierarchicalTypeDefinition
>
typeDefinitions
=
createHiveTypes
();
submitTypes
(
typeDefinitions
);
createHiveTypes
();
submitTypes
();
}
@Test
public
void
testSubmitEntity
()
throws
Exception
{
ITypedReferenceableInstance
tableInstance
=
createHiveTableInstance
();
tableInstanceAsJSON
=
Serialization
$
.
MODULE
$
.
toJson
(
tableInstance
);
tableInstance
=
createHiveTableInstance
();
String
tableInstanceAsJSON
=
Serialization
$
.
MODULE
$
.
toJson
(
tableInstance
);
LOG
.
debug
(
"tableInstance = "
+
tableInstanceAsJSON
);
WebResource
resource
=
service
.
path
(
"api/metadata/entities/submit"
)
...
...
@@ -119,12 +121,24 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
final
String
definition
=
response
.
getString
(
"definition"
);
Assert
.
assertNotNull
(
definition
);
LOG
.
debug
(
"tableInstanceAfterGet = "
+
definition
);
// todo - this fails with type error, strange
// ITypedReferenceableInstance tableInstanceAfterGet = Serialization$.MODULE$.fromJson(definition);
// Assert.assertTrue(areEqual(tableInstance, tableInstanceAfterGet));
}
System
.
out
.
println
(
"definition = "
+
definition
);
System
.
out
.
println
(
"tableInstanceAsJSON = "
+
tableInstanceAsJSON
);
// Assert.assertEquals(definition, tableInstanceAsJSON);
/*
private boolean areEqual(ITypedReferenceableInstance actual,
ITypedReferenceableInstance expected) throws Exception {
for (AttributeInfo attributeInfo : actual.fieldMapping().fields.values()) {
Assert.assertEquals(actual.get(attributeInfo.name), expected.get(attributeInfo.name));
}
return true;
}
*/
@Test
public
void
testGetInvalidEntityDefinition
()
{
WebResource
resource
=
service
...
...
@@ -165,15 +179,19 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
System
.
out
.
println
(
"response = "
+
response
);
}
private
List
<
HierarchicalTypeDefinition
>
createHiveTypes
()
throws
Exception
{
ArrayList
<
HierarchicalTypeDefinition
>
typeDefinitions
=
new
ArrayList
<>();
private
void
createHiveTypes
()
throws
Exception
{
HierarchicalTypeDefinition
<
ClassType
>
databaseTypeDefinition
=
createClassTypeDef
(
DATABASE_TYPE
,
ImmutableList
.<
String
>
of
(),
createRequiredAttrDef
(
"name"
,
DataTypes
.
STRING_TYPE
),
createRequiredAttrDef
(
"description"
,
DataTypes
.
STRING_TYPE
));
typeDefinitions
.
add
(
databaseTypeDefinition
);
StructTypeDefinition
structTypeDefinition
=
new
StructTypeDefinition
(
"serdeType"
,
new
AttributeDefinition
[]
{
createRequiredAttrDef
(
"name"
,
DataTypes
.
STRING_TYPE
),
createRequiredAttrDef
(
"serde"
,
DataTypes
.
STRING_TYPE
)
});
HierarchicalTypeDefinition
<
ClassType
>
tableTypeDefinition
=
createClassTypeDef
(
TABLE_TYPE
,
...
...
@@ -181,32 +199,31 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
createRequiredAttrDef
(
"name"
,
DataTypes
.
STRING_TYPE
),
createRequiredAttrDef
(
"description"
,
DataTypes
.
STRING_TYPE
),
createRequiredAttrDef
(
"type"
,
DataTypes
.
STRING_TYPE
),
new
AttributeDefinition
(
DATABASE_TYPE
,
DATABASE_TYPE
,
Multiplicity
.
REQUIRED
,
true
,
DATABASE_TYPE
));
typeDefinitions
.
add
(
tableTypeDefinition
);
HierarchicalTypeDefinition
<
TraitType
>
fetlTypeDefinition
=
createTraitTypeDef
(
TRAIT_TYPE
,
new
AttributeDefinition
(
"serde1"
,
"serdeType"
,
Multiplicity
.
REQUIRED
,
false
,
null
),
new
AttributeDefinition
(
"serde2"
,
"serdeType"
,
Multiplicity
.
REQUIRED
,
false
,
null
),
new
AttributeDefinition
(
"database"
,
DATABASE_TYPE
,
Multiplicity
.
REQUIRED
,
true
,
null
));
HierarchicalTypeDefinition
<
TraitType
>
classificationTypeDefinition
=
createTraitTypeDef
(
"classification"
,
ImmutableList
.<
String
>
of
(),
createRequiredAttrDef
(
"level"
,
DataTypes
.
INT_TYPE
));
typeDefinitions
.
add
(
fetlTypeDefinition
);
createRequiredAttrDef
(
"tag"
,
DataTypes
.
STRING_TYPE
));
typeSystem
.
defineTypes
(
ImmutableList
.
<
StructTypeDefinition
>
of
(
),
ImmutableList
.
of
(
fetl
TypeDefinition
),
ImmutableList
.
of
(
structTypeDefinition
),
ImmutableList
.
of
(
classification
TypeDefinition
),
ImmutableList
.
of
(
databaseTypeDefinition
,
tableTypeDefinition
));
return
typeDefinitions
;
}
private
void
submitTypes
(
List
<
HierarchicalTypeDefinition
>
typeDefinitions
)
throws
Exception
{
for
(
HierarchicalTypeDefinition
typeDefinition
:
typeDefinitions
)
{
String
typesAsJSON
=
TypesSerialization
.
toJson
(
typeSystem
,
typeDefinition
.
typeName
);
private
void
submitTypes
()
throws
Exception
{
String
typesAsJSON
=
TypesSerialization
.
toJson
(
typeSystem
,
Arrays
.
asList
(
new
String
[]{
DATABASE_TYPE
,
TABLE_TYPE
,
"serdeType"
,
"classification"
}));
WebResource
resource
=
service
.
path
(
"api/metadata/types/submit"
)
.
path
(
typeDefinition
.
typeName
);
.
path
(
TABLE_TYPE
);
ClientResponse
clientResponse
=
resource
.
accept
(
MediaType
.
APPLICATION_JSON
)
...
...
@@ -218,25 +235,34 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
Assert
.
assertNotNull
(
responseAsString
);
JSONObject
response
=
new
JSONObject
(
responseAsString
);
Assert
.
assertEquals
(
response
.
get
(
"typeName"
),
typeDefinition
.
typeName
);
Assert
.
assertEquals
(
response
.
get
(
"typeName"
),
TABLE_TYPE
);
Assert
.
assertNotNull
(
response
.
get
(
"types"
));
Assert
.
assertNotNull
(
response
.
get
(
"requestId"
));
}
}
protected
ITypedReferenceableInstance
createHiveTableInstance
()
throws
Exception
{
Referenceable
databaseInstance
=
new
Referenceable
(
DATABASE_TYPE
);
databaseInstance
.
set
(
"name"
,
DATABASE_NAME
);
databaseInstance
.
set
(
"description"
,
"foo database"
);
Referenceable
tableInstance
=
new
Referenceable
(
TABLE_TYPE
,
TRAIT_TYPE
);
Referenceable
tableInstance
=
new
Referenceable
(
TABLE_TYPE
,
"classification"
);
tableInstance
.
set
(
"name"
,
TABLE_NAME
);
tableInstance
.
set
(
"description"
,
"bar table"
);
tableInstance
.
set
(
"type"
,
"managed"
);
tableInstance
.
set
(
DATABASE_TYPE
,
databaseInstance
);
tableInstance
.
set
(
"database"
,
databaseInstance
);
Struct
traitInstance
=
(
Struct
)
tableInstance
.
getTrait
(
"classification"
);
traitInstance
.
set
(
"tag"
,
"foundation_etl"
);
Struct
serde1Instance
=
new
Struct
(
"serdeType"
);
serde1Instance
.
set
(
"name"
,
"serde1"
);
serde1Instance
.
set
(
"serde"
,
"serde1"
);
tableInstance
.
set
(
"serde1"
,
serde1Instance
);
Struct
traitInstance
=
(
Struct
)
tableInstance
.
getTrait
(
TRAIT_TYPE
);
traitInstance
.
set
(
"level"
,
1
);
Struct
serde2Instance
=
new
Struct
(
"serdeType"
);
serde2Instance
.
set
(
"name"
,
"serde2"
);
serde2Instance
.
set
(
"serde"
,
"serde2"
);
tableInstance
.
set
(
"serde2"
,
serde2Instance
);
ClassType
tableType
=
typeSystem
.
getDataType
(
ClassType
.
class
,
TABLE_TYPE
);
return
tableType
.
convert
(
tableInstance
,
Multiplicity
.
REQUIRED
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment