如何逐步加载协处理器

时间:2016-10-12 11:37:05

标签: hadoop hbase nosql

任何人都可以解释如何通过shell加载regionCoprocessor.i无法获得有关加载和部署Coprocessor.Thanks in advance

的正确信息

1 个答案:

答案 0 :(得分:1)

请按照以下步骤操作:

第1步:创建界面并扩展org.apache.hadoop.hbase.ipc.CoprocessorProtocol

步骤2:在进行协处理器调用后,在要执行的界面中定义方法

第3步:创建HTable

的实例

第4步:使用所有必需参数调用HTable.coprocessorExec()方法

请找到以下示例:

在这个例子中,我们试图获得注册号在我们感兴趣的某个范围内的学生名单。

创建接口协议:

public interface CoprocessorTestProtocol extends org.apache.hadoop.hbase.ipc.CoprocessorProtocol{
    List<Student> getStudentList(byte[] startRegistrationNumber, byte[] endRegistrationNumber) throws IOException;
}

示例学生班级:

public class Student implements Serializable{
    byte[] registrationNumber;
    String name;

    public void setRegistrationNumber(byte[] registrationNumber){
        this.registrationNumber = registrationNumber;
    }

    public byte[] getRegistrationNumber(){
        return this.registrationNumber;
    }

    public void setName(String name){
        this.name = name;
    }

    public int getName(){
        return this.name;
    }

    public String toString(){
        return "Student[ registration number = " + Bytes.toInt(this.getRegistrationNumber()) + " name = " + this.getName() + " ]"
    }
}

模型类: [编写从HBase获取数据的业务逻辑]

public class MyModel extends org.apache.hadoop.hbase.coprocessor.BaseEndpointCoprocessor implements CoprocessorTestProtocol{

    @Override
    List<Student> getStudentList(byte[] startRegistrationNumber, byte[] endRegistrationNumber){
        Scan scan = new Scan();
        scan.setStartRow(startRegistrationNumber);
        scan.setStopRow(endRegistrationNumber);

        InternalScanner scanner = ((RegionCoprocessorEnvironment) getEnvironment()).getRegion().getScanner(scan);

        List<KeyValue> currentTempObj = new ArrayList<KeyValue>();
        List<Student> studentList = new ArrayList<Student>();

        try{
            Boolean hasNext = false;
            Student student;

            do{
                currentTempObj.clear();
                hasNext = scanner.next(currentTempObj);

                if(!currentTempObj.isEmpty()){
                    student = new Student();
                    for(KeyValue keyValue: currentTempObj){
                        bytes[] qualifier = keyValue.getQualifier();
                        if(Arrays.equals(qualifier, Bytes.toBytes("registrationNumber")))
                            student.setRegistrationNumber(keyValue.getValue());
                        else if(Arrays.equals(qualifier, Bytes.toBytes("name")))
                            student.setName(Bytes.toString(keyValue.getValue()));
                    }
                    StudentList.add(student);
                }
            }while(hasNext);

        }catch (Exception e){
            // catch the exception the way you want
        }
        finally{
            scanner.close();
        }
    }
}

客户端类: [对协处理器的调用]

public class MyClient{

    if (args.length < 2) {
        System.out.println("Usage : startRegistrationNumber endRegistrationNumber");
        return;
    }

    public List<Student> displayStudentInfo(int startRegistrationNumber, int endRegistrationNumber){
        final byte[] startKey=Bytes.toBytes(startRegistrationNumber);
        final byte[] endKey=Bytes.toBytes(endRegistrationNumber);


    String zkPeers = SystemInfo.getHBaseZkConnectString();
    Configuration configuration=HBaseConfiguration.create();
    configuration.set(HConstants.ZOOKEEPER_QUORUM, zkPeers);

    HTableInterface table = new HTable(configuration, TABLE_NAME);

        Map<byte[],List<Student>> allRegionOutput;

        allRegionOutput = table.coprocessorExec(CoprocessorTestProtocol.class, startKey,endKey,
                new Batch.Call<CoprocessorTestProtocol, List<Student>>() {
            public List<Student> call(CoprocessorTestProtocol instance)throws IOException{
                return instance.getStudentList(startKey, endKey);
            }
        });

        table.close();

        List<Student> anotherList = new ArrayList<Student>();

        for (List<Student> studentData: allRegionOutput.values()){
            anotherList.addAll(studentData);
        }

        return anotherList;
    }

    public static void main(String args){

        if (args.length < 2) {
            System.out.println("Usage : startRegistrationNumber endRegistrationNumber");
            return;
        }

        int startRegistrationNumber = args[0];
        int endRegistrationNumber = args[1];

        for (Student student : displayStudentInfo(startRegistrationNumber, endRegistrationNumber)){
            System.out.println(student);
        }
    }
}

请注意:请在示例中对Scanner.next(Object)方法进行特殊说明。这将返回boolean并将当前对象存储在Object参数