Usage Metering
Backfill Meters and Customers
Java sample to backfill customers in Stripe and Amberflo from Postgres
2min
the following java sample shows how to create customers from a postgres database the sample performs the following tasks query customer postgres customer table for each row, check if customer does not already exist in amberflo create the customer in stripe create the customer in amberflo and set the stripe id from previous step in the traits assign the customer to a default rate plan maven dependencies (check for latest versions) pom xml \<dependencies> \<dependency> \<groupid>io amberflo\</groupid> \<artifactid>metering java client\</artifactid> \<version>1 3 6\</version> \</dependency> \<dependency> \<groupid>org postgresql\</groupid> \<artifactid>postgresql\</artifactid> \<version>42 3 1\</version> \</dependency> \<dependency> \<groupid>com stripe\</groupid> \<artifactid>stripe java\</artifactid> \<version>20 52 0\</version> \</dependency> \</dependencies> sample java code import com amberflo metering customer details clients customerdetailsclient; import com amberflo metering customer details clients customerproductplanclient; import com amberflo metering customer details model customerdetails; import com amberflo metering customer details model customerproductplan; import com stripe stripe; import com stripe model customer; import com stripe param customercreateparams; import java sql connection; import java sql drivermanager; import java sql resultset; import java sql statement; import java util hashmap; import java util map; public class customerpostgresbackfillprovider { //todo set the database connection, queries, amberflo, and stripe info final static string db host = "sample host us west 2 rds amazonaws com"; final static string db name = "sampledb"; final static string db pass = "sample pass"; final static string db user = "sample user"; final static string schema = "sampleschema"; final static string count query = "select count( ) as total from customers;"; final static string select query = "select id, customername, customeralternateid, email from customers;"; final static string amberflo product plan id = "default product plan id"; final static string amberflo api key = "amberflo api key"; final static string stripe api key = "sk api key"; //end todo final static customerproductplanclient customerproductplanclient = new customerproductplanclient(amberflo api key); final static customerdetailsclient customerdetailsclient = new customerdetailsclient(amberflo api key); static int counter = 0; static int existing = 0; public static void main(final string\[] args) throws exception { statement stmt = null; connection con = null; resultset rs = null; try { final string url = string format("jdbc\ postgresql //%s 5432/%s?currentschema=%s", db host, db name, schema); con = drivermanager getconnection(url, db user, db pass); stmt = con createstatement(); //query to get count of customers rs = stmt executequery(count query); rs next(); int total = rs getint("total"); system out println("total customers = " + total); //read all customer rows from database table rs = stmt executequery(select query); while (rs next()) { int id = rs getint("id"); final string customername = rs getstring("customername"); final string customeralternateid = rs getstring("customeralternateid"); final string email = rs getstring("email"); system out println(string format("id=%s, customername=%s, customeralternateid=%s, email=%s", id, customername, customeralternateid, email)); createcustomerinstripeandamberflo(integer tostring(id), email, customername, customeralternateid); } system out println("done!!! total customers migrated = " + counter); system out println("customers that already existed = " + existing); } catch (exception ex) { ex printstacktrace(); } finally { if (rs != null) rs close(); if (stmt != null) stmt close(); if (con != null) con close(); } } private static void createcustomerinstripeandamberflo(final string customerid, final string customeremail, final string customername, final string customeralternateid ) { try { stripe apikey = stripe api key; final map\<string, string> metadata = new hashmap(); metadata put("customerid", customerid); metadata put("customeralternateid", customeralternateid); final customercreateparams params = customercreateparams builder() setemail(customeremail) setmetadata(metadata) build(); //check if customer does not already exist in amberflo final customerdetails customerdetails1 = customerdetailsclient get(customerid); if (customerdetails1 != null && customerid equals(customerdetails1 getcustomerid()) && customerdetails1 gettraits() containskey("stripeid")) { system out println(string format("amberflo customer with id %s already exists in amberflo and in stripe with id %s, so skipping ", customerid, customerdetails1 gettraits() get("stripeid"))); existing++; return; } //create customer in stripe final customer stripecustomer = customer create(params); system out println(string format("amberflo customer with id %s created in stripe with id %s", customerid, stripecustomer getid())); final map\<string, string> traits = new hashmap(); traits put("customeralternateid", customeralternateid); traits put("stripeid", stripecustomer getid()); final customerdetails customerdetails = new customerdetails(customerid, customername, customeremail, traits); //create customer in amberflo customerdetailsclient addorupdate(customerdetails); system out println(string format("amberflo customer with id %s created in amberflo", customerid)); //assign customer to a default product plan final string productplanid = amberflo product plan id; final customerproductplan customerproductplan = new customerproductplan(productplanid, customerid); customerproductplanclient addorupdate(customerproductplan); system out println(string format("amberflo customer with id %s assigned to product plan %s in amberflo", customerid, productplanid)); counter++; } catch (exception ex) { system out println(string format("amberflo customer creation failed %s", ex getmessage())); ex printstacktrace(); } } }