001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.util.bulkdatagenerator;
019
020import java.io.IOException;
021import java.util.Map;
022import org.apache.hadoop.hbase.TableName;
023import org.apache.hadoop.hbase.client.Admin;
024import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
025import org.apache.hadoop.hbase.client.TableDescriptor;
026import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
027
028import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
029
030public final class Utility {
031
032  /**
033   * Schema for HBase table to be generated by generated and populated by
034   * {@link BulkDataGeneratorTool}
035   */
036  public enum TableColumnNames {
037    ORG_ID("orgId".getBytes()),
038    TOOL_EVENT_ID("toolEventId".getBytes()),
039    EVENT_ID("eventId".getBytes()),
040    VEHICLE_ID("vehicleId".getBytes()),
041    SPEED("speed".getBytes()),
042    LATITUDE("latitude".getBytes()),
043    LONGITUDE("longitude".getBytes()),
044    LOCATION("location".getBytes()),
045    TIMESTAMP("timestamp".getBytes());
046
047    private final byte[] columnName;
048
049    TableColumnNames(byte[] column) {
050      this.columnName = column;
051    }
052
053    public byte[] getColumnName() {
054      return this.columnName;
055    }
056  }
057
058  public static final String COLUMN_FAMILY = "cf";
059
060  public static final int SPLIT_PREFIX_LENGTH = 6;
061
062  public static final int MAX_SPLIT_COUNT = (int) Math.pow(10, SPLIT_PREFIX_LENGTH);
063
064  /**
065   * Private Constructor
066   */
067  private Utility() {
068
069  }
070
071  public static void deleteTable(Admin admin, String tableName) throws IOException {
072    admin.disableTable(TableName.valueOf(tableName));
073    admin.deleteTable(TableName.valueOf(tableName));
074  }
075
076  /**
077   * Creates a pre-splitted HBase Table having single column family ({@link #COLUMN_FAMILY}) and
078   * sequential splits with {@link #SPLIT_PREFIX_LENGTH} length character prefix. Example: If a
079   * table (TEST_TABLE_1) need to be generated with splitCount as 10, table would be created with
080   * (10+1) regions with boundaries end-keys as (000000-000001, 000001-000002, 000002-000003, ....,
081   * 0000010-)
082   * @param admin        - Admin object associated with HBase connection
083   * @param tableName    - Name of table to be created
084   * @param splitCount   - Number of splits for the table (Number of regions will be splitCount + 1)
085   * @param tableOptions - Additional HBase metadata properties to be set for the table
086   */
087  public static void createTable(Admin admin, String tableName, int splitCount,
088    Map<String, String> tableOptions) throws IOException {
089    Preconditions.checkArgument(splitCount > 0, "Split count must be greater than 0");
090    TableDescriptorBuilder tableDescriptorBuilder =
091      TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
092    tableOptions.forEach(tableDescriptorBuilder::setValue);
093    TableDescriptor tableDescriptor = tableDescriptorBuilder
094      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(COLUMN_FAMILY)).build();
095    // Pre-splitting table based on splitCount
096    byte[][] splitKeys = new byte[splitCount][];
097    for (int i = 0; i < splitCount; i++) {
098      splitKeys[i] = String.format("%0" + Utility.SPLIT_PREFIX_LENGTH + "d", i + 1).getBytes();
099    }
100    admin.createTable(tableDescriptor, splitKeys);
101  }
102}