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
386d8d38
Commit
386d8d38
authored
Jan 16, 2015
by
Venkatesh Seetharam
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ISSUE-35 Add typed instance put/get end to end. Contributed by Venkatesh Seetharam
parent
8285e3b9
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
564 additions
and
300 deletions
+564
-300
DefaultMetadataService.java
...ache/hadoop/metadata/services/DefaultMetadataService.java
+9
-16
GraphBackedMetadataRepository.java
...doop/metadata/services/GraphBackedMetadataRepository.java
+370
-218
MetadataService.java
.../org/apache/hadoop/metadata/services/MetadataService.java
+0
-9
GraphUtils.java
...main/java/org/apache/hadoop/metadata/util/GraphUtils.java
+36
-0
EntityResource.java
.../apache/hadoop/metadata/web/resources/EntityResource.java
+6
-6
BaseResourceIT.java
.../apache/hadoop/metadata/web/resources/BaseResourceIT.java
+6
-1
EntityJerseyResourceIT.java
...hadoop/metadata/web/resources/EntityJerseyResourceIT.java
+134
-44
TypesJerseyResourceIT.java
.../hadoop/metadata/web/resources/TypesJerseyResourceIT.java
+3
-6
No files found.
repository/src/main/java/org/apache/hadoop/metadata/services/DefaultMetadataService.java
View file @
386d8d38
...
...
@@ -83,8 +83,10 @@ public class DefaultMetadataService implements MetadataService {
return
response
;
}
catch
(
ParseException
e
)
{
LOG
.
error
(
"Unable to parse JSON for type {}"
,
typeName
,
e
);
throw
new
MetadataException
(
"validation failed for: "
+
typeName
);
}
catch
(
JSONException
e
)
{
LOG
.
error
(
"Unable to persist type {}"
,
typeName
,
e
);
throw
new
MetadataException
(
"Unable to create response for: "
+
typeName
);
}
}
...
...
@@ -147,6 +149,7 @@ public class DefaultMetadataService implements MetadataService {
Serialization
$
.
MODULE
$
.
fromJson
(
entityDefinition
);
return
repository
.
createEntity
(
entityInstance
,
entityType
);
}
catch
(
ParseException
e
)
{
LOG
.
error
(
"Unable to parse JSON {} for type {}"
,
entityDefinition
,
entityType
,
e
);
throw
new
MetadataException
(
"validation failed for: "
+
entityType
);
}
}
...
...
@@ -154,7 +157,9 @@ public class DefaultMetadataService implements MetadataService {
private
void
validateEntity
(
String
entity
,
String
entityType
)
throws
ParseException
{
Preconditions
.
checkNotNull
(
entity
,
"entity cannot be null"
);
Preconditions
.
checkNotNull
(
entityType
,
"entity type cannot be null"
);
JSONValue
.
parseWithException
(
entity
);
// todo: this is failing for instances but not types
// JSONValue.parseWithException(entity);
}
/**
...
...
@@ -167,21 +172,9 @@ public class DefaultMetadataService implements MetadataService {
public
String
getEntityDefinition
(
String
guid
)
throws
MetadataException
{
final
ITypedReferenceableInstance
instance
=
repository
.
getEntityDefinition
(
guid
);
return
Serialization
$
.
MODULE
$
.
toJson
(
instance
);
}
/**
* Return the definition for the given entity name and type.
*
* @param entityName name
* @param entityType type
* @return entity definition as JSON
*/
@Override
public
String
getEntityDefinition
(
String
entityName
,
String
entityType
)
throws
MetadataException
{
throw
new
UnsupportedOperationException
();
return
instance
==
null
?
null
:
Serialization
$
.
MODULE
$
.
toJson
(
instance
);
}
/**
...
...
repository/src/main/java/org/apache/hadoop/metadata/services/GraphBackedMetadataRepository.java
View file @
386d8d38
...
...
@@ -18,8 +18,12 @@
package
org
.
apache
.
hadoop
.
metadata
.
services
;
import
com.thinkaurelius.titan.core.TitanProperty
;
import
com.thinkaurelius.titan.core.TitanVertex
;
import
com.tinkerpop.blueprints.Direction
;
import
com.tinkerpop.blueprints.Edge
;
import
com.tinkerpop.blueprints.Graph
;
import
com.tinkerpop.blueprints.GraphQuery
;
import
com.tinkerpop.blueprints.TransactionalGraph
;
import
com.tinkerpop.blueprints.Vertex
;
import
org.apache.hadoop.metadata.IReferenceableInstance
;
...
...
@@ -36,20 +40,25 @@ import org.apache.hadoop.metadata.types.DataTypes;
import
org.apache.hadoop.metadata.types.IDataType
;
import
org.apache.hadoop.metadata.types.Multiplicity
;
import
org.apache.hadoop.metadata.types.ObjectGraphWalker
;
import
org.apache.hadoop.metadata.types.StructType
;
import
org.apache.hadoop.metadata.types.TraitType
;
import
org.apache.hadoop.metadata.types.TypeSystem
;
import
org.apache.hadoop.metadata.util.GraphUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
javax.inject.Inject
;
import
java.io.IOException
;
import
java.math.BigDecimal
;
import
java.math.BigInteger
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.UUID
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.atomic.AtomicInteger
;
import
javax.inject.Inject
;
/**
* An implementation backed by a Graph database provided
...
...
@@ -60,25 +69,26 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
private
static
final
Logger
LOG
=
LoggerFactory
.
getLogger
(
GraphBackedMetadataRepository
.
class
);
private
static
final
String
GUID_PROPERTY_KEY
=
"guid"
;
private
static
final
String
TIMESTAMP_PROPERTY_KEY
=
"timestamp"
;
private
static
final
String
GUID_PROPERTY_KEY
=
"GUID"
;
private
static
final
String
ENTITY_TYPE_PROPERTY_KEY
=
"entityType"
;
private
static
final
String
VERSION_PROPERTY_KEY
=
"version"
;
private
static
final
String
TRAIT_PROPERTY_SUFFIX
=
"trait."
;
private
final
AtomicInteger
ID_SEQ
=
new
AtomicInteger
(
0
);
// todo: remove this
private
final
ConcurrentHashMap
<
String
,
ITypedReferenceableInstance
>
instances
;
private
final
TypedInstanceToGraphMapper
instanceToGraphMapper
=
new
TypedInstanceToGraphMapper
();
private
final
GraphToTypedInstanceMapper
graphToInstanceMapper
=
new
GraphToTypedInstanceMapper
();
private
final
GraphService
graphService
;
private
final
TypeSystem
typeSystem
;
@Inject
GraphBackedMetadataRepository
(
GraphService
graphService
)
throws
MetadataException
{
this
.
instances
=
new
ConcurrentHashMap
<>();
this
.
graphService
=
graphService
;
this
.
typeSystem
=
TypeSystem
.
getInstance
();
this
.
graphService
=
graphService
;
this
.
typeSystem
=
TypeSystem
.
getInstance
();
}
/**
...
...
@@ -121,16 +131,7 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
transactionalGraph
.
rollback
();
EntityProcessor
entityProcessor
=
new
EntityProcessor
();
try
{
new
ObjectGraphWalker
(
typeSystem
,
entityProcessor
,
entity
).
walk
();
}
catch
(
MetadataException
me
)
{
throw
new
RepositoryException
(
"TypeSystem error when walking the ObjectGraph"
,
me
);
}
List
<
ITypedReferenceableInstance
>
newInstances
=
discoverInstances
(
entityProcessor
);
entityProcessor
.
createVerticesForClasses
(
transactionalGraph
,
newInstances
);
return
addDiscoveredInstances
(
entity
,
entityProcessor
,
newInstances
);
return
instanceToGraphMapper
.
mapTypedInstanceToGraph
(
entity
,
transactionalGraph
);
}
catch
(
MetadataException
e
)
{
transactionalGraph
.
rollback
();
...
...
@@ -140,204 +141,27 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
}
}
private
String
addDiscoveredInstances
(
IReferenceableInstance
entity
,
EntityProcessor
entityProcessor
,
List
<
ITypedReferenceableInstance
>
newInstances
)
throws
MetadataException
{
String
guid
=
null
;
for
(
ITypedReferenceableInstance
instance
:
newInstances
)
{
// Traverse over newInstances
Id
id
=
instance
.
getId
();
if
(
id
==
null
)
{
// oops
throw
new
RepositoryException
(
"id cannot be null"
);
}
Vertex
entityVertex
=
entityProcessor
.
idToVertexMap
.
get
(
id
);
instances
.
put
((
String
)
entityVertex
.
getProperty
(
GUID_PROPERTY_KEY
),
instance
);
// add the attributes for the instance
final
Map
<
String
,
AttributeInfo
>
fields
=
instance
.
fieldMapping
().
fields
;
addInstanceToVertex
(
instance
,
entityVertex
,
fields
,
entityProcessor
.
idToVertexMap
);
for
(
String
traitName
:
instance
.
getTraits
())
{
ITypedStruct
traitInstance
=
(
ITypedStruct
)
instance
.
getTrait
(
traitName
);
// add the attributes for the trait instance
entityVertex
.
setProperty
(
TRAIT_PROPERTY_SUFFIX
+
traitName
,
traitName
);
addInstanceToVertex
(
traitInstance
,
entityVertex
,
traitInstance
.
fieldMapping
().
fields
,
entityProcessor
.
idToVertexMap
);
}
if
(
instance
.
getId
()
==
entity
.
getId
())
{
guid
=
entityVertex
.
getProperty
(
GUID_PROPERTY_KEY
);
}
}
return
guid
;
}
private
void
addInstanceToVertex
(
ITypedInstance
instance
,
Vertex
entityVertex
,
Map
<
String
,
AttributeInfo
>
fields
,
Map
<
Id
,
Vertex
>
idToVertexMap
)
throws
MetadataException
{
for
(
AttributeInfo
attributeInfo
:
fields
.
values
())
{
System
.
out
.
println
(
"*** attributeInfo = "
+
attributeInfo
);
final
IDataType
dataType
=
attributeInfo
.
dataType
();
Object
attributeValue
=
instance
.
get
(
attributeInfo
.
name
);
switch
(
dataType
.
getTypeCategory
())
{
case
PRIMITIVE:
addPrimitiveToVertex
(
instance
,
entityVertex
,
attributeInfo
);
break
;
case
ENUM:
addToVertex
(
entityVertex
,
attributeInfo
.
name
,
instance
.
getInt
(
attributeInfo
.
name
));
break
;
case
ARRAY:
// todo - Add to/from json for collections
break
;
case
MAP:
// todo - Add to/from json for collections
break
;
case
STRUCT:
ITypedStruct
structInstance
=
(
ITypedStruct
)
attributeValue
;
addInstanceToVertex
(
structInstance
,
entityVertex
,
structInstance
.
fieldMapping
().
fields
,
idToVertexMap
);
break
;
case
TRAIT:
ITypedStruct
traitInstance
=
(
ITypedStruct
)
attributeValue
;
addInstanceToVertex
(
traitInstance
,
entityVertex
,
traitInstance
.
fieldMapping
().
fields
,
idToVertexMap
);
break
;
case
CLASS:
Id
id
=
(
Id
)
instance
.
get
(
attributeInfo
.
name
);
if
(
id
!=
null
)
{
Vertex
referenceVertex
=
idToVertexMap
.
get
(
id
);
addEdge
(
entityVertex
,
referenceVertex
,
"references"
);
}
break
;
default
:
break
;
}
}
}
protected
Edge
addEdge
(
Vertex
fromVertex
,
Vertex
toVertex
,
String
edgeLabel
)
{
return
addEdge
(
fromVertex
,
toVertex
,
edgeLabel
,
null
);
}
protected
Edge
addEdge
(
Vertex
fromVertex
,
Vertex
toVertex
,
String
edgeLabel
,
String
timestamp
)
{
Edge
edge
=
findEdge
(
fromVertex
,
toVertex
,
edgeLabel
);
Edge
edgeToVertex
=
edge
!=
null
?
edge
:
fromVertex
.
addEdge
(
edgeLabel
,
toVertex
);
if
(
timestamp
!=
null
)
{
edgeToVertex
.
setProperty
(
TIMESTAMP_PROPERTY_KEY
,
timestamp
);
}
return
edgeToVertex
;
}
protected
Edge
findEdge
(
Vertex
fromVertex
,
Vertex
toVertex
,
String
edgeLabel
)
{
return
findEdge
(
fromVertex
,
toVertex
.
getProperty
(
GUID_PROPERTY_KEY
),
edgeLabel
);
}
protected
Edge
findEdge
(
Vertex
fromVertex
,
Object
toVertexName
,
String
edgeLabel
)
{
Edge
edgeToFind
=
null
;
for
(
Edge
edge
:
fromVertex
.
getEdges
(
Direction
.
OUT
,
edgeLabel
))
{
if
(
edge
.
getVertex
(
Direction
.
IN
).
getProperty
(
GUID_PROPERTY_KEY
).
equals
(
toVertexName
))
{
edgeToFind
=
edge
;
break
;
}
}
return
edgeToFind
;
}
/*
* Step 2: Traverse oldIdToInstance map create newInstances :
* List[ITypedReferenceableInstance]
* - create a ITypedReferenceableInstance.
* replace any old References ( ids or object references) with new Ids.
*/
private
List
<
ITypedReferenceableInstance
>
discoverInstances
(
EntityProcessor
entityProcessor
)
throws
RepositoryException
{
List
<
ITypedReferenceableInstance
>
newInstances
=
new
ArrayList
<>();
for
(
IReferenceableInstance
transientInstance
:
entityProcessor
.
idToInstanceMap
.
values
())
{
LOG
.
debug
(
"instance {}"
,
transientInstance
);
try
{
ClassType
cT
=
typeSystem
.
getDataType
(
ClassType
.
class
,
transientInstance
.
getTypeName
());
ITypedReferenceableInstance
newInstance
=
cT
.
convert
(
transientInstance
,
Multiplicity
.
REQUIRED
);
newInstances
.
add
(
newInstance
);
/*
* Now replace old references with new Ids
*/
MapIds
mapIds
=
new
MapIds
(
entityProcessor
.
idToNewIdMap
);
new
ObjectGraphWalker
(
typeSystem
,
mapIds
,
newInstances
).
walk
();
@Override
public
ITypedReferenceableInstance
getEntityDefinition
(
String
guid
)
throws
RepositoryException
{
LOG
.
info
(
"Retrieving entity with guid={}"
,
guid
);
}
catch
(
MetadataException
me
)
{
throw
new
RepositoryException
(
String
.
format
(
"Failed to create Instance(id = %s"
,
transientInstance
.
getId
()),
me
);
final
Graph
graph
=
graphService
.
getBlueprintsGraph
();
try
{
GraphQuery
query
=
graph
.
query
().
has
(
GUID_PROPERTY_KEY
,
guid
);
Iterator
<
Vertex
>
results
=
query
.
vertices
().
iterator
();
// returning one since name/type is unique
Vertex
instanceVertex
=
results
.
hasNext
()
?
results
.
next
()
:
null
;
if
(
instanceVertex
==
null
)
{
return
null
;
}
}
return
newInstances
;
}
private
void
addPrimitiveToVertex
(
ITypedInstance
instance
,
Vertex
entityVertex
,
AttributeInfo
attributeInfo
)
throws
MetadataException
{
if
(
instance
.
get
(
attributeInfo
.
name
)
==
null
)
{
// add only if instance has this attribute
return
;
}
return
graphToInstanceMapper
.
mapGraphToTypedInstance
(
guid
,
instanceVertex
);
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
STRING_TYPE
)
{
entityVertex
.
setProperty
(
attributeInfo
.
name
,
instance
.
getString
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
SHORT_TYPE
)
{
entityVertex
.
setProperty
(
attributeInfo
.
name
,
instance
.
getShort
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
INT_TYPE
)
{
entityVertex
.
setProperty
(
attributeInfo
.
name
,
instance
.
getInt
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
BIGINTEGER_TYPE
)
{
entityVertex
.
setProperty
(
attributeInfo
.
name
,
instance
.
getBigInt
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
BOOLEAN_TYPE
)
{
entityVertex
.
setProperty
(
attributeInfo
.
name
,
instance
.
getBoolean
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
BYTE_TYPE
)
{
entityVertex
.
setProperty
(
attributeInfo
.
name
,
instance
.
getByte
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
LONG_TYPE
)
{
entityVertex
.
setProperty
(
attributeInfo
.
name
,
instance
.
getLong
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
FLOAT_TYPE
)
{
entityVertex
.
setProperty
(
attributeInfo
.
name
,
instance
.
getFloat
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
DOUBLE_TYPE
)
{
entityVertex
.
setProperty
(
attributeInfo
.
name
,
instance
.
getDouble
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
BIGDECIMAL_TYPE
)
{
entityVertex
.
setProperty
(
attributeInfo
.
name
,
instance
.
getBigDecimal
(
attributeInfo
.
name
));
}
catch
(
Exception
e
)
{
throw
new
RepositoryException
(
e
);
}
}
public
static
void
addToVertex
(
Vertex
entityVertex
,
String
name
,
int
value
)
{
entityVertex
.
setProperty
(
name
,
value
);
}
@Override
public
ITypedReferenceableInstance
getEntityDefinition
(
String
guid
)
throws
RepositoryException
{
LOG
.
info
(
"Retrieving entity with guid={}"
,
guid
);
return
instances
.
get
(
guid
);
}
@Override
public
List
<
String
>
getEntityList
(
String
entityType
)
throws
RepositoryException
{
LOG
.
info
(
"Retrieving entity list for type={}"
,
entityType
);
...
...
@@ -388,17 +212,345 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
}
}
public
void
createVerticesForClasses
(
TransactionalGraph
transactionalGraph
,
List
<
ITypedReferenceableInstance
>
newInstances
)
{
for
(
ITypedReferenceableInstance
i
nstance
:
newInstances
)
{
final
Vertex
entity
Vertex
=
transactionalGraph
.
addVertex
(
null
);
entityVertex
.
setProperty
(
ENTITY_TYPE_PROPERTY_KEY
,
i
nstance
.
getTypeName
());
public
void
createVerticesForClass
Typ
es
(
TransactionalGraph
transactionalGraph
,
List
<
ITypedReferenceableInstance
>
newInstances
)
{
for
(
ITypedReferenceableInstance
typedI
nstance
:
newInstances
)
{
final
Vertex
instance
Vertex
=
transactionalGraph
.
addVertex
(
null
);
instanceVertex
.
setProperty
(
ENTITY_TYPE_PROPERTY_KEY
,
typedI
nstance
.
getTypeName
());
// entityVertex.setProperty("entityName", instance.getString("name"));
final
String
guid
=
UUID
.
randomUUID
().
toString
();
entityVertex
.
setProperty
(
GUID_PROPERTY_KEY
,
guid
);
instanceVertex
.
setProperty
(
GUID_PROPERTY_KEY
,
guid
);
final
Id
typedInstanceId
=
typedInstance
.
getId
();
instanceVertex
.
setProperty
(
VERSION_PROPERTY_KEY
,
typedInstanceId
.
version
);
idToVertexMap
.
put
(
typedInstanceId
,
instanceVertex
);
}
}
}
private
final
class
TypedInstanceToGraphMapper
{
private
String
mapTypedInstanceToGraph
(
IReferenceableInstance
entity
,
TransactionalGraph
transactionalGraph
)
throws
MetadataException
{
EntityProcessor
entityProcessor
=
new
EntityProcessor
();
try
{
new
ObjectGraphWalker
(
typeSystem
,
entityProcessor
,
entity
).
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
);
}
/*
* Step 2: Traverse oldIdToInstance map create newInstances :
* List[ITypedReferenceableInstance]
* - create a ITypedReferenceableInstance.
* replace any old References ( ids or object references) with new Ids.
*/
private
List
<
ITypedReferenceableInstance
>
discoverInstances
(
EntityProcessor
entityProcessor
)
throws
RepositoryException
{
List
<
ITypedReferenceableInstance
>
newTypedInstances
=
new
ArrayList
<>();
for
(
IReferenceableInstance
transientInstance
:
entityProcessor
.
idToInstanceMap
.
values
())
{
LOG
.
debug
(
"instance {}"
,
transientInstance
);
try
{
ClassType
cT
=
typeSystem
.
getDataType
(
ClassType
.
class
,
transientInstance
.
getTypeName
());
ITypedReferenceableInstance
newInstance
=
cT
.
convert
(
transientInstance
,
Multiplicity
.
REQUIRED
);
newTypedInstances
.
add
(
newInstance
);
/*
* Now replace old references with new Ids
*/
MapIds
mapIds
=
new
MapIds
(
entityProcessor
.
idToNewIdMap
);
new
ObjectGraphWalker
(
typeSystem
,
mapIds
,
newTypedInstances
).
walk
();
}
catch
(
MetadataException
me
)
{
throw
new
RepositoryException
(
String
.
format
(
"Failed to create Instance(id = %s"
,
transientInstance
.
getId
()),
me
);
}
}
return
newTypedInstances
;
}
private
String
addDiscoveredInstances
(
IReferenceableInstance
entity
,
EntityProcessor
entityProcessor
,
List
<
ITypedReferenceableInstance
>
newTypedInstances
)
throws
MetadataException
{
String
guid
=
null
;
for
(
ITypedReferenceableInstance
typedInstance
:
newTypedInstances
)
{
// Traverse over newInstances
Id
id
=
typedInstance
.
getId
();
if
(
id
==
null
)
{
// oops
throw
new
RepositoryException
(
"id cannot be null"
);
}
Vertex
instanceVertex
=
entityProcessor
.
idToVertexMap
.
get
(
id
);
// add the attributes for the instance
final
Map
<
String
,
AttributeInfo
>
fields
=
typedInstance
.
fieldMapping
().
fields
;
addInstanceToVertex
(
typedInstance
,
instanceVertex
,
fields
,
entityProcessor
.
idToVertexMap
);
for
(
String
traitName
:
typedInstance
.
getTraits
())
{
((
TitanVertex
)
instanceVertex
).
addProperty
(
"traits"
,
traitName
);
ITypedStruct
traitInstance
=
(
ITypedStruct
)
typedInstance
.
getTrait
(
traitName
);
// add the attributes for the trait instance
instanceVertex
.
setProperty
(
TRAIT_PROPERTY_SUFFIX
+
traitName
,
traitName
);
addInstanceToVertex
(
traitInstance
,
instanceVertex
,
traitInstance
.
fieldMapping
().
fields
,
entityProcessor
.
idToVertexMap
);
}
if
(
typedInstance
.
getId
()
==
entity
.
getId
())
{
guid
=
instanceVertex
.
getProperty
(
GUID_PROPERTY_KEY
);
}
}
return
guid
;
}
private
void
addInstanceToVertex
(
ITypedInstance
typedInstance
,
Vertex
instanceVertex
,
Map
<
String
,
AttributeInfo
>
fields
,
Map
<
Id
,
Vertex
>
idToVertexMap
)
throws
MetadataException
{
for
(
AttributeInfo
attributeInfo
:
fields
.
values
())
{
System
.
out
.
println
(
"*** attributeInfo = "
+
attributeInfo
);
final
IDataType
dataType
=
attributeInfo
.
dataType
();
Object
attributeValue
=
typedInstance
.
get
(
attributeInfo
.
name
);
switch
(
dataType
.
getTypeCategory
())
{
case
PRIMITIVE:
addPrimitiveToVertex
(
typedInstance
,
instanceVertex
,
attributeInfo
);
break
;
case
ENUM:
addToVertex
(
instanceVertex
,
attributeInfo
.
name
,
typedInstance
.
getInt
(
attributeInfo
.
name
));
break
;
case
ARRAY:
// todo - Add to/from json for collections
break
;
case
MAP:
// todo - Add to/from json for collections
break
;
case
STRUCT:
ITypedStruct
structInstance
=
(
ITypedStruct
)
attributeValue
;
addInstanceToVertex
(
structInstance
,
instanceVertex
,
structInstance
.
fieldMapping
().
fields
,
idToVertexMap
);
break
;
case
TRAIT:
ITypedStruct
traitInstance
=
(
ITypedStruct
)
attributeValue
;
addInstanceToVertex
(
traitInstance
,
instanceVertex
,
traitInstance
.
fieldMapping
().
fields
,
idToVertexMap
);
break
;
case
CLASS:
Id
id
=
(
Id
)
typedInstance
.
get
(
attributeInfo
.
name
);
if
(
id
!=
null
)
{
Vertex
referenceVertex
=
idToVertexMap
.
get
(
id
);
GraphUtils
.
addEdge
(
instanceVertex
,
referenceVertex
,
id
.
id
);
}
break
;
default
:
break
;
}
}
}
private
void
addPrimitiveToVertex
(
ITypedInstance
typedInstance
,
Vertex
instanceVertex
,
AttributeInfo
attributeInfo
)
throws
MetadataException
{
if
(
typedInstance
.
get
(
attributeInfo
.
name
)
==
null
)
{
// add only if instance has this attribute
return
;
}
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
STRING_TYPE
)
{
instanceVertex
.
setProperty
(
attributeInfo
.
name
,
typedInstance
.
getString
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
SHORT_TYPE
)
{
instanceVertex
.
setProperty
(
attributeInfo
.
name
,
typedInstance
.
getShort
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
INT_TYPE
)
{
instanceVertex
.
setProperty
(
attributeInfo
.
name
,
typedInstance
.
getInt
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
BIGINTEGER_TYPE
)
{
instanceVertex
.
setProperty
(
attributeInfo
.
name
,
typedInstance
.
getBigInt
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
BOOLEAN_TYPE
)
{
instanceVertex
.
setProperty
(
attributeInfo
.
name
,
typedInstance
.
getBoolean
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
BYTE_TYPE
)
{
instanceVertex
.
setProperty
(
attributeInfo
.
name
,
typedInstance
.
getByte
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
LONG_TYPE
)
{
instanceVertex
.
setProperty
(
attributeInfo
.
name
,
typedInstance
.
getLong
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
FLOAT_TYPE
)
{
instanceVertex
.
setProperty
(
attributeInfo
.
name
,
typedInstance
.
getFloat
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
DOUBLE_TYPE
)
{
instanceVertex
.
setProperty
(
attributeInfo
.
name
,
typedInstance
.
getDouble
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
BIGDECIMAL_TYPE
)
{
instanceVertex
.
setProperty
(
attributeInfo
.
name
,
typedInstance
.
getBigDecimal
(
attributeInfo
.
name
));
}
}
public
void
addToVertex
(
Vertex
instanceVertex
,
String
name
,
int
value
)
{
instanceVertex
.
setProperty
(
name
,
value
);
}
}
private
final
class
GraphToTypedInstanceMapper
{
private
ITypedReferenceableInstance
mapGraphToTypedInstance
(
String
guid
,
Vertex
instanceVertex
)
throws
MetadataException
{
String
typeName
=
instanceVertex
.
getProperty
(
ENTITY_TYPE_PROPERTY_KEY
);
List
<
String
>
traits
=
new
ArrayList
<>();
for
(
TitanProperty
property
:
((
TitanVertex
)
instanceVertex
).
getProperties
(
"traits"
))
{
traits
.
add
((
String
)
property
.
getValue
());
}
Id
id
=
new
Id
(
guid
,
instanceVertex
.<
Integer
>
getProperty
(
"version"
),
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
);
return
typedInstance
;
}
private
void
mapVertexToInstance
(
Vertex
instanceVertex
,
ITypedInstance
typedInstance
,
Map
<
String
,
AttributeInfo
>
fields
)
throws
MetadataException
{
for
(
AttributeInfo
attributeInfo
:
fields
.
values
())
{
System
.
out
.
println
(
"*** attributeInfo = "
+
attributeInfo
);
final
IDataType
dataType
=
attributeInfo
.
dataType
();
switch
(
dataType
.
getTypeCategory
())
{
case
PRIMITIVE:
mapVertexToInstance
(
instanceVertex
,
typedInstance
,
attributeInfo
);
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
(
attributeInfo
.
name
));
break
;
case
ARRAY:
// todo - Add to/from json for collections
break
;
case
MAP:
// todo - Add to/from json for collections
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
);
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
);
break
;
case
CLASS:
Id
referenceId
=
null
;
for
(
Edge
edge
:
instanceVertex
.
getEdges
(
Direction
.
IN
))
{
final
Vertex
vertex
=
edge
.
getVertex
(
Direction
.
OUT
);
if
(
vertex
.
getProperty
(
ENTITY_TYPE_PROPERTY_KEY
).
equals
(
attributeInfo
.
name
))
{
referenceId
=
new
Id
(
vertex
.<
String
>
getProperty
(
GUID_PROPERTY_KEY
),
vertex
.<
Integer
>
getProperty
(
"version"
),
attributeInfo
.
name
);
break
;
}
}
if
(
referenceId
!=
null
)
{
typedInstance
.
set
(
attributeInfo
.
name
,
referenceId
);
}
break
;
default
:
break
;
}
}
}
private
void
mapVertexToInstance
(
Vertex
instanceVertex
,
ITypedInstance
typedInstance
,
AttributeInfo
attributeInfo
)
throws
MetadataException
{
if
(
instanceVertex
.
getProperty
(
attributeInfo
.
name
)
==
null
)
{
return
;
}
idToVertexMap
.
put
(
instance
.
getId
(),
entityVertex
);
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
STRING_TYPE
)
{
typedInstance
.
setString
(
attributeInfo
.
name
,
instanceVertex
.<
String
>
getProperty
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
SHORT_TYPE
)
{
typedInstance
.
setShort
(
attributeInfo
.
name
,
instanceVertex
.<
Short
>
getProperty
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
INT_TYPE
)
{
typedInstance
.
setInt
(
attributeInfo
.
name
,
instanceVertex
.<
Integer
>
getProperty
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
BIGINTEGER_TYPE
)
{
typedInstance
.
setBigInt
(
attributeInfo
.
name
,
instanceVertex
.<
BigInteger
>
getProperty
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
BOOLEAN_TYPE
)
{
typedInstance
.
setBoolean
(
attributeInfo
.
name
,
instanceVertex
.<
Boolean
>
getProperty
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
BYTE_TYPE
)
{
typedInstance
.
setByte
(
attributeInfo
.
name
,
instanceVertex
.<
Byte
>
getProperty
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
LONG_TYPE
)
{
typedInstance
.
setLong
(
attributeInfo
.
name
,
instanceVertex
.<
Long
>
getProperty
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
FLOAT_TYPE
)
{
typedInstance
.
setFloat
(
attributeInfo
.
name
,
instanceVertex
.<
Float
>
getProperty
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
DOUBLE_TYPE
)
{
typedInstance
.
setDouble
(
attributeInfo
.
name
,
instanceVertex
.<
Double
>
getProperty
(
attributeInfo
.
name
));
}
else
if
(
attributeInfo
.
dataType
()
==
DataTypes
.
BIGDECIMAL_TYPE
)
{
typedInstance
.
setBigDecimal
(
attributeInfo
.
name
,
instanceVertex
.<
BigDecimal
>
getProperty
(
attributeInfo
.
name
));
}
}
}
...
...
repository/src/main/java/org/apache/hadoop/metadata/services/MetadataService.java
View file @
386d8d38
...
...
@@ -73,15 +73,6 @@ public interface MetadataService extends Service {
String
getEntityDefinition
(
String
guid
)
throws
MetadataException
;
/**
* Return the definition for the given entity name and type.
*
* @param entityName name
* @param entityType type
* @return entity definition as JSON
*/
String
getEntityDefinition
(
String
entityName
,
String
entityType
)
throws
MetadataException
;
/**
* Return the list of entity names for the given type in the repository.
*
* @param entityType type
...
...
repository/src/main/java/org/apache/hadoop/metadata/util/GraphUtils.java
View file @
386d8d38
...
...
@@ -23,9 +23,45 @@ public final class GraphUtils {
private
static
final
Logger
LOG
=
LoggerFactory
.
getLogger
(
GraphUtils
.
class
);
private
static
final
String
GUID_PROPERTY_KEY
=
"guid"
;
private
static
final
String
TIMESTAMP_PROPERTY_KEY
=
"timestamp"
;
private
GraphUtils
()
{
}
public
static
Edge
addEdge
(
Vertex
fromVertex
,
Vertex
toVertex
,
String
edgeLabel
)
{
return
addEdge
(
fromVertex
,
toVertex
,
edgeLabel
,
null
);
}
public
static
Edge
addEdge
(
Vertex
fromVertex
,
Vertex
toVertex
,
String
edgeLabel
,
String
timestamp
)
{
Edge
edge
=
findEdge
(
fromVertex
,
toVertex
,
edgeLabel
);
Edge
edgeToVertex
=
edge
!=
null
?
edge
:
fromVertex
.
addEdge
(
edgeLabel
,
toVertex
);
if
(
timestamp
!=
null
)
{
edgeToVertex
.
setProperty
(
TIMESTAMP_PROPERTY_KEY
,
timestamp
);
}
return
edgeToVertex
;
}
public
static
Edge
findEdge
(
Vertex
fromVertex
,
Vertex
toVertex
,
String
edgeLabel
)
{
return
findEdge
(
fromVertex
,
toVertex
.
getProperty
(
GUID_PROPERTY_KEY
),
edgeLabel
);
}
public
static
Edge
findEdge
(
Vertex
fromVertex
,
Object
toVertexName
,
String
edgeLabel
)
{
Edge
edgeToFind
=
null
;
for
(
Edge
edge
:
fromVertex
.
getEdges
(
Direction
.
OUT
,
edgeLabel
))
{
if
(
edge
.
getVertex
(
Direction
.
IN
).
getProperty
(
GUID_PROPERTY_KEY
).
equals
(
toVertexName
))
{
edgeToFind
=
edge
;
break
;
}
}
return
edgeToFind
;
}
public
static
Vertex
findVertex
(
Graph
blueprintsGraph
,
String
guid
)
{
LOG
.
debug
(
"Finding vertex for: guid={}"
,
guid
);
...
...
webapp/src/main/java/org/apache/hadoop/metadata/web/resources/EntityResource.java
View file @
386d8d38
...
...
@@ -67,22 +67,23 @@ public class EntityResource {
}
@POST
@Path
(
"submit/{
entityTyp
e}"
)
@Path
(
"submit/{
typeNam
e}"
)
@Consumes
(
MediaType
.
APPLICATION_JSON
)
@Produces
(
MediaType
.
APPLICATION_JSON
)
public
Response
submit
(
@Context
HttpServletRequest
request
,
@PathParam
(
"
entityType"
)
final
String
entityTyp
e
)
{
@PathParam
(
"
typeName"
)
final
String
typeNam
e
)
{
try
{
final
String
entity
=
Servlets
.
getRequestPayload
(
request
);
System
.
out
.
println
(
"entity = "
+
entity
);
LOG
.
debug
(
"submitting entity {} "
,
entity
);
final
String
guid
=
metadataService
.
createEntity
(
entity
,
entityType
);
final
String
guid
=
metadataService
.
createEntity
(
typeName
,
entity
);
JSONObject
response
=
new
JSONObject
();
response
.
put
(
"GUID"
,
guid
);
response
.
put
(
"requestId"
,
Thread
.
currentThread
().
getName
());
return
Response
.
ok
(
response
).
build
();
}
catch
(
Exception
e
)
{
LOG
.
error
(
"Unable to persist instance for type {}"
,
typeName
,
e
);
throw
new
WebApplicationException
(
Servlets
.
getErrorResponse
(
e
,
Response
.
Status
.
BAD_REQUEST
));
}
...
...
@@ -109,8 +110,7 @@ public class EntityResource {
return
Response
.
status
(
status
).
entity
(
response
).
build
();
}
catch
(
Exception
e
)
{
LOG
.
error
(
"Action failed: {}\nError: {}"
,
Response
.
Status
.
INTERNAL_SERVER_ERROR
,
e
.
getMessage
());
LOG
.
error
(
"Unable to get instance definition for GUID {}"
,
guid
,
e
);
throw
new
WebApplicationException
(
e
,
Response
.
status
(
Response
.
Status
.
INTERNAL_SERVER_ERROR
)
.
entity
(
e
.
getMessage
())
...
...
webapp/src/test/java/org/apache/hadoop/metadata/web/resources/BaseResourceIT.java
View file @
386d8d38
...
...
@@ -10,16 +10,21 @@ import org.apache.hadoop.metadata.types.HierarchicalTypeDefinition;
import
org.apache.hadoop.metadata.types.IDataType
;
import
org.apache.hadoop.metadata.types.Multiplicity
;
import
org.apache.hadoop.metadata.types.TraitType
;
import
org.apache.hadoop.metadata.types.TypeSystem
;
import
org.testng.annotations.BeforeClass
;
import
javax.ws.rs.core.UriBuilder
;
public
class
BaseResourceIT
{
public
abstract
class
BaseResourceIT
{
protected
TypeSystem
typeSystem
;
protected
WebResource
service
;
@BeforeClass
public
void
setUp
()
throws
Exception
{
typeSystem
=
TypeSystem
.
getInstance
();
typeSystem
.
reset
();
String
baseUrl
=
"http://localhost:21000/"
;
DefaultClientConfig
config
=
new
DefaultClientConfig
();
...
...
webapp/src/test/java/org/apache/hadoop/metadata/web/resources/EntityJerseyResourceIT.java
View file @
386d8d38
...
...
@@ -18,54 +18,78 @@
package
org
.
apache
.
hadoop
.
metadata
.
web
.
resources
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.UUID
;
import
com.google.common.collect.ImmutableList
;
import
com.sun.jersey.api.client.ClientResponse
;
import
com.sun.jersey.api.client.WebResource
;
import
org.apache.hadoop.metadata.ITypedReferenceableInstance
;
import
org.apache.hadoop.metadata.Referenceable
;
import
org.apache.hadoop.metadata.Struct
;
import
org.apache.hadoop.metadata.json.Serialization
$
;
import
org.apache.hadoop.metadata.json.TypesSerialization
;
import
org.apache.hadoop.metadata.types.AttributeDefinition
;
import
org.apache.hadoop.metadata.types.ClassType
;
import
org.apache.hadoop.metadata.types.DataTypes
;
import
org.apache.hadoop.metadata.types.HierarchicalTypeDefinition
;
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.testng.Assert
;
import
org.testng.annotations.BeforeClass
;
import
org.testng.annotations.Test
;
import
javax.ws.rs.HttpMethod
;
import
javax.ws.rs.core.MediaType
;
import
javax.ws.rs.core.Response
;
import
org.json.simple.JSONValue
;
import
org.testng.Assert
;
import
org.testng.annotations.Test
;
import
com.google.gson.JsonElement
;
import
com.google.gson.JsonParser
;
import
com.sun.jersey.api.client.ClientResponse
;
import
com.sun.jersey.api.client.WebResource
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.UUID
;
/**
* Integration tests for Entity Jersey Resource.
*/
@Test
(
enabled
=
false
)
public
class
EntityJerseyResourceIT
extends
BaseResourceIT
{
private
static
final
String
ENTITY_NAME
=
"clicks-table"
;
private
static
final
String
ENTITY_TYPE
=
"hive-table"
;
private
static
final
String
DATABASE_NAME
=
"ads"
;
private
static
final
String
TABLE_NAME
=
"clicks-table"
;
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
guid
;
@Test
(
enabled
=
false
)
public
void
testSubmitEntity
()
{
String
entityStream
=
getTestEntityJSON
();
JsonParser
parser
=
new
JsonParser
();
@BeforeClass
public
void
setUp
()
throws
Exception
{
super
.
setUp
();
List
<
HierarchicalTypeDefinition
>
typeDefinitions
=
createHiveTypes
();
submitTypes
(
typeDefinitions
);
}
@Test
public
void
testSubmitEntity
()
throws
Exception
{
ITypedReferenceableInstance
tableInstance
=
createHiveTableInstance
();
String
instanceAsJSON
=
Serialization
$
.
MODULE
$
.
toJson
(
tableInstance
);
WebResource
resource
=
service
.
path
(
"api/metadata/entities/submit"
)
.
path
(
ENTITY
_TYPE
);
.
path
(
TABLE
_TYPE
);
ClientResponse
clientResponse
=
resource
.
accept
(
MediaType
.
APPLICATION_JSON
)
.
type
(
MediaType
.
APPLICATION_JSON
)
.
method
(
HttpMethod
.
POST
,
ClientResponse
.
class
,
entityStream
);
.
method
(
HttpMethod
.
POST
,
ClientResponse
.
class
,
instanceAsJSON
);
Assert
.
assertEquals
(
clientResponse
.
getStatus
(),
Response
.
Status
.
OK
.
getStatusCode
());
String
response
=
clientResponse
.
getEntity
(
String
.
class
);
Assert
.
assertNotNull
(
response
);
JsonElement
elem
=
parser
.
parse
(
response
);
String
guid
=
elem
.
getAsJsonObject
().
get
(
"GUID"
).
getAsString
();
String
responseAsString
=
clientResponse
.
getEntity
(
String
.
class
);
Assert
.
assertNotNull
(
responseAsString
);
JSONObject
response
=
new
JSONObject
(
responseAsString
);
Assert
.
assertNotNull
(
response
.
get
(
"requestId"
));
guid
=
response
.
get
(
"GUID"
).
toString
();
Assert
.
assertNotNull
(
guid
);
try
{
Assert
.
assertNotNull
(
UUID
.
fromString
(
guid
));
...
...
@@ -74,12 +98,11 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
}
}
@Test
(
dependsOnMethods
=
"testSubmitEntity"
,
enabled
=
false
)
@Test
(
dependsOnMethods
=
"testSubmitEntity"
)
public
void
testGetEntityDefinition
()
{
WebResource
resource
=
service
.
path
(
"api/metadata/entities/definition"
)
.
path
(
ENTITY_TYPE
)
.
path
(
ENTITY_NAME
);
.
path
(
guid
);
ClientResponse
clientResponse
=
resource
.
accept
(
MediaType
.
APPLICATION_JSON
)
...
...
@@ -90,20 +113,10 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
System
.
out
.
println
(
"response = "
+
response
);
}
private
static
String
getTestEntityJSON
()
{
Map
<
String
,
String
>
props
=
new
HashMap
<>();
props
.
put
(
"entityName"
,
ENTITY_NAME
);
props
.
put
(
"entityType"
,
ENTITY_TYPE
);
props
.
put
(
"database"
,
DATABASE_NAME
);
props
.
put
(
"table"
,
TABLE_NAME
);
return
JSONValue
.
toJSONString
(
props
);
}
@Test
(
enabled
=
false
)
@Test
public
void
testGetInvalidEntityDefinition
()
{
WebResource
resource
=
service
.
path
(
"api/metadata/entities/definition"
)
.
path
(
ENTITY_TYPE
)
.
path
(
"blah"
);
ClientResponse
clientResponse
=
resource
...
...
@@ -119,7 +132,7 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
public
void
testGetEntityList
()
{
ClientResponse
clientResponse
=
service
.
path
(
"api/metadata/entities/list/"
)
.
path
(
ENTITY
_TYPE
)
.
path
(
TABLE
_TYPE
)
.
accept
(
MediaType
.
APPLICATION_JSON
)
.
type
(
MediaType
.
APPLICATION_JSON
)
.
method
(
HttpMethod
.
GET
,
ClientResponse
.
class
);
...
...
@@ -139,4 +152,81 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
String
response
=
clientResponse
.
getEntity
(
String
.
class
);
System
.
out
.
println
(
"response = "
+
response
);
}
private
List
<
HierarchicalTypeDefinition
>
createHiveTypes
()
throws
Exception
{
ArrayList
<
HierarchicalTypeDefinition
>
typeDefinitions
=
new
ArrayList
<>();
HierarchicalTypeDefinition
<
ClassType
>
databaseTypeDefinition
=
createClassTypeDef
(
DATABASE_TYPE
,
ImmutableList
.<
String
>
of
(),
createRequiredAttrDef
(
"name"
,
DataTypes
.
STRING_TYPE
),
createRequiredAttrDef
(
"description"
,
DataTypes
.
STRING_TYPE
));
typeDefinitions
.
add
(
databaseTypeDefinition
);
HierarchicalTypeDefinition
<
ClassType
>
tableTypeDefinition
=
createClassTypeDef
(
TABLE_TYPE
,
ImmutableList
.<
String
>
of
(),
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
,
ImmutableList
.<
String
>
of
(),
createRequiredAttrDef
(
"level"
,
DataTypes
.
INT_TYPE
));
typeDefinitions
.
add
(
fetlTypeDefinition
);
typeSystem
.
defineTypes
(
ImmutableList
.<
StructTypeDefinition
>
of
(),
ImmutableList
.
of
(
fetlTypeDefinition
),
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
);
WebResource
resource
=
service
.
path
(
"api/metadata/types/submit"
)
.
path
(
typeDefinition
.
typeName
);
ClientResponse
clientResponse
=
resource
.
accept
(
MediaType
.
APPLICATION_JSON
)
.
type
(
MediaType
.
APPLICATION_JSON
)
.
method
(
HttpMethod
.
POST
,
ClientResponse
.
class
,
typesAsJSON
);
Assert
.
assertEquals
(
clientResponse
.
getStatus
(),
Response
.
Status
.
OK
.
getStatusCode
());
String
responseAsString
=
clientResponse
.
getEntity
(
String
.
class
);
Assert
.
assertNotNull
(
responseAsString
);
JSONObject
response
=
new
JSONObject
(
responseAsString
);
Assert
.
assertEquals
(
response
.
get
(
"typeName"
),
typeDefinition
.
typeName
);
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
);
tableInstance
.
set
(
"name"
,
TABLE_NAME
);
tableInstance
.
set
(
"description"
,
"bar table"
);
tableInstance
.
set
(
"type"
,
"managed"
);
tableInstance
.
set
(
DATABASE_TYPE
,
databaseInstance
);
Struct
traitInstance
=
(
Struct
)
tableInstance
.
getTrait
(
TRAIT_TYPE
);
traitInstance
.
set
(
"level"
,
1
);
ClassType
tableType
=
typeSystem
.
getDataType
(
ClassType
.
class
,
TABLE_TYPE
);
return
tableType
.
convert
(
tableInstance
,
Multiplicity
.
REQUIRED
);
}
}
webapp/src/test/java/org/apache/hadoop/metadata/web/resources/TypesJerseyResourceIT.java
View file @
386d8d38
...
...
@@ -21,7 +21,6 @@ package org.apache.hadoop.metadata.web.resources;
import
com.google.common.collect.ImmutableList
;
import
com.sun.jersey.api.client.ClientResponse
;
import
com.sun.jersey.api.client.WebResource
;
import
org.apache.hadoop.metadata.MetadataException
;
import
org.apache.hadoop.metadata.json.TypesSerialization
;
import
org.apache.hadoop.metadata.types.AttributeDefinition
;
import
org.apache.hadoop.metadata.types.ClassType
;
...
...
@@ -30,7 +29,6 @@ import org.apache.hadoop.metadata.types.HierarchicalTypeDefinition;
import
org.apache.hadoop.metadata.types.Multiplicity
;
import
org.apache.hadoop.metadata.types.StructTypeDefinition
;
import
org.apache.hadoop.metadata.types.TraitType
;
import
org.apache.hadoop.metadata.types.TypeSystem
;
import
org.codehaus.jettison.json.JSONArray
;
import
org.codehaus.jettison.json.JSONObject
;
import
org.testng.Assert
;
...
...
@@ -49,14 +47,12 @@ import java.util.List;
*/
public
class
TypesJerseyResourceIT
extends
BaseResourceIT
{
private
TypeSystem
typeSystem
;
private
List
<
HierarchicalTypeDefinition
>
typeDefinitions
;
@BeforeClass
public
void
setUp
()
throws
Exception
{
super
.
setUp
();
typeSystem
=
TypeSystem
.
getInstance
();
typeDefinitions
=
createHiveTypes
();
}
...
...
@@ -151,11 +147,12 @@ public class TypesJerseyResourceIT extends BaseResourceIT {
Assert
.
assertNotNull
(
list
);
}
private
List
<
HierarchicalTypeDefinition
>
createHiveTypes
()
throws
Metadata
Exception
{
private
List
<
HierarchicalTypeDefinition
>
createHiveTypes
()
throws
Exception
{
ArrayList
<
HierarchicalTypeDefinition
>
typeDefinitions
=
new
ArrayList
<>();
HierarchicalTypeDefinition
<
ClassType
>
databaseTypeDefinition
=
createClassTypeDef
(
"database"
,
ImmutableList
.<
String
>
of
(),
createClassTypeDef
(
"database"
,
ImmutableList
.<
String
>
of
(),
createRequiredAttrDef
(
"name"
,
DataTypes
.
STRING_TYPE
),
createRequiredAttrDef
(
"description"
,
DataTypes
.
STRING_TYPE
));
typeDefinitions
.
add
(
databaseTypeDefinition
);
...
...
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