Beispiel #1
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());
     }
   }
 }
Beispiel #2
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);
   }
 }
Beispiel #3
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());
   }
 }
Beispiel #4
0
 /** alter用于修改表明。需要新旧两个表明作为参数。 */
 @Override
 public Builder alter() {
   if (type == 2) {
     // 修改表名。snapshot
     final String snapshotName = "snapShotName";
     try {
       if (!hBaseAdmin.tableExists(tableName)
           || tableName == null
           || tableName.trim().equals("")) {
         Log.error("=== LiMingji Debug Info === TableName is invalid");
         return this;
       }
       if (newTableName == null || newTableName.trim().equals("")) {
         Log.error("=== LiMingji error Info === newTableName is invalid");
         return this;
       }
       if (!hBaseAdmin.isTableDisabled(tableName)) hBaseAdmin.disableTable(tableName);
       // 删除可能存在的snapShotName
       try {
         hBaseAdmin.deleteSnapshot(snapshotName);
       } catch (IOException e) {
         // Do nothing.
       }
       hBaseAdmin.snapshot(snapshotName, tableName);
       hBaseAdmin.cloneSnapshot(snapshotName, newTableName);
       hBaseAdmin.deleteSnapshot(snapshotName);
       hBaseAdmin.deleteTable(tableName);
     } catch (InterruptedException e) {
       Log.info("=== LiMingji error Info === alter@DTL interrupt");
     } catch (IOException e) {
       Log.error("=== LiMingji error Info === alter@DTL IOException");
     }
   }
   return this;
 }
Beispiel #5
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;
  }
Beispiel #6
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!");
  }
Beispiel #7
0
 protected void dropTable(String tableName) throws IOException {
   if (admin.tableExists(tableName)) {
     try {
       admin.disableTable(tableName);
     } catch (TableNotEnabledException ignored) {
     }
     admin.deleteTable(tableName);
   }
 }
 /**
  * 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.
   }
 }
 /**
  * 删除表
  *
  * @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);
   }
 }
Beispiel #10
0
 public static void main(String... args)
     throws IOException, ClassNotFoundException, InterruptedException {
   UserGroupInformation.loginUserFromKeytab("web_dp", "web_dp.keytab");
   Configuration conf = HBaseConfiguration.create();
   HBaseAdmin admin = new HBaseAdmin(conf);
   if (admin.tableExists(QueryDataListener.TN_BYTES_DOMAIN)) {
     admin.disableTable(QueryDataListener.TN_BYTES_DOMAIN);
     admin.deleteTable(QueryDataListener.TN_BYTES_DOMAIN);
   }
   if (admin.tableExists(QueryDataListener.TN_BYTES_URL)) {
     admin.disableTable(QueryDataListener.TN_BYTES_URL);
     admin.deleteTable(QueryDataListener.TN_BYTES_URL);
   }
   if (admin.tableExists(QueryDataListener.TN_BYTES_QUERY)) {
     admin.disableTable(QueryDataListener.TN_BYTES_QUERY);
     admin.deleteTable(QueryDataListener.TN_BYTES_QUERY);
   }
 }
Beispiel #11
0
 private void deleteTable(final TableName tableName) throws Exception {
   // NOTE: We need a latch because admin is not sync,
   // so the postOp coprocessor method may be called after the admin operation returned.
   MasterSyncObserver observer =
       (MasterSyncObserver)
           UTIL.getHBaseCluster()
               .getMaster()
               .getMasterCoprocessorHost()
               .findCoprocessor(MasterSyncObserver.class.getName());
   ADMIN.deleteTable(tableName);
   observer.tableDeletionLatch.await();
 }
 @Test
 public void testTableExists() throws IOException {
   final String name = "testTableExists";
   final byte[] nameBytes = Bytes.toBytes(name);
   assertFalse(MetaReader.tableExists(ct, name));
   UTIL.createTable(nameBytes, HConstants.CATALOG_FAMILY);
   assertTrue(MetaReader.tableExists(ct, name));
   HBaseAdmin admin = UTIL.getHBaseAdmin();
   admin.disableTable(name);
   admin.deleteTable(name);
   assertFalse(MetaReader.tableExists(ct, name));
   assertTrue(MetaReader.tableExists(ct, Bytes.toString(HConstants.META_TABLE_NAME)));
   assertTrue(MetaReader.tableExists(ct, Bytes.toString(HConstants.ROOT_TABLE_NAME)));
 }
 private static void deleteTables() {
   if (admin != null) {
     for (TableName tableName : createdTables) {
       try {
         if (admin.tableExists(tableName)) {
           admin.disableTable(tableName);
           admin.deleteTable(tableName);
         }
       } catch (IOException e) {
         assertNull("Exception found deleting the table", e);
       }
     }
   }
 }
Beispiel #14
0
 @BeforeClass
 public static void doBeforeTestSetup() throws Exception {
   HBaseAdmin admin = driver.getConnectionQueryServices(getUrl(), TEST_PROPERTIES).getAdmin();
   try {
     try {
       admin.disableTable(HBASE_DYNAMIC_COLUMNS_BYTES);
       admin.deleteTable(HBASE_DYNAMIC_COLUMNS_BYTES);
     } catch (org.apache.hadoop.hbase.TableNotFoundException e) {
     }
     ensureTableCreated(getUrl(), HBASE_DYNAMIC_COLUMNS);
     initTableValues();
   } finally {
     admin.close();
   }
 }
Beispiel #15
0
  // 创建一张表,通过HBaseAdmin HTableDescriptor来创建
  public static void creat(String tablename, String columnFamily) throws Exception {

    HBaseAdmin admin = new HBaseAdmin(configuration);
    if (admin.tableExists(tablename)) {
      try {
        admin.disableTable(tablename);
        admin.deleteTable(tablename);
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    }
    HTableDescriptor tableDesc = new HTableDescriptor(tablename.valueOf(tablename));
    tableDesc.addFamily(new HColumnDescriptor(columnFamily));
    admin.createTable(tableDesc);
    admin.close();
    System.out.println("create table success!");
  }
Beispiel #16
0
 private static void clearHBase(Configuration conf) throws IOException {
   HBaseAdmin admin = new HBaseAdmin(conf);
   if (admin.tableExists(table_bytes)) {
     try {
       admin.disableTable(table_bytes);
     } catch (Exception ex) {
     }
     try {
       admin.deleteTable(table_bytes);
     } catch (Exception ex) {
     }
   }
   HTableDescriptor tdesc = new HTableDescriptor(table_bytes);
   HColumnDescriptor cdesc = new HColumnDescriptor(colfam0_bytes);
   tdesc.addFamily(cdesc);
   admin.createTable(tdesc);
 }
  @Before
  public void before() throws IOException {
    HBaseAdmin admin = new HBaseAdmin(configuration);

    boolean exists = admin.tableExists(TABLE_NAME);
    if (exists) {
      admin.disableTable(TABLE_NAME);
      admin.deleteTable(TABLE_NAME);
    }

    // tworzenie tabeli HBase
    HTableDescriptor table = new HTableDescriptor(TABLE_NAME);
    table.addFamily(new HColumnDescriptor(FAMILY_NAME));
    table.addFamily(new HColumnDescriptor(LoadMovieData.FAMILY_NAME));
    table.addFamily(new HColumnDescriptor(LoadMovieRatingData.FAMILY_NAME));

    admin.createTable(table);
  }
 /**
  * Uses SchemaTool to register the required schemas and create the required tables.
  *
  * @param conf The HBaseConfiguration.
  * @param schemaManager The schema manager SchemaTool needs to create the schemas.
  */
 private void registerSchemas(Configuration conf, SchemaManager schemaManager) {
   HBaseAdmin admin;
   try {
     // Construct an HBaseAdmin object (required by schema tool), and delete it
     // if it exists so we start fresh.
     admin = new HBaseAdmin(conf);
     if (admin.tableExists("cdk_example_user_profiles")) {
       admin.disableTable("cdk_example_user_profiles");
       admin.deleteTable("cdk_example_user_profiles");
     }
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
   // Use the SchemaTool to create the schemas that are in the example-models
   // directory, and create the table and column families required by those
   // schemas.
   SchemaTool tool = new SchemaTool(admin, schemaManager);
   tool.createOrMigrateSchemaDirectory("classpath:example-models", true);
 }
 public static void deleteTableTest(String tableStr) {
   try {
     Configuration conf = HBaseConfiguration.create();
     byte[] tableName = Bytes.toBytes(tableStr);
     if (conf == null) {
       throw new IOException("conf not set.");
     } else {
       HBaseAdmin admin = new HBaseAdmin(conf);
       if (admin.tableExists(tableName)) {
         admin.disableTable(tableName); // 关闭一个表
         admin.deleteTable(tableName); // 删除一个表
       } else {
         throw new IOException("no such table.");
       }
     }
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
 @BeforeClass
 public static void doBeforeTestSetup() throws Exception {
   HBaseAdmin admin = driver.getConnectionQueryServices(getUrl(), TEST_PROPERTIES).getAdmin();
   try {
     try {
       admin.disableTable(HBASE_NATIVE_BYTES);
       admin.deleteTable(HBASE_NATIVE_BYTES);
     } catch (org.apache.hadoop.hbase.TableNotFoundException e) {
     }
     HTableDescriptor descriptor = new HTableDescriptor(HBASE_NATIVE_BYTES);
     HColumnDescriptor columnDescriptor = new HColumnDescriptor(FAMILY_NAME);
     columnDescriptor.setKeepDeletedCells(true);
     descriptor.addFamily(columnDescriptor);
     admin.createTable(descriptor, SPLITS);
     initTableValues();
   } finally {
     admin.close();
   }
 }
Beispiel #21
0
  public HTable createTable(String tableName, String[] metrics, int[] max_version)
      throws IOException {
    System.out.println("create table for " + tableName);
    try {
      if (admin.tableExists(tableName)) {
        System.out.println(admin.listTables());
        admin.disableTable(tableName);
        admin.deleteTable(tableName);
      }
      HTableDescriptor td = this.createTableDescription(tableName, metrics, max_version);
      System.out.println(tableName + ": <=>table descirption : " + td.toString());
      this.admin.createTable(td);
      // this.admin.createTable(td, ("0-").getBytes(), ("2-"+Long.MAX_VALUE).getBytes(),80);

    } catch (Exception e) {
      e.printStackTrace();
      // log.info(e.fillInStackTrace());
    }
    return new HTable(conf, tableName);
  }
  @AfterClass
  public static void tearDownAfterClass() throws Exception {
    if (lilyProxy != null) lilyProxy.stop();
    TestHomeUtil.cleanupTestHome(tmpDir);

    if (lilyProxy.getMode() == LilyProxy.Mode.CONNECT
        || lilyProxy.getMode() == LilyProxy.Mode.HADOOP_CONNECT) {
      // We're in connect mode, drop the tables again so that the remainder of the tests
      // don't have the overhead of the extra splits
      Configuration conf = HBaseConfiguration.create();
      conf.set("hbase.zookeeper.quorum", "localhost");
      HBaseAdmin hbaseAdmin = new HBaseAdmin(conf);
      for (String tableName : TABLE_NAMES) {
        if (hbaseAdmin.tableExists(tableName)) {
          hbaseAdmin.disableTable(tableName);
          hbaseAdmin.deleteTable(tableName);
        }
      }
      HConnectionManager.deleteConnection(hbaseAdmin.getConfiguration(), true);
    }
  }
Beispiel #23
0
 /** 删除给定的表。 */
 @Override
 public Builder delete() {
   if (type == 1) {
     try {
       if (tableName == null || tableName.trim().equals("")) {
         Log.info("=== LiMingji Error Info === No tableName");
         return this;
       }
       if (hBaseAdmin.tableExists(tableName)) {
         hBaseAdmin.disableTable(tableName);
         hBaseAdmin.deleteTable(tableName);
       } else {
         Log.info("=== LiMingji Debug Info === Table does not exit");
         return this;
       }
     } catch (IOException e) {
       Log.info("=== LiMingji Debug Info === deleteTable@DDL IOException");
       e.printStackTrace();
     }
   }
   return this;
 }
  @BeforeClass
  public static void setUpBeforeClass() throws Exception {
    lilyProxy = new LilyProxy();

    //
    // Make multiple record table splits
    //

    if (lilyProxy.getMode() == LilyProxy.Mode.CONNECT
        || lilyProxy.getMode() == LilyProxy.Mode.HADOOP_CONNECT) {
      // The tables will likely already exist and not be recreated, hence we won't be able to change
      // the number of regions. Therefore, drop them.
      Configuration conf = HBaseConfiguration.create();
      conf.set("hbase.zookeeper.quorum", "localhost");
      HBaseAdmin hbaseAdmin = new HBaseAdmin(conf);
      for (String tableName : TABLE_NAMES) {
        if (hbaseAdmin.tableExists(tableName)) {
          hbaseAdmin.disableTable(tableName);
          hbaseAdmin.deleteTable(tableName);
        }
      }
      HConnectionManager.deleteConnection(hbaseAdmin.getConfiguration(), true);
    }

    // Temp dir where we will create conf dir
    tmpDir = TestHomeUtil.createTestHome("lily-tablesplit-test-");

    File customConfDir = setupConfDirectory(tmpDir);
    String oldCustomConfDir = setProperty("lily.conf.customdir", customConfDir.getAbsolutePath());
    String oldRestoreTemplate = setProperty("lily.lilyproxy.restoretemplatedir", "false");

    try {
      lilyProxy.start();
    } finally {
      // Make sure the properties won't be used by later-running tests
      setProperty("lily.conf.customdir", oldCustomConfDir);
      setProperty("lily.lilyproxy.restoretemplatedir", oldRestoreTemplate);
    }
  }
Beispiel #25
0
 public static void deleteTable(String keyspace) throws Exception {
   Configuration conf = HBaseConfiguration.create();
   HBaseAdmin admin = new HBaseAdmin(conf);
   admin.deleteTable(keyspace);
 }
  /**
   * Tests overriding compaction handling via coprocessor hooks
   *
   * @throws Exception
   */
  @Test
  public void testCompactionOverride() throws Exception {
    byte[] compactTable = Bytes.toBytes("TestCompactionOverride");
    HBaseAdmin admin = util.getHBaseAdmin();
    if (admin.tableExists(compactTable)) {
      admin.disableTable(compactTable);
      admin.deleteTable(compactTable);
    }

    HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(compactTable));
    htd.addFamily(new HColumnDescriptor(A));
    htd.addCoprocessor(EvenOnlyCompactor.class.getName());
    admin.createTable(htd);

    HTable table = new HTable(util.getConfiguration(), compactTable);
    for (long i = 1; i <= 10; i++) {
      byte[] iBytes = Bytes.toBytes(i);
      Put put = new Put(iBytes);
      put.setDurability(Durability.SKIP_WAL);
      put.add(A, A, iBytes);
      table.put(put);
    }

    HRegion firstRegion = cluster.getRegions(compactTable).get(0);
    Coprocessor cp =
        firstRegion.getCoprocessorHost().findCoprocessor(EvenOnlyCompactor.class.getName());
    assertNotNull("EvenOnlyCompactor coprocessor should be loaded", cp);
    EvenOnlyCompactor compactor = (EvenOnlyCompactor) cp;

    // force a compaction
    long ts = System.currentTimeMillis();
    admin.flush(compactTable);
    // wait for flush
    for (int i = 0; i < 10; i++) {
      if (compactor.lastFlush >= ts) {
        break;
      }
      Thread.sleep(1000);
    }
    assertTrue("Flush didn't complete", compactor.lastFlush >= ts);
    LOG.debug("Flush complete");

    ts = compactor.lastFlush;
    admin.majorCompact(compactTable);
    // wait for compaction
    for (int i = 0; i < 30; i++) {
      if (compactor.lastCompaction >= ts) {
        break;
      }
      Thread.sleep(1000);
    }
    LOG.debug("Last compaction was at " + compactor.lastCompaction);
    assertTrue("Compaction didn't complete", compactor.lastCompaction >= ts);

    // only even rows should remain
    ResultScanner scanner = table.getScanner(new Scan());
    try {
      for (long i = 2; i <= 10; i += 2) {
        Result r = scanner.next();
        assertNotNull(r);
        assertFalse(r.isEmpty());
        byte[] iBytes = Bytes.toBytes(i);
        assertArrayEquals("Row should be " + i, r.getRow(), iBytes);
        assertArrayEquals("Value should be " + i, r.getValue(A, A), iBytes);
      }
    } finally {
      scanner.close();
    }
    table.close();
  }
 /**
  * Deletes a HBase table.
  *
  * @param admin HBase admin
  * @param tableDescriptor descriptor for the table to delete
  * @throws IOException
  */
 private static void deleteTable(final HBaseAdmin admin, final HTableDescriptor tableDescriptor)
     throws IOException {
   LOG.info("deleting table: " + tableDescriptor.getNameAsString());
   admin.disableTable(tableDescriptor.getName());
   admin.deleteTable(tableDescriptor.getName());
 }
  @Test
  public void testHostRank() throws Exception {

    if (System.getProperty("prop.mapred.job.tracker") != null) {
      if (LOG.isInfoEnabled())
        LOG.info("testHBaseInputOutput: Ignore this test if not local mode.");
      return;
    }

    File jarTest = new File(System.getProperty("prop.jarLocation"));
    if (!jarTest.exists()) {
      fail(
          "Could not find Giraph jar at "
              + "location specified by 'prop.jarLocation'. "
              + "Make sure you built the main Giraph artifact?.");
    }

    MiniHBaseCluster cluster = null;
    MiniZooKeeperCluster zkCluster = null;
    FileSystem fs = null;

    try {
      // using the restart method allows us to avoid having the hbase
      // root directory overwritten by /home/$username
      zkCluster = testUtil.startMiniZKCluster();
      testUtil.restartHBaseCluster(2);
      cluster = testUtil.getMiniHBaseCluster();

      final byte[] OL_BYTES = Bytes.toBytes("ol");
      final byte[] S_BYTES = Bytes.toBytes("s");
      final byte[] METADATA_BYTES = Bytes.toBytes("mtdt");
      final byte[] HR_BYTES = Bytes.toBytes("_hr_");
      final byte[] TAB = Bytes.toBytes(TABLE_NAME);

      Configuration conf = cluster.getConfiguration();
      HTableDescriptor desc = new HTableDescriptor(TAB);
      desc.addFamily(new HColumnDescriptor(OL_BYTES));
      desc.addFamily(new HColumnDescriptor(S_BYTES));
      desc.addFamily(new HColumnDescriptor(METADATA_BYTES));
      HBaseAdmin hbaseAdmin = new HBaseAdmin(conf);
      if (hbaseAdmin.isTableAvailable(TABLE_NAME)) {
        hbaseAdmin.disableTable(TABLE_NAME);
        hbaseAdmin.deleteTable(TABLE_NAME);
      }
      hbaseAdmin.createTable(desc);

      /**
       * Enter the initial data (a,b), (b,c), (a,c) a = 1.0 - google b = 1.0 - yahoo c = 1.0 - bing
       */
      HTable table = new HTable(conf, TABLE_NAME);

      Put p1 = new Put(Bytes.toBytes("com.google.www"));
      p1.add(OL_BYTES, Bytes.toBytes("www.yahoo.com"), Bytes.toBytes("ab"));

      Put p2 = new Put(Bytes.toBytes("com.google.www"));
      p2.add(OL_BYTES, Bytes.toBytes("www.bing.com"), Bytes.toBytes("ac"));
      p2.add(OL_BYTES, Bytes.toBytes("www.bing.com"), Bytes.toBytes("invalid1"));
      p2.add(OL_BYTES, Bytes.toBytes("www.google.com"), Bytes.toBytes("invalid2"));

      Put p3 = new Put(Bytes.toBytes("com.yahoo.www"));
      p3.add(OL_BYTES, Bytes.toBytes("www.bing.com"), Bytes.toBytes("bc"));
      // p3.add(OL_BYTES, Bytes.toBytes(""), Bytes.toBytes("invalid4"));

      Put p4 = new Put(Bytes.toBytes("com.bing.www"));
      // TODO: Handle below case. use apache isValid method.
      p4.add(OL_BYTES, Bytes.toBytes("http://invalidurl"), Bytes.toBytes("invalid5"));
      p4.add(S_BYTES, S_BYTES, Bytes.toBytes(10.0d));

      Put p5 = new Put(Bytes.toBytes("dummy"));
      p5.add(S_BYTES, S_BYTES, Bytes.toBytes(10.0d));

      table.put(p1);
      table.put(p2);
      table.put(p3);
      table.put(p4);
      table.put(p5);

      // Set Giraph configuration
      // now operate over HBase using Vertex I/O formats
      conf.set(TableInputFormat.INPUT_TABLE, TABLE_NAME);
      conf.set(TableOutputFormat.OUTPUT_TABLE, TABLE_NAME);

      // Start the giraph job
      GiraphJob giraphJob = new GiraphJob(conf, BspCase.getCallingMethodName());
      GiraphConfiguration giraphConf = giraphJob.getConfiguration();
      giraphConf.setZooKeeperConfiguration(cluster.getMaster().getZooKeeper().getQuorum());
      setupConfiguration(giraphJob);
      giraphConf.setComputationClass(LinkRankComputation.class);
      giraphConf.setMasterComputeClass(LinkRankVertexMasterCompute.class);
      giraphConf.setOutEdgesClass(ByteArrayEdges.class);
      giraphConf.setVertexInputFormatClass(Nutch2HostInputFormat.class);
      giraphConf.setVertexOutputFormatClass(Nutch2HostOutputFormat.class);
      giraphConf.setInt("giraph.linkRank.superstepCount", 10);
      giraphConf.setInt("giraph.linkRank.scale", 10);
      giraphConf.set("giraph.linkRank.family", "mtdt");
      giraphConf.set("giraph.linkRank.qualifier", "_hr_");
      giraphConf.setVertexInputFilterClass(HostRankVertexFilter.class);
      assertTrue(giraphJob.run(true));

      if (LOG.isInfoEnabled()) LOG.info("Giraph job successful. Checking output qualifier.");

      /** Check the results * */
      Result result;
      String key;
      byte[] calculatedScoreByte;
      HashMap expectedValues = new HashMap<String, Double>();
      expectedValues.put("com.google.www", 1.3515060339386287d);
      expectedValues.put("com.yahoo.www", 4.144902009567587d);
      expectedValues.put("com.bing.www", 9.063893290511482d);

      for (Object keyObject : expectedValues.keySet()) {
        key = keyObject.toString();
        result = table.get(new Get(key.getBytes()));
        calculatedScoreByte = result.getValue(METADATA_BYTES, HR_BYTES);
        assertNotNull(calculatedScoreByte);
        assertTrue(calculatedScoreByte.length > 0);
        Assert.assertEquals(
            "Scores are not the same",
            (Double) expectedValues.get(key),
            Bytes.toDouble(calculatedScoreByte),
            DELTA);
      }
    } finally {
      if (cluster != null) {
        cluster.shutdown();
      }
      if (zkCluster != null) {
        zkCluster.shutdown();
      }
      // clean test files
      if (fs != null) {
        fs.delete(hbaseRootdir);
      }
    }
  }
  // Does a Put, Get and a Scan against an hbase table.
  @Test
  public void exampleTest() {

    Configuration config = HBaseConfiguration.create();
    config.set("hbase.zookeeper.quorum", "mulisch");
    HBaseAdmin admin = null;
    try {
      admin = new HBaseAdmin(config);
    } catch (MasterNotRunningException e) {
      e.printStackTrace();
    } catch (ZooKeeperConnectionException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    try {
      // You need a configuration object to tell the client where to connect.
      // When you create a HBaseConfiguration, it reads in whatever you've set
      // into your hbase-site.xml and in hbase-default.xml, as long as these can
      // be found on the CLASSPATH
      // HBaseConfiguration config = new HBaseConfiguration();

      // This instantiates an HTable object that connects you to
      // the "myLittleHBaseTable" table.

      HTableDescriptor ext = new HTableDescriptor("testtable");
      HColumnDescriptor c1 = new HColumnDescriptor("c1");
      HColumnDescriptor c2 = new HColumnDescriptor("c2");

      ext.addFamily(c1);
      ext.addFamily(c2);

      if (!admin.tableExists("testtable")) {
        admin.createTable(ext);
      }

      HTable table = new HTable(config, "testtable");

      // To add to a row, use Put. A Put constructor takes the name of the row
      // you want to insert into as a byte array. In HBase, the Bytes class has
      // utility for converting all kinds of java types to byte arrays. In the
      // below, we are converting the String "myLittleRow" into a byte array to
      // use as a row key for our update. Once you have a Put instance, you can
      // adorn it by setting the names of columns you want to update on the row,
      // the timestamp to use in your update, etc.If no timestamp, the server
      // applies current time to the edits.
      Put p = new Put(Bytes.toBytes("row1"));

      // To set the value you'd like to update in the row 'myLittleRow', specify
      // the column family, column qualifier, and value of the table cell you'd
      // like to update. The column family must already exist in your table
      // schema. The qualifier can be anything. All must be specified as byte
      // arrays as hbase is all about byte arrays. Lets pretend the table
      // 'myLittleHBaseTable' was created with a family 'myLittleFamily'.
      p.add(Bytes.toBytes("c1"), Bytes.toBytes("someQualifier"), Bytes.toBytes("Some Value"));
      p.add(
          Bytes.toBytes("c2"),
          Bytes.toBytes("someOtherQualifier"),
          Bytes.toBytes("Some Other Value"));
      // Once you've adorned your Put instance with all the updates you want to
      // make, to commit it do the following (The HTable#put method takes the
      // Put instance you've been building and pushes the changes you made into
      // hbase)
      table.put(p);

      // Now, to retrieve the data we just wrote. The values that come back are
      // Result instances. Generally, a Result is an object that will package up
      // the hbase return into the form you find most palatable.
      Get g = new Get(Bytes.toBytes("row1"));
      Result r = table.get(g);
      byte[] value = r.getValue(Bytes.toBytes("c1"), Bytes.toBytes("someQualifier"));
      // If we convert the value bytes, we should get back 'Some Value', the
      // value we inserted at this location.
      String valueStr = Bytes.toString(value);
      System.out.println("GET: " + valueStr);

      // Sometimes, you won't know the row you're looking for. In this case, you
      // use a Scanner. This will give you cursor-like interface to the contents
      // of the table. To set up a Scanner, do like you did above making a Put
      // and a Get, create a Scan. Adorn it with column names, etc.
      Scan s = new Scan();
      s.addColumn(Bytes.toBytes("c1"), Bytes.toBytes("someQualifier"));
      ResultScanner scanner = table.getScanner(s);
      try {
        // Scanners return Result instances.
        // Now, for the actual iteration. One way is to use a while loop like so:
        for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
          // print out the row we found and the columns we were looking for
          System.out.println("Found row: " + rr);
        }

        // The other approach is to use a foreach loop. Scanners are iterable!
        // for (Result rr : scanner) {
        // System.out.println("Found row: " + rr);
        // }
      } finally {
        // Make sure you close your scanners when you are done!
        // Thats why we have it inside a try/finally clause
        scanner.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
      fail("failed with IOException");
    }

    try {
      if (admin.tableExists("testtable")) {
        admin.disableTable("testtable");
        admin.deleteTable("testtable");
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Beispiel #30
0
 public void dropTable(String table) throws IOException {
   if (existsTable(table)) {
     disableTable(table);
     admin.deleteTable(table);
   }
 }