基于Hadoop平台的生产实践应用

配置文件:

       1、conf.properties

       2、core-site.xml

       3、hbase-site.xml

       4、hive-site.xml

       5、hosts

       6、mapred-site.xml

       7、yarn-site.xml

依赖jars包:

       1、jdk 1.7

       2、log4j

       3、mysql

       4、ojdbc

       5、blackAndincrFilter.jar

       6、hadoop

       7、hive

       8、hbase

 

日期类处理:

 

DataUtil:

  1. minVal(DataType dataType) 根据操作符判断类型  返回该类型得最小值
  2. maxVal(DataType dataType) 同上   返回最大值
  3. compare(Data v1, CompareOp op, Data v2) 根据操作符进行判断v1 v2得大小
  4. DataType 枚举类型 包括INT, DOUBLE, LONG, STRING;

Data implements Comparable<Data>

  1. 包含 intDate doubleDate longData stringData
  2. 根据类型参数赋初始值data() 多态初始化
  3. 重写toString方法
  4. 重写compareTo方法
  5. add divide multiply subtract

CompareOp

  1. 枚举类型 LESS, LESS_OR_EQUAL, EQUAL, NOT_EQUAL, GREATER_OR_EQUAL, IN, GREATER, LIKE,NOT_IN;
  2. toEnum方法  根据op的数值返回枚举类型得值

DateUtil:

  1. 属性 simpledateformat df =  new simpledateformat("yyyy-MM-dd HH:mm:ss.SSS")
  2. calendarToStr(Calendar calendar)
  3. calendarToStr(Calendar calendar, SimpleDateFormat df)
  4. getYearInteger(Calendar dateFrom, Calendar dateTo)
  5. getYearInteger(String dateFromStr, String dateToStr)
  6. getYearInteger(String dateFromStr, String dateToStr, SimpleDateFormat df)
  7. getYearInteger(String dateFromStr, String dateToStr, SimpleDateFormat df, boolean validate)
  8. getYearInteger(Calendar dateFrom, Calendar dateTo, boolean validate)
  9. getYearFromNow(String dateStr, SimpleDateFormat df)

10、strDateToCalendar(String val)

11、strDateToCalendar(String val, SimpleDateFormat df)

12、strDateToCalendar(String val, SimpleDateFormat df, boolean validate)

13、haveBlank(boolean validate, Object... objects)

14、haveNoBlank(boolean validate, Object... objects)

15、logInfo(String debugStr)

 

json类处理:

 

Ralational:

  1. 接口类 定义枚举类型OderType(NUMBER, STRING)compare方法
  2. Equal方法 实现Ralational接口 重写compare方法 判断是否为number 是Long。Parse Long()默认default:return*。Compare(*) != 0
  3. Greater_or_equal类 同上
  4. Greater类
  5. Less_or_equal
  6. Less
  7. Not_equal

CompareOp:

  1. 枚举类型 LESS, LESS_OR_EQUAL, EQUAL, NOT_EQUAL, GREATER_OR_EQUAL, GREATER;

 

HRow:

       1、private HashSet<String> selects = new HashSet<String>();

          private Table<String, CompareOp, String> wheres = HashBasedTable.create();

          private Set<String> orWheres = new HashSet<String>();

          private List<String> groupBys = new LinkedList<String>();

          private LinkedHashMap<String, Boolean> orderBys = new LinkedHashMap<String, Boolean>();

          private JSONObject hRow;

  1. where方法 参数 (String... wheres) 判断条件添加到wheres
  2. select\orderby\groupby\from \limit\count\sum\where
  3. sql 可以实现sum和 count得功能
  4. initQuery 初始化查询
  5. matchOrderByAndlimit()  orderBys
  6. OrderComparator()继承comparator
  7. getSum()
  8. getCount()

10、matchSelect()

11、matchWhere()

 

Map类:

      

MapKEyComparator:实现comparator接口  重写compare方法 字符串比较

 

MapUitls :sortMapByKey(Map<String, String> map)  实现对map得排序tree Map putall()

 

CommonFun类:

  1. getBeforeOrNextDate(String DateStr,int beforeOrNext)获取前几个月之前的日期
  2. getProvinceBypidReverse(String pidRervse) 获取省份pid得反转
  3. getProvinceByPid(String paPid) 获取省份前四位或者前二位之后二位补0
  4. getProvinceList() 获取所以省份清单
  5. getPidkeybyKey() key:pid+~+date 截取 第一个即是

 

BulkLoadUtil:实现closeable接口

  1. htable  outputpath configuration (Boolean) closed对象
  2. isClosed() close()二个函数
  3. shell函数 process process = runtime.getruntime().exec(cmds)   cmds = {“/bin/sh”,”-c”,cmd}
  4. load() shell命令修改文件权限为777 loader.doBulkLoad(outputpath,htable)
  5. putSortAndWrite() put – map ---write   每百行输出索引

 

configUtil:读取配置文件

       readProp():返回properties得key-valuue得set试图  进行元素的迭代 循环输出key-value

configHelper:

       同下  但是少了一个以来的jars包

configHelperKerb:新版的Hadoop conf 兼容了kerberos认证

       1、private static Configuration conf=null;

          private final static String krb5ConfPathKey = "java.security.krb5.conf";

       private final static String krb5ConfFileNameKey = "krb5_conf_file";

       private final static String krb5UserNameKey = "kerberos_user";

       private final static String krb5UserKeytabFileKey = "kerberos_keytab";

       private static ClassLoader classLoader = ConfigHelperKerb.class.getClassLoader();

       private static final String confFileName = "conf.properties";

     private final static String addClassPathKey = "runjar_path";

    2、getConf() 若conf为空 则执行 loadxml() loadconf() krbsInit() addClassPath() add ConfCache()

    3、loadXml() conf = hbaseConfiguration.create()

           Conf.addResource(“core-site.xml”)

                                       Hdfs-site.xml  hive mapred  yarn

  1. loadConf()
    1. properties pop = new properties
    2. inputstream is = classloader.getresourceasStream(confFileName)
    3. 不为空 pop.load(is)
    4. Set<String> keys = pop.stringPropertynames()
    5. 循环 keys  conf.set(key,pop.getproperty(key)
  2. krb5init():安全认证机制反射获取路径 判断是否是安全获得
  3. addClassPath() 文件上传 jars包缓存到cache
  4. addConfToCache() fileStates[] fstates = fs.listStatus(new Path())
  5. DistributeCache.addFileToClassPath(fileStstus.getPath(),conf)

 

CreateTable:创建hbase的工具类

       1、static Log LOG = LogFactory.getLog(Createtable.class);

       public static final String COMPREESSTYPE = "compressType";

       public static final String DATABLOCKENCODING = "dataBlockEncoding";

       public static final String BLOOMFILTER = "bloomFilter";

       static Map<String, DataBlockEncoding> dataBlockEncodingTypes = new HashMap();

       static Map<String, Compression.Algorithm> compressionTypes = new HashMap();

       static Map<String, BloomType> bloomFilterTypes = new HashMap();

       2、static   DataBlockEncoding  Compression  BloomType  类型的确定

       3、main

              a)hbaseadmin admin = null

              b)tablename columnFamilyName regionReplications

              c)configuration conf = ConfigHelper.getconf()

              d)args参数获取 以及判断数目赋值  四参数  三参数是如何赋值

              e)三个类型值由配置文件中的设置获得 判断赋值

              f)tablename的获得 日志

              h)表对象描述获取  列对象描述获取 表描述对象 添加列

              i)admin.createTable(tabledesc,splits(regionnum)  日志 抛异常

       4、splits(int regionnum)

      

Custinfoaccu_tagutil:客户单标签合并以及验证

       1、属性 省份代码 出生日期 中文姓名  英文姓名 地址  手机  电话 邮箱 工作单位

       2、validateName() 姓名中英文的验证 赋值

       3、validateBirthday(String birthday,String idNo)证件是身份证并准确时验证出生日期是否正确

       4、validateSex()验证性别是否正确 validateID()validateAddr()validatePhone()validateIdType()validateEmail()validateEmail()validateEmail()validateEarning()getVerifyCode()校验码函数formatBirthDate()

parseBirthDate()

 

deleterowkey:删除表中含有中文的行

  1. 删除中文记录 模式的选择判断
  2. 不删除 只是统计数量
  3. Tablemapreduceutil.initablejob(source_table,scan,deletecharmapper.class)
  4. Deletecharmapper  初始化setup() map()重写 reduce()重写

 

HbaseTablerow:一行读入封装到map中  进行运算

  1. Map<String,List<String>> row
  2. getTableJsonList(tablename)

表名转换成小写  变成list集合

返回值  list 为空时返回Array List  否 list

  1. 有参构造初始化
  2. HBaseTableRow(Result rs)

this.row = new HashMap<>();

Cell[] cells = rs.rawCells();

 

HbaseScanUtil:Hbase scan 对象的工具类

  1. 创建默认的Scan 使用ConfigHelper.con配置

CreateScan()无参

CreateScan(ConfigHelper.getConf())

  1. 使用指定的conf配置scan

CreateScan(Configuration conf)

Scan scan =  new Scan()

Scan.setCacheblock(false)

Scan.setCaching(Conf.getInt(“Scancaching”,400)

返回一个scan 对象

  1. 向指定的scan对象中针对hstamp字段增加过滤条件   大于  小于

Scan setTimeRangeFilter() 四个参数 scan对象 列 开始时间 结束时间

FilterList filterList = new FilterList();

SingleColumnValuefilter filter = new SingleColumnValueFilter() 四个参数  CompareFilter.CompareOp.GREATER_OR_EQUAL,

Filter.set FilterIfMissinf()

filterList.addFilterfilter()

 

filter 再来一次   参数  CompareFilter.CompareOp.LESS_OR_EQUAL,

add Filter

sacn.setFilter

 

三个对象    引用上百年的那个  判断是否为空   为空不添加    不为空添加

 

  1. 如果时间戳为空的话就不进行过滤

setTimeRangeFilter2 四个参数

前面和3 一样   filter List添加过滤以后进行判断

HBaseResultUtil:

  1. 设置日期格式SimpleDateFormat
  2. getCalendar(Result result, String col)

返回getCalendar(result, col, Calendar.getInstance(), DF);

  1. getCalendar(Result result, String col, SimpleDateFormat df)

getCalendar(result, col, Calendar.getInstance(), df);

  1. getCalendar(Result result, String col, Calendar defaultCal)

getCalendar(result, col, defaultCal, DF);

  1. getCalendar(Result result, String col, Calendar defaultCal, SimpleDateFormat df)
  2. get(Result result, String col) 从Result中获取指定列的值,如果不存在指定列,返回"",不验证返回值的有效性返回get(result, col, "", false)
  3. get(Result result, String col, String defaultVal)

get(result, col, defaultVal, true);

  1. get(Result result, String col, String defaultVal, boolean validateVal)
  2. getWithPrefix(Result result, String prefix, String defaultVal, boolean validateVal)

10、getWithPrefix(Result result, String prefix)

       getWithPrefix(result, prefix, "", false);

11、getWithPrefix(Result result, String prefix, String defaultVal)

       getWithPrefix(result, prefix, defaultVal, true)

12、getWithPrefix(Result result, String prefix, boolean validateVal)

       getWithPrefix(result, prefix, "", validateVal)

13、getJsonWithPrefix(Result result, String prefix, String defaultVal, boolean validateVal)

       new JSONObject(getWithPrefix(result, prefix, defaultVal, validateVal))

14、getJsonWithPrefix(Result result, String prefix)

       return getJsonWithPrefix(result, prefix, "{}", false)

15、getJsonWithPrefix(Result result, String prefix, boolean validateVal)

       getJsonWithPrefix(result, prefix, "{}", validateVal)

16、getJsonMapWithPrefix(Result result, String prefix)

       getJsonMapWithPrefix(result, prefix, true)

17、getJsonMapWithPrefix(Result result, String prefix, boolean validateVal)

获取符合前缀条件的所有列的信息,使用Map封装返回值,通过validateVal指定是否验证列值的有效性,value转成json后返回

18、getMapStringWithPrefix(Result result, String prefix, boolean validateVal)

获取符合前缀条件的所有列的信息,使用Map封装返回值,通过validateVal指定是否验证列值的有效性

19、getJsonListWithPrefix(Result result, String prefix)

       getJsonListWithPrefix(result, prefix, true)

20、getJsonListWithPrefix(Result result, String prefix, boolean validate)

       获取符合前缀条件的所有列的信息,通过validateVal指定是否验证列值的有效性,value转成json后返回

21、getMapWithPrefix(Result result, String prefix)

       getMapWithPrefix(result, prefix, true)

22、getMapWithPrefix(Result result, String prefix, boolean validateVal)

       获取符合前缀条件的所有列的信息,通过validateVal指定是否验证列值的有效性

23、getValueByCol(Result result, String colmn)

       从Result获取列的值

24、getMapWithPrefix(Result result, String prefix, boolean validateVal)

       获取符合前缀条件的所有列的信息,通过validateVal指定是否验证列值的有效性

25、getValueByCol(Result result, String colmn)

       从Result获取列的值

 

HbaseController:

  1. public static Configuration configuration = ConfigHelper.getConf();
  2. selectRowKeyFamily(String tablename, String rowKey, String family)  类型result
  3. selectRowKeyFamilyColumn(String tablename, String rowKey, String family, String column) 类型 void  比上面多了一个数据输出
  4. selectFilter(String tablename, List<String> arr)

创建htable对象  scan对象 filterList 数组 逗号切割 添加到list中 For循环 处理arr 数组

Scan.setFilter(list)

Table.getsacner(scan)

  1. getOneRowByKey(String tablename, String rowKey) 类型result
  2. selectOneByRowKey(String tablename, String rowKey) 类型 void  多一个输出

 

DtslogicUtil 工具类:

  1. 参数 简单的日期格式设置

Htable对象

上一次增量的时间

源表

目标表

列簇

资源关闭Boolean

  1. DtslogicUtil 有参函数

一个参数 表名 初始化表

二个参数 表名 列簇 初始化表和对列簇赋值

无参 初始化表 表名 == 表对象

  1. initTable()参数 表名

new HTable(ConfHelperKerb.getConf,disLogic_Table_name==null? ? "c_dtslogic" : dtslogic_table_name)

  1. updateDtslogicTime() 参数 原表 目标表 更新时间

updateDtslogicTime() 参数 原表 目标表 无时间参数  引用上一个函数  事件参数为当前系统时间  df.format(new Date())

  1. getTimeStamp() 获取当前表中相对应的时间戳
  2. close() 关闭hbase表连接 IOUtils.closeQuietly()
  3. update()函数判断上次修改的时间错上是否为空  否  二个参数的update  三个参数的update
  4. Scan getIncrScan()四个参数 扫描对象  列 原表 目标表

时间戳更改  返回一个时间戳过滤函数

HbaseScanUtil.setTimeRangeFilter(scan,column,start_stamp,end_stamp)

Scan getIncrScan() 三个参数 scan 原表 目标表

返回一个四个参数的Scan getIncrScan() 列默认 hstamp

Scan getIncrScan()三个参数 column  原表  目标表  

返回Scan getIncrScan() 四个参数 new Scan()  hstamp 原表 目标表

Scan getIncrScan()二个参数 返回一个确实scan的三个参数的Scan getIncrScan()

  1. 如果没有该列则返回缺省的时间戳

getTimeStamp()三个参数   原表名 目标表明  默认时间戳

get get = new Get(bytes.tobytes(source_table)

result result = c_dtslogic.get(get)

判断 不为空 或者包含列   返回该列数值

否则返回默认时间戳

 

HdfsUtil:

  1. rm(String path) 删除文件的方法 进入回收站 由管理员定期处理

返回 rm(path, true, false)

  1. rm(String pathStr, boolean recursive, boolean skipTrash)

是否递归删除 是否跳过回收站

  1. put(boolean delSrc, boolean overwrite, String srcdir, String wildcard, String dstdir)
  2. put(boolean delSrc, boolean overwrite, Path[] srcs, Path dst)

 

hiveutil:

  1. 属性

private String DRIVER_CLASS;

private String HIVE_JDBC_URL;

private String HIVE_USER;

private String HIVE_PASSWD;

private String hiveTable;

private String hiveLocal;

private static final Log LOG = LogFactory.getLog(HiveUtil.class);

private static Configuration conf = null;

private static final String SCHEMA_STRING = "schema";

private static final String SCHEMA_COLUMNS = "columns";

private static boolean isAPathInUse = false;

private static HiveUtil hiveUtil = null;

  1. HiveUtil(String DRIVER_CLASS, String HIVE_JDBC_URL, String HIVE_USER, String HIVE_PASSWD, String hiveTable)
  2. setJob(Job job)
  3. setHiveJob(Job job)
  4. clean()
  5. alterTableLocation()
  6. modifyFlag()
  7. getOutputPath(Configuration conf)
  8. getColumns()

10、getConnection()

 

JsonUtil:

  1. get(JSONObject json, String key, String defaultVal, ValidationType type)

从json对象中获取指定key对应的值,通过validate指定是否验证值有效如果无效,返回缺省值

  1. isValidated(JSONObject json, ValidationType type, String res)
  2. isIncrFlagValidate(JSONObject json)
  3. isIncrFlagValidate2(JSONObject json)
  4. get(JSONObject json, String key, String defaultVal)

返回 get(json, key, defaultVal, INCRFLAG_AND_NOTBLANK)

  1. get(JSONObject json, String key)

get(json, key, "", INCRFLAG_AND_NOTBLANK)

  1. get(JSONObject json, String key, ValidationType type)

get(json, key, "", type)

 

SaleUtils:

       1、getPriceByDid(String did)通过did或者省机构号获得价格

 

LogUtil:异常文件处理类 目前只提供记录异常信息

  1. 属性 conf bdmppath filepath 日期格式
  2. writeLog2(Exception e, String module) 日志记录 拼装

ByteArrayOutputStream buf = new ByteArrayOutputStream();

  1. writeLogInfo2(String info) 相比于上个函数少了一个日志信息
  2. infoToLog(String info, String filePathAll) synchronized
  3. insertData(String value1, String value2) 写入数据库

 

 

 

Constant:

       public static final String SOURCE_TABLE_KEY = "source";//源表名

    public static final String SOURCE_TABLE_LABEL_KEY = "source.label";//源表名

    public static final String TARGET_TABLE_KEY = "target";

    public static final String TARGET_TABLE_LABEL_KEY = "target.label";

    public static final String INCREMENT_KEY = "increment.switch";//是否增量更新

    public static final String INCREMENT_COLUMN_KEY = "increment.column";//是否增量更新

    public static final String BULKLOAD_KEY = "bulload.switch"; // 是否使用 bulkload

    public static final String SNAPSHOT_SWITCH = "snapshot.switch"; // 是否使用 snapshot

    public static final String SNAPSHOT_NAME = "snapshot"; // snapshot名称

    public static final String COLUMN_FILTER_KEY = "filter.switch";//是否使用指定的列过滤

    public static final String MAPPER_KEY = "mapper.template";//mapperClassName

    public static final String REDUCER_KEY = "reducer.template";//reduceClassName

    public static final String COLUMNS_KEY = "columnNames";//指定需要过滤的 列

    public static final String XML_KEY = "xml_location"; // XML 文件路径

    public static final String TEST_ROWKEY_KEY = "test_row";// 指定rowkey跑测试

    public static final String ITEM_ALIAS_KEY = "item.alias";

    public static final String MAP_ITEM_LIST_KEY = "map.item.list";

    public static final String REDUCE_ITEM_LIST_KEY = "reduce.item.list";

    public static final String ITEMS_ROOT_DIR_KEY = "items.root.dir";

    public static final String SPLITER = "^";//指定过滤列的,分隔符

    public static final String INPUT_SETUP_KEY = "input.setup";

    public static final String BLACKLIST_DIR_KEY = "black.list.dir";

    public static final String HIVE_PROP = "hive.prop";

    public static final String HIVE_LOCATION = "hive.local";

    public static final String REDUCE_NUM = "reduce.num";

    public static final String ITEMS_INFO_DIR_KEY = "items.info.dir";

    public static final String UNIT_TEST_KEY = "UNIT_TEST";

 

发布了57 篇原创文章 · 获赞 33 · 访问量 14万+

猜你喜欢

转载自blog.csdn.net/u014156013/article/details/82379355