阿里云(kafka->flink->rds mysql->datav)

1. 背景

使用阿里云做了一个demo,从数据的生产到数据的展现,一条数据的链路

kafka->flink->rds mysql->datav

2. 数据的产生

首先使用python脚本生成一个实时的数据流存放到阿里云的kafka中

# -*- coding: utf-8 -*-
"""
Created on Sun Jun 27 20:11:50 2021

@author: 44373
"""
import msvcrt
import random
import configparser , time , hmac , hashlib , logging , os , sys , random
from linkkit import linkkit
from datetime import datetime

LOG_FORMAT = "%(thread)d %(asctime)s  %(levelname)s %(filename)s %(lineno)d %(message)s"
DATE_FORMAT = "%m/%d/%Y-%H:%M:%S-%p"
logging.basicConfig(format=LOG_FORMAT , datefmt=DATE_FORMAT)

lk = linkkit.LinkKit(
    host_name="",
    product_key="",
    device_name="",
    device_secret="")

lk.thing_setup('./默认模块.json')   #物模型路径

# 连接
def on_connect(session_flag , rc , userdata) :
    logging.info("on_connect:%d,rc:%d,userdata:" % (session_flag , rc))

# 断开连接
def on_disconnect(rc , userdata) :
    logging.info("on_disconnect:rc:%d,userdata:" % rc)

# Topic消息
def on_topic_message(topic , payload , qos , userdata) :
    logging.info("on_topic_message:" + topic + " payload:" + str(payload) + " qos:" + str(qos))

# 订阅
def on_subscribe_topic(mid , granted_qos , userdata) :
    logging.info("on_subscribe_topic mid:%d, granted_qos:%s" %
                 (mid , str(','.join('%s' % it for it in granted_qos))))

# 发布消息
def on_publish_topic(mid , userdata) :
    logging.info("on_publish_topic mid:%d" % mid)
    
# 上报属性
def on_thing_prop_post(request_id , code , data , message , userdata) :
    logging.info("on_thing_prop_post request id:%s, code:%d message:%s, data:%s,userdata:%s" %
                 (request_id , code , message , data , userdata))


lk.on_connect = on_connect
lk.on_disconnect = on_disconnect
lk.on_thing_enable = on_thing_enable
lk.on_subscribe_topic = on_subscribe_topic
#lk.on_unsubscribe_topic = on_unsubscribe_topic
lk.on_topic_message = on_topic_message
lk.on_publish_topic = on_publish_topic
#lk.on_thing_call_service = on_thing_call_service
#lk.on_thing_event_post = on_thing_event_post
#lk.on_thing_prop_changed = on_thing_prop_changed
lk.on_thing_prop_post = on_thing_prop_post
lk.connect_async()
lk.start_worker_loop()
time.sleep(2)  # 延时


a = 1
while True:
#    if ord(msvcrt.getch()) in [68, 100]:
#        break
    now_time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    # now_time = datetime.strptime(now_time_str, "%Y-%m-%d %H:%M:%S")
    # prop_data = {
    #     "tmp_hum:iTs": now_time_str,
    #     "tmp_hum:temperature": 55.2+random.randint(-10,10),
    #     "tmp_hum:humidity": 66.3+random.randint(-10,10)
    #     }
    prop_data = {
        "iTs": now_time_str,
        "temperature": 55.2+random.randint(-10,10),
        "humidity": 66.3+random.randint(-10,10)
        }
    rc ,request_id = lk.thing_post_property(prop_data)
    print('run:', a)
    print(prop_data)
    a += 1
    time.sleep(30)








代码块

{
  "schema": "",
  "profile": {
    "version": "1.5",
    "productKey": "a1QtKtiXgO5"
  },
  "properties": [
    {
      "identifier": "humidity",
      "name": "湿度",
      "accessMode": "rw",
      "required": false,
      "dataType": {
        "type": "float",
        "specs": {
          "min": "0",
          "max": "100",
          "unit": "%RH",
          "unitName": "相对湿度",
          "step": "0.1"
        }
      }
    },
    {
      "identifier": "temperature",
      "name": "温度",
      "accessMode": "rw",
      "required": false,
      "dataType": {
        "type": "float",
        "specs": {
          "min": "-20",
          "max": "100",
          "unit": "°C",
          "unitName": "摄氏度",
          "step": "0.1"
        }
      }
    },
    {
      "identifier": "iTs",
      "name": "时间",
      "accessMode": "rw",
      "required": false,
      "dataType": {
        "type": "text",
        "specs": {
          "length": "255"
        }
      }
    }
  ],
  "events": [
    {
      "identifier": "post",
      "name": "post",
      "type": "info",
      "required": true,
      "desc": "属性上报",
      "method": "thing.event.property.post",
      "outputData": [
        {
          "identifier": "humidity",
          "name": "湿度",
          "dataType": {
            "type": "float",
            "specs": {
              "min": "0",
              "max": "100",
              "unit": "%RH",
              "unitName": "相对湿度",
              "step": "0.1"
            }
          }
        },
        {
          "identifier": "temperature",
          "name": "温度",
          "dataType": {
            "type": "float",
            "specs": {
              "min": "-20",
              "max": "100",
              "unit": "°C",
              "unitName": "摄氏度",
              "step": "0.1"
            }
          }
        },
        {
          "identifier": "iTs",
          "name": "时间",
          "dataType": {
            "type": "text",
            "specs": {
              "length": "255"
            }
          }
        }
      ]
    }
  ],
  "services": [
    {
      "identifier": "set",
      "name": "set",
      "required": true,
      "callType": "async",
      "desc": "属性设置",
      "method": "thing.service.property.set",
      "inputData": [
        {
          "identifier": "humidity",
          "name": "湿度",
          "dataType": {
            "type": "float",
            "specs": {
              "min": "0",
              "max": "100",
              "unit": "%RH",
              "unitName": "相对湿度",
              "step": "0.1"
            }
          }
        },
        {
          "identifier": "temperature",
          "name": "温度",
          "dataType": {
            "type": "float",
            "specs": {
              "min": "-20",
              "max": "100",
              "unit": "°C",
              "unitName": "摄氏度",
              "step": "0.1"
            }
          }
        },
        {
          "identifier": "iTs",
          "name": "时间",
          "dataType": {
            "type": "text",
            "specs": {
              "length": "255"
            }
          }
        }
      ],
      "outputData": []
    },
    {
      "identifier": "get",
      "name": "get",
      "required": true,
      "callType": "async",
      "desc": "属性获取",
      "method": "thing.service.property.get",
      "inputData": [
        "humidity",
        "temperature",
        "iTs"
      ],
      "outputData": [
        {
          "identifier": "humidity",
          "name": "湿度",
          "dataType": {
            "type": "float",
            "specs": {
              "min": "0",
              "max": "100",
              "unit": "%RH",
              "unitName": "相对湿度",
              "step": "0.1"
            }
          }
        },
        {
          "identifier": "temperature",
          "name": "温度",
          "dataType": {
            "type": "float",
            "specs": {
              "min": "-20",
              "max": "100",
              "unit": "°C",
              "unitName": "摄氏度",
              "step": "0.1"
            }
          }
        },
        {
          "identifier": "iTs",
          "name": "时间",
          "dataType": {
            "type": "text",
            "specs": {
              "length": "255"
            }
          }
        }
      ]
    }
  ],
  "functionBlocks": [
    {
      "functionBlockId": "tmp_hum",
      "functionBlockName": "device2_model",
      "gmtCreated": 1624793314627,
      "productKey": "a1QtKtiXgO5"
    }
  ]
}

3. kafka查看数据

4. 编写flink sql

--********************************************************************--
-- Author:         Write your name here
-- Created Time:   2021-06-28 17:26:42
-- Description:    Write your description here
--********************************************************************--
CREATE TEMPORARY TABLE kafka_source (
   iTs VARCHAR,
   temperature varchar,
   humidity varchar
) WITH (
  'connector' = 'kafka',
  'topic' = '',
  'properties.bootstrap.servers' = '',
  'properties.group.id' = '',
  'format'='json',
  'scan.startup.mode' = ''
) ;


CREATE TEMPORARY TABLE kafkasink (
    iTs VARCHAR,
   temperature varchar,
   humidity varchar
) WITH (
   'connector' = '',
   'password' = '',
   'tableName' = '',
   'url' = '',
   'userName' = ''
);
INSERT INTO kafkasink SELECT JSON_VALUE(iTs,'$.value') AS iTs,JSON_VALUE(temperature,'$.value') AS temperature, JSON_VALUE(humidity,'$.value') AS humidity FROM kafka_source;

在此遇到一种情况:数据入库的时候数据类型是json格式如:

{"temperature":{"time":1624871857849,"value":49.2},"humidity":{"time":1624871857849,"value":61.3},"iTs":{"time":1624871857849,"value":"2021-06-28 17:17:37"}}

查找文档之后解决:https://help.aliyun.com/document_detail/207984.html?spm=5176.21213303.J_6028563670.21.35d03edaEW9nt9&scm=20140722.S_help%40%40%E6%96%87%E6%A1%A3%40%40207984.S_hot.ID_207984-RL_json-OR_s%2Bmain-V_1-P0_2

5. 在rds中查询数据

6.数据的展示

将iTs作为横坐标,温湿度分别为纵坐标,展示两张图

动态的显示温度和湿度的模拟值

https://datav.aliyuncs.com/share/ef9c235d2373fcb3063e03212d802123

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
课程总体架构请观看89讲。数据仓库是一个面向主题的、集成的、随时间变化的、但信息本身相对稳定的数据集合,用于对管理决策过程的支持。数据仓库的应用有:1.数据分析、数据挖掘、人工智能、机器学习、风险控制、无人驾驶。2.数据化运营、精准运营。3.广告精准、智能投放等等。数据仓库是伴随着企业信息化发展起来的,在企业信息化的过程中,随着信息化工具的升级和新工具的应用,数据量变的越来越大,数据格式越来越多,决策要求越来越苛刻,数据仓库技术也在不停的发展。数据仓库有两个环节:数据仓库的构建与数据仓库的应用。随着IT技术走向互联网、移动化,数据源变得越来越丰富,在原来业  务数据库的基础上出现了非结构化数据,比如网站log,IoT设备数据,APP埋点数据等,这些数据量比以往结构化的数据大了几个量级,对ETL过程、存储都提出了更高的要求。互联网的在线特性也将业务需求推向了实时化 ,随时根据当前客户行为而调整策略变得越来越常见,比如大促过程中库存管理,运营管理等(即既有中远期策略型,也有短期操作型)。同时公司业务互联网化之后导致同时服务的客户剧增,有些情况人工难以完全处理,这就需要机器 自动决策 。比如欺诈检测和用户审核。总结来看,对数据仓库的需求可以抽象成两方面: 实时产生结果、处理和保存大量异构数据。本课程基于真实热门的互联网电商业务场景为案例讲解,结合分层理论和实战对数仓设计进行详尽的讲解,基于Flink+DorisDB实现真正的实时数仓,数据来及分析,实时报表应用。具体数仓报表应用指标包括:实时大屏分析、流量分析、订单分析、商品分析、商家分析等,数据涵盖全端(PC、移动、小程序)应用,与互联网企业大数据技术同步,让大家能够学到大数据企业级实时数据仓库的实战经验。本课程包含的技术: 开发工具为:IDEA、WebStorm Flink 1.11.3Hadoop 2.7.5Hive 2.2.0ZookeeperKafka 2.1.0、Spring boot 2.0.8.RELEASESpring Cloud Finchley.SR2Flume 、Hbase 2.2.6DorisDB 0.13.9、RedisVUE+jQuery+Ajax+NodeJS+ElementUI+Echarts+Datav等课程亮点: 1.与企业接轨、真实工业界产品2.DorisDB高性能分布式数据库3.大数据热门技术Flink最新版4.真正的实时数仓以及分层设计5.海量数据大屏实时报表6.数据分析涵盖全端(PC、移动、小程序)应用7.主流微服务后端系统8.数据库实时同步解决方案9.涵盖主流前端技术VUE+jQuery+Ajax+NodeJS+ElementUI+Echarts+Datav10.集成SpringCloud实现统一整合方案11.互联网大数据企业热门技术栈12.支持海量数据的实时数仓报表分析13.支持全端实时实时数仓报表分析14.全程代码实操,提供全部代码和资料 15.提供答疑和提供企业技术方案咨询企业一线架构师讲授,代码在老师的指导下企业可以复用,提供企业解决方案。  版权归作者所有,盗版将进行法律维权。 

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值