Repository: metamodel Updated Branches: refs/heads/master edb10a137 -> 44f6c608b
METAMODEL-79: Added support for Insert, Create and Drop table Project: http://git-wip-us.apache.org/repos/asf/metamodel/repo Commit: http://git-wip-us.apache.org/repos/asf/metamodel/commit/6717c268 Tree: http://git-wip-us.apache.org/repos/asf/metamodel/tree/6717c268 Diff: http://git-wip-us.apache.org/repos/asf/metamodel/diff/6717c268 Branch: refs/heads/master Commit: 6717c268d8f2b360bfc3d50f47c602a3eb2ff073 Parents: 3a7483c Author: Kasper Sørensen <[email protected]> Authored: Sun Feb 15 22:13:45 2015 +0100 Committer: Kasper Sørensen <[email protected]> Committed: Sun Feb 15 22:13:45 2015 +0100 ---------------------------------------------------------------------- .../ElasticSearchCreateTableBuilder.java | 143 +++++++++++++++++++ .../elasticsearch/ElasticSearchDataContext.java | 29 +++- .../ElasticSearchDropTableBuilder.java | 66 +++++++++ .../ElasticSearchInsertIntoBuilder.java | 76 ++++++++++ .../ElasticSearchUpdateCallback.java | 103 +++++++++++++ .../ElasticSearchDataContextTest.java | 104 ++++++++++++-- 6 files changed, 506 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/metamodel/blob/6717c268/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchCreateTableBuilder.java ---------------------------------------------------------------------- diff --git a/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchCreateTableBuilder.java b/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchCreateTableBuilder.java new file mode 100644 index 0000000..c23fc88 --- /dev/null +++ b/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchCreateTableBuilder.java @@ -0,0 +1,143 @@ +/** + * 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.ArrayList; +import java.util.List; + +import org.apache.metamodel.MetaModelException; +import org.apache.metamodel.create.AbstractTableCreationBuilder; +import org.apache.metamodel.schema.Column; +import org.apache.metamodel.schema.ColumnType; +import org.apache.metamodel.schema.MutableColumn; +import org.apache.metamodel.schema.MutableSchema; +import org.apache.metamodel.schema.MutableTable; +import org.apache.metamodel.schema.Schema; +import org.apache.metamodel.schema.Table; +import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequestBuilder; +import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse; +import org.elasticsearch.client.IndicesAdminClient; +import org.elasticsearch.common.base.Strings; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +final class ElasticSearchCreateTableBuilder extends AbstractTableCreationBuilder<ElasticSearchUpdateCallback> { + + private static final Logger logger = LoggerFactory.getLogger(ElasticSearchCreateTableBuilder.class); + + public ElasticSearchCreateTableBuilder(ElasticSearchUpdateCallback updateCallback, Schema schema, String name) { + super(updateCallback, schema, name); + } + + @Override + public Table execute() throws MetaModelException { + final MutableTable table = getTable(); + + if (table.getColumnByName(ElasticSearchDataContext.FIELD_ID) == null) { + final MutableColumn idColumn = new MutableColumn(ElasticSearchDataContext.FIELD_ID, ColumnType.STRING) + .setTable(table).setPrimaryKey(true); + table.addColumn(0, idColumn); + } + + final ElasticSearchDataContext dataContext = getUpdateCallback().getDataContext(); + final IndicesAdminClient indicesAdmin = dataContext.getElasticSearchClient().admin().indices(); + final String indexName = dataContext.getIndexName(); + + final List<Object> sourceProperties = new ArrayList<Object>(); + for (Column column : table.getColumns()) { + // each column is defined as a property pair of the form: ("field1", + // "type=string,store=true") + final String columnName = column.getName(); + if (ElasticSearchDataContext.FIELD_ID.equals(columnName)) { + // do nothing - the ID is a client-side construct + continue; + } + sourceProperties.add(columnName); + + String type = getType(column); + if (type == null) { + sourceProperties.add("store=true"); + } else { + sourceProperties.add("type=" + type + ",store=true"); + } + } + + final PutMappingRequestBuilder requestBuilder = new PutMappingRequestBuilder(indicesAdmin) + .setIndices(indexName).setType(table.getName()); + requestBuilder.setSource(sourceProperties.toArray()); + final PutMappingResponse result = requestBuilder.execute().actionGet(); + + logger.debug("PutMapping response: acknowledged={}", result.isAcknowledged()); + + final MutableSchema schema = (MutableSchema) getSchema(); + schema.addTable(table); + + return table; + } + + /** + * Determines the best fitting type. For reference of ElasticSearch types, + * see + * + * <pre> + * http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-core-types.html + * </pre> + * + * + * @param column + * @return + */ + private String getType(Column column) { + String nativeType = column.getNativeType(); + if (!Strings.isNullOrEmpty(nativeType)) { + return nativeType; + } + + final ColumnType type = column.getType(); + if (type == null) { + return "object"; + } + + if (type.isLiteral()) { + return "string"; + } else if (type == ColumnType.FLOAT) { + return "float"; + } else if (type == ColumnType.DOUBLE || type == ColumnType.NUMERIC || type == ColumnType.NUMBER) { + return "double"; + } else if (type == ColumnType.SMALLINT) { + return "short"; + } else if (type == ColumnType.TINYINT) { + return "byte"; + } else if (type == ColumnType.INTEGER) { + return "integer"; + } else if (type == ColumnType.DATE || type == ColumnType.TIMESTAMP) { + return "date"; + } else if (type == ColumnType.BINARY || type == ColumnType.VARBINARY) { + return "binary"; + } else if (type == ColumnType.BOOLEAN || type == ColumnType.BIT) { + return "boolean"; + } else if (type == ColumnType.MAP) { + return "object"; + } + + logger.warn("Unhandled column type {} - the column '{}' will not have any type defined", type, column.getName()); + + return "object"; + } +} http://git-wip-us.apache.org/repos/asf/metamodel/blob/6717c268/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchDataContext.java ---------------------------------------------------------------------- diff --git a/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchDataContext.java b/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchDataContext.java index 06353f1..d465144 100644 --- a/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchDataContext.java +++ b/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchDataContext.java @@ -29,6 +29,8 @@ import java.util.Map; import org.apache.metamodel.DataContext; import org.apache.metamodel.MetaModelException; import org.apache.metamodel.QueryPostprocessDataContext; +import org.apache.metamodel.UpdateScript; +import org.apache.metamodel.UpdateableDataContext; import org.apache.metamodel.data.DataSet; import org.apache.metamodel.data.DataSetHeader; import org.apache.metamodel.data.Row; @@ -77,7 +79,7 @@ import org.slf4j.LoggerFactory; * This implementation supports either automatic discovery of a schema or manual * specification of a schema, through the {@link SimpleTableDef} class. */ -public class ElasticSearchDataContext extends QueryPostprocessDataContext implements DataContext { +public class ElasticSearchDataContext extends QueryPostprocessDataContext implements DataContext, UpdateableDataContext { private static final Logger logger = LoggerFactory.getLogger(ElasticSearchDataContext.class); @@ -300,4 +302,29 @@ public class ElasticSearchDataContext extends QueryPostprocessDataContext implem private boolean limitMaxRowsIsSet(int maxRows) { return (maxRows != -1); } + + @Override + public void executeUpdate(UpdateScript update) { + final ElasticSearchUpdateCallback callback = new ElasticSearchUpdateCallback(this); + update.run(callback); + callback.onExecuteUpdateFinished(); + } + + /** + * Gets the {@link Client} that this {@link DataContext} is wrapping. + * + * @return + */ + public Client getElasticSearchClient() { + return elasticSearchClient; + } + + /** + * Gets the name of the index that this {@link DataContext} is working on. + * + * @return + */ + public String getIndexName() { + return indexName; + } } http://git-wip-us.apache.org/repos/asf/metamodel/blob/6717c268/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 new file mode 100644 index 0000000..32becdf --- /dev/null +++ b/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchDropTableBuilder.java @@ -0,0 +1,66 @@ +/** + * 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.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(documentType); + final DeleteMappingResponse result = requestBuilder.execute().actionGet(); + logger.debug("Delete mapping response: acknowledged={}", result.isAcknowledged()); + + final MutableSchema schema = (MutableSchema) table.getSchema(); + schema.removeTable(table); + } +} http://git-wip-us.apache.org/repos/asf/metamodel/blob/6717c268/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchInsertIntoBuilder.java ---------------------------------------------------------------------- diff --git a/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchInsertIntoBuilder.java b/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchInsertIntoBuilder.java new file mode 100644 index 0000000..754624f --- /dev/null +++ b/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchInsertIntoBuilder.java @@ -0,0 +1,76 @@ +/** + * 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 ElasticSearchInsertIntoBuilder extends AbstractRowInsertionBuilder<ElasticSearchUpdateCallback> { + + private static final Logger logger = LoggerFactory.getLogger(ElasticSearchInsertIntoBuilder.class); + + public ElasticSearchInsertIntoBuilder(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<String, Object>(); + 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={}, created={}", result.getId(), result.isCreated()); + } + +} http://git-wip-us.apache.org/repos/asf/metamodel/blob/6717c268/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 new file mode 100644 index 0000000..e162cc6 --- /dev/null +++ b/elasticsearch/src/main/java/org/apache/metamodel/elasticsearch/ElasticSearchUpdateCallback.java @@ -0,0 +1,103 @@ +/** + * 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.apache.metamodel.update.RowUpdationBuilder; +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 ElasticSearchInsertIntoBuilder(this, table); + } + + @Override + public boolean isUpdateSupported() { + // TODO + return false; + } + + @Override + public RowUpdationBuilder update(Table table) throws IllegalArgumentException, IllegalStateException, + UnsupportedOperationException { + // TODO + throw new UnsupportedOperationException(); + } + + @Override + public boolean isDeleteSupported() { + // TODO + return false; + } + + @Override + public RowDeletionBuilder deleteFrom(Table table) throws IllegalArgumentException, IllegalStateException, + UnsupportedOperationException { + // TODO + throw new UnsupportedOperationException(); + } + + 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/6717c268/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 index 449490b..6544686 100644 --- a/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchDataContextTest.java +++ b/elasticsearch/src/test/java/org/apache/metamodel/elasticsearch/ElasticSearchDataContextTest.java @@ -19,7 +19,10 @@ package org.apache.metamodel.elasticsearch; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import java.util.Arrays; import java.util.Date; @@ -29,17 +32,22 @@ import java.util.Map; import javax.swing.table.TableModel; -import org.apache.metamodel.DataContext; +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.FilteredDataSet; import org.apache.metamodel.data.InMemoryDataSet; +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.elasticsearch.action.bulk.BulkRequestBuilder; import org.elasticsearch.client.Client; @@ -58,7 +66,7 @@ public class ElasticSearchDataContextTest { private static final String peopleIndexType = "peopletype"; private static EmbeddedElasticsearchServer embeddedElasticsearchServer; private static Client client; - private static DataContext dataContext; + private static UpdateableDataContext dataContext; @BeforeClass public static void beforeTests() throws Exception { @@ -66,6 +74,19 @@ public class ElasticSearchDataContextTest { client = embeddedElasticsearchServer.getClient(); indexOneTweeterDocumentPerIndex(indexType1, 1); indexOneTweeterDocumentPerIndex(indexType2, 1); + insertPeopleDocuments(); + indexOneTweeterDocumentPerIndex(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() { indexOnePeopleDocument("female", 20, 5); indexOnePeopleDocument("female", 17, 8); indexOnePeopleDocument("female", 18, 9); @@ -75,14 +96,6 @@ public class ElasticSearchDataContextTest { indexOnePeopleDocument("male", 17, 2); indexOnePeopleDocument("male", 18, 3); indexOnePeopleDocument("male", 18, 4); - indexOneTweeterDocumentPerIndex(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!"); } @AfterClass @@ -168,7 +181,7 @@ public class ElasticSearchDataContextTest { @Test public void testNumberIsHandledAsNumber() throws Exception { - Table table = dataContext.getDefaultSchema().getTableByName("peopletype"); + Table table = dataContext.getDefaultSchema().getTableByName(peopleIndexType); Column column = table.getColumnByName("age"); ColumnType type = column.getType(); assertEquals(ColumnType.BIGINT, type); @@ -181,6 +194,71 @@ public class ElasticSearchDataContextTest { } @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(); + } + }); + + final DataSet ds = dataContext.query().from(table).selectAll().orderBy("bar").execute(); + try { + 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()); + } finally { + ds.close(); + } + + 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 { DataSet ds = dataContext.query().from(bulkIndexType).select("user").and("message").where("user") .isEquals("user4").execute(); @@ -283,8 +361,6 @@ public class ElasticSearchDataContextTest { dataContext.query().from("nonExistingTable").select("user").and("message").execute(); } catch (IllegalArgumentException IAex) { thrown = true; - } finally { - // ds.close(); } assertTrue(thrown); }
