大数据学习----接口获取数据

用kettle的转换和作业实现

一、首先写转换

1、表输入

在这里面写入时间戳的时间,先进行数据库的连接(一般找_metadata的数据库,然后选中timestamp的时间戳字段)

SELECT timestamp
FROM a_TIMESTAMP
WHERE id=' '

运用好这段sql语句从而获取时间戳的日期时间。

2、JavaScript代码

一定要去postman上解析接口看他的headers是不是为contentype=“application/json”,还有在他的body中的Token和date的数值为多少。(注意date的格式化)

//Script here
var contentype="application/json";


//Alert(body)
//以下是格式化日期的方法,跟主流程无关,目的是为了格式化当前字段所给的日期
 
Date.prototype.format = function(fmt) { 
     var o = { 
        "M+" : timestamp.getMonth()+1,                 //月份 
        "d+" : timestamp.getDate(),                    //日 
        "h+" : timestamp.getHours(),                   //小时 
        "m+" : timestamp.getMinutes(),                 //分 
        "s+" : timestamp.getSeconds(),                 //秒 
        "q+" : Math.floor((this.getMonth()+3)/3), //季度 
        "S"  : this.getMilliseconds()             //毫秒 
    }; 
    if(/(y+)/.test(fmt)) {
            fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
    }
     for(var k in o) {
        if(new RegExp("("+ k +")").test(fmt)){
             fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
         }
     }
    return fmt; 
}      
//
 
 
//自己构造格式化方法,同样也适用于job
var timestamp_format1 = new Date().format("yyyy-MM-dd");//是因为date的格式为2021-1-1
var body;
body="{\"Token\": \"60e7f580c2f9e91a7825e3cd\",\"date\": \""+timestamp_format1+"\"}";

3、HTTP Post

在该步骤中的General中需要写入URL的地址(也就是你所调用接口的地址)、Encoding为UTF-8,Result entity field 为body,Result field name为result,在Fields中需要写入地址的头部文件Name和Parameter都要写为Content-Type,是put in header。

4、Json输入

在文件中选择源定义在一个字段里,从字段获取源为result,然后在字段中写json解析路径。

JsonPath (点击链接测试)结果
$.store.book[*].author获取json中store下book下的所有author值
$…author获取所有json中所有author的值
$.store.*所有的东西,书籍和自行车
$.store…price获取json中store下所有price的值
$…book[2]获取json中book数组的第3个值
$…book[-2]倒数的第二本书
$…book[0,1]前两本书
$…book[:2]从索引0(包括)到索引2(排除)的所有图书
$…book[1:2]从索引1(包括)到索引2(排除)的所有图书
$…book[-2:]获取json中book数组的最后两个值
$…book[2:]获取json中book数组的第3个到最后一个的区间值
$…book[?(@.isbn)]获取json中book数组中包含isbn的所有值
$.store.book[?(@.price < 10)]获取json中book数组中price<10的所有值
$…book[?(@.price <= $[‘expensive’])]获取json中book数组中price<=expensive的所有值
$…book[?(@.author =~ /.*REES/i)]获取json中book数组中的作者以REES结尾的所有值(REES不区分大小写)
$…*逐层列出json中的所有值,层级由外到内
$…book.length()获取json中book数组的长度
{
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            },
            {
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
            {
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}

这是下面的代码的json解析路径

{
    "Code": 0,
    "Message": "OK",
    "Response": [
        {
            "mrdate": "2021-07-01 00:00:00",
            "custid": "5290181",
            "no": "51442443",
            "mrread": 42
        },
        {
            "mrdate": "2021-07-01 00:00:00",
            "custid": "5290180",
            "no": "51442057",
            "mrread": 59
        },

5、插入/更新

是将我们获取到的新数据插入到我们提前建立好的数据库表中,首先选择对应的数据库连接和自己建立的目标表,在选择用来查询的关键字(为数据库中的主键),让它等于流里的字段,最后在更新字段中选择需要更新的字段(主键不需要更新,选择N)。

6、JavaScript代码(时间戳日期时间自动更新)

如果当前日期与时间戳日期相比相差大于等于一天,则自动+1。

//Script here

var mk
mk=0;
Date.prototype.format = function(fmt) { 
     var o = { 
        "M+" : timestamp.getMonth()+1,                 //月份 
        "d+" : timestamp.getDate(),                    //日 
        "h+" : timestamp.getHours(),                   //小时 
        "m+" : timestamp.getMinutes(),                 //分 
        "s+" : timestamp.getSeconds(),                 //秒 
        "q+" : Math.floor((this.getMonth()+3)/3), //季度 
        "S"  : this.getMilliseconds()             //毫秒 
    }; 
    if(/(y+)/.test(fmt)) {
            fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
    }
     for(var k in o) {
        if(new RegExp("("+ k +")").test(fmt)){
             fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
         }
     }
    return fmt; 
}
var d1 =dateAdd(timestamp,"d",0);
var d2 = new Date();
var t=dateDiff(d1,d2,"d")
//时间戳如果和当前日期比超过一天则mk=1,推进时间戳,日期加一天
if(t>1){
mk=1;
}

var timestamp2=date2str(dateAdd(d1, "d", mk),"yyyy-MM-dd HH:mm:ss");

7、插入/更新(将最新的时间戳日期写入数据库中)

用关键字timestamp=timestamp
更新字段中的表字段为timestamp,流字段为timestamp2 更新Y。

转换图

请添加图片描述

作业图

请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

佛系小樂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值