Github user arjansh commented on a diff in the pull request:
https://github.com/apache/metamodel/pull/182#discussion_r196366302
--- Diff: hbase/src/main/java/org/apache/metamodel/hbase/HBaseClient.java
---
@@ -0,0 +1,207 @@
+/**
+ * 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.Set;
+
+import org.apache.hadoop.hbase.HColumnDescriptor;
+import org.apache.hadoop.hbase.HTableDescriptor;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.client.Admin;
+import org.apache.hadoop.hbase.client.Connection;
+import org.apache.hadoop.hbase.client.Delete;
+import org.apache.hadoop.hbase.client.Get;
+import org.apache.hadoop.hbase.client.Put;
+import org.apache.hadoop.hbase.client.Table;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.metamodel.MetaModelException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This class can perform client-operations on a HBase datastore
+ */
+final class HBaseClient {
+
+ private static final Logger logger =
LoggerFactory.getLogger(HBaseClient.class);
+
+ private final Connection _connection;
+
+ public HBaseClient(final Connection connection) {
+ this._connection = connection;
+ }
+
+ /**
+ * Insert a single row of values to a HBase table.
+ * @param tableName
+ * @param columns
+ * @param values
+ * @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 caught
+ */
+ public void insertRow(final String tableName, final HBaseColumn[]
columns, final Object[] values,
+ final 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");
+ }
+ if (columns.length != values.length) {
+ throw new IllegalArgumentException("The amount of columns
don't match the amount of values");
+ }
+ try (final Table table =
_connection.getTable(TableName.valueOf(tableName))) {
+ // Create a put with the values of indexOfIdColumn as rowkey
+ final Put put = new
Put(getValueAsByteArray(values[indexOfIdColumn]));
+
+ // Add the other values to the put
+ for (int i = 0; i < columns.length; i++) {
+ if (i != indexOfIdColumn) {
+ // NullChecker is already forced within the
HBaseColumn class
+ final byte[] columnFamily =
Bytes.toBytes(columns[i].getColumnFamily());
+ // An HBaseColumn doesn't need a qualifier, this only
works when the qualifier is empty (not
+ // null). Otherwise NullPointer exceptions will happen
+ byte[] qualifier = null;
+ if (columns[i].getQualifier() != null) {
+ qualifier =
Bytes.toBytes(columns[i].getQualifier());
+ } else {
+ qualifier = Bytes.toBytes(new String(""));
+ }
+ final byte[] value = getValueAsByteArray(values[i]);
+ // A NULL value, doesn't get inserted in HBase
+ // TODO: Do we delete the cell (and therefore the
qualifier) if the table get's updated?
+ if (value == null) {
+ logger.warn("The value of column '{}:{}' is null.
This insertion is skipped", columns[i]
+ .getColumnFamily()
+ .toString(),
columns[i].getQualifier().toString());
--- End diff --
It is possible that an HBaseColumn doesn't have a qualifier, in which case
this will throw a NullPointerException, so either do
`Bytes.toString(qualifier)`, or an inline if to check if the qualifier is null
and in that case return "" for it.
---