如何自定义语法与其他语句进行映射

1、如何自定义注解与hbase的api进行通信,自动进行类映射

2、如何定义一个对象 与sql中语法进行匹配

各种场景,都可以写一个针对特定软件的语法,进映射

package hbase;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Row;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.beans.factory.InitializingBean;

import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;

public abstract class AbstractHbaseService<DO> implements InitializingBean {


    private final Map<String, Field> columnFieldMap=new LinkedHashMap<>();

    private static final ConcurrentHashMap<Class, List<Field>> query_field_cache=new ConcurrentHashMap();

    protected abstract Class<DO> getHbaseDOClass();


    protected Row convertToRow(DO doObj) throws IllegalAccessException {
       return convertToRow(doObj,null);
    }

    protected DO convertToDO(Row rowObj) throws InstantiationException, IllegalAccessException {
        DO doObj=getHbaseDOClass().newInstance();
        for(Map.Entry<String,Field> entry:columnFieldMap.entrySet()){
            byte[] row = rowObj.getRow();
            entry.getValue().set(doObj,row);
        }
        return doObj;
    }


    protected Row convertToRow(DO doObj,Long tsLong) throws IllegalAccessException {
        for(Map.Entry<String,Field> entry:columnFieldMap.entrySet()){
            Object value=entry.getValue().get(doObj);
            if(value!=null){
                if(tsLong==null){
                    return new Put(Bytes.toBytes((String)value));
                }else {
                    return new Put(Bytes.toBytes((String)value));
                }
            }
        }
        return null;
    }


    private List<Field> getIndexFields(BaseQueryParams queryParams){
        List<Field> fieldList=query_field_cache.get(queryParams.getClass());
        if(fieldList!=null){
            return fieldList;
        }
        fieldList=new ArrayList<>();
        for(Field field:queryParams.getClass().getDeclaredFields()){
            HbaseIndex index=field.getAnnotation(HbaseIndex.class);
            if(index==null){
                continue;
            }
            fieldList.add(field);
        }
        query_field_cache.put(queryParams.getClass(),fieldList);
        return fieldList;
    }

    private Object getFieldValue(Field field,Object input){
        Object fieldValue=null;
        try {
            field.setAccessible(true);
            fieldValue=field.get(input);
        }catch (Exception e){

        }
        return fieldValue;
    }

    protected List<Row> buildHbaseQuery(BaseQueryParams queryParams){
        List<Row> conditions=new ArrayList<>();
        for(Field field :getIndexFields(queryParams)){
            HbaseIndex index=field.getAnnotation(HbaseIndex.class);
            if(index==null|| index.relation()==null){
                continue;
            }
            Object fieldObjct=getFieldValue(field,queryParams);
            if(fieldObjct==null){
                continue;
            }
            if(index.relation().getOp()!=null){
                //此处就是把自定义的与hbase的请求进行映射,
            }
        }
        return conditions;
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        buildFieldMap(getHbaseDOClass());
        if(getHbaseDOClass().getSuperclass()!=null){
            buildFieldMap(getHbaseDOClass().getSuperclass());
        }
    }

    private void  buildFieldMap(Class c){
        Field[] fields = c.getDeclaredFields();
        Stream.of(fields).forEach(field -> {
            HbaseColumn column=field.getAnnotation(HbaseColumn.class);
            if(column==null|| StringUtils.isBlank(column.name())){
                return;
            }
            if(!field.isAccessible()){
                field.setAccessible(true);
            }
            columnFieldMap.put(column.name(),field);
        });
    }
}

package hbase;

public abstract class BaseQueryParams {

    private Integer pageSize=10;

    private int start=0;

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }
}

package hbase;

import java.lang.annotation.*;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface HbaseColumn {

    String name() default "";
}

package hbase;

import java.lang.annotation.*;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface HbaseIndex {

    String name() default "";


    HbaseRelationEnum relation() default HbaseRelationEnum.EQUAL;

}

package hbase;

import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.RowFilter;

public enum HbaseRelationEnum {
    LESS("<", CompareFilter.CompareOp.LESS),

    LESS_OR_EQUAL("<=", CompareFilter.CompareOp.LESS_OR_EQUAL),

    EQUAL("=", CompareFilter.CompareOp.EQUAL),

    NOT_EQUAL("<>", CompareFilter.CompareOp.NOT_EQUAL),

    GREATER_OR_EQUAL(">=", CompareFilter.CompareOp.GREATER_OR_EQUAL),

    GREATERL(">", CompareFilter.CompareOp.GREATER),

    IN("in",null),

    NOT_IN("not in",null);
    ;

    private String code;

    private RowFilter.CompareOp op;


    HbaseRelationEnum(String code, RowFilter.CompareOp op) {
        this.code = code;
        this.op = op;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public RowFilter.CompareOp getOp() {
        return op;
    }

    public void setOp(RowFilter.CompareOp op) {
        this.op = op;
    }
}
相关推荐
cwj&xyp19 分钟前
Python(二)str、list、tuple、dict、set
前端·python·算法
是十一月末23 分钟前
Opencv实现图片的边界填充和阈值处理
人工智能·python·opencv·计算机视觉
Kisorge1 小时前
【C语言】指针数组、数组指针、函数指针、指针函数、函数指针数组、回调函数
c语言·开发语言
轻口味2 小时前
命名空间与模块化概述
开发语言·前端·javascript
晓纪同学3 小时前
QT-简单视觉框架代码
开发语言·qt
威桑3 小时前
Qt SizePolicy详解:minimum 与 minimumExpanding 的区别
开发语言·qt·扩张策略
飞飞-躺着更舒服3 小时前
【QT】实现电子飞行显示器(简易版)
开发语言·qt
明月看潮生3 小时前
青少年编程与数学 02-004 Go语言Web编程 16课题、并发编程
开发语言·青少年编程·并发编程·编程与数学·goweb
明月看潮生3 小时前
青少年编程与数学 02-004 Go语言Web编程 17课题、静态文件
开发语言·青少年编程·编程与数学·goweb
Java Fans3 小时前
C# 中串口读取问题及解决方案
开发语言·c#