package ck.kbc.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Hello world!
*
*/
public class App
{
public static Configuration getHBaseConfiguration() {
Configuration configuration = HBaseConfiguration.create();
// configuration.set("hbase.zookeeper.quorum", "localhost");
// configuration.set("hbase.zookeeper.property.clientPort", "2181");
// configuration.set("hbase.master","localhost:16000");
configuration.addResource(new Path("/opt/bigdata/hadoop/hbase120/conf/hbase-site.xml"));
configuration.addResource(new Path("/opt/bigdata/hadoop/hadoop260/etc/hadoop/core-site.xml"));
return configuration;
}
public static void main( String[] args ) throws IOException {
Connection con = ConnectionFactory.createConnection(getHBaseConfiguration());
String type = args[0];
TableName tableName = TableName.valueOf(args[1]);
if(type.equals("create")){
//student name address
if(args.length<4){
return;
}
Admin admin = con.getAdmin();
if(!admin.isTableAvailable(tableName)){
HTableDescriptor table = new HTableDescriptor(tableName);
for (int i = 2; i < args.length; i++) {
table.addFamily(new HColumnDescriptor(args[i]));
}
admin.createTable(table);
//admin.deleteColumn(TableName,ColumnFamily:ColumnName);
//admin.deleteTable(TableName);
}
}else if(type.equals("put")){
if(args.length<2){
return;
}
Table table = con.getTable(tableName);
String[][] stus = {
{"1","chen","henry","jiangsu","yancheng","jianhu"},
{"2","li","pola","jiangsu","nanjing","xuanwu"},
{"3","chen","allen","jiangsu","nanjing","xuanwu"},
{"4","fang","zmin","jiangsu","nantong","anqin"},
{"5","chen","yinchao","anhui","suqian","siyang"},
};
for (int i = 0; i < stus.length; i++) {
Put put = new Put(Bytes.toBytes(stus[i][0]));
put.addColumn(Bytes.toBytes("name"),Bytes.toBytes("firstName"),Bytes.toBytes(stus[i][1]));
put.addColumn(Bytes.toBytes("name"),Bytes.toBytes("lastName"),Bytes.toBytes(stus[i][2]));
put.addColumn(Bytes.toBytes("address"),Bytes.toBytes("province"),Bytes.toBytes(stus[i][3]));
put.addColumn(Bytes.toBytes("address"),Bytes.toBytes("city"),Bytes.toBytes(stus[i][4]));
put.addColumn(Bytes.toBytes("address"),Bytes.toBytes("district"),Bytes.toBytes(stus[i][5]));
table.put(put);
}
}else if(type.equals("scan")){
Table table = con.getTable(tableName);
table.setOperationTimeout(20);
Scan scan = new Scan();
scan.addColumn(Bytes.toBytes("name"),Bytes.toBytes("firstName"));
scan.addColumn(Bytes.toBytes("name"),Bytes.toBytes("lastName"));
scan.addColumn(Bytes.toBytes("address"),Bytes.toBytes("province"));
scan.addColumn(Bytes.toBytes("address"),Bytes.toBytes("city"));
scan.addColumn(Bytes.toBytes("address"),Bytes.toBytes("district"));
ResultScanner rst = table.getScanner(scan);
Iterator<Result> it = rst.iterator();
while (it.hasNext()){
Result next = it.next();
String firstName = Bytes.toString(next.getValue(Bytes.toBytes("name"),Bytes.toBytes("firstName")));
String lastName = Bytes.toString(next.getValue(Bytes.toBytes("name"),Bytes.toBytes("lastName")));
String province = Bytes.toString(next.getValue(Bytes.toBytes("address"),Bytes.toBytes("province")));
String city = Bytes.toString(next.getValue(Bytes.toBytes("address"),Bytes.toBytes("city")));
String district = Bytes.toString(next.getValue(Bytes.toBytes("address"),Bytes.toBytes("district")));
System.out.println(MessageFormat.format("{0}\t{1}\t{2}\t{3}\t{4}",firstName,lastName,province,city,district));
}
}else if(type.equals("filter")){
if(args.length<5){
return;
}
Table table = con.getTable(tableName);
table.setOperationTimeout(20);
//SingleColumnValueExcludeFilter
SingleColumnValueFilter firstNameFilter = new SingleColumnValueFilter(
Bytes.toBytes(args[2]),Bytes.toBytes(args[3]), CompareFilter.CompareOp.EQUAL,Bytes.toBytes(args[4]));
//多个Filter FilterList
//SingleColumnValueFilter provinceFilter = new SingleColumnValueFilter(
//Bytes.toBytes("address"),Bytes.toBytes("province"), CompareFilter.CompareOp.EQUAL,Bytes.toBytes("jiangsu"));
//FilterList.Operator.MUST_PASS_ONE
//FilterList fst = new FilterList(FilterList.Operator.MUST_PASS_ALL);
//fst.addFilter(firstNameFilter);
//fst.addFilter(provinceFilter);
Scan scan = new Scan();
scan.setFilter(firstNameFilter);
//scan.setFilter(fst);
scan.addColumn(Bytes.toBytes("name"),Bytes.toBytes("firstName"));
scan.addColumn(Bytes.toBytes("name"),Bytes.toBytes("lastName"));
scan.addColumn(Bytes.toBytes("address"),Bytes.toBytes("province"));
scan.addColumn(Bytes.toBytes("address"),Bytes.toBytes("city"));
scan.addColumn(Bytes.toBytes("address"),Bytes.toBytes("district"));
ResultScanner rst = table.getScanner(scan);
Iterator<Result> it = rst.iterator();
while (it.hasNext()){
Result next = it.next();
String firstName = Bytes.toString(next.getValue(Bytes.toBytes("name"),Bytes.toBytes("firstName")));
String lastName = Bytes.toString(next.getValue(Bytes.toBytes("name"),Bytes.toBytes("lastName")));
String province = Bytes.toString(next.getValue(Bytes.toBytes("address"),Bytes.toBytes("province")));
String city = Bytes.toString(next.getValue(Bytes.toBytes("address"),Bytes.toBytes("city")));
String district = Bytes.toString(next.getValue(Bytes.toBytes("address"),Bytes.toBytes("district")));
System.out.println(MessageFormat.format("{0}\t{1}\t{2}\t{3}\t{4}",firstName,lastName,province,city,district));
}
}else if(type.equals("get")){
Table table = con.getTable(tableName);
table.setOperationTimeout(5);
Get get = new Get(Bytes.toBytes(args[2]));
Result next = table.get(get);
String firstName = Bytes.toString(next.getValue(Bytes.toBytes("name"),Bytes.toBytes("firstName")));
String lastName = Bytes.toString(next.getValue(Bytes.toBytes("name"),Bytes.toBytes("lastName")));
String province = Bytes.toString(next.getValue(Bytes.toBytes("address"),Bytes.toBytes("province")));
String city = Bytes.toString(next.getValue(Bytes.toBytes("address"),Bytes.toBytes("city")));
String district = Bytes.toString(next.getValue(Bytes.toBytes("address"),Bytes.toBytes("district")));
System.out.println(MessageFormat.format("{0}\t{1}\t{2}\t{3}\t{4}",firstName,lastName,province,city,district));
}else if(type.equals("delRows")){
if(args.length<3){
return;
}
Table table = con.getTable(tableName);
List<Delete> list = new ArrayList<>();
list.add(new Delete(Bytes.toBytes(args[2])));
for (int i = 3; i < args.length; i++) {
list.add(new Delete(Bytes.toBytes(args[i])));
}
table.delete(list);
}else if(type.equals("delRange")){
if(args.length<4){
return;
}
Table table = con.getTable(tableName);
List<Delete> list = new ArrayList<>();
for (int i = Integer.parseInt(args[2]); i <= Integer.parseInt(args[3]); i++) {
list.add(new Delete(Bytes.toBytes(i+"")));
}
table.delete(list);
}else if(type.equals("drop")){
if(args.length<2){
return;
}
Admin admin = con.getAdmin();
if(admin.isTableAvailable(tableName)){
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
}
}
}
Hbase-Java端增删改查!
最新推荐文章于 2024-11-07 12:32:06 发布
本文详细介绍了一个使用Java进行HBase操作的示例应用,包括创建表、插入数据、扫描全表、过滤查询、获取单行、删除多行及删除指定范围行等核心功能,为HBase初学者提供了一个全面的操作指南。

8874

被折叠的 条评论
为什么被折叠?



