Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

Hbase Java API fails in Cloudera VM

Hbase Java API fails in Cloudera VM

New Contributor

I am working on Cloudera VM 5.4.0 and have created a simple Hbase progran which compiles fine. The program is as below . 

 

package cloudera.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;


public class hbaseput {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, "testtable");
Put put = new Put(Bytes.toBytes("row1"));
put.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),
Bytes.toBytes("val1"));
put.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2"),
Bytes.toBytes("val2"));
table.put(put);
}
}

 

I then try to compile the .java file in the Cloudera VM . Hbase is running fine ( checked from the Staus command in Hbase shell ) . Compiling code is as below .

 

javac -cp `hbase classpath` hbaseput.java.

 

Problem is that after running this there is no new table created in Hbase when viewed through the Hbase Shell . It also does not give an error after execution. 

 

 

 

 

 

 

1 REPLY 1
Highlighted

Re: Hbase Java API fails in Cloudera VM

Rising Star

There are changes in HBase 1.0.0 - below is some code that should work for creating and deleting a table.

See slide 34 and on in this slide presentation: http://www.slideshare.net/enissoz/meet-hbase-10

 

Sample code:

 

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HBaseConfiguration;

import org.apache.hadoop.hbase.client.HBaseAdmin;

import org.apache.hadoop.hbase.HTableDescriptor;

import org.apache.hadoop.hbase.TableName;

import org.apache.hadoop.hbase.HColumnDescriptor;

import org.apache.hadoop.hbase.client.HTable;

import org.apache.hadoop.hbase.client.Put;

import org.apache.hadoop.hbase.util.Bytes;

import org.apache.hadoop.hbase.client.Connection;

import org.apache.hadoop.hbase.client.ConnectionFactory;

 

public class createDeleteTbl {

public static void main (String[] args) throws IOException {

 

   Configuration configuration = new Configuration();

   Connection conn = ConnectionFactory.createConnection(configuration);

   HBaseAdmin admin = (HBaseAdmin)conn.getAdmin();

 

   String oneTable = "tstTable";

   HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(oneTable));

   HColumnDescriptor columnDescriptor = new HColumnDescriptor(Bytes.toBytes("desc"));

   descriptor.addFamily(columnDescriptor);

 

   System.out.println("\nCreating table " + oneTable + "\n");

   admin.createTable(descriptor);

 

   HTableDescriptor desc = admin.getTableDescriptor(Bytes.toBytes(oneTable));

   System.out.println("\nDetails about table " + oneTable + "\n");

   System.out.println(desc + "\n");

 

   System.out.println("Preparing to delete table " + oneTable + "\n");

 

   System.out.println("... Disabling HBase table: " + oneTable + "\n");

   admin.disableTable(oneTable);

   Boolean isDisabled = admin.isTableDisabled(oneTable);

 

   if (true == isDisabled) {

     System.out.println("... Table " + oneTable + " successfully disabled.\n");

     System.out.println("... Deleting table " + oneTable + "\n");

     admin.deleteTable(oneTable);

     System.out.println("\nTable: " + oneTable + " successfully deleted.\n");

   }

   else {

     System.out.println("ERROR: Table " + oneTable + " disable failed.\n");

     System.out.println("\n");

   }

 

   admin.close();

   }

}