Tag Archives: JAVA

How to Create A Table in HBase

Classes used:-

Configuration -to set configuration for hbase like ip address,port,time to wait for establishing a connection
HBaseAdmin – has a createTable(table) method
HTableDescriptor- represents a Hbase Table
HColumnDescriptor-represents a Col. family in a Hbase table

import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.io.hfile.Compression.Algorithm;
import org.apache.hadoop.hbase.util.Bytes;

public class CreateInsert {

public static void main(String[] args) {
String name=”SubscriberDemo”;
try {
createTable(name);
} catch (IOException e) {

e.printStackTrace();
}
}
public static void createTable(String name) throws IOException{
Configuration conf = HBaseConfiguration.create();
conf.setInt(“timeout”, 120000);
conf.set(“hbase.master”,”localhost:60000″);
conf.set(“hbase.zookeeper.quorum”,”localhost”);
conf.set(“hbase.zookeeper.property.clientPort”, “2181”);
HBaseAdmin admin = new HBaseAdmin(conf);
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
byte[] tableName = Bytes.toBytes(name);
HTableDescriptor table = new HTableDescriptor(tableName);
HColumnDescriptor family = new HColumnDescriptor(Bytes.toBytes(“virtualidinfo”));
family.setCompressionType(Algorithm.GZ);
table.addFamily(family);

System.out.println(“Table “+name+” exist: “+admin.tableExists(tableName)) ;
System.out.println(“Creating “+name+” table…”);
admin.createTable(table);
System.out.println(“Table “+name+” exist: “+
admin.tableExists(tableName)) ;
admin.close();
System.out.println(“Done……”);
}
}

Java program to Distribute N elements into Subgroup each with M element

package com.distributor;

import java.util.ArrayList;
import java.util.List;

public class Distributor {
public static void main(String args[]) {
int[] ar = { 1, 2, 3, 4, 5, 6 ,7,8,9};//array holding n elements n=9

int subgrplen = 3;//elements in each sub group m=3
int counter = 0;
int temp = 1;
List<Integer> lstE = new ArrayList<Integer>();
System.out.println(“the size” + lstE.size());

int[] subar = new int[3];

for (int i = 0; (i < ar.length – subgrplen + 1); i++) {
counter = 0;
counter++;

for (int j = i + 1; j <= ar.length – (subgrplen – 1); j++) {
System.out.print(ar[i]);
for (int k = j; counter < subgrplen; k++) {

counter++;

System.out.print(ar[k]);

}
counter = 1;
System.out.println();
}
// System.out.println();
}

}

}

Java Constructors can not be Synchronized

We synchronize a method to ensure that only a single thread can execute the statements in the method at a time  , thread automatically acquires lock on the object used for invoking the synchronize method
Since an object will never exist until and unless the constructor is called and constructor can not be called twice for the same instance,same obect there is no sense of synchronizing a constructor as two threads will never be able to execute it on the same instance ,although we can have a synchronize block inside the constructor

Method local inner class can access only final local variables

A method local inner class can be instantiated only from within the method where it is declared after its defination.Since we know that all the local variables resides on stack which gets blown once the method completes,but the object of inner class resides on heap and we can hold a refernce to it by passing it to some other method as a argument thus even after the method call completes we can have access to instance of method local inner class but no access to local variables

What happens when the local variables are declared final?
when you declare the local variables as final they still reside on stack but this time when the inner class needs the access to final local variables the value of the variable is copied from stack to heap when the inner class is instantiated by declaring local variables  final we are preventing the object from modifying it’s value as there is also a copy present which will be uneffected.

Difference between JRE,JVM and JIT

JRE(java Runtime Enviroment)JRE is Superset of JVM it contains JVM,Class liberaries and other  packages required for running a java program.JVM runs the program and uses class liberaries and other files required provided by the JRE therefore on any system where we want to run the java program we need to have JRE

JVM(Java Virtual machine)
JVM is responsible for converting the byte code to machine code whereas Javac is the java compiler for converting .java to .class that is for compliling the java programs
JVM resides on RAM the class loader component loads the class files and ByteCodeVerifier verifies if there is any access violation

JIT(Just In Time Complier)
JVM uses JIT technology so the bytecode source is only translated once to the native CPU instruction set.