HBase Improvements - did some refactoring, added comments, added unittest
Project: http://git-wip-us.apache.org/repos/asf/metamodel/repo Commit: http://git-wip-us.apache.org/repos/asf/metamodel/commit/9ab30040 Tree: http://git-wip-us.apache.org/repos/asf/metamodel/tree/9ab30040 Diff: http://git-wip-us.apache.org/repos/asf/metamodel/diff/9ab30040 Branch: refs/heads/master Commit: 9ab30040e3311f5c9947615470ad1d366de92c0f Parents: 8608486 Author: Gerard Dellemann <g.dellem...@quadient.com> Authored: Tue May 29 15:28:27 2018 +0200 Committer: Gerard Dellemann <g.dellem...@quadient.com> Committed: Tue May 29 15:28:27 2018 +0200 ---------------------------------------------------------------------- .../org/apache/metamodel/hbase/HBaseClient.java | 92 ++-- .../org/apache/metamodel/hbase/HBaseColumn.java | 165 +++++-- .../hbase/HBaseCreateTableBuilder.java | 10 +- .../hbase/HBaseRowDeletionBuilder.java | 21 +- .../hbase/HBaseRowInsertionBuilder.java | 52 ++- .../org/apache/metamodel/hbase/HBaseTable.java | 119 +++-- .../metamodel/hbase/HBaseTableDropBuilder.java | 17 +- .../metamodel/hbase/HBaseUpdateCallback.java | 26 +- .../apache/metamodel/hbase/CreateTableTest.java | 193 ++++++++ .../apache/metamodel/hbase/DeleteRowTest.java | 169 +++++++ .../apache/metamodel/hbase/DropTableTest.java | 87 ++++ .../metamodel/hbase/HBaseDataContextTest.java | 39 +- .../apache/metamodel/hbase/HBaseTestCase.java | 57 +-- .../hbase/HBaseUpdateCallbackTest.java | 252 ++++++----- .../apache/metamodel/hbase/InsertRowTest.java | 435 +++++++++++++++++++ 15 files changed, 1344 insertions(+), 390 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/metamodel/blob/9ab30040/hbase/src/main/java/org/apache/metamodel/hbase/HBaseClient.java ---------------------------------------------------------------------- diff --git a/hbase/src/main/java/org/apache/metamodel/hbase/HBaseClient.java b/hbase/src/main/java/org/apache/metamodel/hbase/HBaseClient.java index ed7b17c..979322e 100644 --- a/hbase/src/main/java/org/apache/metamodel/hbase/HBaseClient.java +++ b/hbase/src/main/java/org/apache/metamodel/hbase/HBaseClient.java @@ -49,16 +49,22 @@ public final class HBaseClient { } /** - * Write a single row of values to a HBase table - * @param hBaseTable + * Insert a single row of values to a HBase table. + * @param tableName * @param columns * @param values - * @throws IOException + * @throws IllegalArgumentException when any parameter is null or the indexOfIdColumn is impossible + * @throws MetaModelException when no ID-column is found. + * @throws MetaModelException when a {@link IOException} is catched */ - public void writeRow(HBaseTable hBaseTable, HBaseColumn[] columns, Object[] values) throws IOException { - try (final Table table = _connection.getTable(TableName.valueOf(hBaseTable.getName()))) { - int indexOfIdColumn = getIndexOfIdColumn(columns); - + // TODO: Use the ColumnTypes to determine the inserts. Now the toString() method is called on the object. + public void insertRow(String tableName, HBaseColumn[] columns, Object[] values, int indexOfIdColumn) { + if (tableName == null || columns == null || values == null || indexOfIdColumn >= values.length + || values[indexOfIdColumn] == null) { + throw new IllegalArgumentException( + "Can't insert a row without having (correct) tableName, columns, values or indexOfIdColumn"); + } + try (final Table table = _connection.getTable(TableName.valueOf(tableName))) { // Create a put with the values of indexOfIdColumn as rowkey final Put put = new Put(Bytes.toBytes(values[indexOfIdColumn].toString())); @@ -71,56 +77,42 @@ public final class HBaseClient { } // Add the put to the table table.put(put); + } catch (IOException e) { + throw new MetaModelException(e); } } /** - * Gets the index of the ID-column - * Throws an {@link MetaModelException} when no ID-column is found. - * @param columns - * @return - */ - private int getIndexOfIdColumn(HBaseColumn[] columns) { - int indexOfIdColumn = 0; - boolean idColumnFound = false; - while (!idColumnFound && indexOfIdColumn < columns.length) { - if (columns[indexOfIdColumn].getColumnFamily().equals(HBaseDataContext.FIELD_ID)) { - idColumnFound = true; - } else { - indexOfIdColumn++; - } - } - if (!idColumnFound) { - throw new MetaModelException("The ID Column family was not found"); - } - return indexOfIdColumn; - } - - /** * Delete 1 row based on the key - * @param hBaseTable - * @param key - * @throws IOException + * @param tableName + * @param rowKey + * @throws IllegalArgumentException when any parameter is null + * @throws MetaModelException when a {@link IOException} is catched */ - public void deleteRow(HBaseTable hBaseTable, Object key) throws IOException { - try (final Table table = _connection.getTable(TableName.valueOf(hBaseTable.getName()));) { - if (rowExists(table, key) == true) { - table.delete(new Delete(Bytes.toBytes(key.toString()))); + public void deleteRow(String tableName, Object rowKey) { + if (tableName == null || rowKey == null) { + throw new IllegalArgumentException("Can't delete a row without having tableName or rowKey"); + } + try (final Table table = _connection.getTable(TableName.valueOf(tableName));) { + if (rowExists(table, rowKey) == true) { + table.delete(new Delete(Bytes.toBytes(rowKey.toString()))); } else { - logger.warn("Rowkey with value " + key.toString() + " doesn't exist in the table"); + logger.warn("Rowkey with value " + rowKey.toString() + " doesn't exist in the table"); } + } catch (IOException e) { + throw new MetaModelException(e); } } /** * Checks in the HBase datastore if a row exists based on the key * @param table - * @param key + * @param rowKey * @return boolean * @throws IOException */ - private boolean rowExists(Table table, Object key) throws IOException { - final Get get = new Get(Bytes.toBytes(key.toString())); + private boolean rowExists(Table table, Object rowKey) throws IOException { + final Get get = new Get(Bytes.toBytes(rowKey.toString())); return !table.get(get).isEmpty(); } @@ -128,9 +120,13 @@ public final class HBaseClient { * Creates a HBase table based on a tableName and it's columnFamilies * @param tableName * @param columnFamilies - * @throws IOException + * @throws IllegalArgumentException when any parameter is null + * @throws MetaModelException when a {@link IOException} is catched */ - public void createTable(String tableName, Set<String> columnFamilies) throws IOException { + public void createTable(String tableName, Set<String> columnFamilies) { + if (tableName == null || columnFamilies == null || columnFamilies.isEmpty()) { + throw new IllegalArgumentException("Can't create a table without having the tableName or columnFamilies"); + } try (final Admin admin = _connection.getAdmin()) { final TableName hBasetableName = TableName.valueOf(tableName); final HTableDescriptor tableDescriptor = new HTableDescriptor(hBasetableName); @@ -142,19 +138,27 @@ public final class HBaseClient { } } admin.createTable(tableDescriptor); + } catch (IOException e) { + throw new MetaModelException(e); } } /** * Disable and drop a table from a HBase datastore * @param tableName - * @throws IOException + * @throws IllegalArgumentException when tableName is null + * @throws MetaModelException when a {@link IOException} is catched */ - public void dropTable(String tableName) throws IOException { + public void dropTable(String tableName) { + if (tableName == null) { + throw new IllegalArgumentException("Can't drop a table without having the tableName"); + } try (final Admin admin = _connection.getAdmin()) { final TableName hBasetableName = TableName.valueOf(tableName); admin.disableTable(hBasetableName); // A table must be disabled first, before it can be deleted admin.deleteTable(hBasetableName); + } catch (IOException e) { + throw new MetaModelException(e); } } } http://git-wip-us.apache.org/repos/asf/metamodel/blob/9ab30040/hbase/src/main/java/org/apache/metamodel/hbase/HBaseColumn.java ---------------------------------------------------------------------- diff --git a/hbase/src/main/java/org/apache/metamodel/hbase/HBaseColumn.java b/hbase/src/main/java/org/apache/metamodel/hbase/HBaseColumn.java index bd57c1c..d6554c4 100644 --- a/hbase/src/main/java/org/apache/metamodel/hbase/HBaseColumn.java +++ b/hbase/src/main/java/org/apache/metamodel/hbase/HBaseColumn.java @@ -18,22 +18,27 @@ */ package org.apache.metamodel.hbase; +import java.util.Arrays; +import java.util.Iterator; import java.util.LinkedHashSet; +import java.util.List; import java.util.Set; +import java.util.stream.Collectors; -import org.apache.metamodel.schema.AbstractColumn; +import org.apache.metamodel.schema.Column; import org.apache.metamodel.schema.ColumnType; import org.apache.metamodel.schema.ColumnTypeImpl; +import org.apache.metamodel.schema.MutableColumn; import org.apache.metamodel.schema.SuperColumnType; import org.apache.metamodel.schema.Table; -public final class HBaseColumn extends AbstractColumn { +public final class HBaseColumn extends MutableColumn { + public final static ColumnType DEFAULT_COLUMN_TYPE_FOR_ID_COLUMN = new ColumnTypeImpl("BYTE[]", + SuperColumnType.LITERAL_TYPE); + public final static ColumnType DEFAULT_COLUMN_TYPE_FOR_COLUMN_FAMILIES = ColumnType.LIST; + private final String columnFamily; private final String qualifier; - private final Table table; - private final boolean primaryKey; - private final ColumnType columnType; - private final int columnNumber; public HBaseColumn(final String columnFamily, final Table table) { this(columnFamily, null, table, -1); @@ -48,23 +53,32 @@ public final class HBaseColumn extends AbstractColumn { } public HBaseColumn(final String columnFamily, final String qualifier, final Table table, final int columnNumber) { + this(columnFamily, qualifier, table, columnNumber, null); + } + + public HBaseColumn(final String columnFamily, final String qualifier, final Table table, final int columnNumber, + final ColumnType columnType) { + super(columnFamily, table); if (columnFamily == null) { throw new IllegalArgumentException("Column family isn't allowed to be null."); - } else if (table == null) { - throw new IllegalArgumentException("Table isn't allowed to be null."); + } else if (table == null || !(table instanceof HBaseTable)) { + throw new IllegalArgumentException("Table is null or isn't a HBaseTable."); } this.columnFamily = columnFamily; this.qualifier = qualifier; - this.table = table; - this.columnNumber = columnNumber; + setColumnNumber(columnNumber); + setPrimaryKey(HBaseDataContext.FIELD_ID.equals(columnFamily)); - primaryKey = HBaseDataContext.FIELD_ID.equals(columnFamily); - - if (primaryKey || qualifier != null) { - columnType = new ColumnTypeImpl("BYTE[]", SuperColumnType.LITERAL_TYPE); + // Set the columnType + if (columnType != null) { + setType(columnType); } else { - columnType = ColumnType.LIST; + if (isPrimaryKey() || qualifier != null) { + setType(DEFAULT_COLUMN_TYPE_FOR_ID_COLUMN); + } else { + setType(DEFAULT_COLUMN_TYPE_FOR_COLUMN_FAMILIES); + } } } @@ -85,23 +99,8 @@ public final class HBaseColumn extends AbstractColumn { } @Override - public int getColumnNumber() { - return columnNumber; - } - - @Override - public ColumnType getType() { - return columnType; - } - - @Override - public Table getTable() { - return table; - } - - @Override public Boolean isNullable() { - return !primaryKey; + return !isPrimaryKey(); } @Override @@ -126,25 +125,107 @@ public final class HBaseColumn extends AbstractColumn { } @Override - public boolean isPrimaryKey() { - return primaryKey; - } - - @Override public String getQuote() { return null; } /** - * Creates a set of columnFamilies out of an array of hbaseColumns - * @param hbaseColumns + * Creates a set of columnFamilies out of a list of hbaseColumns + * @param columns * @return {@link LinkedHashSet} */ - public static Set<String> getColumnFamilies(HBaseColumn[] hbaseColumns) { - final LinkedHashSet<String> columnFamilies = new LinkedHashSet<String>(); - for (int i = 0; i < hbaseColumns.length; i++) { - columnFamilies.add(hbaseColumns[i].getColumnFamily()); + public static Set<String> getColumnFamilies(List<HBaseColumn> columns) { + final LinkedHashSet<String> columnFamilies = new LinkedHashSet<>(); + for (HBaseColumn column : columns) { + columnFamilies.add(column.getColumnFamily()); } return columnFamilies; } + + /** + * Returns the index of the ID-column (see {@link HBaseDataContext#FIELD_ID}) in an array of HBaseColumns. + * When no ID-column is found, then null is returned. + * @param columns + * @return {@link Integer} + */ + public static Integer findIndexOfIdColumn(List<HBaseColumn> columns) { + int i = 0; + Integer indexOfIDColumn = null; + Iterator<HBaseColumn> iterator = columns.iterator(); + while (indexOfIDColumn == null && iterator.hasNext()) { + indexOfIDColumn = findIndexOfIdColumn(iterator.next().getColumnFamily(), i); + if (indexOfIDColumn == null) { + i++; + } + } + return indexOfIDColumn; + } + + /** + * Returns the index of the ID-column (see {@link HBaseDataContext#FIELD_ID}) in an array of columnNames. + * When no ID-column is found, then null is returned. + * @param columnNames + * @return {@link Integer} + */ + public static Integer findIndexOfIdColumn(String[] columnNames) { + int i = 0; + Integer indexOfIDColumn = null; + while (indexOfIDColumn == null && i < columnNames.length) { + indexOfIDColumn = findIndexOfIdColumn(columnNames[i], i); + if (indexOfIDColumn == null) { + i++; + } + } + return indexOfIDColumn; + } + + /** + * Returns the index of the ID-column (see {@link HBaseDataContext#FIELD_ID}) + * When no ID-column is found, then null is returned. + * @param columnNames + * @return {@link Integer} + */ + private static Integer findIndexOfIdColumn(String columnName, int index) { + Integer indexOfIDColumn = null; + if (columnName.equals(HBaseDataContext.FIELD_ID)) { + indexOfIDColumn = new Integer(index); + } + return indexOfIDColumn; + } + + /** + * Converts a list of {@link Column}'s to a list of {@link HBaseColumn}'s + * @param columns + * @return {@link List}<{@link HBaseColumn}> + */ + public static List<HBaseColumn> convertToHBaseColumnsList(List<Column> columns) { + return columns.stream().map(column -> (HBaseColumn) column).collect(Collectors.toList()); + } + + /** + * Converts a list of {@link HBaseColumn}'s to a list of {@link Column}'s + * @param columns + * @return {@link List}<{@link Column}> + */ + public static List<Column> convertToColumnsList(List<HBaseColumn> columns) { + return columns.stream().map(column -> (Column) column).collect(Collectors.toList()); + } + + /** + * Converts a list of {@link HBaseColumn}'s to an array of {@link HBaseColumn}'s + * @param columns + * @return Array of {@link HBaseColumn} + */ + public static HBaseColumn[] convertToHBaseColumnsArray(List<HBaseColumn> columns) { + return columns.stream().map(column -> column).toArray(size -> new HBaseColumn[size]); + } + + /** + * Converts a array of {@link Column}'s to an array of {@link HBaseColumn}'s + * @param columns + * @return Array of {@link HBaseColumn} + */ + public static HBaseColumn[] convertToHBaseColumnsArray(Column[] columns) { + return Arrays.stream(columns).map(column -> (HBaseColumn) column).toArray(size -> new HBaseColumn[size]); + } } http://git-wip-us.apache.org/repos/asf/metamodel/blob/9ab30040/hbase/src/main/java/org/apache/metamodel/hbase/HBaseCreateTableBuilder.java ---------------------------------------------------------------------- diff --git a/hbase/src/main/java/org/apache/metamodel/hbase/HBaseCreateTableBuilder.java b/hbase/src/main/java/org/apache/metamodel/hbase/HBaseCreateTableBuilder.java index ffdc20a..138e32b 100644 --- a/hbase/src/main/java/org/apache/metamodel/hbase/HBaseCreateTableBuilder.java +++ b/hbase/src/main/java/org/apache/metamodel/hbase/HBaseCreateTableBuilder.java @@ -18,7 +18,6 @@ */ package org.apache.metamodel.hbase; -import java.io.IOException; import java.util.Iterator; import java.util.Set; @@ -64,12 +63,7 @@ public class HBaseCreateTableBuilder extends AbstractTableCreationBuilder<HBaseU final Table table = getTable(); // Add the table to the datastore - try { - final HBaseClient hBaseClient = getUpdateCallback().getHBaseClient(); - hBaseClient.createTable(table.getName(), _columnFamilies); - } catch (IOException e) { - throw new MetaModelException(e); - } + getUpdateCallback().getHBaseClient().createTable(table.getName(), _columnFamilies); // Update the schema addNewTableToSchema(table); @@ -108,7 +102,7 @@ public class HBaseCreateTableBuilder extends AbstractTableCreationBuilder<HBaseU /** * Add the new {@link Table} to the {@link MutableSchema} * @param table - * @param updateCallback + * @param data.updateCallback * @return {@link MutableSchema} */ private void addNewTableToSchema(final Table table) { http://git-wip-us.apache.org/repos/asf/metamodel/blob/9ab30040/hbase/src/main/java/org/apache/metamodel/hbase/HBaseRowDeletionBuilder.java ---------------------------------------------------------------------- diff --git a/hbase/src/main/java/org/apache/metamodel/hbase/HBaseRowDeletionBuilder.java b/hbase/src/main/java/org/apache/metamodel/hbase/HBaseRowDeletionBuilder.java index 3ba4ef8..99d2571 100644 --- a/hbase/src/main/java/org/apache/metamodel/hbase/HBaseRowDeletionBuilder.java +++ b/hbase/src/main/java/org/apache/metamodel/hbase/HBaseRowDeletionBuilder.java @@ -18,38 +18,41 @@ */ package org.apache.metamodel.hbase; -import java.io.IOException; - import org.apache.metamodel.MetaModelException; import org.apache.metamodel.delete.AbstractRowDeletionBuilder; import org.apache.metamodel.schema.Table; /** - * A builder-class to delete rows in a HBase datastore + * A builder-class to delete rows based on their keys in a HBase datastore */ public class HBaseRowDeletionBuilder extends AbstractRowDeletionBuilder { private HBaseClient _hBaseClient; private Object _key; + /** + * Creates a {@link HBaseRowDeletionBuilder} + * @param hBaseWriter + * @param table + * @throws IllegalArgumentException when the hBaseWriter is null + */ public HBaseRowDeletionBuilder(final HBaseClient hBaseWriter, final Table table) { super(table); if (hBaseWriter == null) { - throw new IllegalArgumentException("UpdateCallback cannot be null"); + throw new IllegalArgumentException("hBaseClient cannot be null"); } this._hBaseClient = hBaseWriter; } + /** + * @throws MetaModelException when value is null + */ @Override public synchronized void execute() { if (_key == null) { throw new MetaModelException("Key cannot be null"); } - try { - _hBaseClient.deleteRow((HBaseTable) getTable(), _key); - } catch (IOException e) { - throw new MetaModelException(e); - } + _hBaseClient.deleteRow(getTable().getName(), _key); } public void setKey(Object _key) { http://git-wip-us.apache.org/repos/asf/metamodel/blob/9ab30040/hbase/src/main/java/org/apache/metamodel/hbase/HBaseRowInsertionBuilder.java ---------------------------------------------------------------------- diff --git a/hbase/src/main/java/org/apache/metamodel/hbase/HBaseRowInsertionBuilder.java b/hbase/src/main/java/org/apache/metamodel/hbase/HBaseRowInsertionBuilder.java index 7272f76..9e36a4d 100644 --- a/hbase/src/main/java/org/apache/metamodel/hbase/HBaseRowInsertionBuilder.java +++ b/hbase/src/main/java/org/apache/metamodel/hbase/HBaseRowInsertionBuilder.java @@ -18,8 +18,6 @@ */ package org.apache.metamodel.hbase; -import java.io.IOException; -import java.util.Arrays; import java.util.List; import org.apache.metamodel.MetaModelException; @@ -27,20 +25,44 @@ import org.apache.metamodel.insert.AbstractRowInsertionBuilder; import org.apache.metamodel.schema.Column; /** - * A builder-class to insert rows in a HBase datastore + * A builder-class to insert rows in a HBase datastore. */ +// TODO: Possible future improvement: Make it possible to change the columns for each execute. +// Now each row will get exactly the same columns. public class HBaseRowInsertionBuilder extends AbstractRowInsertionBuilder<HBaseUpdateCallback> { + final Integer _indexOfIdColumn; + + /** + * Creates a {@link HBaseRowInsertionBuilder}. The table and the column's columnFamilies are checked to exist in the schema. + * @param updateCallback + * @param table + * @param columns + * @throws IllegalArgumentException the columns list can't be null or empty + * @throws MetaModelException when no ID-column is found. + */ public HBaseRowInsertionBuilder(final HBaseUpdateCallback updateCallback, final HBaseTable table, final List<Column> columns) { super(updateCallback, table, columns); + if (columns.isEmpty()) { // TODO: Columns null will already result in a NullPointer at the super. Should the + // super get a extra check? + throw new IllegalArgumentException("The hbaseColumns list is null or empty"); + } + + this._indexOfIdColumn = HBaseColumn.findIndexOfIdColumn(HBaseColumn.convertToHBaseColumnsList(columns)); + if (_indexOfIdColumn == null) { + throw new MetaModelException("The ID-Column was not found"); + } + checkTable(updateCallback, table); + table.checkForNotMatchingColumnFamilies(HBaseColumn.getColumnFamilies(HBaseColumn.convertToHBaseColumnsList( + columns))); } /** - * Check if the table exits and it's columnFamilies exist - * If the table doesn't exist, then a {@link MetaModelException} is thrown + * Check if the table and it's columnFamilies exist in the schema * @param updateCallback * @param tableGettingInserts + * @throws MetaModelException If the table or the columnFamilies don't exist */ private void checkTable(final HBaseUpdateCallback updateCallback, final HBaseTable tableGettingInserts) { final HBaseTable tableInSchema = (HBaseTable) updateCallback.getDataContext().getDefaultSchema().getTableByName( @@ -49,28 +71,18 @@ public class HBaseRowInsertionBuilder extends AbstractRowInsertionBuilder<HBaseU throw new MetaModelException("Trying to insert data into table: " + tableGettingInserts.getName() + ", which doesn't exist yet"); } - tableInSchema.checkForNotMatchingColumns(tableGettingInserts.getColumnNames()); + tableInSchema.checkForNotMatchingColumnFamilies(HBaseColumn.getColumnFamilies(tableGettingInserts + .getHBaseColumnsInternal())); } @Override public synchronized void execute() { - if (getColumns() == null || getColumns().length == 0) { - throw new MetaModelException("The hbaseColumns-array is null or empty"); - } - if (getValues() == null || getValues().length == 0) { - throw new MetaModelException("The values-array is null or empty"); - } - try { - final HBaseClient hBaseClient = getUpdateCallback().getHBaseClient(); - hBaseClient.writeRow((HBaseTable) getTable(), getColumns(), getValues()); - } catch (IOException e) { - throw new MetaModelException(e); - } + getUpdateCallback().getHBaseClient().insertRow(getTable().getName(), getColumns(), getValues(), _indexOfIdColumn + .intValue()); } @Override public HBaseColumn[] getColumns() { - return Arrays.stream(super.getColumns()).map(column -> (HBaseColumn) column).toArray( - size -> new HBaseColumn[size]); + return HBaseColumn.convertToHBaseColumnsArray(super.getColumns()); } } http://git-wip-us.apache.org/repos/asf/metamodel/blob/9ab30040/hbase/src/main/java/org/apache/metamodel/hbase/HBaseTable.java ---------------------------------------------------------------------- diff --git a/hbase/src/main/java/org/apache/metamodel/hbase/HBaseTable.java b/hbase/src/main/java/org/apache/metamodel/hbase/HBaseTable.java index fee2f5d..9f482ac 100644 --- a/hbase/src/main/java/org/apache/metamodel/hbase/HBaseTable.java +++ b/hbase/src/main/java/org/apache/metamodel/hbase/HBaseTable.java @@ -18,13 +18,14 @@ */ package org.apache.metamodel.hbase; +import java.util.Iterator; import java.util.List; +import java.util.Set; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.metamodel.MetaModelException; 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.TableType; @@ -44,8 +45,8 @@ final class HBaseTable extends MutableTable { private final transient ColumnType _defaultRowKeyColumnType; /** - * Creates an HBaseTable. If the tableDef variable doesn't include the ID column (see {@link HBaseDataContext#FIELD_ID}). - * Then it's first inserted. + * Creates an HBaseTable. If the tableDef variable doesn't include the ID-column (see {@link HBaseDataContext#FIELD_ID}). + * Then it's first added. * @param dataContext * @param tableDef Table definition. The tableName, columnNames and columnTypes variables are used. * @param schema {@link MutableSchema} where the table belongs to. @@ -57,7 +58,15 @@ final class HBaseTable extends MutableTable { super(tableDef.getName(), TableType.TABLE, schema); _dataContext = dataContext; _defaultRowKeyColumnType = defaultRowKeyColumnType; + addColumns(tableDef, defaultRowKeyColumnType); + } + /** + * Add multiple columns to this table + * @param tableDef + * @param defaultRowKeyColumnType + */ + private void addColumns(SimpleTableDef tableDef, ColumnType defaultRowKeyColumnType) { // Add the columns final String[] columnNames = tableDef.getColumnNames(); if (columnNames == null || columnNames.length == 0) { @@ -66,79 +75,55 @@ final class HBaseTable extends MutableTable { final ColumnType[] columnTypes = tableDef.getColumnTypes(); // Find the ID-Column - boolean idColumnFound = false; - int indexOfIDColumn = 0; - while (!idColumnFound && indexOfIDColumn < columnNames.length) { - if (columnNames[indexOfIDColumn].equals(HBaseDataContext.FIELD_ID)) { - idColumnFound = true; - } else { - indexOfIDColumn++; - } - } + Integer indexOfIDColumn = HBaseColumn.findIndexOfIdColumn(columnNames); + boolean idColumnFound = indexOfIDColumn != null; - int columnNumber = indexOfIDColumn + 1; // ColumnNumbers start from 1 - - // Add the ID-Column, even if the column wasn't included in columnNames - ColumnType columnType; + // ColumnNumbers start from 1 if (idColumnFound) { - columnType = columnTypes[indexOfIDColumn]; + addColumn(HBaseDataContext.FIELD_ID, columnTypes[indexOfIDColumn.intValue()], indexOfIDColumn.intValue() + + 1); } else { - columnType = defaultRowKeyColumnType; + addColumn(HBaseDataContext.FIELD_ID, defaultRowKeyColumnType, 1); } - final MutableColumn idColumn = new MutableColumn(HBaseDataContext.FIELD_ID, columnType) - .setPrimaryKey(true) - .setColumnNumber(columnNumber) - .setTable(this); - addColumn(idColumn); // Add the other columns for (int i = 0; i < columnNames.length; i++) { - final String columnName = columnNames[i]; - if (idColumnFound) { - columnNumber = i + 1; // ColumnNumbers start from 1 - } else { - columnNumber = i + 2; // ColumnNumbers start from 1 + the ID-column has just been created - } - if (!HBaseDataContext.FIELD_ID.equals(columnName)) { - final ColumnType type = columnTypes[i]; - final MutableColumn column = new MutableColumn(columnName, type); - column.setTable(this); - column.setColumnNumber(columnNumber); - addColumn(column); - columnNumber++; + if (!HBaseDataContext.FIELD_ID.equals(columnNames[i])) { + if (idColumnFound) { + addColumn(columnNames[i], columnTypes[i], i + 1); + } else { + addColumn(columnNames[i], columnTypes[i], i + 2); + } } } } } + /** + * Add a column to this table + * @param columnName + * @param columnType + * @param columnNumber + */ + private void addColumn(final String columnName, final ColumnType columnType, final int columnNumber) { + addColumn(new HBaseColumn(columnName, null, this, columnNumber, columnType)); + } + @Override protected synchronized List<Column> getColumnsInternal() { final List<Column> columnsInternal = super.getColumnsInternal(); if (columnsInternal.isEmpty() && _dataContext != null) { - try { - final org.apache.hadoop.hbase.client.Table table = _dataContext.getHTable(getName()); - int columnNumber = 1; - - final MutableColumn idColumn = new MutableColumn(HBaseDataContext.FIELD_ID, _defaultRowKeyColumnType) - .setPrimaryKey(true) - .setColumnNumber(columnNumber) - .setTable(this); - addColumn(idColumn); - columnNumber++; + try (final org.apache.hadoop.hbase.client.Table table = _dataContext.getHTable(getName())) { + // Add the ID-Column (with columnNumber = 1) + addColumn(HBaseDataContext.FIELD_ID, _defaultRowKeyColumnType, 1); // What about timestamp? + // Add the other column (with columnNumbers starting from 2) final HColumnDescriptor[] columnFamilies = table.getTableDescriptor().getColumnFamilies(); for (int i = 0; i < columnFamilies.length; i++) { - final HColumnDescriptor columnDescriptor = columnFamilies[i]; - final String columnFamilyName = columnDescriptor.getNameAsString(); - // HBase column families are always unstructured maps. - final ColumnType type = ColumnType.MAP; - final MutableColumn column = new MutableColumn(columnFamilyName, type); - column.setTable(this); - column.setColumnNumber(columnNumber); - columnNumber++; - addColumn(column); + addColumn(columnFamilies[i].getNameAsString(), HBaseColumn.DEFAULT_COLUMN_TYPE_FOR_COLUMN_FAMILIES, + i + 2); } } catch (Exception e) { throw new MetaModelException("Could not resolve table ", e); @@ -149,19 +134,18 @@ final class HBaseTable extends MutableTable { /** * Check if a list of columnNames all exist in this table - * If a column doesn't exist, then a {@link MetaModelException} is thrown * @param columnNamesOfCheckedTable + * @throws MetaModelException If a column doesn't exist */ - public void checkForNotMatchingColumns(final List<String> columnNamesOfCheckedTable) { - final List<String> columnsNamesOfExistingTable = getColumnNames(); + public void checkForNotMatchingColumnFamilies(final Set<String> columnNamesOfCheckedTable) { + Set<String> columnFamilyNamesOfExistingTable = HBaseColumn.getColumnFamilies(getHBaseColumnsInternal()); + for (String columnNameOfCheckedTable : columnNamesOfCheckedTable) { boolean matchingColumnFound = false; - int i = 0; - while (!matchingColumnFound && i < columnsNamesOfExistingTable.size()) { - if (columnNameOfCheckedTable.equals(columnsNamesOfExistingTable.get(i))) { + Iterator<String> iterator = columnFamilyNamesOfExistingTable.iterator(); + while (!matchingColumnFound && iterator.hasNext()) { + if (columnNameOfCheckedTable.equals(iterator.next())) { matchingColumnFound = true; - } else { - i++; } } if (!matchingColumnFound) { @@ -170,4 +154,13 @@ final class HBaseTable extends MutableTable { } } } + + /** + * Returns a list of {@link HBaseColumn}'s from {@link HBaseTable#getColumnsInternal()}, + * which returns a list of {@link Column}'s + * @return {@link List}<{@link HBaseColumn}> + */ + public List<HBaseColumn> getHBaseColumnsInternal() { + return HBaseColumn.convertToHBaseColumnsList(getColumnsInternal()); + } } http://git-wip-us.apache.org/repos/asf/metamodel/blob/9ab30040/hbase/src/main/java/org/apache/metamodel/hbase/HBaseTableDropBuilder.java ---------------------------------------------------------------------- diff --git a/hbase/src/main/java/org/apache/metamodel/hbase/HBaseTableDropBuilder.java b/hbase/src/main/java/org/apache/metamodel/hbase/HBaseTableDropBuilder.java index 374e325..a08ab83 100644 --- a/hbase/src/main/java/org/apache/metamodel/hbase/HBaseTableDropBuilder.java +++ b/hbase/src/main/java/org/apache/metamodel/hbase/HBaseTableDropBuilder.java @@ -18,8 +18,6 @@ */ package org.apache.metamodel.hbase; -import java.io.IOException; - import org.apache.metamodel.MetaModelException; import org.apache.metamodel.drop.AbstractTableDropBuilder; import org.apache.metamodel.schema.MutableSchema; @@ -41,16 +39,11 @@ public class HBaseTableDropBuilder extends AbstractTableDropBuilder { @Override public void execute() { - try { - // Remove from the datastore - final HBaseClient hBaseClient = _updateCallback.getHBaseClient(); - final Table table = getTable(); - hBaseClient.dropTable(table.getName()); + // Remove from the datastore + final Table table = getTable(); + _updateCallback.getHBaseClient().dropTable(table.getName()); - // Remove from schema - ((MutableSchema) table.getSchema()).removeTable(table); - } catch (IOException e) { - throw new MetaModelException(e); - } + // Remove from schema + ((MutableSchema) table.getSchema()).removeTable(table); } } http://git-wip-us.apache.org/repos/asf/metamodel/blob/9ab30040/hbase/src/main/java/org/apache/metamodel/hbase/HBaseUpdateCallback.java ---------------------------------------------------------------------- diff --git a/hbase/src/main/java/org/apache/metamodel/hbase/HBaseUpdateCallback.java b/hbase/src/main/java/org/apache/metamodel/hbase/HBaseUpdateCallback.java index 6d35508..b154275 100644 --- a/hbase/src/main/java/org/apache/metamodel/hbase/HBaseUpdateCallback.java +++ b/hbase/src/main/java/org/apache/metamodel/hbase/HBaseUpdateCallback.java @@ -20,7 +20,6 @@ package org.apache.metamodel.hbase; import java.util.List; import java.util.Set; -import java.util.stream.Collectors; import org.apache.metamodel.AbstractUpdateCallback; import org.apache.metamodel.UpdateCallback; @@ -28,7 +27,6 @@ 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.Column; import org.apache.metamodel.schema.Schema; import org.apache.metamodel.schema.Table; @@ -70,19 +68,28 @@ public class HBaseUpdateCallback extends AbstractUpdateCallback implements Updat return new HBaseTableDropBuilder(table, this); } + /** + * @throws IllegalArgumentException when table isn't a {@link HBaseTable} + */ @Override public RowInsertionBuilder insertInto(final Table table) { throw new UnsupportedOperationException( "We need an explicit list of columns when inserting into an HBase table."); } - public RowInsertionBuilder insertInto(final Table table, final List<HBaseColumn> columns) { + /** + * Initiates the building of a row insertion operation. + * @param table Table to get inserts. + * @param columns List of {@link HBaseColumn} to insert on. + * @return {@link HBaseRowInsertionBuilder} + * @throws IllegalArgumentException The table must be an {@link HBaseTable} and the columns list can't be null or empty + */ + public HBaseRowInsertionBuilder insertInto(final Table table, final List<HBaseColumn> columns) { + if (columns == null || columns.isEmpty()) { + throw new IllegalArgumentException("The hbaseColumns list is null or empty"); + } if (table instanceof HBaseTable) { - return new HBaseRowInsertionBuilder(this, (HBaseTable) table, columns - .stream() - .map(obj -> (Column) obj) - .collect( - Collectors.toList())); + return new HBaseRowInsertionBuilder(this, (HBaseTable) table, HBaseColumn.convertToColumnsList(columns)); } else { throw new IllegalArgumentException("Not an HBase table: " + table); } @@ -93,6 +100,9 @@ public class HBaseUpdateCallback extends AbstractUpdateCallback implements Updat return true; } + /** + * @throws IllegalArgumentException when table isn't a {@link HBaseTable} + */ @Override public RowDeletionBuilder deleteFrom(Table table) { if (table instanceof HBaseTable) { http://git-wip-us.apache.org/repos/asf/metamodel/blob/9ab30040/hbase/src/test/java/org/apache/metamodel/hbase/CreateTableTest.java ---------------------------------------------------------------------- diff --git a/hbase/src/test/java/org/apache/metamodel/hbase/CreateTableTest.java b/hbase/src/test/java/org/apache/metamodel/hbase/CreateTableTest.java new file mode 100644 index 0000000..e804a67 --- /dev/null +++ b/hbase/src/test/java/org/apache/metamodel/hbase/CreateTableTest.java @@ -0,0 +1,193 @@ +/** + * 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.hbase; + +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.Set; + +import org.apache.metamodel.MetaModelException; +import org.apache.metamodel.schema.ImmutableSchema; + +public class CreateTableTest extends HBaseUpdateCallbackTest { + + /** + * Check if creating table is supported + */ + public void testDropTableSupported() { + assertTrue(getUpdateCallback().isCreateTableSupported()); + } + + /** + * Create a table with an immutableSchema, should throw a IllegalArgumentException + */ + public void testWrongSchema() { + final ImmutableSchema immutableSchema = new ImmutableSchema(getSchema()); + try { + getUpdateCallback().createTable(immutableSchema, TABLE_NAME).execute(); + fail("Should get an exception that the schema isn't mutable"); + } catch (IllegalArgumentException e) { + assertEquals("Not a mutable schema: " + immutableSchema, e.getMessage()); + } + } + + /** + * Create a table without columnFamilies, should throw a MetaModelException + */ + public void testCreateTableWithoutColumnFamilies() { + try { + getUpdateCallback().createTable(getSchema(), TABLE_NAME).execute(); + fail("Should get an exception that the columnFamilies haven't been set"); + } catch (MetaModelException e) { + assertEquals("Creating a table without columnFamilies", e.getMessage()); + } + } + + /** + * Create a table with columnFamilies null, should throw a MetaModelException + */ + public void testColumnFamiliesNull() { + try { + getUpdateCallback().createTable(getSchema(), TABLE_NAME, null).execute(); + fail("Should get an exception that the columnFamilies haven't been set"); + } catch (MetaModelException e) { + assertEquals("Creating a table without columnFamilies", e.getMessage()); + } + } + + /** + * Create a table with columnFamilies empty, should throw a MetaModelException + */ + public void testColumnFamiliesEmpty() { + try { + final LinkedHashSet<String> columnFamilies = new LinkedHashSet<String>(); + getUpdateCallback().createTable(getSchema(), TABLE_NAME, columnFamilies).execute(); + fail("Should get an exception that the columnFamilies haven't been set"); + } catch (MetaModelException e) { + assertEquals("Creating a table without columnFamilies", e.getMessage()); + } + } + + /** + * Create a table without the ID-Column, should throw a MetaModelException + */ + public void testCreateTableWithoutIDColumn() { + if (isConfigured()) { + final HBaseTable table = createHBaseTable(TABLE_NAME, HBaseDataContext.FIELD_ID, CF_FOO, CF_BAR, null); + final LinkedHashMap<HBaseColumn, Object> row = createRow(table, null, CF_FOO, CF_BAR); + final Set<String> columnFamilies = HBaseColumn.getColumnFamilies(getHBaseColumnsFromMap(row)); + try { + final HBaseCreateTableBuilder hBaseCreateTableBuilder = (HBaseCreateTableBuilder) getUpdateCallback() + .createTable(getSchema(), TABLE_NAME); + + hBaseCreateTableBuilder.setColumnFamilies(columnFamilies); + hBaseCreateTableBuilder.execute(); + fail("Should get an exception that the ID-colum is missing"); + } catch (MetaModelException e) { + assertEquals("ColumnFamily: " + HBaseDataContext.FIELD_ID + " not found", e.getMessage()); + } + } else { + warnAboutANotExecutedTest(getClass().getName(), new Object() { + }.getClass().getEnclosingMethod().getName()); + } + } + + /** + * Creating a HBaseClient with the tableName null, should throw a exception + */ + public void testCreatingTheHBaseClientWithTableNameNull() { + try { + final LinkedHashSet<String> columnFamilies = new LinkedHashSet<>(); + columnFamilies.add("1"); + new HBaseClient(getDataContext().getConnection()).createTable(null, columnFamilies); + fail("Should get an exception that tableName is null"); + } catch (IllegalArgumentException e) { + assertEquals("Can't create a table without having the tableName or columnFamilies", e.getMessage()); + } + } + + /** + * Creating a HBaseClient with the tableName null, should throw a exception + */ + public void testCreatingTheHBaseClientWithColumnFamiliesNull() { + try { + new HBaseClient(getDataContext().getConnection()).createTable("1", null); + fail("Should get an exception that columnFamilies is null"); + } catch (IllegalArgumentException e) { + assertEquals("Can't create a table without having the tableName or columnFamilies", e.getMessage()); + } + } + + /** + * Creating a HBaseClient with the tableName null, should throw a exception + */ + public void testCreatingTheHBaseClientWithColumnFamiliesEmpty() { + try { + final LinkedHashSet<String> columnFamilies = new LinkedHashSet<>(); + new HBaseClient(getDataContext().getConnection()).createTable("1", columnFamilies); + fail("Should get an exception that columnFamilies is empty"); + } catch (IllegalArgumentException e) { + assertEquals("Can't create a table without having the tableName or columnFamilies", e.getMessage()); + } + } + + /** + * Goodflow. Create a table including the ID-Column (columnFamilies not in constructor), should work + */ + public void testSettingColumnFamiliesAfterConstrutor() { + if (isConfigured()) { + final HBaseTable table = createHBaseTable(TABLE_NAME, HBaseDataContext.FIELD_ID, CF_FOO, CF_BAR, null); + final LinkedHashMap<HBaseColumn, Object> row = createRow(table, HBaseDataContext.FIELD_ID, CF_FOO, CF_BAR); + final Set<String> columnFamilies = HBaseColumn.getColumnFamilies(getHBaseColumnsFromMap(row)); + try { + final HBaseCreateTableBuilder hBaseCreateTableBuilder = (HBaseCreateTableBuilder) getUpdateCallback() + .createTable(getSchema(), TABLE_NAME); + + hBaseCreateTableBuilder.setColumnFamilies(columnFamilies); + hBaseCreateTableBuilder.execute(); + checkSuccesfullyInsertedTable(); + } catch (Exception e) { + fail("Should not get an exception"); + } + } else { + warnAboutANotExecutedTest(getClass().getName(), new Object() { + }.getClass().getEnclosingMethod().getName()); + } + } + + /** + * Goodflow. Create a table including the ID-Column (columnFamilies in constructor), should work + */ + public void testCreateTableColumnFamiliesInConstrutor() { + if (isConfigured()) { + final HBaseTable table = createHBaseTable(TABLE_NAME, HBaseDataContext.FIELD_ID, CF_FOO, CF_BAR, null); + final LinkedHashMap<HBaseColumn, Object> row = createRow(table, HBaseDataContext.FIELD_ID, CF_FOO, CF_BAR); + final Set<String> columnFamilies = HBaseColumn.getColumnFamilies(getHBaseColumnsFromMap(row)); + try { + getUpdateCallback().createTable(getSchema(), TABLE_NAME, columnFamilies).execute(); + checkSuccesfullyInsertedTable(); + } catch (Exception e) { + fail("Should not get an exception"); + } + } else { + warnAboutANotExecutedTest(getClass().getName(), new Object() { + }.getClass().getEnclosingMethod().getName()); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/metamodel/blob/9ab30040/hbase/src/test/java/org/apache/metamodel/hbase/DeleteRowTest.java ---------------------------------------------------------------------- diff --git a/hbase/src/test/java/org/apache/metamodel/hbase/DeleteRowTest.java b/hbase/src/test/java/org/apache/metamodel/hbase/DeleteRowTest.java new file mode 100644 index 0000000..8d21194 --- /dev/null +++ b/hbase/src/test/java/org/apache/metamodel/hbase/DeleteRowTest.java @@ -0,0 +1,169 @@ +/** + * 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.hbase; + +import java.io.IOException; +import java.util.LinkedHashMap; +import java.util.List; + +import org.apache.metamodel.MetaModelException; +import org.apache.metamodel.schema.MutableTable; + +public class DeleteRowTest extends HBaseUpdateCallbackTest { + + /** + * Delete is supported + */ + public void testDeleteSupported() { + assertTrue(getUpdateCallback().isDeleteSupported()); + } + + /** + * Having the table type wrong, should throw an exception + */ + public void testTableWrongType() { + final MutableTable mutableTable = new MutableTable(); + try { + getUpdateCallback().deleteFrom(mutableTable); + fail("Should get an exception that the type of the table is wrong."); + } catch (IllegalArgumentException e) { + assertEquals("Not an HBase table: " + mutableTable, e.getMessage()); + } + } + + /** + * Creating a HBaseRowDeletionBuilder with the hBaseClient null, should throw an exception + * @throws IOException + */ + public void testHBaseClientNullAtBuilder() throws IOException { + if (isConfigured()) { + try { + final HBaseTable existingTable = createAndInsertTable(TABLE_NAME, HBaseDataContext.FIELD_ID, CF_FOO, + CF_BAR); + new HBaseRowDeletionBuilder(null, existingTable); + fail("Should get an exception that hBaseClient can't be null."); + } catch (IllegalArgumentException e) { + assertEquals("hBaseClient cannot be null", e.getMessage()); + } + } else { + warnAboutANotExecutedTest(getClass().getName(), new Object() { + }.getClass().getEnclosingMethod().getName()); + } + } + + /** + * Not setting the rowkey, should throw an exception + * @throws IOException + */ + public void testNotSettingRowkey() throws IOException { + if (isConfigured()) { + try { + final HBaseTable existingTable = createAndInsertTable(TABLE_NAME, HBaseDataContext.FIELD_ID, CF_FOO, + CF_BAR); + getUpdateCallback().deleteFrom(existingTable).execute(); + fail("Should get an exception that the columnFamily doesn't exist."); + } catch (MetaModelException e) { + assertEquals("Key cannot be null", e.getMessage()); + } + } else { + warnAboutANotExecutedTest(getClass().getName(), new Object() { + }.getClass().getEnclosingMethod().getName()); + } + } + + /** + * Creating a HBaseClient with the tableName null, should throw a exception + */ + public void testCreatingTheHBaseClientWithTableNameNull() { + try { + new HBaseClient(getDataContext().getConnection()).deleteRow(null, new String("1")); + fail("Should get an exception that tableName is null"); + } catch (IllegalArgumentException e) { + assertEquals("Can't delete a row without having tableName or rowKey", e.getMessage()); + } + } + + /** + * Creating a HBaseClient with the rowKey null, should throw a exception + */ + public void testCreatingTheHBaseClientWithRowKeyNull() { + try { + new HBaseClient(getDataContext().getConnection()).deleteRow("tableName", null); + fail("Should get an exception that rowKey is null"); + } catch (IllegalArgumentException e) { + assertEquals("Can't delete a row without having tableName or rowKey", e.getMessage()); + } + } + + /** + * Goodflow. Deleting a row, that doesn't exist, should not throw an exception + */ + public void testDeletingNotExistingRow() { + if (isConfigured()) { + try { + final HBaseTable existingTable = createAndInsertTable(TABLE_NAME, HBaseDataContext.FIELD_ID, CF_FOO, + CF_BAR); + + checkRows(false); + final HBaseRowDeletionBuilder rowDeletionBuilder = (HBaseRowDeletionBuilder) getUpdateCallback() + .deleteFrom(existingTable); + rowDeletionBuilder.setKey(RK_1); + rowDeletionBuilder.execute(); + checkRows(false); + } catch (Exception e) { + fail("Should not get an exception that the row doesn't exist."); + } + } else { + warnAboutANotExecutedTest(getClass().getName(), new Object() { + }.getClass().getEnclosingMethod().getName()); + } + } + + /** + * Goodflow. Deleting a row succesfully. + */ + public void testDeleteRowSuccesfully() { + if (isConfigured()) { + try { + final HBaseTable existingTable = createAndInsertTable(TABLE_NAME, HBaseDataContext.FIELD_ID, CF_FOO, + CF_BAR); + final LinkedHashMap<HBaseColumn, Object> row = createRow(existingTable, HBaseDataContext.FIELD_ID, + CF_FOO, CF_BAR); + final List<HBaseColumn> columns = getHBaseColumnsFromMap(row); + + checkRows(false); + final HBaseRowInsertionBuilder rowInsertionBuilder = getUpdateCallback().insertInto(existingTable, + columns); + setValuesInInsertionBuilder(row, rowInsertionBuilder); + rowInsertionBuilder.execute(); + checkRows(true); + final HBaseRowDeletionBuilder rowDeletionBuilder = (HBaseRowDeletionBuilder) getUpdateCallback() + .deleteFrom(existingTable); + rowDeletionBuilder.setKey(RK_1); + rowDeletionBuilder.execute(); + checkRows(false); + } catch (Exception e) { + fail("Should not get an exception on deleting a row."); + } + } else { + warnAboutANotExecutedTest(getClass().getName(), new Object() { + }.getClass().getEnclosingMethod().getName()); + } + } +} http://git-wip-us.apache.org/repos/asf/metamodel/blob/9ab30040/hbase/src/test/java/org/apache/metamodel/hbase/DropTableTest.java ---------------------------------------------------------------------- diff --git a/hbase/src/test/java/org/apache/metamodel/hbase/DropTableTest.java b/hbase/src/test/java/org/apache/metamodel/hbase/DropTableTest.java new file mode 100644 index 0000000..d5b19ef --- /dev/null +++ b/hbase/src/test/java/org/apache/metamodel/hbase/DropTableTest.java @@ -0,0 +1,87 @@ +/** + * 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.hbase; + +import java.io.IOException; + +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.metamodel.MetaModelException; + +public class DropTableTest extends HBaseUpdateCallbackTest { + + /** + * Check if drop table is supported + */ + public void testDropTableSupported() { + assertTrue(getUpdateCallback().isDropTableSupported()); + } + + /** + * Trying to drop a table, that doesn't exist in the datastore, should throw a exception + */ + public void testDropTableThatDoesntExist() { + if (isConfigured()) { + try { + final HBaseTable table = createHBaseTable(TABLE_NAME, HBaseDataContext.FIELD_ID, CF_FOO, CF_BAR, null); + getUpdateCallback().dropTable(table).execute(); + fail("Should get an exception that the table doesn't exist in the datastore"); + } catch (MetaModelException e) { + assertEquals("Trying to delete a table that doesn't exist in the datastore.", e.getMessage()); + } + } else { + warnAboutANotExecutedTest(getClass().getName(), new Object() { + }.getClass().getEnclosingMethod().getName()); + } + } + + /** + * Creating a HBaseClient with the tableName null, should throw a exception + */ + public void testCreatingTheHBaseClientWithTableNameNull() { + try { + new HBaseClient(getDataContext().getConnection()).dropTable(null); + fail("Should get an exception that tableName is null"); + } catch (IllegalArgumentException e) { + assertEquals("Can't drop a table without having the tableName", e.getMessage()); + } + } + + /** + * Goodflow. Droping a table succesfully. + * @throws IOException + */ + public void testDropTableSuccesfully() throws IOException { + if (isConfigured()) { + try { + final HBaseTable existingTable = createAndInsertTable(TABLE_NAME, HBaseDataContext.FIELD_ID, CF_FOO, + CF_BAR); + getUpdateCallback().dropTable(existingTable).execute(); + try (final Admin admin = getDataContext().getAdmin()) { + assertFalse(admin.tableExists(TableName.valueOf(TABLE_NAME))); + } + } catch (Exception e) { + fail("Should not get an exception that the table doesn't exist in the datastore"); + } + } else { + warnAboutANotExecutedTest(getClass().getName(), new Object() { + }.getClass().getEnclosingMethod().getName()); + } + } +} http://git-wip-us.apache.org/repos/asf/metamodel/blob/9ab30040/hbase/src/test/java/org/apache/metamodel/hbase/HBaseDataContextTest.java ---------------------------------------------------------------------- diff --git a/hbase/src/test/java/org/apache/metamodel/hbase/HBaseDataContextTest.java b/hbase/src/test/java/org/apache/metamodel/hbase/HBaseDataContextTest.java index 3872611..e4e647c 100644 --- a/hbase/src/test/java/org/apache/metamodel/hbase/HBaseDataContextTest.java +++ b/hbase/src/test/java/org/apache/metamodel/hbase/HBaseDataContextTest.java @@ -54,14 +54,13 @@ public class HBaseDataContextTest extends HBaseTestCase { assertEquals("[" + HBaseDataContext.FIELD_ID + ", " + CF_BAR + ", " + CF_FOO + "]", Arrays.toString(table .getColumnNames() .toArray())); - assertEquals(ColumnType.MAP, table.getColumn(1).getType()); + assertEquals(HBaseColumn.DEFAULT_COLUMN_TYPE_FOR_COLUMN_FAMILIES, table.getColumn(1).getType()); // insert two records insertRecordsNatively(); // query using regular configuration - final DataSet dataSet1 = getDataContext().query().from(TABLE_NAME).selectAll().execute(); - try { + try (final DataSet dataSet1 = getDataContext().query().from(TABLE_NAME).selectAll().execute()) { assertTrue(dataSet1.next()); assertEquals("Row[values=[" + RK_1 + ", {" + Q_HEY + "=" + V_YO + "," + Q_HI + "=" + V_THERE + "}, {" + Q_HELLO + "=" + V_WORLD + "}]]", dataSet1.getRow().toString()); @@ -69,8 +68,6 @@ public class HBaseDataContextTest extends HBaseTestCase { assertEquals("Row[values=[" + RK_2 + ", {" + Q_BAH + "=" + new String(V_123_BYTE_ARRAY) + "," + Q_HI + "=" + V_YOU + "}, {}]]", dataSet1.getRow().toString()); assertFalse(dataSet1.next()); - } finally { - dataSet1.close(); } // query using custom table definitions @@ -79,17 +76,16 @@ public class HBaseDataContextTest extends HBaseTestCase { final String columnName3 = CF_BAR + ":" + Q_HEY; final String[] columnNames = new String[] { columnName1, columnName2, columnName3 }; final ColumnType[] columnTypes = new ColumnType[] { ColumnType.MAP, ColumnType.VARCHAR, ColumnType.VARCHAR }; - final SimpleTableDef[] tableDefinitions = new SimpleTableDef[] { new SimpleTableDef(TABLE_NAME, - columnNames, columnTypes) }; + final SimpleTableDef[] tableDefinitions = new SimpleTableDef[] { new SimpleTableDef(TABLE_NAME, columnNames, + columnTypes) }; setDataContext(new HBaseDataContext(new HBaseConfiguration("SCH", getZookeeperHostname(), getZookeeperPort(), tableDefinitions, ColumnType.VARCHAR))); - final DataSet dataSet2 = getDataContext() + try (final DataSet dataSet2 = getDataContext() .query() .from(TABLE_NAME) .select(columnName1, columnName2, columnName3) - .execute(); - try { + .execute()) { assertTrue(dataSet2.next()); assertEquals("Row[values=[{" + Q_HELLO + "=" + V_WORLD + "}, " + V_THERE + ", " + V_YO + "]]", dataSet2 .getRow() @@ -97,52 +93,39 @@ public class HBaseDataContextTest extends HBaseTestCase { assertTrue(dataSet2.next()); assertEquals("Row[values=[{}, " + V_YOU + ", null]]", dataSet2.getRow().toString()); assertFalse(dataSet2.next()); - } finally { - dataSet2.close(); } // query count - final DataSet dataSet3 = getDataContext().query().from(TABLE_NAME).selectCount().execute(); - try { + try (final DataSet dataSet3 = getDataContext().query().from(TABLE_NAME).selectCount().execute()) { assertTrue(dataSet3.next()); assertEquals("Row[values=[" + NUMBER_OF_ROWS + "]]", dataSet3.getRow().toString()); assertFalse(dataSet3.next()); - } finally { - dataSet3.close(); } // query only id - final DataSet dataSet4 = getDataContext() + try (final DataSet dataSet4 = getDataContext() .query() .from(TABLE_NAME) .select(HBaseDataContext.FIELD_ID) - .execute(); - - try { + .execute()) { assertTrue(dataSet4.next()); assertEquals("Row[values=[" + RK_1 + "]]", dataSet4.getRow().toString()); assertTrue(dataSet4.next()); assertEquals("Row[values=[" + RK_2 + "]]", dataSet4.getRow().toString()); assertFalse(dataSet4.next()); - } finally { - dataSet4.close(); } // primary key lookup query - using GET - final DataSet dataSet5 = getDataContext() + try (final DataSet dataSet5 = getDataContext() .query() .from(TABLE_NAME) .select(HBaseDataContext.FIELD_ID) .where(HBaseDataContext.FIELD_ID) .eq(RK_1) - .execute(); - - try { + .execute()) { assertTrue(dataSet5.next()); assertEquals("Row[values=[" + RK_1 + "]]", dataSet5.getRow().toString()); assertFalse(dataSet5.next()); - } finally { - dataSet5.close(); } } http://git-wip-us.apache.org/repos/asf/metamodel/blob/9ab30040/hbase/src/test/java/org/apache/metamodel/hbase/HBaseTestCase.java ---------------------------------------------------------------------- diff --git a/hbase/src/test/java/org/apache/metamodel/hbase/HBaseTestCase.java b/hbase/src/test/java/org/apache/metamodel/hbase/HBaseTestCase.java index b9524a8..91180cb 100644 --- a/hbase/src/test/java/org/apache/metamodel/hbase/HBaseTestCase.java +++ b/hbase/src/test/java/org/apache/metamodel/hbase/HBaseTestCase.java @@ -20,9 +20,11 @@ package org.apache.metamodel.hbase; import java.io.File; import java.io.FileReader; +import java.io.IOException; import java.util.Properties; import org.apache.metamodel.schema.ColumnType; +import org.junit.AfterClass; import junit.framework.TestCase; @@ -59,33 +61,44 @@ public abstract class HBaseTestCase extends TestCase { private String zookeeperHostname; private int zookeeperPort; private boolean _configured; - private HBaseDataContext _dataContext; + private static HBaseDataContext _dataContext; + + private boolean setUpIsDone = false; @Override protected void setUp() throws Exception { super.setUp(); - Properties properties = new Properties(); - File file = new File(getPropertyFilePath()); - if (file.exists()) { - properties.load(new FileReader(file)); - zookeeperHostname = properties.getProperty("hbase.zookeeper.hostname"); - String zookeeperPortPropertyValue = properties.getProperty("hbase.zookeeper.port"); - if (zookeeperPortPropertyValue != null && !zookeeperPortPropertyValue.isEmpty()) { - zookeeperPort = Integer.parseInt(zookeeperPortPropertyValue); + if (!setUpIsDone) { + Properties properties = new Properties(); + File file = new File(getPropertyFilePath()); + if (file.exists()) { + properties.load(new FileReader(file)); + zookeeperHostname = properties.getProperty("hbase.zookeeper.hostname"); + String zookeeperPortPropertyValue = properties.getProperty("hbase.zookeeper.port"); + if (zookeeperPortPropertyValue != null && !zookeeperPortPropertyValue.isEmpty()) { + zookeeperPort = Integer.parseInt(zookeeperPortPropertyValue); + } + + _configured = (zookeeperHostname != null && !zookeeperHostname.isEmpty()); + } else { + _configured = false; } - - _configured = (zookeeperHostname != null && !zookeeperHostname.isEmpty()); - } else { - _configured = false; - } - if (isConfigured()) { - final HBaseConfiguration configuration = new HBaseConfiguration(zookeeperHostname, zookeeperPort, - ColumnType.VARCHAR); - setDataContext(new HBaseDataContext(configuration)); + if (isConfigured()) { + final HBaseConfiguration configuration = new HBaseConfiguration(zookeeperHostname, zookeeperPort, + ColumnType.VARCHAR); + setDataContext(new HBaseDataContext(configuration)); + } + setUpIsDone = true; } } + @AfterClass + public static void oneTimeTeardown() throws IOException { + _dataContext.getConnection().close(); + ; + } + private String getPropertyFilePath() { String userHome = System.getProperty("user.home"); return userHome + "/metamodel-integrationtest-configuration.properties"; @@ -113,12 +126,6 @@ public abstract class HBaseTestCase extends TestCase { } public void setDataContext(HBaseDataContext dataContext) { - this._dataContext = dataContext; - } - - @Override - protected void tearDown() throws Exception { - super.tearDown(); - _dataContext.getConnection().close(); + HBaseTestCase._dataContext = dataContext; } } http://git-wip-us.apache.org/repos/asf/metamodel/blob/9ab30040/hbase/src/test/java/org/apache/metamodel/hbase/HBaseUpdateCallbackTest.java ---------------------------------------------------------------------- diff --git a/hbase/src/test/java/org/apache/metamodel/hbase/HBaseUpdateCallbackTest.java b/hbase/src/test/java/org/apache/metamodel/hbase/HBaseUpdateCallbackTest.java index 359be01..b010c71 100644 --- a/hbase/src/test/java/org/apache/metamodel/hbase/HBaseUpdateCallbackTest.java +++ b/hbase/src/test/java/org/apache/metamodel/hbase/HBaseUpdateCallbackTest.java @@ -20,56 +20,62 @@ package org.apache.metamodel.hbase; import java.io.IOException; import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.LinkedHashSet; -import java.util.Set; +import java.util.List; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Admin; -import org.apache.metamodel.MetaModelException; +import org.apache.hadoop.hbase.client.Get; +import org.apache.hadoop.hbase.client.Result; +import org.apache.hadoop.hbase.util.Bytes; import org.apache.metamodel.schema.ColumnType; -import org.apache.metamodel.schema.ImmutableSchema; import org.apache.metamodel.schema.MutableSchema; import org.apache.metamodel.schema.Table; import org.apache.metamodel.util.SimpleTableDef; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -public class HBaseUpdateCallbackTest extends HBaseTestCase { +public abstract class HBaseUpdateCallbackTest extends HBaseTestCase { + + private static final Logger logger = LoggerFactory.getLogger(HBaseClient.class); private HBaseUpdateCallback updateCallback; private MutableSchema schema; + private boolean setUpIsDone = false; + @Override protected void setUp() throws Exception { super.setUp(); if (isConfigured()) { - updateCallback = new HBaseUpdateCallback(getDataContext()); - schema = (MutableSchema) getDataContext().getDefaultSchema(); - - if (schema.getTableByName(TABLE_NAME) != null) { + if (setUpIsDone) { dropTableIfItExists(); + } else { + updateCallback = new HBaseUpdateCallback(getDataContext()); + schema = (MutableSchema) getDataContext().getDefaultSchema(); + dropTableIfItExists(); + setUpIsDone = true; } } } - public void testDropTable() throws IOException { - dropTableIfItExists(); - - try { - HBaseTable table = createHBaseTable(); - updateCallback.dropTable(table).execute(); - fail("Should get an exception that the table doesn't exist in the datastore"); - } catch (MetaModelException e) { - assertEquals("Trying to delete a table that doesn't exist in the datastore.", e.getMessage()); + @Override + public void tearDown() throws Exception { + if (isConfigured()) { + dropTableIfItExists(); } + super.tearDown(); } - private void dropTableIfItExists() { - Table table = schema.getTableByName(TABLE_NAME); + protected void dropTableIfItExists() { + final Table table = schema.getTableByName(TABLE_NAME); if (table != null) { updateCallback.dropTable(table).execute(); // Check schema assertNull(schema.getTableByName(TABLE_NAME)); // Check in the datastore - try (Admin admin = getDataContext().getAdmin()) { + try (final Admin admin = getDataContext().getAdmin()) { assertFalse(admin.tableExists(TableName.valueOf(TABLE_NAME))); } catch (IOException e) { fail("Should not an exception checking if the table exists"); @@ -77,137 +83,121 @@ public class HBaseUpdateCallbackTest extends HBaseTestCase { } } - public void testCreateTable() { - // Drop the table if it exists - dropTableIfItExists(); - - // Test 1: Create a table with an immutableSchema, should throw a IllegalArgumentException - ImmutableSchema immutableSchema = new ImmutableSchema(schema); - try { - updateCallback.createTable(immutableSchema, TABLE_NAME).execute(); - fail("Should get an exception that the schema isn't mutable"); - } catch (IllegalArgumentException e) { - assertEquals("Not a mutable schema: " + immutableSchema, e.getMessage()); + protected void checkSuccesfullyInsertedTable() throws IOException { + // Check the schema + assertNotNull(schema.getTableByName(TABLE_NAME)); + // Check in the datastore + try (final Admin admin = getDataContext().getAdmin()) { + assertTrue(admin.tableExists(TableName.valueOf(TABLE_NAME))); + } catch (IOException e) { + fail("Should not an exception checking if the table exists"); } + } - // Test 2: Create a table without columnFamilies, should throw a MetaModelException - try { - updateCallback.createTable(schema, TABLE_NAME).execute(); - fail("Should get an exception that the columnFamilies haven't been set"); - } catch (MetaModelException e) { - assertEquals("Creating a table without columnFamilies", e.getMessage()); - } + protected HBaseTable createAndInsertTable(final String tableName, final String idColumn, final String columnFamily1, + final String columnFamily2) throws IOException { + final LinkedHashSet<String> columnFamilies = new LinkedHashSet<>(); + columnFamilies.add(idColumn); + columnFamilies.add(columnFamily1); + columnFamilies.add(columnFamily2); + updateCallback.createTable(schema, tableName, columnFamilies).execute(); + checkSuccesfullyInsertedTable(); + return (HBaseTable) getDataContext().getDefaultSchema().getTableByName(tableName); + } - // Test 3: Create a table with columnFamilies null, should throw a MetaModelException - try { - updateCallback.createTable(schema, TABLE_NAME, null).execute(); - fail("Should get an exception that the columnFamilies haven't been set"); - } catch (MetaModelException e) { - assertEquals("Creating a table without columnFamilies", e.getMessage()); + protected HBaseTable createHBaseTable(final String tableName, final String idColumn, final String columnFamily1, + final String columnFamily2, final String columnFamily3) { + String[] columnNames; + ColumnType[] columnTypes; + if (columnFamily3 == null) { + columnNames = new String[] { idColumn, columnFamily1, columnFamily2 }; + columnTypes = new ColumnType[] { ColumnType.STRING, ColumnType.STRING, ColumnType.STRING }; + } else { + columnNames = new String[] { idColumn, columnFamily1, columnFamily2, columnFamily3 }; + columnTypes = new ColumnType[] { ColumnType.STRING, ColumnType.STRING, ColumnType.STRING, + ColumnType.STRING }; } + final SimpleTableDef tableDef = new SimpleTableDef(tableName, columnNames, columnTypes); + return new HBaseTable(getDataContext(), tableDef, schema, ColumnType.STRING); + } - // Test 4: Create a table with columnFamilies empty, should throw a MetaModelException - try { - final LinkedHashSet<String> columnFamilies = new LinkedHashSet<String>(); - updateCallback.createTable(schema, TABLE_NAME, columnFamilies).execute(); - fail("Should get an exception that the columnFamilies haven't been set"); - } catch (MetaModelException e) { - assertEquals("Creating a table without columnFamilies", e.getMessage()); - } + protected static LinkedHashMap<HBaseColumn, Object> createRow(final HBaseTable table, final String idColumn, + final String columnFamily1, final String columnFamily2) { + final LinkedHashMap<HBaseColumn, Object> map = new LinkedHashMap<>(); - HBaseTable table = createHBaseTable(); - - // Test 5: Create a table without the ID-Column, should throw a MetaModelException - ArrayList<HBaseColumn> hBaseColumnsAsArrayList = createListWithHBaseColumnsExcludingIDColumn(table); - HBaseColumn[] hBaseColumnsAsArray = convertToHBaseColumnArray(hBaseColumnsAsArrayList); - Set<String> columnFamilies = HBaseColumn.getColumnFamilies(hBaseColumnsAsArray); - try { - HBaseCreateTableBuilder hBaseCreateTableBuilder = (HBaseCreateTableBuilder) updateCallback.createTable( - schema, TABLE_NAME); - - hBaseCreateTableBuilder.setColumnFamilies(columnFamilies); - hBaseCreateTableBuilder.execute(); - fail("Should get an exception that the ID-colum is missing"); - } catch (MetaModelException e) { - assertEquals("ColumnFamily: " + HBaseDataContext.FIELD_ID + " not found", e.getMessage()); + // Columns + final ArrayList<HBaseColumn> columns = new ArrayList<>(); + if (idColumn != null) { + columns.add(new HBaseColumn(idColumn, table)); } - - // Test 6: Create a table including the ID-Column (columnFamilies not in constructor), should work - hBaseColumnsAsArrayList = createListWithHBaseColumnsIncludingIDColumn(table); - hBaseColumnsAsArray = convertToHBaseColumnArray(hBaseColumnsAsArrayList); - columnFamilies = HBaseColumn.getColumnFamilies(hBaseColumnsAsArray); - try { - HBaseCreateTableBuilder hBaseCreateTableBuilder = (HBaseCreateTableBuilder) updateCallback.createTable( - schema, TABLE_NAME); - - hBaseCreateTableBuilder.setColumnFamilies(HBaseColumn.getColumnFamilies(hBaseColumnsAsArray)); - hBaseCreateTableBuilder.execute(); - checkSuccesfullyInsertedTable(); - } catch (Exception e) { - fail("Should not get an exception"); + columns.add(new HBaseColumn(columnFamily1, Q_HELLO, table)); + columns.add(new HBaseColumn(columnFamily1, Q_HI, table)); + columns.add(new HBaseColumn(columnFamily2, Q_HEY, table)); + columns.add(new HBaseColumn(columnFamily2, Q_BAH, table)); + + // Values + final ArrayList<Object> values = new ArrayList<>(); + if (idColumn != null) { + values.add(RK_1); } - dropTableIfItExists(); - - // Test 7: Create a table including the ID-Column (columnFamilies in constructor), should work - try { - updateCallback.createTable(schema, TABLE_NAME, columnFamilies).execute(); - checkSuccesfullyInsertedTable(); - } catch (Exception e) { - fail("Should not get an exception"); + values.add(V_WORLD); + values.add(V_THERE); + values.add(V_YO); + values.add(V_123_BYTE_ARRAY); + + // Fill the map + for (int i = 0; i < columns.size(); i++) { + map.put(columns.get(i), values.get(i)); } - dropTableIfItExists(); + + return map; } - private void checkSuccesfullyInsertedTable() throws IOException { - // Check the schema - assertNotNull(schema.getTableByName(TABLE_NAME)); - // Check in the datastore - try (Admin admin = getDataContext().getAdmin()) { - assertTrue(admin.tableExists(TableName.valueOf(TABLE_NAME))); - } catch (IOException e) { - fail("Should not an exception checking if the table exists"); - } + protected static List<HBaseColumn> getHBaseColumnsFromMap(final LinkedHashMap<HBaseColumn, Object> map) { + final List<HBaseColumn> columns = new ArrayList<>(); + columns.addAll(map.keySet()); + return columns; } - // public void testInsertRows() throws IOException { - // // Drop the table if it exists - // dropTableIfItExists(); - // - // insertTable(); - // } - - private void insertTable() throws IOException { - HBaseTable table = createHBaseTable(); - ArrayList<HBaseColumn> hBaseColumnsAsArrayList = createListWithHBaseColumnsIncludingIDColumn(table); - HBaseColumn[] hBaseColumnsAsArray = convertToHBaseColumnArray(hBaseColumnsAsArrayList); - Set<String> columnFamilies = HBaseColumn.getColumnFamilies(hBaseColumnsAsArray); - updateCallback.createTable(schema, TABLE_NAME, columnFamilies).execute(); - checkSuccesfullyInsertedTable(); + protected void setValuesInInsertionBuilder(final LinkedHashMap<HBaseColumn, Object> row, + final HBaseRowInsertionBuilder rowInsertionBuilder) { + int i = 0; + for (Object value : row.values()) { + rowInsertionBuilder.value(i, value); + i++; + } } - private HBaseTable createHBaseTable() { - String[] columnNames = new String[] { CF_FOO, CF_BAR }; - ColumnType[] columnTypes = new ColumnType[] { ColumnType.STRING, ColumnType.STRING }; - SimpleTableDef tableDef = new SimpleTableDef(TABLE_NAME, columnNames, columnTypes); - return new HBaseTable(getDataContext(), tableDef, schema, ColumnType.STRING); + protected void checkRows(final boolean rowsExist) throws IOException { + try (org.apache.hadoop.hbase.client.Table table = getDataContext().getConnection().getTable(TableName.valueOf( + TABLE_NAME))) { + final Get get = new Get(Bytes.toBytes(RK_1)); + final Result result = table.get(get); + if (rowsExist) { + assertFalse(result.isEmpty()); + assertEquals(V_WORLD, new String(result.getValue(Bytes.toBytes(CF_FOO), Bytes.toBytes(Q_HELLO)))); + assertEquals(V_THERE, new String(result.getValue(Bytes.toBytes(CF_FOO), Bytes.toBytes(Q_HI)))); + assertEquals(V_YO, new String(result.getValue(Bytes.toBytes(CF_BAR), Bytes.toBytes(Q_HEY)))); + assertEquals(V_123_BYTE_ARRAY.toString(), new String(result.getValue(Bytes.toBytes(CF_BAR), Bytes + .toBytes(Q_BAH)))); + } else { + assertTrue(result.isEmpty()); + } + } } - private static ArrayList<HBaseColumn> createListWithHBaseColumnsExcludingIDColumn(final HBaseTable table) { - ArrayList<HBaseColumn> hbaseColumns = new ArrayList<HBaseColumn>(); - hbaseColumns.add(new HBaseColumn(CF_FOO, Q_HELLO, table)); - hbaseColumns.add(new HBaseColumn(CF_FOO, Q_HI, table)); - hbaseColumns.add(new HBaseColumn(CF_BAR, Q_HEY, table)); - hbaseColumns.add(new HBaseColumn(CF_BAR, Q_BAH, table)); - return hbaseColumns; + protected void warnAboutANotExecutedTest(String className, String methodName) { + String logWarning = "Test(method) \"" + className + "#" + methodName + + "\" is not executed, because the HBasetest is not configured."; + // System.out.println(logWarning); + logger.warn(logWarning); } - private static ArrayList<HBaseColumn> createListWithHBaseColumnsIncludingIDColumn(final HBaseTable table) { - ArrayList<HBaseColumn> hbaseColumns = createListWithHBaseColumnsExcludingIDColumn(table); - hbaseColumns.add(new HBaseColumn(HBaseDataContext.FIELD_ID, table)); - return hbaseColumns; + protected HBaseUpdateCallback getUpdateCallback() { + return updateCallback; } - private static HBaseColumn[] convertToHBaseColumnArray(final ArrayList<HBaseColumn> hBaseColumnsAsArrayList) { - return hBaseColumnsAsArrayList.toArray(new HBaseColumn[hBaseColumnsAsArrayList.size()]); + protected MutableSchema getSchema() { + return schema; } }