阿里云产品 之 datahub

/**
 * Created by AresBlank on 2018/10/30.
 */
public class PushZazdResult2ThirdDataHubHandler {
    private static final Logger logger = LoggerFactory.getLogger(PushZazdResult2ThirdDataHubHandler.class);

    private static String accessId = "*************";
    private static String accessKey = "************";
    private static String endpoint = "http://datahub.**********";
    private static String projectName = "projectname";
    //private static String topicName = "topicname";
    private RecordSchema schema = null;
    private DatahubConfiguration conf;
    private DatahubClient client;

    public PushZazdResult2ThirdDataHubHandler(String topicName) {
        init(topicName);
    }

    private void init(String topicName) {
        AliyunAccount account = new AliyunAccount(accessId, accessKey);
        this.conf = new DatahubConfiguration(account, endpoint);
        this.client = new DatahubClient(conf);
        GetTopicResult topicSnap1=this.client.getTopic(projectName,topicName);
        this.schema=topicSnap1.getRecordSchema();
    }


    public boolean PutDataRecords(Map<String,String> map, String topicName) {
        boolean success = false;
        long startTime = System.currentTimeMillis();
        long tempStartTime = startTime;

        String taskId = map.get("taskId") ;
        String logHead = "耗子接口[" + taskId + "]";

        ListShardResult listShardResult = client.listShard(projectName, topicName);
        List<RecordEntry> recordEntries = new ArrayList<RecordEntry>();
        // 此处可选用listShardResult任意status为ACTIVE的shard进行写入,这里取第一个shard写入
        String shardId = listShardResult.getShards().get(0).getShardId();

        RecordEntry entry = new RecordEntry(schema);
    
            entry.setString("uuid", map.get("taskId"));
            entry.setString("rxid", map.get("rxid"));
            entry.setString("xm", map.get("xm"));
            entry.setString("gmsfhm", map.get("gmsfhm"));
            entry.setString("rxxsd", map.get("rxxsd"));
            //entry.setString("xp", map.get("xp"));
            entry.setString("sjgsdw", map.get("sjgsdw"));
            entry.setString("reptime", map.get("reptime"));
            entry.setString("xh",map.get("xh"));
       
        //将base64转为url
        //后去系统服务参数
        AppInfoBo bigdata = ConfigurationUtil.getAppInfo(ConstantFms.AppInfoCode.BIGDATA);
        AppInfoBo picstoreCover = ConfigurationUtil.getAppInfo(ConstantHandler.AppInfoCode.PICSTORE_COVER);
        String storeType = ConfigurationUtil.getStoreType();

        //上传图片,获取云存储地址前缀
        String downloadUrl = PicStoreHandler.getImageDownloadUrl(storeType, picstoreCover);
        byte[] fileData = null;
        String file = map.get("xp") ;
        if (StringUtils.isNoneBlank(file)) {
            fileData = Base64.decode(file);
        }
        String faceModel = "";
        String bkgPicUrl = null;
        if (null != fileData && fileData.length > 0) {
            tempStartTime = System.currentTimeMillis();
            logger.info("{}:收到图片大小:{}", logHead,fileData.length / 1024.0);
            // 上传图片至云存储,返回url中path组件,
            // 如:/pic?4ddf04i47-e*368102383048--1c0ca07547293i5b8*
            try {
                bkgPicUrl = PicStoreHandler.uploadImageDataToPicStore(UUIDUtil.getRandomUUID() + ".jpg", fileData, storeType, picstoreCover);
            } catch (BaseException e) {
                e.printStackTrace();
            }

            logger.info("{}上传大图:{},耗时{}ms,当前时间{}",logHead, bkgPicUrl,System.currentTimeMillis()-tempStartTime,System.currentTimeMillis());
            if (StringUtils.isNotBlank(bkgPicUrl)) {
                bkgPicUrl=new StringBuilder(downloadUrl).append(bkgPicUrl).toString();
            }

        }

        entry.setString("xp", bkgPicUrl);

        logger.info("送入DataHub数据"+entry);
        entry.setShardId(shardId);
        recordEntries.add(entry);

        PutRecordsResult result = client.putRecords(projectName, topicName, recordEntries);
        if (result.getFailedRecordCount() != 0) {
            List<ErrorEntry> errors = result.getFailedRecordError();
            logger.error("推送 耗子接口 查询结果到第三方的数据写入DataHub失败" + errors.toString());
        } else {
            success = true;
            logger.info("推送 耗子接口 查询结果第三方的数据写入DataHub完成");
        }
        return success;
    }
}

  上述是主要代码。

思路如下:获取主要配置


private void init(String topicName) {
AliyunAccount account = new AliyunAccount(accessId, accessKey);
this.conf = new DatahubConfiguration(account, endpoint);
this.client = new DatahubClient(conf);
GetTopicResult topicSnap1=this.client.getTopic(projectName,topicName);
this.schema=topicSnap1.getRecordSchema();
}

然后:

ListShardResult listShardResult = client.listShard(projectName, topicName);
List<RecordEntry> recordEntries = new ArrayList<RecordEntry>();
// 此处可选用listShardResult任意status为ACTIVE的shard进行写入,这里取第一个shard写入
String shardId = listShardResult.getShards().get(0).getShardId();

RecordEntry entry = new RecordEntry(schema);

然后往 entry赛数据,类似于map

然后:

PutRecordsResult result = client.putRecords(projectName, topicName, recordEntries);
然后判断这个 result 就可以判断状态了

posted @ 2018-11-19 17:28  耗子不光顾  阅读(1062)  评论(0)    收藏  举报