Hbase-Java端增删改查!

本文详细介绍了一个使用Java进行HBase操作的示例应用,包括创建表、插入数据、扫描全表、过滤查询、获取单行、删除多行及删除指定范围行等核心功能,为HBase初学者提供了一个全面的操作指南。
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);
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值