Java SDK- ingest with S3
1min
1. create a metering client from configuration file
Java
1private static void createS3MeteringClientWithFile() throws Exception {
2 try(MeteringContext context = MeteringContext.contextWithS3ClientFromFile(S3_METERING_CONFIG_FILE)) {
3 final MeterMessage meter = MeterMessageBuilder
4 .createInstance(METER_NAME, EVENT_TIME, CUSTOMER_ID)
5 .setMeterValue(METER_VALUE)
6 .build();
7
8 metering().meter(meter);
9 }
10 }
2. create a s3 metering client with with credentials from code
Java
1/***
2Full sample at
3https://github.com/amberflo/metering-java-sample/blob/main/metering-sample/src/main/java/demo/ingest/S3MeteringExamples.java
4***/
5
6import com.amberflo.metering.ingest.MeteringContext;
7import com.amberflo.metering.ingest.meter_message.MeterMessage;
8import com.amberflo.metering.ingest.meter_message.MeterMessageBuilder;
9import java.time.LocalDateTime;
10import static com.amberflo.metering.ingest.MeteringContext.metering;
11
12/**
13 * In this app we describe different ways to call the S3metering services:
14 * 1. Builder.
15 * 2. Factory.
16 **
17 * For more info about each of our api please see the java doc.
18 */
19public class S3MeteringExamples {
20 private final static String METER_NAME = "TrancsactionCount";
21 private final static int METER_VALUE = 2210;
22 private final static String CUSTOMER_ID = "Batman";
23 private final static LocalDateTime EVENT_TIME = LocalDateTime.now();
24 private final static String S3_METERING_CONFIG_FILE = "s3-metering.json";
25 private final static String ACCESS_KEY = "accessKey";
26 private final static String SECRET_KEY = "secretKey";
27 private final static String BUCKET_NAME = "bucketName";
28
29 public static void main(String[] args) {
30
31 createS3MeteringClientWithCredentials();
32
33 }
34
35 /**
36 * A way to to create S3Metering client using the config file by specifying the clientType, bucketName,
37 * httpRetriesCount, httpTimeOutSeconds, isAsync parameters in the json file"
38 */
39 private static void createS3MeteringClientWithFile() throws Exception {
40 try(MeteringContext context = MeteringContext.contextWithS3ClientFromFile(S3_METERING_CONFIG_FILE)) {
41 final MeterMessage meter = MeterMessageBuilder
42 .createInstance(METER_NAME, EVENT_TIME, CUSTOMER_ID)
43 .setMeterValue(METER_VALUE)
44 .build();
45
46 metering().meter(meter);
47 }
48 }
49
50 /**
51 * A way to to create S3Metering client using the bucket name, access key, secret key, retries, timeout
52 * to the context.
53 */
54 private static void createS3MeteringClientWithCredentials() {
55 try(MeteringContext context = MeteringContext.contextWithS3Client(BUCKET_NAME, ACCESS_KEY, SECRET_KEY,
56 5, 30)) {
57 final MeterMessage meter = MeterMessageBuilder
58 .createInstance(METER_NAME, EVENT_TIME, CUSTOMER_ID)
59 .setMeterValue(METER_VALUE)
60 .build();
61
62 metering().meter(meter);
63 }
64 }
65}
66
67
Updated 11 Sep 2024
Did this page help you?