阅读量:0
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; } }