Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

HBase Example


Let’s see a HBase example to import data of a file in HBase table.

Use Case

We have to import data present in the file into an HBase table by creating it through Java API.

Data_file.txt contains the below data

  1. 1,India,Bihar,Champaran,2009,April,P1,1,5
  2. 2,India, Bihar,Patna,2009,May,P1,2,10
  3. 3,India, Bihar,Bhagalpur,2010,June,P2,3,15
  4. 4,United States,California,Fresno,2009,April,P2,2,5
  5. 5,United States,California,Long Beach,2010,July,P2,4,10
  6. 6,United States,California,San Francisco,2011,August,P1,6,20

The Java code is shown below

This data has to be inputted into a new HBase table to be created through JAVA API. Following column families have to be created

  1. “sample,region,time.product,sale,profit”.

Column family region has three column qualifiers: country, state, city

Column family Time has two column qualifiers: year, month

Jar Files

Make sure that the following jars are present while writing the code as they are required by the HBase.

  1. commons-loging-1.0.4
  2. commons-loging-api-1.0.4
  3. hadoop-core-0.20.2-cdh3u2
  4. hbase-0.90.4-cdh3u2
  5. log4j-1.2.15
  6. zookeper-3.3.3-cdh3u0

Program Code

  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.StringTokenizer;
  6. import org.apache.hadoop.conf.Configuration;
  7. import org.apache.hadoop.hbase.HBaseConfiguration;
  8. import org.apache.hadoop.hbase.HColumnDescriptor;
  9. import org.apache.hadoop.hbase.HTableDescriptor;
  10. import org.apache.hadoop.hbase.client.HBaseAdmin;
  11. import org.apache.hadoop.hbase.client.HTable;
  12. import org.apache.hadoop.hbase.client.Put;
  13. import org.apache.hadoop.hbase.util.Bytes;
  14. public class readFromFile {
  15.     public static void main(String[] args) throws IOException{
  16.         if(args.length==1)
  17.             {
  18.             Configuration conf = HBaseConfiguration.create(new Configuration());
  19.             HBaseAdmin hba = new HBaseAdmin(conf);
  20.             if(!hba.tableExists(args[0])){
  21.                 HTableDescriptor ht = new HTableDescriptor(args[0]);
  22.                 ht.addFamily(new HColumnDescriptor(“sample”));
  23.                 ht.addFamily(new HColumnDescriptor(“region”));
  24.                 ht.addFamily(new HColumnDescriptor(“time”));
  25.                 ht.addFamily(new HColumnDescriptor(“product”));
  26.                 ht.addFamily(new HColumnDescriptor(“sale”));
  27.                 ht.addFamily(new HColumnDescriptor(“profit”));
  28.                 hba.createTable(ht);
  29.                 System.out.println(“New Table Created”);
  30.                 HTable table = new HTable(conf,args[0]);
  31.                 File f = new File(“/home/training/Desktop/data”);
  32.                 BufferedReader br = new BufferedReader(new FileReader(f));
  33.                 String line = br.readLine();
  34.                 int i =1;
  35.                 String rowname=“row”;
  36.                 while(line!=null && line.length()!=0){
  37.                     System.out.println(“Ok till here”);
  38.                     StringTokenizer tokens = new StringTokenizer(line,”,”);
  39.                     rowname = “row”+i;
  40.                     Put p = new Put(Bytes.toBytes(rowname));
  41.                     p.add(Bytes.toBytes(“sample”),Bytes.toBytes(“sampleNo.”),
  42. Bytes.toBytes(Integer.parseInt(tokens.nextToken())));
  43.                     p.add(Bytes.toBytes(“region”),Bytes.toBytes(“country”),Bytes.toBytes(tokens.nextToken()));
  44.                     p.add(Bytes.toBytes(“region”),Bytes.toBytes(“state”),Bytes.toBytes(tokens.nextToken()));
  45.                     p.add(Bytes.toBytes(“region”),Bytes.toBytes(“city”),Bytes.toBytes(tokens.nextToken()));
  46.                     p.add(Bytes.toBytes(“time”),Bytes.toBytes(“year”),Bytes.toBytes(Integer.parseInt(tokens.nextToken())));
  47.                     p.add(Bytes.toBytes(“time”),Bytes.toBytes(“month”),Bytes.toBytes(tokens.nextToken()));
  48.                     p.add(Bytes.toBytes(“product”),Bytes.toBytes(“productNo.”),Bytes.toBytes(tokens.nextToken()));
  49.                     p.add(Bytes.toBytes(“sale”),Bytes.toBytes(“quantity”),Bytes.toBytes(Integer.parseInt(tokens.nextToken())));
  50.                     p.add(Bytes.toBytes(“profit”),Bytes.toBytes(“earnings”),Bytes.toBytes(tokens.nextToken()));
  51.                     i++;
  52.                     table.put(p);
  53.                     line = br.readLine();
  54.                 }
  55.                     br.close();
  56.                     table.close();
  57.                 }
  58.             else
  59.                 System.out.println(“Table Already exists.Please enter another table name”);
  60.         }
  61.         else
  62.             System.out.println(“Please Enter the table name through command line”);
  63.     }
  64. }

Comments

comments