use of org.teiid.metadata.Table in project teiid by teiid.
the class TestCouchbaseMetadataProcessor method testNestedJson.
@Test
public void testNestedJson() throws ResourceException {
CouchbaseMetadataProcessor mp = new CouchbaseMetadataProcessor();
MetadataFactory mf = new MetadataFactory("vdb", 1, "couchbase", SystemMetadata.getInstance().getRuntimeTypeMap(), new Properties(), null);
Table table = createTable(mf, KEYSPACE, KEYSPACE);
mp.scanRow(KEYSPACE, KEYSPACE_SOURCE, nestedJson(), mf, table, KEYSPACE, false, new Dimension());
helpTest("nestedJson.expected", mf);
}
use of org.teiid.metadata.Table in project teiid by teiid.
the class AccumuloQueryExecution method execute.
@Override
public void execute() throws TranslatorException {
try {
Connector connector = this.connection.getInstance();
List<Range> ranges = this.visitor.getRanges();
Table scanTable = this.visitor.getScanTable();
List<IteratorSetting> scanIterators = visitor.scanIterators();
this.results = runQuery(this.aef, connector, this.connection.getAuthorizations(), ranges, scanTable, scanIterators);
} catch (TableNotFoundException e) {
// Teiid will not let the query come this far with out validating metadata for given table
// so table in user's mind exists, it may be not be in the Accumulo, which should be treated as
// now rows.
this.results = null;
}
}
use of org.teiid.metadata.Table in project teiid by teiid.
the class CouchbaseMetadataProcessor method scanObjectRow.
private void scanObjectRow(String key, String keyInSource, JsonObject value, MetadataFactory mf, Table table, String referenceTableName, boolean isNestedType, Dimension dimension) {
Set<String> names = new TreeSet<String>(value.getNames());
for (String name : names) {
String columnName = name;
Object columnValue = value.get(columnName);
String columnType = getDataType(columnValue);
if (columnType.equals(OBJECT)) {
JsonValue jsonValue = (JsonValue) columnValue;
String newKey = key + UNDERSCORE + columnName;
String newKeyInSource = keyInSource + SOURCE_SEPARATOR + nameInSource(columnName);
if (isObjectJsonType(columnValue)) {
scanRow(newKey, newKeyInSource, jsonValue, mf, table, referenceTableName, true, dimension);
} else if (isArrayJsonType(columnValue)) {
String tableName = repleaceTypedName(table.getName(), newKey);
String tableNameInSource = newKeyInSource + SQUARE_BRACKETS;
Dimension d = new Dimension();
Table subTable = addTable(tableName, tableNameInSource, true, referenceTableName, d, mf);
scanRow(newKey, newKeyInSource, jsonValue, mf, subTable, referenceTableName, true, d);
}
} else {
if (isNestedType) {
columnName = key + UNDERSCORE + columnName;
}
String columnNameInSource = keyInSource + SOURCE_SEPARATOR + nameInSource(name);
addColumn(columnName, columnType, columnValue, true, columnNameInSource, table, mf);
}
}
}
use of org.teiid.metadata.Table in project teiid by teiid.
the class CouchbaseMetadataProcessor method scanArrayRow.
private void scanArrayRow(String keyspace, String keyInSource, JsonArray array, MetadataFactory mf, Table table, String referenceTableName, boolean isNestedType, Dimension dimension) {
if (array.size() > 0) {
for (int i = 0; i < array.size(); i++) {
Object element = array.get(i);
if (isObjectJsonType(element)) {
JsonObject json = (JsonObject) element;
for (String name : new TreeSet<String>(json.getNames())) {
Object columnValue = json.get(name);
String columnType = this.getDataType(columnValue);
if (columnType.equals(OBJECT)) {
JsonValue jsonValue = (JsonValue) columnValue;
if (isObjectJsonType(jsonValue)) {
scanRow(keyspace, keyInSource, jsonValue, mf, table, referenceTableName, true, dimension);
} else if (isArrayJsonType(jsonValue)) {
String tableName = table.getName() + UNDERSCORE + name + UNDERSCORE + dimension.get();
String tableNameInSrc = table.getNameInSource() + SOURCE_SEPARATOR + nameInSource(name) + SQUARE_BRACKETS;
Dimension d = new Dimension();
Table subTable = addTable(tableName, tableNameInSrc, true, referenceTableName, d, mf);
scanRow(keyspace, keyInSource, jsonValue, mf, subTable, referenceTableName, true, d);
}
} else {
String columnName = table.getName() + UNDERSCORE + name;
String columnNameInSource = table.getNameInSource() + SOURCE_SEPARATOR + nameInSource(name);
addColumn(columnName, columnType, columnValue, true, columnNameInSource, table, mf);
}
}
} else if (isArrayJsonType(element)) {
String tableName = table.getName() + UNDERSCORE + dimension.get();
String tableNameInSrc = table.getNameInSource() + SQUARE_BRACKETS;
Dimension d = dimension.copy();
Table subTable = addTable(tableName, tableNameInSrc, true, referenceTableName, d, mf);
scanRow(keyspace, keyInSource, (JsonValue) element, mf, subTable, referenceTableName, true, d);
} else {
String elementType = getDataType(element);
String columnName = table.getName();
String columnNameInSource = table.getNameInSource();
addColumn(columnName, elementType, element, true, columnNameInSource, table, mf);
}
}
} else {
String columnName = table.getName();
addColumn(columnName, null, null, true, null, table, mf);
}
}
use of org.teiid.metadata.Table in project teiid by teiid.
the class CouchbaseMetadataProcessor method addTable.
/**
* Basically, a keyspace be map to a table, keyspace name is the table name, if TranslatorProperty TypeNameList defined,
* a keyspace may map to several tables, for example, if the TypeNameList=`default`:`type`,
* then the {@link CouchbaseMetadataProcessor#addTable(MetadataFactory, CouchbaseConnection, namespace, namespace)}
* will get all distinct `type` attribute referenced values from keyspace, and use all these values as table name.
*
* If multiple keyspaces has same typed value, for example, like TypeNameList=`default`:`type`,`default2`:`type`, both default and default2
* has document defined {"type": "Customer"}, then the default's table name is 'Customer', default2's table name is 'default2_Customer'.
*
* Scan row will add columns to table or create sub-table, nested array be map to a separated table.
*
* @param mf - MetadataFactory
* @param conn - CouchbaseConnection
* @param namespace - couchbase namespace
* @param keyspace - couchbase keyspace
* @throws ResourceException
*/
private void addTable(MetadataFactory mf, CouchbaseConnection conn, String namespace, String keyspace) throws ResourceException {
String nameInSource = nameInSource(keyspace);
Map<String, String> tableNameTypeMap = new HashMap<>();
List<String> typeNameList = getTypeName(nameInSource);
List<String> dataSrcTableList = new ArrayList<>();
if (typeNameList != null && typeNameList.size() > 0) {
String typeQuery = buildN1QLTypeQuery(typeNameList, namespace, keyspace);
LogManager.logTrace(LogConstants.CTX_CONNECTOR, CouchbasePlugin.Util.gs(CouchbasePlugin.Event.TEIID29003, typeQuery));
List<N1qlQueryRow> rows = conn.execute(typeQuery).allRows();
for (N1qlQueryRow row : rows) {
JsonObject rowJson = row.value();
for (String typeName : typeNameList) {
String type = trimWave(typeName);
String value = rowJson.getString(type);
if (value != null) {
dataSrcTableList.add(value);
tableNameTypeMap.put(value, typeName);
break;
}
}
}
} else {
dataSrcTableList.add(keyspace);
}
for (String name : dataSrcTableList) {
String tableName = name;
if (mf.getSchema().getTable(name) != null && !name.equals(keyspace)) {
// handle multiple keyspaces has same typed table name
tableName = keyspace + UNDERSCORE + name;
}
Table table = mf.addTable(tableName);
table.setNameInSource(nameInSource);
table.setSupportsUpdate(true);
table.setProperty(IS_ARRAY_TABLE, FALSE_VALUE);
Column column = mf.addColumn(DOCUMENTID, STRING, table);
column.setUpdatable(true);
// $NON-NLS-1$
mf.addPrimaryKey("PK0", Arrays.asList(DOCUMENTID), table);
if (!name.equals(keyspace)) {
String namedTypePair = buildNamedTypePair(tableNameTypeMap.get(name), name);
table.setProperty(NAMED_TYPE_PAIR, namedTypePair);
}
// scan row
boolean hasTypeIdentifier = true;
if (dataSrcTableList.size() == 1 && dataSrcTableList.get(0).equals(keyspace)) {
hasTypeIdentifier = false;
}
if (this.sampleSize == null || this.sampleSize == 0) {
// default sample size is 100
this.sampleSize = 100;
LogManager.logInfo(LogConstants.CTX_CONNECTOR, CouchbasePlugin.Util.gs(CouchbasePlugin.Event.TEIID29008, this.sampleSize));
}
String query = buildN1QLQuery(tableNameTypeMap.get(name), name, namespace, keyspace, this.sampleSize, hasTypeIdentifier);
LogManager.logTrace(LogConstants.CTX_CONNECTOR, CouchbasePlugin.Util.gs(CouchbasePlugin.Event.TEIID29003, query));
Iterator<N1qlQueryRow> result = conn.execute(query).iterator();
while (result.hasNext()) {
// result.next() always can not be null
JsonObject row = result.next().value();
JsonObject currentRowJson = row.getObject(keyspace);
scanRow(keyspace, nameInSource(keyspace), currentRowJson, mf, table, table.getName(), false, new Dimension());
}
}
}
Aggregations