前两天接到需求需要做这一块的内容来对投放来一个潜在人群做的数据调研;
大概逻辑是这样的:
- 筛选数据源:
- 下载protoc-3.15.5-win64【该资源有需要的可以再网上自找】:
- 1Protocol Buffers(简称protobuf)是一种轻便高效的数据序列化格式,可用于结构化数据的序列化和反序列化。它由Google开发并开源,用于解决数据交换和存储的通用问题。DMP上传文件只支持protobuf的数据格式。现况支持 IMEI_MD5、IDFA_MD5、MOBILE_HASH_SHA256、OAID_MD5的数据类型。
- 下载后编写proto数据格式,然后再protoc-3.15.5-win64所在目录cmd输入:
protoc --java_out=./ dmp_message.proto命令生成Java文件.- 最后转txt文本格式后放进zip压缩包【编写代码ing(⊙﹏⊙)】
这块是Dmp数据格式,可自行diy成你想要的内容来做转换👇
syntax = "xxx";
package xxx.xxx; 有需要可自行添加
option java_outer_classname = "xxxxxxDto";
message DmpData { //上传文件每行一个base64编码的字符串,每个字符串包含一个完整的DmpData消息二进制字节串
repeated IdItem idList = 1; // 每行数据包含的idList大小不能超过10000
}
message IdItem {
optional uint32 timestamp = 1; //若不设置,默认以上传文件的创建时间为此条记录的创建时间
required DataType dataType = 2; //指定此id的类型,如IMEI、IDFA等
required string id = 3; //根据dataType字段的类型,放置对应类型的id的字符串,需要小写
repeated string tags = 4; //标识此id的业务标签字符串
enum DataType {
IMEI = 0; // 无加密 已经不可用
IDFA = 1; // 无加密 已经不可用
UID = 2; // 内部账号id 一般是内部数据使用
IMEI_MD5 = 4;
IDFA_MD5 = 5;
MOBILE_HASH_SHA256 = 6;
OAID = 7;// 无加密 已经不可用
OAID_MD5 = 8;
}
}
话不多说上业务代码:
代码逻辑:
@AuthAdminPermissions @ApiOperation(value = "【巨量引擎】生成潜在人群包的文件") @PostMapping("/export/zip_file") public void exportZIPDmpFile(HttpServletResponse response, @RequestBody CallBackOaidMd5LogSearchDto searchDto) throws IOException { callBackLogService.exportZIPDmpFile(response,searchDto); }
/** * 导出zip格式 * * @param response * @param searchDto * @throws IOException */ public void exportZIPDmpFile(HttpServletResponse response, CallBackOaidMd5LogSearchDto searchDto) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); if (ADPlatformEnum.OCEAN.getCode().equals(searchDto.getBackPlatform())) { Map<String, Object> params = JsonUtils.object2Map(searchDto); List<AdOaidMd5Log> oaidMd5Logs = adCallBackLogRepo.getOaidMd5Logs(params); if (oaidMd5Logs == null || oaidMd5Logs.isEmpty()) throw new AppException(ErrorCode.SYS_PARAMS_ERROR.code(), "广告回调日志不存在"); try (ZipOutputStream zos = new ZipOutputStream(byteArrayOutputStream)) { String fileName = "DMP-" + UUID.randomUUID().toString().replace("-", "").substring(0, 6) + ".txt"; zos.putNextEntry(new ZipEntry(fileName)); // 计算初始容量 StringBuilder fileContent = new StringBuilder(oaidMd5Logs.size() * 120); for (AdOaidMd5Log oaidMd5Log : oaidMd5Logs) { String oaidMd5 = oaidMd5Log.getOaidMd5(); DmpDataProto.DmpData dmpData = DmpDataProto.DmpData.newBuilder().build(); DmpDataProto.IdItem idItem1 = DmpDataProto.IdItem.newBuilder() .setDataType(DmpDataProto.IdItem.DataType.OAID_MD5) .setId(oaidMd5) .setTimestamp((int) (new Date().getTime() / 1000L)) .build(); dmpData = dmpData.toBuilder().addIdList(idItem1).build(); byte[] binaryData = dmpData.toByteArray(); String resultString = Base64.getEncoder().withoutPadding().encodeToString(binaryData); String md5Lower32 = MD5Util.md5Lower32(resultString); fileContent.append(md5Lower32).append("\n"); } zos.write(fileContent.toString().getBytes(StandardCharsets.UTF_8)); zos.closeEntry(); } String zipFileName = "DMP-" + new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date()) + ".zip"; response.setContentType("application/zip"); response.setHeader("Content-Disposition", "attachment; filename=\"" + zipFileName + "\""); response.setCharacterEncoding(StandardCharsets.UTF_8.name()); try (OutputStream outputStream = response.getOutputStream()) { byteArrayOutputStream.writeTo(outputStream); outputStream.flush(); } } }
巨量那边上传所提示的:
- 压缩包大小不超过50M!
- 接口限制了10s超时,建议文件不要太大!