Ejemplo n.º 1
0
  public static boolean createTableOrOverwrite(String tableName, String[] columnFamily) {
    if (tableName == null && columnFamily == null) {
      return false;
    }
    Connection connection = null;
    try {
      connection = ConnectionFactory.createConnection(config);
      Admin admin = connection.getAdmin();
      if (admin.tableExists(TableName.valueOf(tableName))) {
        admin.disableTable(TableName.valueOf(tableName));
        admin.deleteTable(TableName.valueOf(tableName));
      }
      HTableDescriptor table = new HTableDescriptor(TableName.valueOf(tableName));

      for (String cf : columnFamily) {
        table.addFamily(new HColumnDescriptor(cf));
      }

      admin.createTable(table);
      System.out.println("create table successfully.");
      return true;
    } catch (IOException e) {
      e.printStackTrace();
      return false;
    } finally {
      try {
        connection.close();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }
 private void deleteTable() throws IOException {
   admin.disableTable(tn);
   admin.deleteTable(tn);
   if (!admin.tableExists(tn)) {
     System.out.println(tableName + " is delete !");
   }
 }
Ejemplo n.º 3
0
 @Test
 public void createTableInDefaultNamespace() throws Exception {
   HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("default_table"));
   HColumnDescriptor colDesc = new HColumnDescriptor("cf1");
   desc.addFamily(colDesc);
   admin.createTable(desc);
   assertTrue(admin.listTables().length == 1);
   admin.disableTable(desc.getTableName());
   admin.deleteTable(desc.getTableName());
 }
Ejemplo n.º 4
0
 @After
 public void tearDown() throws IOException {
   LOG.info("Cleaning up after test.");
   Admin admin = util.getHBaseAdmin();
   if (admin.tableExists(TABLE_NAME)) {
     if (admin.isTableEnabled(TABLE_NAME)) admin.disableTable(TABLE_NAME);
     admin.deleteTable(TABLE_NAME);
   }
   LOG.info("Restoring cluster.");
   util.restoreCluster();
   LOG.info("Cluster restored.");
 }
Ejemplo n.º 5
0
 @Before
 public void beforeMethod() throws IOException {
   for (HTableDescriptor desc : admin.listTables(prefix + ".*")) {
     admin.disableTable(desc.getTableName());
     admin.deleteTable(desc.getTableName());
   }
   for (NamespaceDescriptor ns : admin.listNamespaceDescriptors()) {
     if (ns.getName().startsWith(prefix)) {
       admin.deleteNamespace(ns.getName());
     }
   }
 }
Ejemplo n.º 6
0
 @Test
 public void createTableInSystemNamespace() throws Exception {
   TableName tableName = TableName.valueOf("hbase:createTableInSystemNamespace");
   HTableDescriptor desc = new HTableDescriptor(tableName);
   HColumnDescriptor colDesc = new HColumnDescriptor("cf1");
   desc.addFamily(colDesc);
   admin.createTable(desc);
   assertEquals(0, admin.listTables().length);
   assertTrue(admin.tableExists(tableName));
   admin.disableTable(desc.getTableName());
   admin.deleteTable(desc.getTableName());
 }
Ejemplo n.º 7
0
 public boolean removeTable(String tableName) {
   try {
     Admin admin = connection.getAdmin();
     TableName t = TableName.valueOf(tableName);
     if (admin.tableExists(t)) {
       admin.disableTable(t);
       admin.deleteTable(t);
       return true;
     }
   } catch (IOException e) {
     e.printStackTrace();
   }
   return false;
 }
Ejemplo n.º 8
0
  @Before
  public void setUp() throws Exception {
    LOG.info(String.format("Initializing cluster with %d region servers.", REGION_SERVER_COUNT));
    util.initializeCluster(REGION_SERVER_COUNT);
    LOG.info("Cluster initialized");

    Admin admin = util.getHBaseAdmin();
    if (admin.tableExists(TABLE_NAME)) {
      LOG.info(String.format("Deleting existing table %s.", TABLE_NAME));
      if (admin.isTableEnabled(TABLE_NAME)) admin.disableTable(TABLE_NAME);
      admin.deleteTable(TABLE_NAME);
      LOG.info(String.format("Existing table %s deleted.", TABLE_NAME));
    }
    LOG.info("Cluster ready");
  }
Ejemplo n.º 9
0
 public static boolean dropTable(Admin admin, String table) {
   TableName tableName = TableName.valueOf(table);
   try {
     admin.disableTable(tableName);
   } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     return false;
   }
   try {
     admin.deleteTable(tableName);
     return true;
   } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     return false;
   }
 }
Ejemplo n.º 10
0
  @Test
  public void createDoubleTest() throws IOException, InterruptedException {
    String testName = "createDoubleTest";
    String nsName = prefix + "_" + testName;
    LOG.info(testName);

    TableName tableName = TableName.valueOf("my_table");
    TableName tableNameFoo = TableName.valueOf(nsName + ":my_table");
    // create namespace and verify
    admin.createNamespace(NamespaceDescriptor.create(nsName).build());
    TEST_UTIL.createTable(tableName, Bytes.toBytes(nsName));
    TEST_UTIL.createTable(tableNameFoo, Bytes.toBytes(nsName));
    assertEquals(2, admin.listTables().length);
    assertNotNull(admin.getTableDescriptor(tableName));
    assertNotNull(admin.getTableDescriptor(tableNameFoo));
    // remove namespace and verify
    admin.disableTable(tableName);
    admin.deleteTable(tableName);
    assertEquals(1, admin.listTables().length);
  }
    @Override
    void perform() throws IOException {

      HTableDescriptor selected = selectTable(disabledTables);
      if (selected == null) {
        return;
      }

      Admin admin = connection.getAdmin();
      try {
        TableName tableName = selected.getTableName();
        LOG.info("Deleting table :" + selected);
        admin.deleteTable(tableName);
        Assert.assertFalse("Table: " + selected + " was not deleted", admin.tableExists(tableName));
        deletedTables.put(tableName, selected);
        LOG.info("Deleted table :" + selected);
      } catch (Exception e) {
        LOG.warn("Caught exception in action: " + this.getClass());
        throw e;
      } finally {
        admin.close();
      }
      verifyTables();
    }
Ejemplo n.º 12
0
  public boolean doTest()
      throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException,
          IOException, InterruptedException {
    /** create config */
    conf_h = HBaseConfiguration.create();
    conf_h.set("hbase.zookeeper.quorum", "localhost");
    conf_h.set("hbase.zookeeper.property.clientPort", "2181");
    Connection con_h = null;
    try {
      con_h = ConnectionFactory.createConnection(conf_h);
    } catch (IOException e) {
      e.printStackTrace();
    }
    Admin admin = con_h.getAdmin();
    HTableDescriptor tableDesc = new HTableDescriptor(tableName_chi);
    HColumnDescriptor colFamDesc = new HColumnDescriptor("count");
    colFamDesc.setMaxVersions(1);
    tableDesc.addFamily(colFamDesc);
    admin.createTable(tableDesc);

    /** counting and insert in chiTable */
    Scan scan = new Scan();
    scan.addColumn(Bytes.toBytes("products"), Bytes.toBytes("product_category_id"));
    scan.addColumn(Bytes.toBytes("orders"), Bytes.toBytes("order_date"));
    // Creates a new Job with no particular Cluster
    Job job =
        Job.getInstance(conf_h, "Count"); // Job.getInstance(Configuration conf, String JobName)
    job.setJarByClass(
        ChiSquaredTest2_abc.class); // Set the Jar by finding where a given class came from
    // initTableMapperJob(String table, Scan scan, Class<? extends TableMapper> mapper, Class<?>
    // outputKeyClass, Class<?> outputValueClass, org.apache.hadoop.mapreduce.Job job)
    TableMapReduceUtil.initTableMapperJob(
        "retail_order", scan, Map1.class, Text.class, IntWritable.class, job);
    // initTableReducerJob(String table, Class<? extends TableReducer> reducer,
    // org.apache.hadoop.mapreduce.Job job)
    TableMapReduceUtil.initTableReducerJob("chiTable", Reduce1.class, job);

    // boolean waitForCompletion(boolean verbose), verbose - print the progress to the user
    job.waitForCompletion(true); // Submit the job to the cluster and wait for it to finish

    /** extract value from chiTable */
    int totalY = 0;
    int totalN = 0;
    ArrayList<CellOfHTable> chiTable = new ArrayList<CellOfHTable>();
    Table table_h = con_h.getTable(tableName_chi);
    Scan s = new Scan();
    s.addFamily(Bytes.toBytes("count"));
    ResultScanner results = table_h.getScanner(s);
    for (Result r : results) {
      CellOfHTable c =
          new CellOfHTable(
              r.getRow(),
              r.getValue(Bytes.toBytes("count"), Bytes.toBytes("Y")) == null
                  ? Bytes.toBytes(0)
                  : r.getValue(Bytes.toBytes("count"), Bytes.toBytes("Y")),
              r.getValue(Bytes.toBytes("count"), Bytes.toBytes("N")) == null
                  ? Bytes.toBytes(0)
                  : r.getValue(
                      Bytes.toBytes("count"), Bytes.toBytes("N"))); // (id, count_Y, count_N)
      chiTable.add(c);
      totalY = totalY + c.countY;
      totalN = totalN + c.countN;
    }

    results.close();
    table_h.close();
    admin.disableTable(tableName_chi);
    admin.deleteTable(tableName_chi);

    double chisquare = 0.0;
    for (int i = 0; i < chiTable.size(); i++) {
      CellOfHTable c = chiTable.get(i);
      double expectY =
          (double) (c.countY + c.countN) * (double) totalY / (double) (totalY + totalN);
      chisquare =
          chisquare + (((double) c.countY - expectY) * ((double) c.countY - expectY) / expectY);
      double expectN =
          (double) (c.countY + c.countN) * (double) totalN / (double) (totalY + totalN);
      chisquare =
          chisquare + (((double) c.countN - expectN) * ((double) c.countN - expectN) / expectN);
    }

    System.out.println(chisquare);
    ChiSquareDist csd = new ChiSquareDist((chiTable.size() - 1));
    if (chisquare > csd.inverseF(1.0 - alpha)) {
      return true;
    }
    return false;
  }
Ejemplo n.º 13
0
 @After
 public void tearDown() throws Exception {
   admin.disableTable(TableName.valueOf(tableName));
   admin.deleteTable(TableName.valueOf(tableName));
   admin.close();
 }