有一些时候需要生成一个自增的id,以便能和mysql的表对应进行同步。参考了相关文档,写了一个python版的udf。
# coding=utf-8 from odps.udf import annotate @annotate("*->bigint") class AutoIncrement(object): count=0 def evaluate(self,*args): base =0 if len(args)==1 and isinstance(args[0],(int,long)) and args[0]>=0 : base=args[0] self.count=self.count+1 return self.count+base
这个方法接受变长的参数,参数可以用来指定auto increment开始的值。当且仅当传入参数的个数为1,并且是大于等于0的int或者long才会生效。
转自临栏