package example;
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.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.Pair;
//Administrator
public class AdvancedCreateTableExample {
public static void printRegion(String tableName)throws Exception{
System.out.println("tableName:"+tableName);
Configuration conf=HBaseConfiguration.create();
HTable table=new HTable(conf,tableName);
Pair<byte[][],byte[][]> pair=table.getStartEndKeys();
byte[][]firsts=pair.getFirst();
for(int i=0;i<firsts.length;i++){
if(firsts[i].length==8){
System.out.println(Bytes.toLong(firsts[i])+"--------"+Bytes.toLong(pair.getSecond()[i]));
}else{
System.out.println(Bytes.toStringBinary(firsts[i])+"--------"+Bytes.toStringBinary(pair.getSecond()[i]));
}
}
}
/**
* @param args
*/
public static void main(String[] args) throws Exception{
Configuration conf=HBaseConfiguration.create();
HBaseAdmin admin=new HBaseAdmin(conf);
HTableDescriptor tabDesc=new HTableDescriptor(Bytes.toBytes("test13"));
HColumnDescriptor colDesc=new HColumnDescriptor("fam1");
tabDesc.addFamily(colDesc);
admin.createTable(tabDesc,Bytes.toBytes(1L),Bytes.toBytes(100L),10);
printRegion("test13");
HTableDescriptor tabDesc2=new HTableDescriptor(Bytes.toBytes("test14"));
HColumnDescriptor colDesc2=new HColumnDescriptor("fam1");
tabDesc2.addFamily(colDesc2);
byte [][]spilit=new byte[][]{
Bytes.toBytes("A"),
Bytes.toBytes("B"),
Bytes.toBytes("C"),
Bytes.toBytes("D")
};
admin.createTable(tabDesc2, spilit);
printRegion("test14");
}
}
package example;
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.client.HBaseAdmin;
//Administrator
public class CreateTableExample {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
Configuration conf=HBaseConfiguration.create();
HBaseAdmin admin=new HBaseAdmin(conf);
HTableDescriptor tableDesc=new HTableDescriptor("test2");
HColumnDescriptor colDesc=new HColumnDescriptor("col1");
tableDesc.addFamily(colDesc);
admin.createTable(tableDesc);
System.out.println(admin.isTableAvailable("test2"));
}
}
package example;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
//Administrator
public class DDEExample {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
Configuration conf=HBaseConfiguration.create();
HBaseAdmin admin=new HBaseAdmin(conf);
System.out.println("available:"+admin.isTableAvailable("test9"));
System.out.println("enalbe:"+admin.isTableEnabled("test9"));
System.out.println("disable:"+admin.isTableDisabled("test9"));
System.out.println("------------");
admin.disableTable("test9");
System.out.println("available:"+admin.isTableAvailable("test9"));
System.out.println("enalbe:"+admin.isTableEnabled("test9"));
System.out.println("disable:"+admin.isTableDisabled("test9"));
System.out.println("------------");
admin.deleteTable("test9");
System.out.println("available:"+admin.isTableAvailable("test9"));
System.out.println("------------");
System.out.println("exist:"+admin.tableExists("test9"));
}
}
package example;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
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.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.FilterList.Operator;
import org.apache.hadoop.hbase.filter.QualifierFilter;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.filter.ValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
//Administrator
public class FilterListExample {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
Configuration conf=HBaseConfiguration.create();
HTable table=new HTable(conf,"testtable");
List<Filter> filterList=new ArrayList<Filter>();
Filter rowFilter=new RowFilter(CompareOp.EQUAL, new SubstringComparator("6"));
filterList.add(rowFilter);
Filter columnFilter=new QualifierFilter(CompareOp.NOT_EQUAL, new SubstringComparator("6"));
filterList.add(columnFilter);
Filter valueFilter=new ValueFilter(CompareOp.LESS, new BinaryComparator(Bytes.toBytes("value8")));
filterList.add(valueFilter);
FilterList filters=new FilterList(filterList);
Scan scan=new Scan();
scan.setFilter(filters);
ResultScanner resultScanner=table.getScanner(scan);
for(Result result:resultScanner){
for(KeyValue kv:result.raw()){
System.out.println(kv+"--------"+Bytes.toString(kv.getValue()));
}
}
resultScanner.close();
System.out.println("------------");
filters=new FilterList(Operator.MUST_PASS_ONE,filterList);
scan.setFilter(filters);
resultScanner=table.getScanner(scan);
for(Result result:resultScanner){
for(KeyValue kv:result.raw()){
System.out.println(kv+"--------"+Bytes.toString(kv.getValue()));
}
}
resultScanner.close();
}
}
package example;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.util.Bytes;
//Administrator
public class IncrementExample {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
Configuration conf=HBaseConfiguration.create();
HTable table=new HTable(conf,"testtable");
long l=table.incrementColumnValue(Bytes.toBytes("row9"), Bytes.toBytes("family1"), Bytes.toBytes("inc"), 1);
System.out.println(l);
l=table.incrementColumnValue(Bytes.toBytes("row9"), Bytes.toBytes("family1"), Bytes.toBytes("inc"), 0);
System.out.println(l);
l=table.incrementColumnValue(Bytes.toBytes("row9"), Bytes.toBytes("family1"), Bytes.toBytes("inc"), -1);
System.out.println(l);
}
}
package example;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.util.Bytes;
//Administrator
public class ListTableExample {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
Configuration conf=HBaseConfiguration.create();
HBaseAdmin admin=new HBaseAdmin(conf);
HTableDescriptor[] descList=admin.listTables();
for(HTableDescriptor desc:descList){
System.out.println(Bytes.toString(desc.getName()));
}
HTableDescriptor desc=admin.getTableDescriptor(Bytes.toBytes("testtable"));
System.out.println(Bytes.toString(desc.getName()));
}
}
package example;
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.client.HBaseAdmin;
import org.apache.hadoop.hbase.util.Bytes;
//Administrator
public class ModifyTableExample {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
Configuration conf=HBaseConfiguration.create();
HBaseAdmin admin=new HBaseAdmin(conf);
HTableDescriptor desc=new HTableDescriptor("modify");
HColumnDescriptor colDesc=new HColumnDescriptor("fam1");
desc.addFamily(colDesc);
admin.createTable(desc);
HTableDescriptor desc2=admin.getTableDescriptor(Bytes.toBytes("modify"));
HColumnDescriptor colDesc2=new HColumnDescriptor("fami2");
desc2.addFamily(colDesc2);
admin.disableTable("modify");
admin.modifyTable("modify", desc2);
admin.enableTable("modify");
HTableDescriptor desc3=admin.getTableDescriptor(Bytes.toBytes("modify"));
System.out.println("is equal:"+desc2.equals(desc3));
System.out.println(desc3);
}
}
package example;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.util.Bytes;
//Administrator
public class ModifyTableExample2 {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
Configuration conf=HBaseConfiguration.create();
HBaseAdmin admin=new HBaseAdmin(conf);
admin.enableTable("test8");
admin.disableTable("test8");
admin.deleteColumn("test8", "fam1");
admin.enableTable("test8");
System.out.println(admin.getTableDescriptor(Bytes.toBytes("test8")));
admin.disableTable("test8");
admin.addColumn("test8", new HColumnDescriptor("fam1"));
System.out.println(admin.getTableDescriptor(Bytes.toBytes("test8")));
}
}
package example;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Increment;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
//Administrator
public class MultiIncrementExample {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
Configuration conf=HBaseConfiguration.create();
HTable table=new HTable(conf,"testtable");
Increment increment=new Increment(Bytes.toBytes("row9"));
increment.addColumn(Bytes.toBytes("family1"), Bytes.toBytes("inc"), 0);
increment.addColumn(Bytes.toBytes("family1"), Bytes.toBytes("inc1"), -1);
increment.addColumn(Bytes.toBytes("family1"), Bytes.toBytes("inc2"), 2);
Result result=table.increment(increment);
for(KeyValue kv:result.raw()){
System.out.println(kv+"-----"+Bytes.toLong(kv.getValue()));
}
}
}
package example;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
//Administrator
public class TablePoolExample {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
Configuration conf=HBaseConfiguration.create();
HTablePool pool=new HTablePool(conf,5);
HTableInterface [] inter=new HTableInterface[10];
for(int i=0;i<inter.length;i++){
inter[i]=pool.getTable("testtable");
}
for(int i=0;i<inter.length;i++){
pool.putTable(inter[i]);
}
pool.closeTablePool("testtable");
}
}