-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get schema from gremlin/sparql query results for Neptune qpt and refa…
…ctor.
- Loading branch information
1 parent
c28f9d6
commit a53d82f
Showing
6 changed files
with
173 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
athena-neptune/src/main/java/com/amazonaws/athena/connectors/neptune/NeptuneSchemaUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/*- | ||
* #%L | ||
* athena-neptune | ||
* %% | ||
* Copyright (C) 2019 - 2020 Amazon Web Services | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
package com.amazonaws.athena.connectors.neptune; | ||
|
||
import com.amazonaws.athena.connector.lambda.data.SchemaBuilder; | ||
import org.apache.arrow.vector.types.Types; | ||
import org.apache.arrow.vector.types.pojo.Field; | ||
import org.apache.arrow.vector.types.pojo.FieldType; | ||
import org.apache.arrow.vector.types.pojo.Schema; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.math.BigInteger; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Collection of helpful utilities that handle Neptune schema inference, type, and naming conversion. | ||
*/ | ||
public class NeptuneSchemaUtils | ||
{ | ||
private static final Logger logger = LoggerFactory.getLogger(NeptuneSchemaUtils.class); | ||
|
||
private NeptuneSchemaUtils() {} | ||
|
||
public static Schema getSchemaFromResults(Map resultsMap, String componentTypeValue, String tableName) | ||
{ | ||
Schema schema; | ||
SchemaBuilder schemaBuilder = SchemaBuilder.newBuilder(); | ||
//Building schema from gremlin/sparql query results. | ||
resultsMap.forEach((columnName, columnValue) -> buildSchema(columnName.toString(), columnValue, schemaBuilder)); | ||
schemaBuilder.addMetadata(Constants.SCHEMA_COMPONENT_TYPE, componentTypeValue); | ||
schemaBuilder.addMetadata(Constants.SCHEMA_GLABEL, tableName); | ||
schema = schemaBuilder.build(); | ||
return schema; | ||
} | ||
|
||
private static void buildSchema(String columnName, Object columnValue, SchemaBuilder schemaBuilder) | ||
{ | ||
schemaBuilder.addField(getArrowFieldForNeptune(columnName, columnValue)); | ||
} | ||
|
||
/** | ||
* Infers the type of a field from Neptune data. | ||
* | ||
* @param key The key of the field we are attempting to infer. | ||
* @param value A value from the key whose type we are attempting to infer. | ||
* @return The Apache Arrow field definition of the inferred key/value. | ||
*/ | ||
private static Field getArrowFieldForNeptune(String key, Object value) | ||
{ | ||
if (value instanceof String || value instanceof java.util.UUID) { | ||
return new Field(key, FieldType.nullable(Types.MinorType.VARCHAR.getType()), null); | ||
} | ||
else if (value instanceof Integer) { | ||
return new Field(key, FieldType.nullable(Types.MinorType.INT.getType()), null); | ||
} | ||
else if (value instanceof BigInteger) { | ||
return new Field(key, FieldType.nullable(Types.MinorType.BIGINT.getType()), null); | ||
} | ||
else if (value instanceof Long) { | ||
return new Field(key, FieldType.nullable(Types.MinorType.BIGINT.getType()), null); | ||
} | ||
else if (value instanceof Boolean) { | ||
return new Field(key, FieldType.nullable(Types.MinorType.BIT.getType()), null); | ||
} | ||
else if (value instanceof Float) { | ||
return new Field(key, FieldType.nullable(Types.MinorType.FLOAT4.getType()), null); | ||
} | ||
else if (value instanceof Double) { | ||
return new Field(key, FieldType.nullable(Types.MinorType.FLOAT8.getType()), null); | ||
} | ||
else if (value instanceof java.util.Date) { | ||
return new Field(key, FieldType.nullable(Types.MinorType.DATEMILLI.getType()), null); | ||
} | ||
else if (value instanceof List) { | ||
return getArrowFieldForNeptune(key, ((List<?>) value).get(0)); | ||
} | ||
|
||
String className = (value == null || value.getClass() == null) ? "null" : value.getClass().getName(); | ||
logger.warn("Unknown type[{}] for field[{}], defaulting to varchar.", className, key); | ||
return new Field(key, FieldType.nullable(Types.MinorType.VARCHAR.getType()), null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...neptune/src/test/java/com/amazonaws/athena/connectors/neptune/NeptuneSchemaUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/*- | ||
* #%L | ||
* athena-neptune | ||
* %% | ||
* Copyright (C) 2019 Amazon Web Services | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
package com.amazonaws.athena.connectors.neptune; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import org.apache.arrow.vector.types.pojo.Schema; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.math.BigInteger; | ||
import java.util.ArrayList; | ||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class NeptuneSchemaUtilsTest { | ||
private static final Logger logger = LoggerFactory.getLogger(NeptuneSchemaUtilsTest.class); | ||
|
||
private final Map<String, Object> objectMap = ImmutableMap.of( | ||
"col1", "String", | ||
"col2", 1, | ||
"col3", 10.33, | ||
"col4", true, | ||
"col5", new BigInteger("12345678901234567890")); | ||
|
||
private final String COMPONENT_TYPE = "vertex"; | ||
|
||
@Test | ||
public void getSchemaFromResults() { | ||
logger.info("getSchemaFromResults - enter"); | ||
Schema schema = NeptuneSchemaUtils.getSchemaFromResults(objectMap, COMPONENT_TYPE, "test"); | ||
|
||
assertEquals(schema.getFields().size(), objectMap.size()); | ||
assertEquals("Utf8", schema.findField("col1").getType().toString()); | ||
assertEquals("Int(32, true)", schema.findField("col2").getType().toString()); | ||
assertEquals("FloatingPoint(DOUBLE)", schema.findField("col3").getType().toString()); | ||
assertEquals("Bool", schema.findField("col4").getType().toString()); | ||
assertEquals("Int(64, true)", schema.findField("col5").getType().toString()); | ||
|
||
assertEquals(COMPONENT_TYPE, schema.getCustomMetadata().get(Constants.SCHEMA_COMPONENT_TYPE)); | ||
logger.info("getSchemaFromResults - exit"); | ||
} | ||
} |