Uploading Real-time Data
RES uses DIS SDKs to upload real-time user data and perform nearline processing. This section uses Java as an example. For more language examples, see the Data Ingestion Service SDK Reference.
Prerequisites
- If you need to use the nearline upload mode, you can use the DIS SDKs to upload data. Download the DIS SDKs as required.
Uploading Real-time Data to RES
- Initialize the DIS client and use code to initialize the DIS SDK client instance. The following is a code example: For details, see Initializing the DIS Client.
1 2 3 4 5 6 7 8
// Create a DIS client instance. DIS dic = DISClientBuilder.standard() .withEndpoint("YOUR_ENDPOINT") .withAk("YOUR_AK") .withSk("YOUR_SK") .withProjectId("YOUR_PROJECT_ID") .withRegion("YOUR_REGION") .build();
Parameter descriptions:
- YOUR_AK and YOUR_SK are access keys. For details on how to obtain them, see Obtaining Access Keys.
- YOUR_PROJECT_ID indicates the project ID, and YOUR_REGION indicates the region ID. For details on how to obtain the project ID and region ID, see Obtaining Account, IAM User, Group, Project, Region, and Agency Information.
- Obtain the ID of the stream (streamId) to which data is uploaded.
- Click Details of a nearline data source. Figure 1 Obtaining the stream ID
- Click Details of a nearline data source.
- Upload real-time data. The sample code is as follows. The value of streamId must be the same as the stream ID in step 2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
// Configure stream ID. String streamId = "xxxx"; // Configure the data to be uploaded. String message = "hello world."; PutRecordsRequest putRecordsRequest = new PutRecordsRequest(); putRecordsRequest.setStreamId(streamId); List < PutRecordsRequestEntry > putRecordsRequestEntryList = new ArrayList < PutRecordsRequestEntry > (); ByteBuffer buffer = ByteBuffer.wrap(message.getBytes()); for (int i = 0; i < 3; i++) { PutRecordsRequestEntry putRecordsRequestEntry = new PutRecordsRequestEntry(); putRecordsRequestEntry.setData(buffer); putRecordsRequestEntry.setPartitionKey(String.valueOf(ThreadLocalRandom.current().nextInt(1000000))); putRecordsRequestEntryList.add(putRecordsRequestEntry); } putRecordsRequest.setRecords(putRecordsRequestEntryList); LOGGER.info("========== BEGIN PUT ============"); PutRecordsResult putRecordsResult = null; try { putRecordsResult = dic.putRecords(putRecordsRequest); } catch (DISClientException e) { LOGGER.error("Failed to get a normal response, please check params and retry. Error message [{}]", e.getMessage(), e); } catch (Exception e) { LOGGER.error(e.getMessage(), e); }
Last Article: Uploading an Offline Data Source to OBS
Next Article: Creating an Offline Data Source
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.