存储过程@field := @field

本文探讨了一段MySQL代码中使用LPAD函数与变量赋值(@:=)的方法,展示了如何通过存储过程为查询结果自动添加前导零,并讨论了变量在每次查询时递增的应用场景。
SET @con_code=0;
SELECT (LPAD((@con_code := @con_code+1),4,'0')) id from user;

 

存储过程不会。今天看到一个代码不懂 := 是啥玩意。

 

测试得知 大概是每查出一条几率就将变量@con_code的值+1赋给当前记录

 

id

0001

0002

0003

0004

0005

0006

 

标记一下 待查详细解释

/* * Copyright (c) 2025, TP-Link Global Inc. All rights reserved. */ package com.tplink.cdd.demo.mongo.po; import lombok.Data; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.index.CompoundIndex; import org.springframework.data.mongodb.core.index.CompoundIndexes; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Field; import org.springframework.data.mongodb.core.mapping.Sharded; /** * @author zhaobingqian * @version 1.0 * @since 2025/3/11 */ @Data @Document("smart_data_frame") @CompoundIndexes({ @CompoundIndex(name = "index_user_dev_channel_time", def = "{'user_id':1, 'dev_id':1, 'channel':1, 'time': 1}"), }) @Sharded(shardKey = "user_id") public class SmartDataFramePO { @Id private String id; @Field(name = "user_id") private String userId; @Field(name = "dev_id") private String devId; @Field(name = "channel") private Short channel; @Field(name = "timestamp") private Long timestamp; @Field(name = "object_id") private Integer objectId; @Field(name = "type") private Integer type; @Field(name = "shape") private Byte shape; @Field(name = "box_tlx") private Integer boxTlX; @Field(name = "box_tly") private Integer boxTlY; @Field(name = "box_w") private Integer boxW; @Field(name = "box_h") private Integer boxH; @Field(name = "confidence") private Byte confidence; @Field(name = "fg_ratio") private Byte fgRatio; @Field(name = "is_fake") private Byte isFake; @Field(name = "is_normal") private Byte isNormal; @Field(name = "point_3dvalid") private Byte point3dValid; @Field(name = "point_3dx") private Short point3dX; @Field(name = "point_3dy") private Short point3dY; @Field(name = "point_3dz") private Short point3dZ; } 根据这个写个指令
03-22
帮我写一个修改方法,通过id修改,然后会修改三个值,deviceStatus一定会修改,lastRunTime不为null时修改,lastStopTime不为null时修改。使用mybatis-plus,写SQL时使用兰姆达表达式:package com.ceprei.rcm.entity; import com.baomidou.mybatisplus.annotation.*; import com.ceprei.rcm.model.bizassetstructuretree.AssetType; import lombok.Data; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import com.alibaba.fastjson.annotation.JSONField; /** 存储设备基础信息 @版本: V5.2.0 @版权: Copyright @ 2025 引迈信息技术有限公司版权所有 @作者: 引迈信息技术有限公司 @日期: 2025-07-01 */ @Data @TableName(“biz_base_device”) public class BizBaseDeviceEntity implements Asset{ @TableId(value =“id” ,type = IdType.AUTO ) @JSONField(name = “id”) private Integer id; @TableField(value = “device_name” , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = “deviceName”) private String deviceName; @TableField(value = “kks” , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = “kks”) private String kks; @TableField(value = “device_type” , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = “deviceType”) private String deviceType; @TableField(value = “device_level” , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = “deviceLevel”) private String deviceLevel; @TableField(value = “device_major” , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = “deviceMajor”) private String deviceMajor; @TableField(value = “device_status” , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = “deviceStatus”) private Integer deviceStatus; @TableField(value = “manufacturer” , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = “manufacturer”) private String manufacturer; @TableField(value = “specifications” , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = “specifications”) private String specifications; @TableField(value = “made_time” , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = “madeTime”) private String madeTime; @TableField(value = “install_time” , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = “installTime”) private String installTime; @TableField(value = “last_run_time” , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = “lastRunTime”) private String lastRunTime; @TableField(value = “last_stop_time” , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = “lastStopTime”) private String lastStopTime; @TableField(value = “last_maintenance_time” , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = “lastMaintenanceTime”) private String lastMaintenanceTime; @TableField(value = “mtbf” , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = “mtbf”) private Double mtbf; @TableField(value = “func_description” , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = “funcDescription”) private String funcDescription; @TableField(“sys_id”) @JSONField(name = “sysId”) private Integer sysId; @TableField(“purchase_time”) @JSONField(name = “purchaseTime”) private String purchaseTime; @TableField(“asset_number”) @JSONField(name = “assetNumber”) private String assetNumber; @TableField(“purchase_channels”) @JSONField(name = “purchaseChannels”) private String purchaseChannels; @TableField(fill = FieldFill.INSERT, value = “created_at”) @JSONField(name = “createdAt”) private Date createdAt; @TableField(fill = FieldFill.INSERT_UPDATE, value = “updated_at”) @JSONField(name = “updatedAt”) private Date updatedAt; @TableField(“deleted_at”) @JSONField(name = “deletedAt”) private Date deletedAt; @TableField(value = “f_tenant_id”,fill = FieldFill.INSERT_UPDATE) @JSONField(name = “tenantId”) private String tenantId; @TableField(“f_flow_id”) @JSONField(name = “flowId”) private String flowId; @TableField(“f_flow_task_id”) @JSONField(name = “flowTaskId”) private String flowTaskId; @TableField(“EVALUATION_SCORE”) @JSONField(name = “evaluationScore”) private Integer evaluationScore; @TableField(“RISK_LEVEL”) @JSONField(name = “riskLevel”) private Integer riskLevel; // 联表参数 @TableField(exist = false) @JSONField(name = “sysName”) private String sysName; @TableField(exist = false) @JSONField(name = “unitName”) private String unitName; @TableField(exist = false) @JSONField(name = “unitId”) private Integer unitId; @TableField(exist = false) @JSONField(name = “deviceTypeName”) private String deviceTypeName; @TableField(exist = false) @JSONField(name = “deviceMajorName”) private String deviceMajorName; @TableField(exist = false) @JSONField(name = “deviceLevelName”) private String deviceLevelName; @TableField(exist = false) @JSONField(name = “deviceStatusName”) private String deviceStatusName; @TableField(exist = false) @JSONField(name = “Param1”) private String Param1; @TableField(exist = false) @JSONField(name = “Param2”) private String Param2; @TableField(exist = false) @JSONField(name = “Param3”) private String Param3; @TableField(exist = false) @JSONField(name = “Param4”) private String Param4; @TableField(exist = false) @JSONField(name = “Param5”) private String Param5; @TableField(exist = false) @JSONField(name = “strategyId”) private String strategyId; @TableField(exist = false) @JSONField(name = “strategyName”) private String strategyName; @TableField(exist = false) @JSONField(name = “runTime”) private String runTime; @TableField(exist = false) @JSONField(name = “sid”) private String sid; @TableField(exist = false) @JSONField(name = “sidName”) private String sidName; @TableField(exist = false) @JSONField(name = “fmeaId”) private String fmeaId; // 可视化大屏需要的设备专业的数量或者机组下设备的数量 @TableField(exist = false) @JSONField(name = “visNum”) private Long visNum; // 可视化大屏需要的rcm名称 @TableField(exist = false) @JSONField(name = “rcmName”) private String rcmName; // 可视化大屏需要的rcm名称 @TableField(exist = false) @JSONField(name = “deviceR”) private Integer deviceR; @Override public Integer getParentId() { return this.getSysId(); } @Override public AssetType getType() { return AssetType.DEVICE; } /** @return / @Override public String getTableName() { return getKks(); } /* @return */ @Override public String getAssetName() { return getDeviceName(); } /** 将 yyyy-MM-dd 格式字符串转时间戳 @param dateStr 输入日期字符串(格式必须为 yyyy-MM-dd) @return 时间戳字符串(毫秒),输入无效时返回null */ public String convertToTimestamp(String dateStr) { if (dateStr == null || dateStr.trim().isEmpty()) { return null; } SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”); sdf.setLenient(false); // 禁止自动纠错 try { Date date = sdf.parse(dateStr); return String.valueOf(date.getTime()); } catch (Exception e) { return null; // 捕获所有格式错误 } } }
最新发布
11-07
现在我想统一给double类型的变量设置小数点。是不是只需要在实体类的get方法上保证小数点后三位就行。不用管其他地方的运算:@Data @TableName("BIZ_UNIT_PERFORMANCE_DATA") public class BizUnitPerformanceDataEntity { @TableId(value ="ID" ) @JSONField(name = "id") private String id; @TableField(value = "UNIT_NAME" ) @JSONField(name = "unitName") private String unitName; @TableField(value = "INDICATOR_NAME" ) @JSONField(name = "indicatorName") private String indicatorName; @TableField(value = "YEAR_VALUE" ) @JSONField(name = "yearValue") private Integer yearValue; @TableField(value = "MONTH_VALUE" ) @JSONField(name = "monthValue") private Integer monthValue; @TableField(value = "DATA_TYPE") @JSONField(name = "dataType") private Integer dataType; @TableField(value = "EQ_AVAIL_MONTH" ) @JSONField(name = "eqAvailMonth") private Double eqAvailMonth; @TableField(value = "EQ_AVAIL_CUM" ) @JSONField(name = "eqAvailCum") private Double eqAvailCum; @TableField(value = "CAL_HRS_MONTH" ) @JSONField(name = "calHrsMonth") private Double calHrsMonth; @TableField(value = "CAL_HRS_CUM" ) @JSONField(name = "calHrsCum") private Double calHrsCum; @TableField(value = "OP_HRS_MONTH" ) @JSONField(name = "opHrsMonth") private Double opHrsMonth; @TableField(value = "OP_HRS_CUM" ) @JSONField(name = "opHrsCum") private Double opHrsCum; @TableField(value = "SB_HRS_MONTH" ) @JSONField(name = "sbHrsMonth") private Double sbHrsMonth; @TableField(value = "SB_HRS_CUM" ) @JSONField(name = "sbHrsCum") private Double sbHrsCum; @TableField(value = "MT_HRS_MONTH" ) @JSONField(name = "mtHrsMonth") private Double mtHrsMonth; @TableField(value = "MT_HRS_CUM" ) @JSONField(name = "mtHrsCum") private Double mtHrsCum; @TableField(value = "FO_HRS_MONTH" ) @JSONField(name = "foHrsMonth") private Double foHrsMonth; @TableField(value = "FO_HRS_CUM" ) @JSONField(name = "foHrsCum") private Double foHrsCum; @TableField(value = "DER_HRS_MONTH" ) @JSONField(name = "derHrsMonth") private Double derHrsMonth; @TableField(value = "DER_HRS_CUM" ) @JSONField(name = "derHrsCum") private Double derHrsCum; @TableField(fill = FieldFill.INSERT, value = "CREATED_AT") @JSONField(name = "createdAt") private Date createdAt; @TableField(fill = FieldFill.INSERT_UPDATE, value = "UPDATED_AT") @JSONField(name = "updatedAt") private Date updatedAt; @TableField("DELETED_AT") @JSONField(name = "deletedAt") private Date deletedAt; @TableField(value = "F_TENANT_ID",fill = FieldFill.INSERT_UPDATE) @JSONField(name = "tenantId") private String tenantId; @TableField("F_FLOW_ID") @JSONField(name = "flowId") private String flowId; @TableField("F_FLOW_TASK_ID") @JSONField(name = "flowTaskId") private String flowTaskId; }
09-17
List<Integer> list = new ArrayList<>();这里面放着设备id集合: @Data @TableName("biz_base_device") public class BizBaseDeviceEntity implements Asset{ @TableId(value ="id" ,type = IdType.AUTO ) @JSONField(name = "id") private Integer id; @TableField(value = "device_name" , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = "deviceName") private String deviceName; @TableField(value = "kks" , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = "kks") private String kks; @TableField(value = "device_type" , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = "deviceType") private String deviceType; @TableField(value = "device_level" , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = "deviceLevel") private String deviceLevel; @TableField(value = "device_major" , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = "deviceMajor") private String deviceMajor; @TableField(value = "device_status" , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = "deviceStatus") private Integer deviceStatus; @TableField(value = "manufacturer" , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = "manufacturer") private String manufacturer; @TableField(value = "specifications" , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = "specifications") private String specifications; @TableField(value = "made_time" , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = "madeTime") private String madeTime; @TableField(value = "install_time" , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = "installTime") private String installTime; @TableField(value = "last_run_time" , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = "lastRunTime") private String lastRunTime; @TableField(value = "last_stop_time" , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = "lastStopTime") private String lastStopTime; @TableField(value = "last_maintenance_time" , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = "lastMaintenanceTime") private String lastMaintenanceTime; @TableField(value = "mtbf" , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = "mtbf") private Double mtbf; @TableField(value = "func_description" , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = "funcDescription") private String funcDescription; @TableField("sys_id") @JSONField(name = "sysId") private Integer sysId; @TableField("purchase_time") @JSONField(name = "purchaseTime") private String purchaseTime; @TableField("asset_number") @JSONField(name = "assetNumber") private String assetNumber; @TableField("purchase_channels") @JSONField(name = "purchaseChannels") private String purchaseChannels; @TableField(fill = FieldFill.INSERT, value = "created_at") @JSONField(name = "createdAt") private Date createdAt; @TableField(fill = FieldFill.INSERT_UPDATE, value = "updated_at") @JSONField(name = "updatedAt") private Date updatedAt; @TableField("deleted_at") @JSONField(name = "deletedAt") private Date deletedAt; @TableField(value = "f_tenant_id",fill = FieldFill.INSERT_UPDATE) @JSONField(name = "tenantId") private String tenantId; @TableField("f_flow_id") @JSONField(name = "flowId") private String flowId; @TableField("f_flow_task_id") @JSONField(name = "flowTaskId") private String flowTaskId; @TableField("EVALUATION_SCORE") @JSONField(name = "evaluationScore") private String evaluationScore; @TableField("RISK_LEVEL") @JSONField(name = "riskLevel") private String riskLevel; // 联表参数 @TableField(exist = false) @JSONField(name = "sysName") private String sysName; @TableField(exist = false) @JSONField(name = "unitName") private String unitName; @TableField(exist = false) @JSONField(name = "deviceTypeName") private String deviceTypeName; @TableField(exist = false) @JSONField(name = "deviceMajorName") private String deviceMajorName; @TableField(exist = false) @JSONField(name = "deviceLevelName") private String deviceLevelName; @TableField(exist = false) @JSONField(name = "deviceStatusName") private String deviceStatusName; @TableField(exist = false) @JSONField(name = "Param1") private String Param1; @TableField(exist = false) @JSONField(name = "Param2") private String Param2; @TableField(exist = false) @JSONField(name = "Param3") private String Param3; @TableField(exist = false) @JSONField(name = "Param4") private String Param4; @TableField(exist = false) @JSONField(name = "Param5") private String Param5; @Override public Integer getParentId() { return this.getSysId(); } @Override public AssetType getType() { return AssetType.DEVICE; } /** * @return */ @Override public String getTableName() { return getKks(); } /** * 将 yyyy-MM-dd 格式字符串转时间戳 * @param dateStr 输入日期字符串(格式必须为 yyyy-MM-dd) * @return 时间戳字符串(毫秒),输入无效时返回null */ public String convertToTimestamp(String dateStr) { if (dateStr == null || dateStr.trim().isEmpty()) { return null; } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); sdf.setLenient(false); // 禁止自动纠错 try { Date date = sdf.parse(dateStr); return String.valueOf(date.getTime()); } catch (Exception e) { return null; // 捕获所有格式错误 } } } 。下面是设备与检修策略映射表: @Data @TableName("BIZ_DEVICE_STRATEGY") public class BizDeviceStrategyEntity { @TableId(value ="ID" ) @JSONField(name = "id") private String id; @TableField(value = "DEVICE_ID" , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = "deviceId") private Integer deviceId; @TableField(value = "STRATEGY_ID" , updateStrategy = FieldStrategy.IGNORED) @JSONField(name = "strategyId") private String strategyId; @TableField(value = "F_TENANT_ID",fill = FieldFill.INSERT_UPDATE) @JSONField(name = "tenantId") private String tenantId; @TableField("F_FLOW_ID") @JSONField(name = "flowId") private String flowId; @TableField("F_FLOW_TASK_ID") @JSONField(name = "flowTaskId") private String flowTaskId; } 下面是检修策略表: @Data @TableName("BIZ_REPAIR_STRATEGY") public class BizRepairStrategyEntity { @TableId(value ="ID" ) @JSONField(name = "id") private String id; @TableField(value = "SID" ) @JSONField(name = "sid") private Integer sid; @TableField(value = "FAULT_MODE_ID" ) @JSONField(name = "faultModeId") private Integer faultModeId; @TableField(value = "FAULT_REASON_ID" ) @JSONField(name = "faultReasonId") private Integer faultReasonId; @TableField(value = "STRATEGY" ) @JSONField(name = "strategy") private String strategy; @TableField(value = "OVERHAUL_CONTENT" ) @JSONField(name = "overhaulContent") private String overhaulContent; @TableField(value = "OVERHAUL_CYCLE" ) @JSONField(name = "overhaulCycle") private String overhaulCycle; @TableField(value = "OVERHAUL_CYCLE_UNIT" ) @JSONField(name = "overhaulCycleUnit") private Integer overhaulCycleUnit; @TableField(value = "REQUIREMENT" ) @JSONField(name = "requirement") private String requirement; @TableField(fill = FieldFill.INSERT,value = "CREATED_AT" ) @JSONField(name = "createdAt") private Date createdAt; @TableField(fill = FieldFill.INSERT_UPDATE, value="UPDATED_AT") @JSONField(name = "updatedAt") private Date updatedAt; @TableField("DELETED_AT") @JSONField(name = "deletedAt") private Date deletedAt; @TableField(value = "F_TENANT_ID",fill = FieldFill.INSERT_UPDATE) @JSONField(name = "tenantId") private String tenantId; @TableField("F_FLOW_ID") @JSONField(name = "flowId") private String flowId; @TableField("F_FLOW_TASK_ID") @JSONField(name = "flowTaskId") private String flowTaskId; // 联表参数 // 故障模式名称 @TableField(exist = false) @JSONField(name = "faultModeName") private String faultModeName; // 故障模式原因 @TableField(exist = false) @JSONField(name = "faultReasonName") private String faultReasonName; // 节点名称sid @TableField(exist = false) @JSONField(name = "sidName") private String sidName; // 检修策略名称 @TableField(exist = false) @JSONField(name = "strategyName") private String strategyName; } 现在是这样,我有一个设备id列表,我需要查询设备全部数据外,还需要通过映射表来查询检修策略表的数据,可以先只查一个strategy
09-30
我需要你写一个方法,你有足够时间来思考与编写。首先,我有一个这样的表存储在数据库: /** * 机组运行性能数据宽表 * * @版本: V5.2.0 * @版权: Copyright @ 2025 工业和信息化部电子第五研究所 * @作者: 工业和信息化部电子第五研究所 * @日期: 2025-09-12 */ @Data @TableName("BIZ_UNIT_PERFORMANCE_DATA") public class BizUnitPerformanceDataEntity { @TableId(value ="ID" ) @JSONField(name = "id") private String id; @TableField(value = "UNIT_NAME" ) @JSONField(name = "unitName") private String unitName; @TableField(value = "INDICATOR_NAME" ) @JSONField(name = "indicatorName") private String indicatorName; @TableField(value = "YEAR_VALUE" ) @JSONField(name = "yearValue") private Integer yearValue; @TableField(value = "MONTH_VALUE" ) @JSONField(name = "monthValue") private Integer monthValue; @TableField(value = "DATA_TYPE") @JSONField(name = "dataType") private Integer dataType; @TableField(value = "EQ_AVAIL_MONTH" ) @JSONField(name = "eqAvailMonth") private Double eqAvailMonth; @TableField(value = "EQ_AVAIL_CUM" ) @JSONField(name = "eqAvailCum") private Double eqAvailCum; @TableField(value = "CAL_HRS_MONTH" ) @JSONField(name = "calHrsMonth") private Double calHrsMonth; @TableField(value = "CAL_HRS_CUM" ) @JSONField(name = "calHrsCum") private Double calHrsCum; @TableField(value = "OP_HRS_MONTH" ) @JSONField(name = "opHrsMonth") private Double opHrsMonth; @TableField(value = "OP_HRS_CUM" ) @JSONField(name = "opHrsCum") private Double opHrsCum; @TableField(value = "SB_HRS_MONTH" ) @JSONField(name = "sbHrsMonth") private Double sbHrsMonth; @TableField(value = "SB_HRS_CUM" ) @JSONField(name = "sbHrsCum") private Double sbHrsCum; @TableField(value = "MT_HRS_MONTH" ) @JSONField(name = "mtHrsMonth") private Double mtHrsMonth; @TableField(value = "MT_HRS_CUM" ) @JSONField(name = "mtHrsCum") private Double mtHrsCum; @TableField(value = "FO_HRS_MONTH" ) @JSONField(name = "foHrsMonth") private Double foHrsMonth; @TableField(value = "FO_HRS_CUM" ) @JSONField(name = "foHrsCum") private Double foHrsCum; @TableField(value = "DER_HRS_MONTH" ) @JSONField(name = "derHrsMonth") private Double derHrsMonth; @TableField(value = "DER_HRS_CUM" ) @JSONField(name = "derHrsCum") private Double derHrsCum; @TableField(fill = FieldFill.INSERT, value = "CREATED_AT") @JSONField(name = "createdAt") private Date createdAt; @TableField(fill = FieldFill.INSERT_UPDATE, value = "UPDATED_AT") @JSONField(name = "updatedAt") private Date updatedAt; @TableField("DELETED_AT") @JSONField(name = "deletedAt") private Date deletedAt; @TableField(value = "F_TENANT_ID",fill = FieldFill.INSERT_UPDATE) @JSONField(name = "tenantId") private String tenantId; @TableField("F_FLOW_ID") @JSONField(name = "flowId") private String flowId; @TableField("F_FLOW_TASK_ID") @JSONField(name = "flowTaskId") private String flowTaskId; } 。这个表一条数据表示一个月的相关值。但是,我展示给前端的不是这样子的,前端结构会是这样: /* * { "unitName": "1号机组", "yearValue": 2025, "dataType": 0, "indicatorName":"系数" "eqAvail": { "title": "系数", "allCount": 100, "monthValue": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], "cumValue": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], "childList": [ { "title": "日历", "allCount": 1, "monthValue": [], "cumValue": [] }, { "title": "运行", "allCount": 1, "monthValue": [], "cumValue": [] } .... ] } } { 机组名称:1号机组, 年份:2025, 系数标题:系数 表类型:0(0预估1实际), 系数:{ 标题:系数 总数:100, 月度:[1,2,3,4,5,6,7,8,9,10,11,12], 累计:[1,2,3,4,5,6,7,8,9,10,11,12], childrend:[ {标题:日历, 总数:1, 月度:[], 累计:[]}, {标题:运行, 总数:1, 月度:[], 累计:[]}, {标题:停备, 总数:1, 月度:[], 累计:[]}, {标题:检修, 总数:1, 月度:[], 累计:[]}, {标题:非停, 总数:1, 月度:[], 累计:[]}, {标题:降出力, 总数:1, 月度:[], 累计:[]}, ] } } * * */ @Data public class BizUnitPerformanceDataVO { @Schema(description = "机组名称") @JsonProperty("unitName") @JSONField(name = "unitName") private String unitName; @Schema(description = "系数标题(255字符限制,允许空值)") @JsonProperty("indicatorName") @JSONField(name = "indicatorName") private String indicatorName; @Schema(description = "年份") @JsonProperty("yearValue") @JSONField(name = "yearValue") private Integer yearValue; @Schema(description = "数据类型(0预估/1实际)") @JsonProperty("dataType") @JSONField(name = "dataType") private Integer dataType; @Schema(description = "等效可用系数") @JsonProperty("eqAvail") @JSONField(name = "eqAvail") private IndicatorInfo eqAvail; } @Data public class IndicatorInfo { @Schema(description = "ID合集") @JsonProperty("id") @JSONField(name = "id") private List<String> id; @Schema(description = "标题") @JsonProperty("title") @JSONField(name = "title") private String title; @Schema(description = "总数") @JsonProperty("allCount") @JSONField(name = "allCount") private Double allCount; @Schema(description = "月份值") @JsonProperty("monthValue") @JSONField(name = "monthValue") private List<Double> monthValue; @Schema(description = "累计值") @JsonProperty("cumValue") @JSONField(name = "cumValue") private List<Double> cumValue; @Schema(description = "子项列表(日历、运行、停备等)") @JsonProperty("childList") @JSONField(name = "childList") private List<IndicatorInfo> childList; } 现在就是,你帮我写一个转换方法,将所有数据一一对应。编写之前先告诉我你的转换逻辑,编写完成后举出一个示例,并给出结果。我好判断是否一致。
09-14
<think>嗯,用户给了一个Java类SmartDataFramePO,想将MongoDB中的数据导入到StarRocks,并建表。首先,我需要理解这个类的结构,每个字段对应的类型和MongoDB中的字段名。然后,考虑如何在StarRocks中创建对应的表结构。 首先,分析Java类中的注解。@Id对应MongoDB的主键_id,但在StarRocks中可能需要转换为字符串类型。其他字段用@Field(name = "...")标注,例如userId对应user_id,类型是String。接下来要确定每个Java字段类型在StarRocks中的合适数据类型。比如,Java中的String对应StarRocks的VARCHAR,Long对应BIGINT,Integer对应INT,Short对应SMALLINT,Byte可能用TINYINT或者SMALLINT,但要看具体值的范围。例如,confidence是Byte,范围0-100的话,TINYINT足够,因为TINYINT是-128到127。 然后,主键在StarRocks中是否需要?StarRocks的建表通常使用主键模型或者聚合模型,但用户要求最简单的建表,可能用Duplicate Key模型,指定排序键即可。比如,用id、timestamp等作为排序键,帮助查询优化。 接下来,处理字段名称的大小写和下划线转换。MongoDB的字段名已经通过@Field指定了,比如user_id,所以在StarRocks中保持相同名称即可。需要注意字段类型是否正确转换,比如timestamp在Java中是Long,对应StarRocks的BIGINT,但如果是时间戳,可能需要用DATETIME类型,但原数据存储的是Long的话,可能保持BIGINT更合适,除非需要转换。 然后,创建数据库的语句很简单,CREATE DATABASE IF NOT EXISTS your_db;。建表时,需要列出所有字段,并选择合适的类型。例如,id字段是String,对应VARCHAR(255),可能根据实际长度调整。box_tlx等是Integer,用INT。point3dX是Short,用SMALLINT。 最后,考虑分区分桶,但用户要求最简单的表,可能先不用分区,分桶键选主键id或者常用查询字段,分桶数默认。ENGINE设为OLAP,DUPLICATE KEY指定排序字段。 需要检查是否有遗漏的字段,比如is_fake、is_normal等,都是Byte类型,用TINYINT。确认所有字段名称和类型正确映射。最后给出示例的建表语句,并提醒用户替换数据库名和表名,按需调整类型和长度。</think>好的,我们分步骤来实现MongoDB到StarRocks的数据迁移建表: 一、字段类型映射分析 根据Java类字段类型和MongoDB字段特征,建议StarRocks类型如下: | MongoDB字段 | Java类型 | StarRocks类型 | 说明 | |-------------------|----------|----------------|-----------------------------| | _id | String | VARCHAR(255) | 主键字段 | | user_id | String | VARCHAR(255) | | | dev_id | String | VARCHAR(255) | | | channel | Short | SMALLINT | | | timestamp | Long | BIGINT | 时间戳建议用DATETIME类型更规范 | | object_id | Integer | INT | | | type | Integer | INT | | | shape | Byte | TINYINT | | | box_tlx~box_h | Integer | INT | 四个坐标字段 | | confidence | Byte | TINYINT | 0-100取值范围 | | fg_ratio | Byte | TINYINT | | | is_fake~point3dz | Byte/Short| TINYINT/SMALLINT| 根据实际取值范围选择 | 二、基础建表语句 ```sql -- 创建数据库(按需修改名称) CREATE DATABASE IF NOT EXISTS surveillance_db; -- 使用数据库 USE surveillance_db; -- 最简单的Duplicate Key表(按查询模式选择排序键) CREATE TABLE IF NOT EXISTS smart_dataframe ( id VARCHAR(255), user_id VARCHAR(255), dev_id VARCHAR(255), channel SMALLINT, timestamp BIGINT, object_id INT, type INT, shape TINYINT, box_tlx INT, box_tly INT, box_w INT, box_h INT, confidence TINYINT, fg_ratio TINYINT, is_fake TINYINT, is_normal TINYINT, point3dvalid TINYINT, point3dx SMALLINT, point3dy SMALLINT, point3dz SMALLINT ) ENGINE = OLAP DUPLICATE KEY(id, timestamp, dev_id) -- 选择高频查询字段作为排序键 COMMENT "智能数据帧表" DISTRIBUTED BY HASH(id) BUCKETS 10; -- 根据数据量调整分桶数 ``` 三、优化建议(根据实际需求添加) 1. 如果timestamp存储的是精确到毫秒的时间戳,建议转换类型: ```sql timestamp DATETIME ``` 并在数据导入时使用FROM_UNIXTIME()函数转换 2. 对于数值型枚举字段(如channel/shape),可考虑: ```sql channel SMALLINT COMMENT "0-视频通道,1-红外通道,2-雷达通道" ``` 3. 添加压缩优化(LZ4适用于OLAP场景): ```sql PROPERTIES ("compression" = "LZ4"); ``` 四、数据迁移注意事项 1. MongoDB中的`_id`字段会自动映射到`id`字段 2. 确保Byte类型字段的值在-128~127范围内 3. 如果使用Spark/Flink导入工具,注意类型转换配置 4. 建议先导入小批量数据验证类型兼容性 是否需要我进一步说明如何编写数据导入管道(Data Pipeline)?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值