Search code examples
javahbase

java.lang.NoSuchMethodError with HBase Scan.setBatch


I am trying to remove rows from HBase table using batch scan. When I am running this code as a class file, it's running fine. But when I run the code as a JAR, it's giving me below exception:

Exception in thread "main" java.lang.NoSuchMethodError: org.apache.hadoop.hbase.client.Scan.setBatch(I)V

Here is my code:

public class Purge { public static void main(String[] args) throws IOException, InterruptedException {

      if (args.length != 3 ) {
          System.out.println("Incorrect number of arguments");
          System.out.println("Correct Usage: java Purge <table> <column name> <value>");
          System.out.println("Exiting .....");
          System.exit(0);
      }

      String tablename = args[0];
      String column = args[1];
      String value = args[2];
      Configuration conf = HBaseConfiguration.create();
      Connection connection = ConnectionFactory.createConnection(conf);
      Table table = connection.getTable(TableName.valueOf(tablename));

      List<Delete> deleteList = new ArrayList<Delete>();
      Scan scan = new Scan();
      scan.setBatch(100);

      scan.addColumn(Bytes.toBytes("cf1"),Bytes.toBytes(column));
      Filter filter = new ValueFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,new BinaryComparator(Bytes.toBytes(value)));
      scan.setFilter(filter);
      ResultScanner scanner=table.getScanner(scan);
      for (Result rr : scanner) {
          Delete d=new Delete(rr.getRow());
          deleteList.add(d);
      }
      table.delete(deleteList);
}

}

I am using below command line:

HADOOP_CLASSPATH=hbase mapredcp hadoop jar purge.jar Purge "$table" "$column" "$timestamp"


Solution

  • What does it mean "when I use as class" ? Anyway, this exception usually come when some lib are missing: lib that probably were visible during the compilation (so the compilation is successfull), but not when launching the compiled jar, because the enviroment is probably different.

    Probably, it works when launched from an IDE (like eclipse) because the libraries are managed by the eclipse classpath, when you put the .jar outside from the classpath where it was compiled, it do not work because there's some jar missing. Try to find the missing lib and then put them in the classpath.