Python示例
http request:put
# 定义函数:refresh segement # curl -X PUT -s --user "****:*****" -H 'Content-Type: application/json' -d '{"startTime":1515283200000, "endTime":1515369600000, "buildType":"REFRESH"}' "*****/kylin/api/cubes/*****/rebuild" || { log "ERROR" "$project" "Build cube failed"; exit 1;} def refreshOneSegment( kylinBaseUrl,cubeName,userName,pwd,startTimestamp,endTimestamp): urlTemp=kylinBaseUrl+"/api/cubes/"+cubeName+"/rebuild" dataTemp ={"startTime":startTimestamp, "endTime":endTimestamp, "buildType":"REFRESH"} base64stringT = base64.encodestring('%s:%s' % (userName, pwd)).replace('\n', '') requestTemp=urllib2.Request(urlTemp) requestTemp.add_header("Authorization", "Basic %s" % base64stringT) requestTemp.add_header("Content-Type", "application/json;charset=UTF-8") requestTemp.add_data(json.dumps(dataTemp)) request2.get_method = lambda: 'PUT' # or 'DELETE' response2 = urllib2.urlopen(request2) data2 = response2.read() return data2;
外部传参
import urllib import urllib2 import json import base64 import datetime import time import sys username="*****" password="*****" kylinbaseurl='*******' startLimitDate=sys.argv[1] endLimitDate=sys.argv[2] cubeName=sys.argv[3]
外部调用
python Refresh_Kylin_Segments "2018-01-07" "2018-1-10" "******"
http request :get
# 定义函数:get segements # curl -X GET --user "****:*****" -H 'Content-Type: application/json' "*****/kylin/api/cubes/*****" def getSegmentsBetweenTimeRange(kylinBaseUrl,cubeName,userName,pwd): urlTemp=kylinBaseUrl+"/api/cubes/"+cubeName base64stringT = base64.encodestring('%s:%s' % (userName, pwd)).replace('\n', '') requestTemp=urllib2.Request(urlTemp) requestTemp.add_header("Authorization", "Basic %s" % base64stringT) requestTemp.add_header("Content-Type", "application/json") responseTemp = urllib2.urlopen(requestTemp) dataTemp = responseTemp.read() return dataTemp;
json data :
# 定义函数:get Segments Between Time Range def getSegmentsBetweenTimeRange(jsonCubeData,minStartDate,maxEndDate): listResult = []; decodejsonT = json.loads(jsonCubeData) segmentsT=decodejsonT['segments'] for i in range(0, len(segmentsT)): startTimestampT=segmentsT[i]['date_range_start'] endTimestampT=segmentsT[i]['date_range_end'] startDateT=datetime.datetime.utcfromtimestamp(startTimestampT/1000) endDateT=datetime.datetime.utcfromtimestamp(endTimestampT/1000) if ( minStartDate < endDateT and minStartDate >= startDateT): listResult.append({"startTimestamp":startTimestampT,"endTimestamp":endTimestampT}); elif( maxEndDate < endDateT and maxEndDate >= startDateT): listResult.append({"startTimestamp":startTimestampT,"endTimestamp":endTimestampT}); return listResult;
list
# main process def main(kylinBaseUrl,cubeName,userName,pwd,startTimestamp,endTimestamp): listResult = []; jsonCubeDataOne=getCubeInfo(kylinBaseUrl,cubeName,userName,pwd) print jsonCubeDataOne minStartDate=datetime.datetime.strptime(startLimitDate, "%Y-%m-%d") maxEndDate=datetime.datetime.strptime(endLimitDate, "%Y-%m-%d") needRefreshSegmentList=getSegmentsBetweenTimeRange(jsonCubeDataOne,minStartDate,maxEndDate) print needRefreshSegmentList for i in range(0, len(needRefreshSegmentList)): startTimestampT=needRefreshSegmentList[i]['startTimestamp'] endTimestampT=needRefreshSegmentList[i]['endTimestamp'] oneResult=refreshOneSegment(kylinBaseUrl,cubeName,userName,pwd,startTimestampT,endTimestampT) listResult.append(oneResult) return listResult