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

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;
    }
}
相关推荐
好喜欢吃红柚子1 分钟前
万字长文解读空间、通道注意力机制机制和超详细代码逐行分析(SE,CBAM,SGE,CA,ECA,TA)
人工智能·pytorch·python·计算机视觉·cnn
小馒头学python5 分钟前
机器学习是什么?AIGC又是什么?机器学习与AIGC未来科技的双引擎
人工智能·python·机器学习
k09337 分钟前
sourceTree回滚版本到某次提交
开发语言·前端·javascript
神奇夜光杯15 分钟前
Python酷库之旅-第三方库Pandas(202)
开发语言·人工智能·python·excel·pandas·标准库及第三方库·学习与成长
Themberfue17 分钟前
Java多线程详解⑤(全程干货!!!)线程安全问题 || 锁 || synchronized
java·开发语言·线程·多线程·synchronized·
plmm烟酒僧19 分钟前
Windows下QT调用MinGW编译的OpenCV
开发语言·windows·qt·opencv
千天夜26 分钟前
使用UDP协议传输视频流!(分片、缓存)
python·网络协议·udp·视频流
测试界的酸菜鱼30 分钟前
Python 大数据展示屏实例
大数据·开发语言·python
羊小猪~~34 分钟前
神经网络基础--什么是正向传播??什么是方向传播??
人工智能·pytorch·python·深度学习·神经网络·算法·机器学习
晨曦_子画39 分钟前
编程语言之战:AI 之后的 Kotlin 与 Java
android·java·开发语言·人工智能·kotlin