Exemple #1
0
  /** 根据给定的表明和列族创建一张新表。 */
  @Override
  public Builder add() {
    if (type == 0) {
      try {
        if (tableName.trim().equals("") || tableName == null) {
          Log.error("=== LiMingji Error Info === Error: No tableName");
          return this;
        }
        if (columnFamilies == null || columnFamilies.size() == 0) {
          Log.error("=== LiMingji Error Info === Error: No columnFamilies");
          return this;
        }
        if (hBaseAdmin.tableExists(tableName)) {
          Log.info("=== LiMingji Debug Info === Table already exits");
          return this;
        } else {
          HTableDescriptor tableDesc = new HTableDescriptor(tableName);
          for (String column : columnFamilies) {
            tableDesc.addFamily(new HColumnDescriptor(column));
          }
          columnFamilies.clear();
          // preDistribute region
          if (split != null) hBaseAdmin.createTable(tableDesc, split);

          hBaseAdmin.createTable(tableDesc);
          Log.info("=== LiMingji Debug Info === Table has been created. ");
        }
      } catch (IOException e) {
        Log.info("=== LiMingji Debug Info === deleteTable@DTL IOException");
        e.printStackTrace();
      }
    }
    return this;
  }
Exemple #2
0
 /**
  * 查询所有表
  *
  * @return 所以表 / null
  * @throws Exception
  */
 public static List<HTableDescriptor> queryALLTable() throws Exception {
   Connection conn = null;
   HBaseAdmin admin = null;
   try {
     conn = ConnectionFactory.createConnection(conf);
     admin = (HBaseAdmin) conn.getAdmin();
     if (admin != null) {
       HTableDescriptor[] listTables = admin.listTables();
       if (null != listTables && listTables.length > 0) {
         return Arrays.asList(listTables);
       }
     }
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     try {
       if (null != admin) {
         admin.close();
       }
     } catch (IOException e) {
       logger.error("HBaseAdmin close exception, errMsg:{}", e.getMessage());
     }
     try {
       if (null != conn) {
         conn.close();
       }
     } catch (Exception e) {
       logger.error("Connection close exception, errMsg:{}", e.getMessage());
     }
   }
   return null;
 }
Exemple #3
0
 /**
  * 删除指定表名
  *
  * @param rowKey
  */
 public void deleteTable(byte[] rowKey) {
   Connection conn = null;
   HBaseAdmin admin = null;
   try {
     conn = ConnectionFactory.createConnection(conf);
     admin = (HBaseAdmin) conn.getAdmin();
     // 在删除一张表前,要使其失效
     admin.disableTable(rowKey);
     admin.deleteTable(rowKey);
     admin.enableTable(rowKey);
   } catch (Exception e) {
     logger.error("HBaseAdmin deleteTable exception, errMsg:{}", e.getMessage());
   } finally {
     try {
       if (null != admin) {
         admin.close();
       }
     } catch (IOException e) {
       logger.error("HBaseAdmin close exception, errMsg:{}", e.getMessage());
     }
     try {
       if (null != conn) {
         conn.close();
       }
     } catch (Exception e) {
       logger.error("Connection close exception, errMsg:{}", e.getMessage());
     }
   }
 }
Exemple #4
0
 /**
  * 删除指定名称的列簇
  *
  * @param tableName 表名
  * @param columnFamilyName 列族
  */
 public static void deleteFamily(byte[] tableName, String columnFamilyName) {
   Connection conn = null;
   HBaseAdmin admin = null;
   try {
     conn = ConnectionFactory.createConnection(conf);
     admin = (HBaseAdmin) conn.getAdmin();
     admin.deleteColumn(tableName, columnFamilyName);
   } catch (Exception e) {
     logger.error("HBaseAdmin deleteColumn exception, errMsg:{}", e.getMessage());
   } finally {
     try {
       if (null != admin) {
         admin.close();
       }
     } catch (IOException e) {
       logger.error("HBaseAdmin close exception, errMsg:{}", e.getMessage());
     }
     try {
       if (null != conn) {
         conn.close();
       }
     } catch (Exception e) {
       logger.error("Connection close exception, errMsg:{}", e.getMessage());
     }
   }
 }
  public static void createTableTest(String tableStr) {
    try {
      Configuration conf = HBaseConfiguration.create();
      byte[] tableName = Bytes.toBytes(tableStr);
      List<byte[]> families = new ArrayList<byte[]>();
      families.add(f0);

      if (conf == null) {
        throw new IOException("conf not set.");
      } else {
        HBaseAdmin admin = new HBaseAdmin(conf);
        if (!admin.tableExists(tableName)) {
          HTableDescriptor tableDesc = new HTableDescriptor(tableName);
          for (byte[] family : families) {
            HColumnDescriptor hc = new HColumnDescriptor(family);
            hc.setMaxVersions(1);
            tableDesc.addFamily(hc);
          }
          admin.createTable(tableDesc);
        } else {
          throw new IOException("table " + new String(tableName) + " already  exists.");
        }
      }
      System.out.println("DONE.");
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  public void createTable(String tableName, List<String> ColumnFamilies) {
    HBaseAdmin admin = null;
    try {
      admin = new HBaseAdmin(conf);
      HTableDescriptor tableDescriptor = new HTableDescriptor(Bytes.toBytes(tableName));

      for (String columnFamily : ColumnFamilies) {
        HColumnDescriptor columnDescriptor = new HColumnDescriptor(columnFamily);
        tableDescriptor.addFamily(columnDescriptor);
      }
      admin.createTable(tableDescriptor);
      admin.close();
    } catch (TableExistsException e) {
      System.out.println("Table already exist:" + tableName);
      try {
        admin.close();
      } catch (IOException e1) {
        System.out.println("Error occurred while cloing the HBaseAdmin conneciton:" + e1);
      }
    } catch (MasterNotRunningException e) {
      throw new RuntimeException("HBase master not running, table creation failed.");
    } catch (ZooKeeperConnectionException e) {
      throw new RuntimeException("Zookeeper not running, table creation failed.");
    } catch (IOException e) {
      throw new RuntimeException("IO error, table creation failed.");
    }
  }
Exemple #7
0
  public static void main(String[] args) throws IOException {

    // Instantiating configuration class
    Configuration con = HBaseConfiguration.create();

    // Instantiating HbaseAdmin class
    HBaseAdmin admin = new HBaseAdmin(con);

    // Instantiating table descriptor class
    HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("emp_nibin"));

    // Adding column families to table descriptor
    tableDescriptor.addFamily(new HColumnDescriptor("personal_nibin"));
    tableDescriptor.addFamily(new HColumnDescriptor("professional_nibin"));

    // Execute the table through admin
    admin.createTable(tableDescriptor);
    System.out.println(" Table created ");
    System.out.println("listing the table now....");

    HTableDescriptor[] tables = admin.listTables();

    for (int i = 0; i < tables.length; i++) {
      System.out.println(tables[i].getNameAsString());
    }
  }
  /**
   * Deletes the tables given tables.
   *
   * @param config cluster configuration
   * @param vertexDataTableName vertex data table name
   * @param edgeDataTableName edge data table name
   * @param graphDataTableName graph data table name
   * @throws IOException
   */
  private static void deleteTablesIfExists(
      final Configuration config,
      final String vertexDataTableName,
      final String edgeDataTableName,
      final String graphDataTableName)
      throws IOException {
    HTableDescriptor vertexDataTableDescriptor =
        new HTableDescriptor(TableName.valueOf(vertexDataTableName));
    HTableDescriptor edgeDataTableDescriptor =
        new HTableDescriptor(TableName.valueOf(edgeDataTableName));
    HTableDescriptor graphsTableDescriptor =
        new HTableDescriptor(TableName.valueOf(graphDataTableName));

    HBaseAdmin admin = new HBaseAdmin(config);

    if (admin.tableExists(vertexDataTableDescriptor.getName())) {
      deleteTable(admin, vertexDataTableDescriptor);
    }
    if (admin.tableExists(edgeDataTableDescriptor.getName())) {
      deleteTable(admin, edgeDataTableDescriptor);
    }
    if (admin.tableExists(graphsTableDescriptor.getName())) {
      deleteTable(admin, graphsTableDescriptor);
    }

    admin.close();
  }
Exemple #9
0
  public static void main(String[] args)
      throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
    Configuration conf = HBaseConfiguration.create();
    conf.set(
        "hbase.zookeeper.quorum", "192.168.10.163:2181,192.168.10.164:2181,192.168.10.165:2181");

    // admin用户
    HBaseAdmin admin = new HBaseAdmin(conf);
    // table
    HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("people"));
    // info列簇
    HColumnDescriptor hcd_info = new HColumnDescriptor("info");
    hcd_info.setMaxVersions(3);
    // data列簇
    HColumnDescriptor hcd_data = new HColumnDescriptor("data");

    // 将列簇添加到htable中
    htd.addFamily(hcd_info);
    htd.addFamily(hcd_data);

    // 创建表
    admin.createTable(htd);

    // 关闭连接
    admin.close();
  }
 public void fixInconsistent() throws IOException {
   if (ifFix == true) {
     for (String segFullName : inconsistentHTables) {
       String[] sepNameList = segFullName.split(",");
       HTableDescriptor desc = hbaseAdmin.getTableDescriptor(TableName.valueOf(sepNameList[0]));
       logger.info(
           "Change the host of htable "
               + sepNameList[0]
               + "belonging to cube "
               + sepNameList[1]
               + " from "
               + desc.getValue(IRealizationConstants.HTableTag)
               + " to "
               + dstCfg.getMetadataUrlPrefix());
       hbaseAdmin.disableTable(sepNameList[0]);
       desc.setValue(IRealizationConstants.HTableTag, dstCfg.getMetadataUrlPrefix());
       hbaseAdmin.modifyTable(sepNameList[0], desc);
       hbaseAdmin.enableTable(sepNameList[0]);
     }
   } else {
     logger.info("------ Inconsistent HTables Needed To Be Fixed ------");
     for (String hTable : inconsistentHTables) {
       String[] sepNameList = hTable.split(",");
       logger.info(sepNameList[0] + " belonging to cube " + sepNameList[1]);
     }
     logger.info("----------------------------------------------------");
   }
 }
  /**
   * Creates the tables used by the graph store.
   *
   * @param config Hadoop configuration
   * @param vertexDataHandler vertex storage handler
   * @param edgeDataHandler edge storage handler
   * @param graphDataHandler graph storage handler
   * @param vertexDataTableName vertex data table name
   * @param edgeTableName edge data table name
   * @param graphDataTableName graph data table name
   * @throws IOException
   */
  private static void createTablesIfNotExists(
      final Configuration config,
      final VertexDataHandler vertexDataHandler,
      final EdgeDataHandler edgeDataHandler,
      final GraphDataHandler graphDataHandler,
      final String vertexDataTableName,
      final String edgeTableName,
      final String graphDataTableName)
      throws IOException {
    HTableDescriptor vertexDataTableDescriptor =
        new HTableDescriptor(TableName.valueOf(vertexDataTableName));
    HTableDescriptor edgeDataTableDescriptor =
        new HTableDescriptor(TableName.valueOf(edgeTableName));
    HTableDescriptor graphDataTableDescriptor =
        new HTableDescriptor(TableName.valueOf(graphDataTableName));

    HBaseAdmin admin = new HBaseAdmin(config);

    if (!admin.tableExists(vertexDataTableDescriptor.getName())) {
      vertexDataHandler.createTable(admin, vertexDataTableDescriptor);
    }
    if (!admin.tableExists(edgeDataTableDescriptor.getName())) {
      edgeDataHandler.createTable(admin, edgeDataTableDescriptor);
    }
    if (!admin.tableExists(graphDataTableDescriptor.getName())) {
      graphDataHandler.createTable(admin, graphDataTableDescriptor);
    }

    admin.close();
  }
Exemple #12
0
 public static synchronized void dropTableIfExists() throws Exception {
   HBaseAdmin admin = HBaseUtils.getHBaseAdmin();
   if (admin.tableExists(TABLE_NAME)) {
     admin.disableTable(TABLE_NAME);
     admin.deleteTable(TABLE_NAME);
   }
 }
Exemple #13
0
 public static void addColumnFamily(String keyspace, String columnFamily) throws Exception {
   Configuration conf = HBaseConfiguration.create();
   HBaseAdmin admin = new HBaseAdmin(conf);
   HColumnDescriptor column = new HColumnDescriptor(columnFamily);
   // admin.deleteColumn(keyspace, columnFamily);
   admin.addColumn(keyspace, column);
 }
  @Test(timeout = 180000)
  public void testRestoreSnapshot() throws Exception {
    String nsp = prefix + "_testRestoreSnapshot";
    NamespaceDescriptor nspDesc =
        NamespaceDescriptor.create(nsp)
            .addConfiguration(TableNamespaceManager.KEY_MAX_REGIONS, "10")
            .build();
    ADMIN.createNamespace(nspDesc);
    assertNotNull("Namespace descriptor found null.", ADMIN.getNamespaceDescriptor(nsp));
    TableName tableName1 = TableName.valueOf(nsp + TableName.NAMESPACE_DELIM + "table1");
    HTableDescriptor tableDescOne = new HTableDescriptor(tableName1);
    ADMIN.createTable(tableDescOne, Bytes.toBytes("AAA"), Bytes.toBytes("ZZZ"), 4);

    NamespaceTableAndRegionInfo nstate = getNamespaceState(nsp);
    assertEquals("Intial region count should be 4.", 4, nstate.getRegionCount());

    String snapshot = "snapshot_testRestoreSnapshot";
    ADMIN.snapshot(snapshot, tableName1);

    List<HRegionInfo> regions = ADMIN.getTableRegions(tableName1);
    Collections.sort(regions);

    ADMIN.split(tableName1, Bytes.toBytes("JJJ"));
    Thread.sleep(2000);
    assertEquals("Total regions count should be 5.", 5, nstate.getRegionCount());

    ADMIN.disableTable(tableName1);
    ADMIN.restoreSnapshot(snapshot);

    assertEquals("Total regions count should be 4 after restore.", 4, nstate.getRegionCount());

    ADMIN.enableTable(tableName1);
    ADMIN.deleteSnapshot(snapshot);
  }
  @Test
  public void testBasicUpsertSelect() throws Exception {
    Connection conn = DriverManager.getConnection(PHOENIX_JDBC_TENANT_SPECIFIC_URL);
    try {
      conn.setAutoCommit(false);
      conn.createStatement()
          .executeUpdate(
              "upsert into "
                  + TENANT_TABLE_NAME
                  + " (id, tenant_col) values (1, 'Cheap Sunglasses')");
      conn.createStatement()
          .executeUpdate(
              "upsert into "
                  + TENANT_TABLE_NAME
                  + " (id, tenant_col) values (2, 'Viva Las Vegas')");
      conn.commit();

      ResultSet rs =
          conn.createStatement()
              .executeQuery("select tenant_col from " + TENANT_TABLE_NAME + " where id = 1");
      assertTrue("Expected 1 row in result set", rs.next());
      assertEquals("Cheap Sunglasses", rs.getString(1));
      assertFalse("Expected 1 row in result set", rs.next());

      // ensure we didn't create a physical HBase table for the tenant-specific table
      HBaseAdmin admin = driver.getConnectionQueryServices(getUrl(), TEST_PROPERTIES).getAdmin();
      assertEquals(0, admin.listTables(TENANT_TABLE_NAME).length);
    } finally {
      conn.close();
    }
  }
Exemple #16
0
 /**
  * Drops table in HBase when it exists.
  *
  * @param conf
  * @throws IOException
  */
 public void dropTable() throws IOException {
   if (exists()) {
     HBaseAdmin admin = new HBaseAdmin(getConfiguration());
     admin.disableTable(getStringName());
     admin.deleteTable(getStringName());
   }
 }
Exemple #17
0
  /*
   * Should we check all assignments or just not in RIT?
   */
  public static void waitUntilAssigned(HBaseAdmin admin, HRegionInfo region)
      throws IOException, InterruptedException {
    long timeout = admin.getConfiguration().getLong("hbase.hbck.assign.timeout", 120000);
    long expiration = timeout + System.currentTimeMillis();
    while (System.currentTimeMillis() < expiration) {
      try {
        Map<String, RegionState> rits = admin.getClusterStatus().getRegionsInTransition();

        if (rits.keySet() != null && !rits.keySet().contains(region.getEncodedName())) {
          // yay! no longer RIT
          return;
        }
        // still in rit
        LOG.info("Region still in transition, waiting for " + "it to become assigned: " + region);
      } catch (IOException e) {
        LOG.warn("Exception when waiting for region to become assigned," + " retrying", e);
      }
      Thread.sleep(1000);
    }
    throw new IOException(
        "Region "
            + region
            + " failed to move out of "
            + "transition within timeout "
            + timeout
            + "ms");
  }
Exemple #18
0
  /**
   * @param table
   * @return
   */
  public boolean createTable(JSONObject table) {

    try {
      String table_name = "";
      if (!table.has(XHBaseConstant.TABLE_DESC_NAME)) return false;

      table_name = (String) table.get(XHBaseConstant.TABLE_DESC_NAME);
      if (table_name.isEmpty()) return false;

      if (admin.tableExists(table_name)) {
        admin.disableTable(table_name);
        admin.deleteTable(table_name);
      }

      HTableDescriptor td = this.createTableDescription(table);
      this.admin.createTable(td);

      return true;

    } catch (Exception e) {
      e.printStackTrace();
    }

    return false;
  }
 @Test
 public void testInvalidColumnFamily() throws IOException, InterruptedException {
   byte[] table = Bytes.toBytes("testInvalidColumnFamily");
   byte[] family = Bytes.toBytes("family");
   byte[] fakecf = Bytes.toBytes("fakecf");
   boolean caughtMinorCompact = false;
   boolean caughtMajorCompact = false;
   Table ht = null;
   try {
     ht = TEST_UTIL.createTable(table, family);
     HBaseAdmin admin = new HBaseAdmin(TEST_UTIL.getConfiguration());
     try {
       admin.compact(table, fakecf);
     } catch (IOException ioe) {
       caughtMinorCompact = true;
     }
     try {
       admin.majorCompact(table, fakecf);
     } catch (IOException ioe) {
       caughtMajorCompact = true;
     }
   } finally {
     if (ht != null) {
       TEST_UTIL.deleteTable(table);
     }
     assertTrue(caughtMinorCompact);
     assertTrue(caughtMajorCompact);
   }
 }
Exemple #20
0
 /**
  * Scans the table and merges two adjacent regions if they are small. This only happens when a lot
  * of rows are deleted.
  *
  * <p>When merging the META region, the HBase instance must be offline. When merging a normal
  * table, the HBase instance must be online, but the table must be disabled.
  *
  * @param conf - configuration object for HBase
  * @param fs - FileSystem where regions reside
  * @param tableName - Table to be compacted
  * @param testMasterRunning True if we are to verify master is down before running merge
  * @throws IOException
  */
 public static void merge(
     Configuration conf, FileSystem fs, final byte[] tableName, final boolean testMasterRunning)
     throws IOException {
   boolean masterIsRunning = false;
   if (testMasterRunning) {
     masterIsRunning =
         HConnectionManager.execute(
             new HConnectable<Boolean>(conf) {
               @Override
               public Boolean connect(HConnection connection) throws IOException {
                 return connection.isMasterRunning();
               }
             });
   }
   if (Bytes.equals(tableName, HConstants.META_TABLE_NAME)) {
     if (masterIsRunning) {
       throw new IllegalStateException("Can not compact META table if instance is on-line");
     }
     new OfflineMerger(conf, fs).process();
   } else {
     if (!masterIsRunning) {
       throw new IllegalStateException("HBase instance must be running to merge a normal table");
     }
     HBaseAdmin admin = new HBaseAdmin(conf);
     if (!admin.isTableDisabled(tableName)) {
       throw new TableNotDisabledException(tableName);
     }
     new OnlineMerger(conf, fs, tableName).process();
   }
 }
  private byte[][] createTable(HTableDescriptor tableDesc) {

    HBaseAdmin admin = HBaseConfigurationManager.getHbaseAdmin();

    try {

      admin.createTable(tableDesc);

    } catch (TableExistsException e) {
      int sel =
          JOptionPane.showConfirmDialog(
              this,
              "Table Already Exist, Add Data ???",
              "Warning!",
              JOptionPane.YES_NO_OPTION,
              JOptionPane.WARNING_MESSAGE);

      if (sel == JOptionPane.NO_OPTION) {
        this.dispose();
      }

    } catch (IOException ex) {
      JOptionPane.showMessageDialog(
          this, "Table Creation Failed", "Error", JOptionPane.ERROR_MESSAGE);
      Logger.getLogger(HBaseManagerTableDesign.class.getName()).log(Level.SEVERE, null, ex);
    }

    byte[][] families = null;

    Set<byte[]> fams = tableDesc.getFamiliesKeys();
    families = new byte[fams.size()][];
    fams.toArray(families);

    return families;
  }
Exemple #22
0
  /*
   * 删除表
   *
   * @tableName 表名
   */
  public static void deleteTable(String tableName) throws IOException {

    @SuppressWarnings("resource")
    HBaseAdmin admin = new HBaseAdmin(conf);
    admin.disableTable(tableName);
    admin.deleteTable(tableName);
    System.out.println(tableName + "is deleted!");
  }
 public static void CreateTable(HBaseConfiguration conf) throws IOException {
   HBaseAdmin admin = new HBaseAdmin(conf);
   HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("people"));
   tableDescriptor.addFamily(new HColumnDescriptor("personal"));
   tableDescriptor.addFamily(new HColumnDescriptor("contactinfo"));
   tableDescriptor.addFamily(new HColumnDescriptor("creditcard"));
   admin.createTable(tableDescriptor);
 }
  private void initTable() throws IOException {
    HBaseAdmin admin = new HBaseAdmin(new HBaseConfiguration());

    if (!admin.tableExists(TABLE_NAME)) {
      throw new RuntimeException("Table not created. Call createTable() first");
    }
    this.table = new HTable(TABLE_NAME);
  }
 /**
  * delete table in preparation for next test
  *
  * @param tablename
  * @throws IOException
  */
 void deleteTable(HBaseAdmin admin, String tablename) throws IOException {
   try {
     byte[] tbytes = Bytes.toBytes(tablename);
     admin.disableTable(tbytes);
     admin.deleteTable(tbytes);
   } catch (Exception e) {
     // Do nothing.
   }
 }
  static void createTable(String tableName, String colFams, Configuration conf) throws IOException {

    HBaseAdmin hbase = new HBaseAdmin(conf);
    HTableDescriptor desc = new HTableDescriptor(tableName);
    HColumnDescriptor meta = new HColumnDescriptor(colFams.getBytes());
    desc.addFamily(meta);
    hbase.createTable(desc);
    table = new HTable(conf, tableName);
  }
Exemple #27
0
 protected void dropTable(String tableName) throws IOException {
   if (admin.tableExists(tableName)) {
     try {
       admin.disableTable(tableName);
     } catch (TableNotEnabledException ignored) {
     }
     admin.deleteTable(tableName);
   }
 }
 /**
  * 删除表
  *
  * @param tableName 表名
  */
 public void deleteTable(String tableName) {
   try {
     HBaseAdmin admin = new HBaseAdmin(conf);
     admin.disableTable(tableName);
     admin.deleteTable(tableName);
   } catch (Exception e) {
     logger.error("deleteTable failed", e);
   }
 }
 /**
  * Test things basically work.
  *
  * @param args
  * @throws IOException
  */
 public static void main(String[] args) throws IOException {
   Configuration conf = HBaseConfiguration.create();
   LocalHBaseCluster cluster = new LocalHBaseCluster(conf);
   cluster.startup();
   HBaseAdmin admin = new HBaseAdmin(conf);
   HTableDescriptor htd = new HTableDescriptor(Bytes.toBytes(cluster.getClass().getName()));
   admin.createTable(htd);
   cluster.shutdown();
 }
 @Test
 public void testCreateTenantSpecificTable() throws Exception {
   // ensure we didn't create a physical HBase table for the tenant-specific table
   HBaseAdmin admin =
       driver
           .getConnectionQueryServices(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES))
           .getAdmin();
   assertEquals(0, admin.listTables(TENANT_TABLE_NAME).length);
 }