Bootstrap

JavaSDK 读取BlobRecords示例

概述

目前DataHub支持TUPLE、BLOB两种数据类型,TUPLE类似表格形式数据,BLOB类似文本类型数据。官方目前给出了JAVA SDK的使用示例,但是关于BLOB的数据读取还不够完善,本文给出消费BLOB的完整示例,供读者参考。


Code Sample
import com.aliyun.datahub.DatahubClient;
import com.aliyun.datahub.DatahubConfiguration;
import com.aliyun.datahub.auth.AliyunAccount;
import com.aliyun.datahub.exception.DatahubClientException;
import com.aliyun.datahub.exception.InvalidCursorException;
import com.aliyun.datahub.model.BlobRecordEntry;
import com.aliyun.datahub.model.GetBlobRecordsResult;
import com.aliyun.datahub.model.GetCursorRequest;
import com.aliyun.datahub.model.GetCursorResult;
import java.util.Base64;
import java.util.List;

public class ReadBlobData {

    public static void main(String[] args) {

        //初始化AliyunAccount与DatahubConfiguration
        String accessId = "******";
        String accessKey = "******";
        String endpoint = "http://dh-cn-hangzhou.aliyuncs.com";//地理位置的选取
        AliyunAccount account = new AliyunAccount(accessId, accessKey);
        DatahubConfiguration conf = new DatahubConfiguration(account, endpoint);

        String projectName = "projectname";
        String topicName = "topicname";
        String shardId = "0"; //Partition Id

        //初始化DataHubClient, DataHub服务所有操作均可用该client进行
        DatahubClient client = new DatahubClient(conf);

        //获取Cursor,可以通过三种方式获取:”OLDEST”, “LATEST”, “SYSTEM_TIME”
//        GetCursorResult cursorRs = client.getCursor(projectName, topicName, shardId, GetCursorRequest.CursorType.OLDEST);
        GetCursorResult cursorRs = client.getCursor(projectName, topicName, shardId, System.currentTimeMillis() - 24 * 3600 * 1000 /* ms */); //可以获取到24小时内的第一条数据Cursor

        //读取数据
        int limit = 100;
        String cursor = cursorRs.getCursor();
        while (true) {
            try {
                GetBlobRecordsResult recordRs = client.getBlobRecords(projectName, topicName, shardId, cursor, limit);
                List<BlobRecordEntry> recordEntries = recordRs.getRecords();

                if (recordEntries.size() != 0)
                {
                    for (BlobRecordEntry blobRecorEntry:recordEntries
                         ) {
                        //输出结果
                        String data = blobRecorEntry.toJsonNode().findValue("Data").asText();
                        byte[] result =  Base64.getDecoder().decode(data);
                        System.out.println("Result:" + new String(result));
                    }
                }

                if (recordEntries.size() == 0) {
                    // 无最新数据,请稍等重试
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                // 拿到下一个游标
                cursor = recordRs.getNextCursor();
            } catch (InvalidCursorException ex) {
                // 非法游标或游标已过期,建议重新定位后开始消费
                cursorRs = client.getCursor(projectName, topicName, shardId, GetCursorRequest.CursorType.OLDEST);
                cursor = cursorRs.getCursor();
            } catch (DatahubClientException ex) {
                // 发生异常,需要重试
                System.out.printf(ex.getMessage());
                ex.printStackTrace();
            }
        }
    }
}
测试结果

_

参考链接

Data Hub Java SDK介绍

;