ElasticsearchQueryStore implements QueryStore { @Override public TableFieldMapping getFieldMappings(String table) throws QueryStoreException { table = ElasticsearchUtils.getValidTableName(table); try { if (!tableMetadataManager.exists(table)) { throw new QueryStoreException(QueryStoreException.ErrorCode.NO_SUCH_TABLE, "No table exists with the name: " + table); } ElasticsearchMappingParser mappingParser = new ElasticsearchMappingParser(mapper); Set<FieldTypeMapping> mappings = new HashSet<FieldTypeMapping>(); GetMappingsResponse mappingsResponse = connection.getClient().admin() .indices().prepareGetMappings(ElasticsearchUtils.getIndices(table)).execute().actionGet(); for (ObjectCursor<String> index : mappingsResponse.getMappings().keys()) { MappingMetaData mappingData = mappingsResponse.mappings().get(index.value).get(ElasticsearchUtils.TYPE_NAME); mappings.addAll(mappingParser.getFieldMappings(mappingData)); } return new TableFieldMapping(table, mappings); } catch (QueryStoreException ex) { throw ex; } catch (Exception ex) { throw new QueryStoreException(QueryStoreException.ErrorCode.METADATA_FETCH_ERROR, ex.getMessage(), ex); } } ElasticsearchQueryStore(TableMetadataManager tableMetadataManager,
                                   ElasticsearchConnection connection,
                                   DataStore dataStore); @Override void save(String table, Document document); @Override void save(String table, List<Document> documents); @Override Document get(String table, String id); @Override List<Document> get(String table, List<String> ids); @Override TableFieldMapping getFieldMappings(String table); @Override void cleanupAll(); @Override void cleanup(final String table); @Override void cleanup(Set<String> tables);  }
@Test public void testGetFieldMappings() throws QueryStoreException, InterruptedException { queryStore.save(TestUtils.TEST_TABLE_NAME, TestUtils.getMappingDocuments(mapper)); Thread.sleep(500); Set<FieldTypeMapping> mappings = new HashSet<FieldTypeMapping>(); mappings.add(new FieldTypeMapping("word", FieldType.STRING)); mappings.add(new FieldTypeMapping("data.data", FieldType.STRING)); mappings.add(new FieldTypeMapping("header.hello", FieldType.STRING)); mappings.add(new FieldTypeMapping("head.hello", FieldType.LONG)); TableFieldMapping tableFieldMapping = new TableFieldMapping(TestUtils.TEST_TABLE_NAME, mappings); TableFieldMapping responseMapping = queryStore.getFieldMappings(TestUtils.TEST_TABLE_NAME); assertEquals(tableFieldMapping.getTable(), responseMapping.getTable()); assertTrue(tableFieldMapping.getMappings().equals(responseMapping.getMappings())); }
@Test public void testGetFieldMappingsNonExistingTable() throws QueryStoreException { try { queryStore.getFieldMappings(TestUtils.TEST_TABLE + "-test"); fail(); } catch (QueryStoreException qse) { assertEquals(QueryStoreException.ErrorCode.NO_SUCH_TABLE, qse.getErrorCode()); } }
@Test public void testGetFieldMappingsNoDocumentsInTable() throws QueryStoreException { TableFieldMapping request = new TableFieldMapping(TestUtils.TEST_TABLE_NAME, new HashSet<FieldTypeMapping>()); TableFieldMapping response = queryStore.getFieldMappings(TestUtils.TEST_TABLE_NAME); assertEquals(request.getTable(), response.getTable()); assertTrue(request.getMappings().equals(response.getMappings())); }