Repository: metamodel Updated Branches: refs/heads/master f7989b85f -> 137caf0d2
http://git-wip-us.apache.org/repos/asf/metamodel/blob/137caf0d/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchDropTableBuilder.java ---------------------------------------------------------------------- diff --git a/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchDropTableBuilder.java b/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchDropTableBuilder.java deleted file mode 100644 index e691ff2..0000000 --- a/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchDropTableBuilder.java +++ /dev/null @@ -1,103 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ -package org.apache.metamodel.elasticsearch; - -import java.lang.reflect.Method; - -import org.apache.metamodel.MetaModelException; -import org.apache.metamodel.drop.AbstractTableDropBuilder; -import org.apache.metamodel.drop.TableDropBuilder; -import org.apache.metamodel.schema.MutableSchema; -import org.apache.metamodel.schema.Table; -import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingRequestBuilder; -import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingResponse; -import org.elasticsearch.client.Client; -import org.elasticsearch.client.IndicesAdminClient; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * {@link TableDropBuilder} for dropping tables (document types) in an - * ElasticSearch index. - */ -final class ElasticSearchDropTableBuilder extends AbstractTableDropBuilder { - - private static final Logger logger = LoggerFactory.getLogger(ElasticSearchDropTableBuilder.class); - - private final ElasticSearchUpdateCallback _updateCallback; - - public ElasticSearchDropTableBuilder(ElasticSearchUpdateCallback updateCallback, Table table) { - super(table); - _updateCallback = updateCallback; - } - - @Override - public void execute() throws MetaModelException { - final ElasticSearchDataContext dataContext = _updateCallback.getDataContext(); - final Table table = getTable(); - final String documentType = table.getName(); - logger.info("Deleting mapping / document type: {}", documentType); - final Client client = dataContext.getElasticSearchClient(); - final IndicesAdminClient indicesAdminClient = client.admin().indices(); - final String indexName = dataContext.getIndexName(); - - final DeleteMappingRequestBuilder requestBuilder = new DeleteMappingRequestBuilder(indicesAdminClient) - .setIndices(indexName); - setType(requestBuilder, documentType); - - final DeleteMappingResponse result = requestBuilder.execute().actionGet(); - logger.debug("Delete mapping response: acknowledged={}", result.isAcknowledged()); - - final MutableSchema schema = (MutableSchema) table.getSchema(); - schema.removeTable(table); - } - - /** - * Invokes the {@link DeleteMappingRequestBuilder#setType(String...)} method - * using reflection. This is done because the API of ElasticSearch was - * changed and the method signature differes between different versions. - * - * @param requestBuilder - * @param documentType - */ - private void setType(DeleteMappingRequestBuilder requestBuilder, String documentType) { - Object argument; - Method method; - try { - try { - method = requestBuilder.getClass().getDeclaredMethod("setType", String[].class); - argument = new String[] {documentType}; - } catch (NoSuchMethodException e) { - logger.debug("No setType(String[]) method found, trying with a single String instead", e); - method = requestBuilder.getClass().getDeclaredMethod("setType", String.class); - argument = documentType; - } - } catch (Exception e) { - logger.error("Failed to resolve DeleteMappingRequestBuilder.setType(...) method", e); - throw new IllegalStateException("Failed to resolve DeleteMappingRequestBuilder.setType(...) method", e); - } - try { - method.setAccessible(true); - method.invoke(requestBuilder, argument); - } catch (Exception e) { - logger.error("Failed to invoke {}", method, e); - throw new IllegalStateException("Failed to invoke " + method, e); - } - } -} http://git-wip-us.apache.org/repos/asf/metamodel/blob/137caf0d/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchInsertBuilder.java ---------------------------------------------------------------------- diff --git a/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchInsertBuilder.java b/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchInsertBuilder.java deleted file mode 100644 index f2d8746..0000000 --- a/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchInsertBuilder.java +++ /dev/null @@ -1,77 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ -package org.apache.metamodel.elasticsearch; - -import java.util.HashMap; -import java.util.Map; - -import org.apache.metamodel.MetaModelException; -import org.apache.metamodel.insert.AbstractRowInsertionBuilder; -import org.apache.metamodel.schema.Column; -import org.apache.metamodel.schema.Table; -import org.elasticsearch.action.index.IndexRequestBuilder; -import org.elasticsearch.action.index.IndexResponse; -import org.elasticsearch.client.Client; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -final class ElasticSearchInsertBuilder extends AbstractRowInsertionBuilder<ElasticSearchUpdateCallback> { - - private static final Logger logger = LoggerFactory.getLogger(ElasticSearchInsertBuilder.class); - - public ElasticSearchInsertBuilder(ElasticSearchUpdateCallback updateCallback, Table table) { - super(updateCallback, table); - } - - @Override - public void execute() throws MetaModelException { - final ElasticSearchDataContext dataContext = getUpdateCallback().getDataContext(); - final Client client = dataContext.getElasticSearchClient(); - final String indexName = dataContext.getIndexName(); - final String documentType = getTable().getName(); - final IndexRequestBuilder requestBuilder = new IndexRequestBuilder(client, indexName).setType(documentType); - - final Map<String, Object> valueMap = new HashMap<>(); - final Column[] columns = getColumns(); - final Object[] values = getValues(); - for (int i = 0; i < columns.length; i++) { - if (isSet(columns[i])) { - final String name = columns[i].getName(); - final Object value = values[i]; - if (ElasticSearchDataContext.FIELD_ID.equals(name)) { - if (value != null) { - requestBuilder.setId(value.toString()); - } - } else { - valueMap.put(name, value); - } - } - } - - assert !valueMap.isEmpty(); - - requestBuilder.setSource(valueMap); - requestBuilder.setCreate(true); - - final IndexResponse result = requestBuilder.execute().actionGet(); - - logger.debug("Inserted document: id={}", result.getId()); - } - -} http://git-wip-us.apache.org/repos/asf/metamodel/blob/137caf0d/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchMetaData.java ---------------------------------------------------------------------- diff --git a/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchMetaData.java b/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchMetaData.java deleted file mode 100644 index 04d2b63..0000000 --- a/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchMetaData.java +++ /dev/null @@ -1,52 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ -package org.apache.metamodel.elasticsearch; - -import org.apache.metamodel.schema.ColumnType; - -/** - * MetaData representation of an ElasticSearch index type. - * - * We will map the elasticsearch fields to columns and their - * types to {@link ColumnType}s. - */ -public class ElasticSearchMetaData { - - private final String[] columnNames; - private final ColumnType[] columnTypes; - - /** - * Constructs a {@link ElasticSearchMetaData}. - * - * @param names - * @param types - */ - public ElasticSearchMetaData(String[] names, ColumnType[] types) { - this.columnNames = names; - this.columnTypes = types; - } - - public String[] getColumnNames() { - return columnNames; - } - - public ColumnType[] getColumnTypes() { - return columnTypes; - } -} http://git-wip-us.apache.org/repos/asf/metamodel/blob/137caf0d/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchMetaDataParser.java ---------------------------------------------------------------------- diff --git a/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchMetaDataParser.java b/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchMetaDataParser.java deleted file mode 100644 index 41cf184..0000000 --- a/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchMetaDataParser.java +++ /dev/null @@ -1,95 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ -package org.apache.metamodel.elasticsearch; - -import java.util.Map; -import java.util.Map.Entry; - -import org.apache.metamodel.schema.ColumnType; - -/** - * Parser that transforms the ElasticSearch metadata response (json-like format) - * into an ElasticSearchMetaData object. - */ -public class ElasticSearchMetaDataParser { - - /** - * Parses the ElasticSearch meta data info into an ElasticSearchMetaData - * object. This method makes much easier to create the ElasticSearch schema. - * - * @param metaDataInfo - * ElasticSearch mapping metadata in Map format - * @return An ElasticSearchMetaData object - */ - public static ElasticSearchMetaData parse(Map<String, ?> metaDataInfo) { - final String[] fieldNames = new String[metaDataInfo.size() + 1]; - final ColumnType[] columnTypes = new ColumnType[metaDataInfo.size() + 1]; - - // add the document ID field (fixed) - fieldNames[0] = ElasticSearchDataContext.FIELD_ID; - columnTypes[0] = ColumnType.STRING; - - int i = 1; - for (Entry<String, ?> metaDataField : metaDataInfo.entrySet()) { - @SuppressWarnings("unchecked") - final Map<String, ?> fieldMetadata = (Map<String, ?>) metaDataField.getValue(); - - fieldNames[i] = metaDataField.getKey(); - columnTypes[i] = getColumnTypeFromMetadataField(fieldMetadata); - i++; - - } - return new ElasticSearchMetaData(fieldNames, columnTypes); - } - - private static ColumnType getColumnTypeFromMetadataField(Map<String, ?> fieldMetadata) { - final ColumnType columnType; - final String metaDataFieldType = getMetaDataFieldTypeFromMetaDataField(fieldMetadata); - - if (metaDataFieldType == null) { - return ColumnType.STRING; - } - - if (metaDataFieldType.startsWith("date")) { - columnType = ColumnType.DATE; - } else if (metaDataFieldType.equals("long")) { - columnType = ColumnType.BIGINT; - } else if (metaDataFieldType.equals("string")) { - columnType = ColumnType.STRING; - } else if (metaDataFieldType.equals("float")) { - columnType = ColumnType.FLOAT; - } else if (metaDataFieldType.equals("boolean")) { - columnType = ColumnType.BOOLEAN; - } else if (metaDataFieldType.equals("double")) { - columnType = ColumnType.DOUBLE; - } else { - columnType = ColumnType.STRING; - } - return columnType; - } - - private static String getMetaDataFieldTypeFromMetaDataField(Map<String, ?> metaDataField) { - final Object type = metaDataField.get("type"); - if (type == null) { - return null; - } - return type.toString(); - } - -} http://git-wip-us.apache.org/repos/asf/metamodel/blob/137caf0d/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchUpdateCallback.java ---------------------------------------------------------------------- diff --git a/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchUpdateCallback.java b/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchUpdateCallback.java deleted file mode 100644 index a3c6629..0000000 --- a/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchUpdateCallback.java +++ /dev/null @@ -1,87 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ -package org.apache.metamodel.elasticsearch; - -import org.apache.metamodel.AbstractUpdateCallback; -import org.apache.metamodel.UpdateCallback; -import org.apache.metamodel.create.TableCreationBuilder; -import org.apache.metamodel.delete.RowDeletionBuilder; -import org.apache.metamodel.drop.TableDropBuilder; -import org.apache.metamodel.insert.RowInsertionBuilder; -import org.apache.metamodel.schema.Schema; -import org.apache.metamodel.schema.Table; -import org.elasticsearch.client.Client; - -/** - * {@link UpdateCallback} implementation for {@link ElasticSearchDataContext}. - */ -final class ElasticSearchUpdateCallback extends AbstractUpdateCallback { - - public ElasticSearchUpdateCallback(ElasticSearchDataContext dataContext) { - super(dataContext); - } - - @Override - public ElasticSearchDataContext getDataContext() { - return (ElasticSearchDataContext) super.getDataContext(); - } - - @Override - public TableCreationBuilder createTable(Schema schema, String name) throws IllegalArgumentException, - IllegalStateException { - return new ElasticSearchCreateTableBuilder(this, schema, name); - } - - @Override - public boolean isDropTableSupported() { - return true; - } - - @Override - public TableDropBuilder dropTable(Table table) throws IllegalArgumentException, IllegalStateException, - UnsupportedOperationException { - return new ElasticSearchDropTableBuilder(this, table); - } - - @Override - public RowInsertionBuilder insertInto(Table table) throws IllegalArgumentException, IllegalStateException, - UnsupportedOperationException { - return new ElasticSearchInsertBuilder(this, table); - } - - @Override - public boolean isDeleteSupported() { - return true; - } - - @Override - public RowDeletionBuilder deleteFrom(Table table) throws IllegalArgumentException, IllegalStateException, - UnsupportedOperationException { - return new ElasticSearchDeleteBuilder(this, table); - } - - public void onExecuteUpdateFinished() { - // force refresh of the index - final ElasticSearchDataContext dataContext = getDataContext(); - final Client client = dataContext.getElasticSearchClient(); - final String indexName = dataContext.getIndexName(); - client.admin().indices().prepareRefresh(indexName).execute().actionGet(); - } - -} http://git-wip-us.apache.org/repos/asf/metamodel/blob/137caf0d/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchUtils.java ---------------------------------------------------------------------- diff --git a/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchUtils.java b/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchUtils.java deleted file mode 100644 index 20f56df..0000000 --- a/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchUtils.java +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ -package org.apache.metamodel.elasticsearch; - -import java.util.Date; -import java.util.Map; - -import org.apache.metamodel.data.DataSetHeader; -import org.apache.metamodel.data.DefaultRow; -import org.apache.metamodel.data.Row; -import org.apache.metamodel.query.SelectItem; -import org.apache.metamodel.schema.Column; -import org.apache.metamodel.schema.ColumnType; -import org.apache.metamodel.util.TimeComparator; - -/** - * Shared/common util functions for the ElasticSearch MetaModel module. - */ -final class ElasticSearchUtils { - - public static Row createRow(Map<String, Object> sourceMap, String documentId, DataSetHeader header) { - final Object[] values = new Object[header.size()]; - for (int i = 0; i < values.length; i++) { - final SelectItem selectItem = header.getSelectItem(i); - final Column column = selectItem.getColumn(); - - assert column != null; - assert selectItem.getFunction() == null; - - if (column.isPrimaryKey()) { - values[i] = documentId; - } else { - Object value = sourceMap.get(column.getName()); - - if (column.getType() == ColumnType.DATE) { - Date valueToDate = ElasticSearchDateConverter.tryToConvert((String) value); - if (valueToDate == null) { - values[i] = value; - } else { - values[i] = valueToDate; - } - } else { - values[i] = value; - } - } - } - - return new DefaultRow(header, values); - } -} http://git-wip-us.apache.org/repos/asf/metamodel/blob/137caf0d/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchDataContextTest.java ---------------------------------------------------------------------- diff --git a/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchDataContextTest.java b/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchDataContextTest.java deleted file mode 100644 index 5c17587..0000000 --- a/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchDataContextTest.java +++ /dev/null @@ -1,605 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ -package org.apache.metamodel.elasticsearch; - -import java.io.IOException; -import java.util.Arrays; -import java.util.Date; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import javax.swing.table.TableModel; - -import org.apache.metamodel.MetaModelHelper; -import org.apache.metamodel.UpdateCallback; -import org.apache.metamodel.UpdateScript; -import org.apache.metamodel.UpdateableDataContext; -import org.apache.metamodel.create.CreateTable; -import org.apache.metamodel.data.DataSet; -import org.apache.metamodel.data.DataSetTableModel; -import org.apache.metamodel.data.InMemoryDataSet; -import org.apache.metamodel.data.Row; -import org.apache.metamodel.delete.DeleteFrom; -import org.apache.metamodel.drop.DropTable; -import org.apache.metamodel.elasticsearch.utils.EmbeddedElasticsearchServer; -import org.apache.metamodel.query.FunctionType; -import org.apache.metamodel.query.Query; -import org.apache.metamodel.query.SelectItem; -import org.apache.metamodel.schema.Column; -import org.apache.metamodel.schema.ColumnType; -import org.apache.metamodel.schema.Schema; -import org.apache.metamodel.schema.Table; -import org.apache.metamodel.update.Update; -import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; -import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; -import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingRequestBuilder; -import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; -import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequestBuilder; -import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse; -import org.elasticsearch.action.bulk.BulkRequestBuilder; -import org.elasticsearch.client.Client; -import org.elasticsearch.client.IndicesAdminClient; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; -import static org.junit.Assert.*; - -public class ElasticSearchDataContextTest { - - private static final String indexName = "twitter"; - private static final String indexType1 = "tweet1"; - private static final String indexType2 = "tweet2"; - private static final String indexName2 = "twitter2"; - private static final String indexType3 = "tweet3"; - private static final String bulkIndexType = "bulktype"; - private static final String peopleIndexType = "peopletype"; - private static final String mapping = "{\"date_detection\":\"false\",\"properties\":{\"message\":{\"type\":\"string\",\"index\":\"not_analyzed\",\"doc_values\":\"true\"}}}"; - private static EmbeddedElasticsearchServer embeddedElasticsearchServer; - private static Client client; - private static UpdateableDataContext dataContext; - - @BeforeClass - public static void beforeTests() throws Exception { - embeddedElasticsearchServer = new EmbeddedElasticsearchServer(); - client = embeddedElasticsearchServer.getClient(); - indexTweeterDocument(indexType1, 1); - indexTweeterDocument(indexType2, 1); - indexTweeterDocument(indexType2, 2, null); - insertPeopleDocuments(); - indexTweeterDocument(indexType2, 1); - indexBulkDocuments(indexName, bulkIndexType, 10); - - // The refresh API allows to explicitly refresh one or more index, - // making all operations performed since the last refresh available for - // search - client.admin().indices().prepareRefresh().execute().actionGet(); - dataContext = new ElasticSearchDataContext(client, indexName); - System.out.println("Embedded ElasticSearch server created!"); - } - - private static void insertPeopleDocuments() throws IOException { - indexOnePeopleDocument("female", 20, 5); - indexOnePeopleDocument("female", 17, 8); - indexOnePeopleDocument("female", 18, 9); - indexOnePeopleDocument("female", 19, 10); - indexOnePeopleDocument("female", 20, 11); - indexOnePeopleDocument("male", 19, 1); - indexOnePeopleDocument("male", 17, 2); - indexOnePeopleDocument("male", 18, 3); - indexOnePeopleDocument("male", 18, 4); - } - - @AfterClass - public static void afterTests() { - embeddedElasticsearchServer.shutdown(); - System.out.println("Embedded ElasticSearch server shut down!"); - } - - @Test - public void testSimpleQuery() throws Exception { - assertEquals("[bulktype, peopletype, tweet1, tweet2]", - Arrays.toString(dataContext.getDefaultSchema().getTableNames())); - - Table table = dataContext.getDefaultSchema().getTableByName("tweet1"); - - assertEquals("[_id, message, postDate, user]", Arrays.toString(table.getColumnNames())); - - assertEquals(ColumnType.STRING, table.getColumnByName("user").getType()); - assertEquals(ColumnType.DATE, table.getColumnByName("postDate").getType()); - assertEquals(ColumnType.BIGINT, table.getColumnByName("message").getType()); - - try(DataSet ds = dataContext.query().from(indexType1).select("user").and("message").execute()) { - assertEquals(ElasticSearchDataSet.class, ds.getClass()); - - assertTrue(ds.next()); - assertEquals("Row[values=[user1, 1]]", ds.getRow().toString()); - } - } - - @Test - public void testDocumentIdAsPrimaryKey() throws Exception { - Table table = dataContext.getDefaultSchema().getTableByName("tweet2"); - Column[] pks = table.getPrimaryKeys(); - assertEquals(1, pks.length); - assertEquals("_id", pks[0].getName()); - - try (DataSet ds = dataContext.query().from(table).select("user", "_id").orderBy("_id").asc().execute()) { - assertTrue(ds.next()); - assertEquals("Row[values=[user1, tweet_tweet2_1]]", ds.getRow().toString()); - } - } - - @Test - public void testExecutePrimaryKeyLookupQuery() throws Exception { - Table table = dataContext.getDefaultSchema().getTableByName("tweet2"); - Column[] pks = table.getPrimaryKeys(); - - try (DataSet ds = dataContext.query().from(table).selectAll().where(pks[0]).eq("tweet_tweet2_1").execute()) { - assertTrue(ds.next()); - Object dateValue = ds.getRow().getValue(2); - assertEquals("Row[values=[tweet_tweet2_1, 1, " + dateValue + ", user1]]", ds.getRow().toString()); - - assertFalse(ds.next()); - - assertEquals(InMemoryDataSet.class, ds.getClass()); - } - } - - @Test - public void testDateIsHandledAsDate() throws Exception { - Table table = dataContext.getDefaultSchema().getTableByName("tweet1"); - Column column = table.getColumnByName("postDate"); - ColumnType type = column.getType(); - assertEquals(ColumnType.DATE, type); - - DataSet dataSet = dataContext.query().from(table).select(column).execute(); - while (dataSet.next()) { - Object value = dataSet.getRow().getValue(column); - assertTrue("Got class: " + value.getClass() + ", expected Date (or subclass)", value instanceof Date); - } - } - - @Test - public void testNumberIsHandledAsNumber() throws Exception { - Table table = dataContext.getDefaultSchema().getTableByName(peopleIndexType); - Column column = table.getColumnByName("age"); - ColumnType type = column.getType(); - assertEquals(ColumnType.BIGINT, type); - - DataSet dataSet = dataContext.query().from(table).select(column).execute(); - while (dataSet.next()) { - Object value = dataSet.getRow().getValue(column); - assertTrue("Got class: " + value.getClass() + ", expected Number (or subclass)", value instanceof Number); - } - } - - @Test - public void testCreateTableInsertQueryAndDrop() throws Exception { - final Schema schema = dataContext.getDefaultSchema(); - final CreateTable createTable = new CreateTable(schema, "testCreateTable"); - createTable.withColumn("foo").ofType(ColumnType.STRING); - createTable.withColumn("bar").ofType(ColumnType.NUMBER); - dataContext.executeUpdate(createTable); - - final Table table = schema.getTableByName("testCreateTable"); - assertEquals("[" + ElasticSearchDataContext.FIELD_ID + ", foo, bar]", Arrays.toString(table.getColumnNames())); - - final Column fooColumn = table.getColumnByName("foo"); - final Column idColumn = table.getPrimaryKeys()[0]; - assertEquals("Column[name=_id,columnNumber=0,type=STRING,nullable=null,nativeType=null,columnSize=null]", - idColumn.toString()); - - dataContext.executeUpdate(new UpdateScript() { - @Override - public void run(UpdateCallback callback) { - callback.insertInto(table).value("foo", "hello").value("bar", 42).execute(); - callback.insertInto(table).value("foo", "world").value("bar", 43).execute(); - } - }); - - dataContext.refreshSchemas(); - - try (DataSet ds = dataContext.query().from(table).selectAll().orderBy("bar").execute()) { - assertTrue(ds.next()); - assertEquals("hello", ds.getRow().getValue(fooColumn).toString()); - assertNotNull(ds.getRow().getValue(idColumn)); - assertTrue(ds.next()); - assertEquals("world", ds.getRow().getValue(fooColumn).toString()); - assertNotNull(ds.getRow().getValue(idColumn)); - assertFalse(ds.next()); - } - - dataContext.executeUpdate(new DropTable(table)); - - dataContext.refreshSchemas(); - - assertNull(dataContext.getTableByQualifiedLabel(table.getName())); - } - - @Test - public void testDetectOutsideChanges() throws Exception { - ElasticSearchDataContext elasticSearchDataContext = (ElasticSearchDataContext) dataContext; - - // Create the type in ES - final IndicesAdminClient indicesAdmin = elasticSearchDataContext.getElasticSearchClient().admin().indices(); - final String tableType = "outsideTable"; - - Object[] sourceProperties = { "testA", "type=string, store=true", "testB", "type=string, store=true" }; - - new PutMappingRequestBuilder(indicesAdmin).setIndices(indexName).setType(tableType).setSource(sourceProperties) - .execute().actionGet(); - - dataContext.refreshSchemas(); - - assertNotNull(dataContext.getDefaultSchema().getTableByName(tableType)); - - new DeleteMappingRequestBuilder(indicesAdmin).setIndices(indexName).setType(tableType).execute().actionGet(); - dataContext.refreshSchemas(); - assertNull(dataContext.getTableByQualifiedLabel(tableType)); - } - - @Test - public void testDeleteAll() throws Exception { - final Schema schema = dataContext.getDefaultSchema(); - final CreateTable createTable = new CreateTable(schema, "testCreateTable"); - createTable.withColumn("foo").ofType(ColumnType.STRING); - createTable.withColumn("bar").ofType(ColumnType.NUMBER); - dataContext.executeUpdate(createTable); - - final Table table = schema.getTableByName("testCreateTable"); - - dataContext.executeUpdate(new UpdateScript() { - @Override - public void run(UpdateCallback callback) { - callback.insertInto(table).value("foo", "hello").value("bar", 42).execute(); - callback.insertInto(table).value("foo", "world").value("bar", 43).execute(); - } - }); - - dataContext.executeUpdate(new DeleteFrom(table)); - - Row row = MetaModelHelper.executeSingleRowQuery(dataContext, dataContext.query().from(table).selectCount() - .toQuery()); - assertEquals("Row[values=[0]]", row.toString()); - - dataContext.executeUpdate(new DropTable(table)); - } - - @Test - public void testDeleteByQuery() throws Exception { - final Schema schema = dataContext.getDefaultSchema(); - final CreateTable createTable = new CreateTable(schema, "testCreateTable"); - createTable.withColumn("foo").ofType(ColumnType.STRING); - createTable.withColumn("bar").ofType(ColumnType.NUMBER); - dataContext.executeUpdate(createTable); - - final Table table = schema.getTableByName("testCreateTable"); - - dataContext.executeUpdate(new UpdateScript() { - @Override - public void run(UpdateCallback callback) { - callback.insertInto(table).value("foo", "hello").value("bar", 42).execute(); - callback.insertInto(table).value("foo", "world").value("bar", 43).execute(); - } - }); - - dataContext.executeUpdate(new DeleteFrom(table).where("foo").eq("hello").where("bar").eq(42)); - - Row row = MetaModelHelper.executeSingleRowQuery(dataContext, - dataContext.query().from(table).select("foo", "bar").toQuery()); - assertEquals("Row[values=[world, 43]]", row.toString()); - - dataContext.executeUpdate(new DropTable(table)); - } - - @Test - public void testDeleteUnsupportedQueryType() throws Exception { - final Schema schema = dataContext.getDefaultSchema(); - final CreateTable createTable = new CreateTable(schema, "testCreateTable"); - createTable.withColumn("foo").ofType(ColumnType.STRING); - createTable.withColumn("bar").ofType(ColumnType.NUMBER); - dataContext.executeUpdate(createTable); - - final Table table = schema.getTableByName("testCreateTable"); - try { - - dataContext.executeUpdate(new UpdateScript() { - @Override - public void run(UpdateCallback callback) { - callback.insertInto(table).value("foo", "hello").value("bar", 42).execute(); - callback.insertInto(table).value("foo", "world").value("bar", 43).execute(); - } - }); - - // greater than is not yet supported - try { - dataContext.executeUpdate(new DeleteFrom(table).where("bar").gt(40)); - fail("Exception expected"); - } catch (UnsupportedOperationException e) { - assertEquals("Could not push down WHERE items to delete by query request: [testCreateTable.bar > 40]", - e.getMessage()); - } - - } finally { - dataContext.executeUpdate(new DropTable(table)); - } - } - - @Test - public void testUpdateRow() throws Exception { - final Schema schema = dataContext.getDefaultSchema(); - final CreateTable createTable = new CreateTable(schema, "testCreateTable"); - createTable.withColumn("foo").ofType(ColumnType.STRING); - createTable.withColumn("bar").ofType(ColumnType.NUMBER); - dataContext.executeUpdate(createTable); - - final Table table = schema.getTableByName("testCreateTable"); - try { - - dataContext.executeUpdate(new UpdateScript() { - @Override - public void run(UpdateCallback callback) { - callback.insertInto(table).value("foo", "hello").value("bar", 42).execute(); - callback.insertInto(table).value("foo", "world").value("bar", 43).execute(); - } - }); - - dataContext.executeUpdate(new Update(table).value("foo", "howdy").where("bar").eq(42)); - - DataSet dataSet = dataContext.query().from(table).select("foo", "bar").orderBy("bar").execute(); - assertTrue(dataSet.next()); - assertEquals("Row[values=[howdy, 42]]", dataSet.getRow().toString()); - assertTrue(dataSet.next()); - assertEquals("Row[values=[world, 43]]", dataSet.getRow().toString()); - assertFalse(dataSet.next()); - dataSet.close(); - } finally { - dataContext.executeUpdate(new DropTable(table)); - } - } - - @Test - public void testDropTable() throws Exception { - Table table = dataContext.getDefaultSchema().getTableByName(peopleIndexType); - - // assert that the table was there to begin with - { - DataSet ds = dataContext.query().from(table).selectCount().execute(); - ds.next(); - assertEquals("Row[values=[9]]", ds.getRow().toString()); - ds.close(); - } - - dataContext.executeUpdate(new DropTable(table)); - try { - DataSet ds = dataContext.query().from(table).selectCount().execute(); - ds.next(); - assertEquals("Row[values=[0]]", ds.getRow().toString()); - ds.close(); - } finally { - // restore the people documents for the next tests - insertPeopleDocuments(); - client.admin().indices().prepareRefresh().execute().actionGet(); - dataContext = new ElasticSearchDataContext(client, indexName); - } - } - - @Test - public void testWhereColumnEqualsValues() throws Exception { - try (DataSet ds = dataContext.query().from(bulkIndexType).select("user").and("message").where("user") - .isEquals("user4").execute()) { - assertEquals(ElasticSearchDataSet.class, ds.getClass()); - - assertTrue(ds.next()); - assertEquals("Row[values=[user4, 4]]", ds.getRow().toString()); - assertFalse(ds.next()); - } - } - - @Test - public void testWhereColumnIsNullValues() throws Exception { - try(DataSet ds = dataContext.query().from(indexType2).select("message").where("postDate") - .isNull().execute()){ - assertEquals(ElasticSearchDataSet.class, ds.getClass()); - - assertTrue(ds.next()); - assertEquals("Row[values=[2]]", ds.getRow().toString()); - assertFalse(ds.next()); - } - } - - @Test - public void testWhereColumnIsNotNullValues() throws Exception { - try(DataSet ds = dataContext.query().from(indexType2).select("message").where("postDate") - .isNotNull().execute()){ - assertEquals(ElasticSearchDataSet.class, ds.getClass()); - - assertTrue(ds.next()); - assertEquals("Row[values=[1]]", ds.getRow().toString()); - assertFalse(ds.next()); - } - } - - @Test - public void testWhereMultiColumnsEqualValues() throws Exception { - try(DataSet ds = dataContext.query().from(bulkIndexType).select("user").and("message").where("user") - .isEquals("user4").and("message").ne(5).execute()){ - assertEquals(ElasticSearchDataSet.class, ds.getClass()); - - assertTrue(ds.next()); - assertEquals("Row[values=[user4, 4]]", ds.getRow().toString()); - assertFalse(ds.next()); - } - } - - @Test - public void testWhereColumnInValues() throws Exception { - try (DataSet ds = dataContext.query().from(bulkIndexType).select("user").and("message").where("user") - .in("user4", "user5").orderBy("message").execute()) { - assertTrue(ds.next()); - - String row1 = ds.getRow().toString(); - assertEquals("Row[values=[user4, 4]]", row1); - assertTrue(ds.next()); - - String row2 = ds.getRow().toString(); - assertEquals("Row[values=[user5, 5]]", row2); - - assertFalse(ds.next()); - } - } - - @Test - public void testGroupByQuery() throws Exception { - Table table = dataContext.getDefaultSchema().getTableByName(peopleIndexType); - - Query q = new Query(); - q.from(table); - q.groupBy(table.getColumnByName("gender")); - q.select(new SelectItem(table.getColumnByName("gender")), - new SelectItem(FunctionType.MAX, table.getColumnByName("age")), - new SelectItem(FunctionType.MIN, table.getColumnByName("age")), new SelectItem(FunctionType.COUNT, "*", - "total"), new SelectItem(FunctionType.MIN, table.getColumnByName("id")).setAlias("firstId")); - q.orderBy("gender"); - DataSet data = dataContext.executeQuery(q); - assertEquals( - "[peopletype.gender, MAX(peopletype.age), MIN(peopletype.age), COUNT(*) AS total, MIN(peopletype.id) AS firstId]", - Arrays.toString(data.getSelectItems())); - - assertTrue(data.next()); - assertEquals("Row[values=[female, 20, 17, 5, 5]]", data.getRow().toString()); - assertTrue(data.next()); - assertEquals("Row[values=[male, 19, 17, 4, 1]]", data.getRow().toString()); - assertFalse(data.next()); - } - - @Test - public void testFilterOnNumberColumn() { - Table table = dataContext.getDefaultSchema().getTableByName(bulkIndexType); - Query q = dataContext.query().from(table).select("user").where("message").greaterThan(7).toQuery(); - DataSet data = dataContext.executeQuery(q); - String[] expectations = new String[] { "Row[values=[user8]]", "Row[values=[user9]]" }; - - assertTrue(data.next()); - assertTrue(Arrays.asList(expectations).contains(data.getRow().toString())); - assertTrue(data.next()); - assertTrue(Arrays.asList(expectations).contains(data.getRow().toString())); - assertFalse(data.next()); - } - - @Test - public void testMaxRows() throws Exception { - Table table = dataContext.getDefaultSchema().getTableByName(peopleIndexType); - Query query = new Query().from(table).select(table.getColumns()).setMaxRows(5); - DataSet dataSet = dataContext.executeQuery(query); - - TableModel tableModel = new DataSetTableModel(dataSet); - assertEquals(5, tableModel.getRowCount()); - } - - @Test - public void testCountQuery() throws Exception { - Table table = dataContext.getDefaultSchema().getTableByName(bulkIndexType); - Query q = new Query().selectCount().from(table); - - List<Object[]> data = dataContext.executeQuery(q).toObjectArrays(); - assertEquals(1, data.size()); - Object[] row = data.get(0); - assertEquals(1, row.length); - assertEquals("[10]", Arrays.toString(row)); - } - - @Test(expected = IllegalArgumentException.class) - public void testQueryForANonExistingTable() throws Exception { - dataContext.query().from("nonExistingTable").select("user").and("message").execute(); - } - - @Test(expected = IllegalArgumentException.class) - public void testQueryForAnExistingTableAndNonExistingField() throws Exception { - indexTweeterDocument(indexType1, 1); - dataContext.query().from(indexType1).select("nonExistingField").execute(); - } - - @Test - public void testNonDynamicMapingTableNames() throws Exception { - createIndex(); - - ElasticSearchDataContext dataContext2 = new ElasticSearchDataContext(client, indexName2); - - assertEquals("[tweet3]", Arrays.toString(dataContext2.getDefaultSchema().getTableNames())); - } - - private static void createIndex() { - CreateIndexRequest cir = new CreateIndexRequest(indexName2); - CreateIndexResponse response = client.admin().indices().create(cir).actionGet(); - - System.out.println("create index: " + response.isAcknowledged()); - - PutMappingRequest pmr = new PutMappingRequest(indexName2).type(indexType3).source(mapping); - - PutMappingResponse response2 = client.admin().indices().putMapping(pmr).actionGet(); - System.out.println("put mapping: " + response2.isAcknowledged()); - } - - private static void indexBulkDocuments(String indexName, String indexType, int numberOfDocuments) { - BulkRequestBuilder bulkRequest = client.prepareBulk(); - - for (int i = 0; i < numberOfDocuments; i++) { - bulkRequest.add(client.prepareIndex(indexName, indexType, Integer.toString(i)).setSource( - buildTweeterJson(i))); - } - bulkRequest.execute().actionGet(); - } - - private static void indexTweeterDocument(String indexType, int id, Date date) { - client.prepareIndex(indexName, indexType).setSource(buildTweeterJson(id, date)) - .setId("tweet_" + indexType + "_" + id).execute().actionGet(); - } - - private static void indexTweeterDocument(String indexType, int id) { - client.prepareIndex(indexName, indexType).setSource(buildTweeterJson(id)) - .setId("tweet_" + indexType + "_" + id).execute().actionGet(); - } - - private static void indexOnePeopleDocument(String gender, int age, int id) throws IOException { - client.prepareIndex(indexName, peopleIndexType).setSource(buildPeopleJson(gender, age, id)).execute() - .actionGet(); - } - - private static Map<String, Object> buildTweeterJson(int elementId) { - return buildTweeterJson(elementId, new Date()); - } - - private static Map<String, Object> buildTweeterJson(int elementId, Date date) { - Map<String, Object> map = new LinkedHashMap<>(); - map.put("user", "user" + elementId); - map.put("postDate", date); - map.put("message", elementId); - return map; - } - - private static XContentBuilder buildPeopleJson(String gender, int age, int elementId) throws IOException { - return jsonBuilder().startObject().field("gender", gender).field("age", age).field("id", elementId).endObject(); - } - -} http://git-wip-us.apache.org/repos/asf/metamodel/blob/137caf0d/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchDateConverterTest.java ---------------------------------------------------------------------- diff --git a/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchDateConverterTest.java b/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchDateConverterTest.java deleted file mode 100644 index b283bd4..0000000 --- a/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchDateConverterTest.java +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ -package org.apache.metamodel.elasticsearch; - -import junit.framework.TestCase; - -import java.util.Date; - -public class ElasticSearchDateConverterTest extends TestCase { - - public void testConvertDateOptionalTime() throws Exception { - String dateToConvert = "2013-01-04T15:55:51.217+01:00"; - Date date = ElasticSearchDateConverter.tryToConvert(dateToConvert); - - assertNotNull(date); - assertTrue(date.toString().startsWith("Fri Jan 04")); - } -} http://git-wip-us.apache.org/repos/asf/metamodel/blob/137caf0d/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchMetaDataParserTest.java ---------------------------------------------------------------------- diff --git a/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchMetaDataParserTest.java b/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchMetaDataParserTest.java deleted file mode 100644 index 4d8bef3..0000000 --- a/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchMetaDataParserTest.java +++ /dev/null @@ -1,65 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ -package org.apache.metamodel.elasticsearch; - -import java.util.LinkedHashMap; -import java.util.Map; - -import junit.framework.TestCase; - -import org.apache.metamodel.schema.ColumnType; -import org.elasticsearch.common.collect.MapBuilder; - -public class ElasticSearchMetaDataParserTest extends TestCase { - - public void testParseMetadataInfo() throws Exception { - Map<String, Object> metadata = new LinkedHashMap<String, Object>(); - metadata.put("message", MapBuilder.newMapBuilder().put("type", "long").immutableMap()); - metadata.put("postDate", MapBuilder.newMapBuilder().put("type", "date").put("format", "dateOptionalTime").immutableMap()); - metadata.put("anotherDate", MapBuilder.newMapBuilder().put("type", "date").put("format", "dateOptionalTime").immutableMap()); - metadata.put("user", MapBuilder.newMapBuilder().put("type", "string").immutableMap()); - metadata.put("critical", MapBuilder.newMapBuilder().put("type", "boolean").immutableMap()); - metadata.put("income", MapBuilder.newMapBuilder().put("type", "double").immutableMap()); - metadata.put("untypedthingie", MapBuilder.newMapBuilder().put("foo", "bar").immutableMap()); - - ElasticSearchMetaData metaData = ElasticSearchMetaDataParser.parse(metadata); - String[] columnNames = metaData.getColumnNames(); - ColumnType[] columnTypes = metaData.getColumnTypes(); - - assertTrue(columnNames.length == 8); - assertEquals(columnNames[0], "_id"); - assertEquals(columnNames[1], "message"); - assertEquals(columnNames[2], "postDate"); - assertEquals(columnNames[3], "anotherDate"); - assertEquals(columnNames[4], "user"); - assertEquals(columnNames[5], "critical"); - assertEquals(columnNames[6], "income"); - assertEquals(columnNames[7], "untypedthingie"); - - assertTrue(columnTypes.length == 8); - assertEquals(columnTypes[0], ColumnType.STRING); - assertEquals(columnTypes[1], ColumnType.BIGINT); - assertEquals(columnTypes[2], ColumnType.DATE); - assertEquals(columnTypes[3], ColumnType.DATE); - assertEquals(columnTypes[4], ColumnType.STRING); - assertEquals(columnTypes[5], ColumnType.BOOLEAN); - assertEquals(columnTypes[6], ColumnType.DOUBLE); - assertEquals(columnTypes[7], ColumnType.STRING); - } -} http://git-wip-us.apache.org/repos/asf/metamodel/blob/137caf0d/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchUtilsTest.java ---------------------------------------------------------------------- diff --git a/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchUtilsTest.java b/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchUtilsTest.java deleted file mode 100644 index acece88..0000000 --- a/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchUtilsTest.java +++ /dev/null @@ -1,63 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ -package org.apache.metamodel.elasticsearch; - -import junit.framework.TestCase; -import org.apache.metamodel.data.DataSetHeader; -import org.apache.metamodel.data.Row; -import org.apache.metamodel.data.SimpleDataSetHeader; -import org.apache.metamodel.query.SelectItem; -import org.apache.metamodel.schema.ColumnType; -import org.apache.metamodel.schema.MutableColumn; - -import java.util.*; - -public class ElasticSearchUtilsTest extends TestCase { - - public void testAssignDocumentIdForPrimaryKeys() throws Exception { - MutableColumn primaryKeyColumn = new MutableColumn("value1", ColumnType.STRING).setPrimaryKey(true); - SelectItem primaryKeyItem = new SelectItem(primaryKeyColumn); - List<SelectItem> selectItems1 = Arrays.asList(primaryKeyItem); - String documentId = "doc1"; - DataSetHeader header = new SimpleDataSetHeader(selectItems1); - Map values = new HashMap<String, Object>(); - values.put("value1", "theValue"); - Row row = ElasticSearchUtils.createRow(values, documentId, header); - String primaryKeyValue = (String) row.getValue(primaryKeyItem); - - assertEquals(primaryKeyValue, documentId); - } - - public void testCreateRowWithParseableDates() throws Exception { - SelectItem item1 = new SelectItem(new MutableColumn("value1", ColumnType.STRING)); - SelectItem item2 = new SelectItem(new MutableColumn("value2", ColumnType.DATE)); - List<SelectItem> selectItems1 = Arrays.asList(item1, item2); - String documentId = "doc1"; - DataSetHeader header = new SimpleDataSetHeader(selectItems1); - Map values = new HashMap<String, Object>(); - values.put("value1", "theValue"); - values.put("value2", "2013-01-04T15:55:51.217+01:00"); - Row row = ElasticSearchUtils.createRow(values, documentId, header); - Object stringValue = row.getValue(item1); - Object dateValue = row.getValue(item2); - - assertTrue(stringValue instanceof String); - assertTrue(dateValue instanceof Date); - } -} http://git-wip-us.apache.org/repos/asf/metamodel/blob/137caf0d/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/utils/EmbeddedElasticsearchServer.java ---------------------------------------------------------------------- diff --git a/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/utils/EmbeddedElasticsearchServer.java b/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/utils/EmbeddedElasticsearchServer.java deleted file mode 100644 index 5e6fcff..0000000 --- a/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/utils/EmbeddedElasticsearchServer.java +++ /dev/null @@ -1,71 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ -package org.apache.metamodel.elasticsearch.utils; - -import org.apache.commons.io.FileUtils; -import org.elasticsearch.client.Client; -import org.elasticsearch.common.settings.ImmutableSettings; -import org.elasticsearch.node.Node; - -import java.io.File; -import java.io.IOException; - -import static org.elasticsearch.node.NodeBuilder.nodeBuilder; - -public class EmbeddedElasticsearchServer { - - private static final String DEFAULT_DATA_DIRECTORY = "target/elasticsearch-data"; - - private final Node node; - private final String dataDirectory; - - public EmbeddedElasticsearchServer() { - this(DEFAULT_DATA_DIRECTORY); - } - - public EmbeddedElasticsearchServer(String dataDirectory) { - this.dataDirectory = dataDirectory; - - ImmutableSettings.Builder elasticsearchSettings = ImmutableSettings.settingsBuilder() - .put("http.enabled", "true") - .put("path.data", dataDirectory); - - node = nodeBuilder() - .local(true) - .settings(elasticsearchSettings.build()) - .node(); - } - - public Client getClient() { - return node.client(); - } - - public void shutdown() { - node.close(); - deleteDataDirectory(); - } - - private void deleteDataDirectory() { - try { - FileUtils.deleteDirectory(new File(dataDirectory)); - } catch (IOException e) { - throw new RuntimeException("Could not delete data directory of embedded elasticsearch server", e); - } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/metamodel/blob/137caf0d/full/pom.xml ---------------------------------------------------------------------- diff --git a/full/pom.xml b/full/pom.xml index f2a5aea..5835eef 100644 --- a/full/pom.xml +++ b/full/pom.xml @@ -147,11 +147,6 @@ under the License. </dependency> <dependency> <groupId>org.apache.metamodel</groupId> - <artifactId>MetaModel-elasticsearch</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>org.apache.metamodel</groupId> <artifactId>MetaModel-excel</artifactId> <version>${project.version}</version> </dependency> @@ -210,6 +205,16 @@ under the License. <artifactId>MetaModel-xml</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>org.apache.metamodel</groupId> + <artifactId>MetaModel-elasticsearch-rest</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.metamodel</groupId> + <artifactId>MetaModel-elasticsearch-native</artifactId> + <version>${project.version}</version> + </dependency> <!-- Test dependencies --> <dependency> <groupId>junit</groupId> http://git-wip-us.apache.org/repos/asf/metamodel/blob/137caf0d/full/src/main/java/org/apache/metamodel/DataContextFactory.java ---------------------------------------------------------------------- diff --git a/full/src/main/java/org/apache/metamodel/DataContextFactory.java b/full/src/main/java/org/apache/metamodel/DataContextFactory.java index 07ff989..37776e9 100644 --- a/full/src/main/java/org/apache/metamodel/DataContextFactory.java +++ b/full/src/main/java/org/apache/metamodel/DataContextFactory.java @@ -31,7 +31,8 @@ import org.apache.metamodel.cassandra.CassandraDataContext; import org.apache.metamodel.couchdb.CouchDbDataContext; import org.apache.metamodel.csv.CsvConfiguration; import org.apache.metamodel.csv.CsvDataContext; -import org.apache.metamodel.elasticsearch.ElasticSearchDataContext; +import org.apache.metamodel.elasticsearch.nativeclient.ElasticSearchDataContext; +import org.apache.metamodel.elasticsearch.rest.ElasticSearchRestDataContext; import org.apache.metamodel.excel.ExcelConfiguration; import org.apache.metamodel.excel.ExcelDataContext; import org.apache.metamodel.fixedwidth.FixedWidthConfiguration; @@ -57,6 +58,8 @@ import com.mongodb.MongoClient; import com.mongodb.MongoCredential; import com.mongodb.ServerAddress; +import io.searchbox.client.JestClient; + /** * A factory for DataContext objects. This class substantially easens the task * of creating and initializing DataContext objects and/or their strategies for @@ -147,9 +150,6 @@ public class DataContextFactory { /** * Creates a DataContext based on a JSON file - * - * @param file - * @return */ public static DataContext createJsonDataContext(File file) { return new JsonDataContext(file); @@ -652,6 +652,18 @@ public class DataContextFactory { } /** + * Creates a new JSON-based ElasticSearch datacontext. + * @param client + * The Jest client + * @param indexName + * The ElasticSearch index name + * @return a DataContext object that matches the request + */ + public static UpdateableDataContext createElasticSearchDataContext(JestClient client, String indexName) { + return new ElasticSearchRestDataContext(client, indexName); + } + + /** * Creates a new ElasticSearch datacontext. * * @param client http://git-wip-us.apache.org/repos/asf/metamodel/blob/137caf0d/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 9f89604..ce1e4d6 100644 --- a/pom.xml +++ b/pom.xml @@ -29,7 +29,7 @@ under the License. <hadoop.version>2.6.0</hadoop.version> <jackson.version>2.4.6</jackson.version> <easymock.version>3.2</easymock.version> - <httpcomponents.version>4.3.1</httpcomponents.version> + <httpcomponents.version>4.4.1</httpcomponents.version> <checksum-maven-plugin.version>1.2</checksum-maven-plugin.version> <skipTests>false</skipTests> </properties>