package m7example;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.NavigableMap;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
/*
* command to run the program
java -cp m7example-0.0.1-jar-with-dependencies.jar:`hbase classpath` m7example.RetrieveData2 <tableName>
*/
public class RetrieveData2{
public static void main(String[] args) throws IOException, Exception{
String tableName=null;
if(args.length>0)
{
if(args[0] != null)
{
System.out.println("Table Name is "+args[0]);
tableName=args[0];
}
}
else
{
System.out.println("Please input table name (with full path) as argument");
System.exit(0);
}
// Instantiating Configuration class
Configuration config = HBaseConfiguration.create();
// Instantiating HTable class
//HTable table = new HTable(config, "/path/students");
HTable table = new HTable(config,tableName);
// Instantiating configuration class
Configuration conf = HBaseConfiguration.create();
// Instantiating HBaseAdmin class
HBaseAdmin admin = new HBaseAdmin(conf);
// Verifying the existance of the table
boolean bool = admin.tableExists(tableName);
if(!bool)
{
System.out.println(tableName+" does n't exist");
System.exit(0);
}
Scan scan = new Scan();
//Scan scan = new Scan(Bytes.toBytes("000"),Bytes.toBytes("001")); //use start-row/stop-row if you want to limit the full table scan
ResultScanner scanner = table.getScanner(scan);
ArrayList<String> cf =new ArrayList<String>();
System.out.println("Please be patient, I am doing a full table scan");
System.out.print("=");
for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
System.out.print(".");
//String rowKey= Bytes.toString(rr.getRow());
Iterator<KeyValue> iter = rr.list().iterator();
while (iter.hasNext()) {
KeyValue kv = iter.next();
if(!cf.contains(Bytes.toString(kv.getFamily())))
{
cf.add(Bytes.toString(kv.getFamily()));
System.out.print("=");
}
}//while
}
ArrayList<String> qf[] = new ArrayList[cf.size()]; //Put the length of the array you need
for(int x = 0; x < qf.length; x++){
qf[x] = new ArrayList<>();
}
scan = new Scan();
scanner = table.getScanner(scan);
int index=0;
System.out.println("Scanning qualifiers of Column family ");
System.out.print("=");
for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
index=0;
for(String y:cf)
{
//System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
//System.out.println("Scanning qualifiers of Column family " + y);
//System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
//index=cf.indexOf(y);
for (String p: getColumnsOfCF(rr,y))
{
if(!qf[index].contains(p))
{
qf[index].add(p);
System.out.print("=");
}
}
index++;
}
}
System.out.println("ColumnFamily and ColumnQualifiers of "+tableName);
System.out.println("============================================================================");
index=0;
for(index=0;index<cf.size();index++)
{
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println(cf.get(index));
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~");
for(String x:qf[index])
{
System.out.println(cf.get(index)+":"+x);
}
}
table.close();
}
public static String[] getColumnsOfCF(Result r, String ColumnFamily)
{
NavigableMap<byte[], byte[]> familyMap = r.getFamilyMap(Bytes.toBytes(ColumnFamily));
String[] qfs = new String[familyMap.size()];
int counter = 0;
for(byte[] bytqfs : familyMap.keySet())
{
qfs [counter++] = Bytes.toString(bytqfs );
}
return qfs ;
}
}
Pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mapr.support</groupId>
<artifactId>m7example</artifactId>
<version>0.0.1</version>
<repositories>
<repository>
<id>mapr-maven</id>
<url>http://repository.mapr.com/maven</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase</artifactId>
<version>0.94.13-mapr-1401-m7-3.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>5</source>
<target>5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>m7example.M7Demo</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Shell script to call the java program
hbase-describe-All.sh
~~~~~~~~~~~~~~~~
#!/bin/bash
ARGS_EXPECTED=1
if [ $# -ne $ARGS_EXPECTED ]
then
echo "[$HOSTNAME]: Usage: `basename $0` table_name(with full path)"
exit 1
fi
java -cp /path-to-jar/m7example-0.0.1-jar-with-dependencies.jar:`hbase classpath` m7example.RetrieveData2 $1