Question

Import past data using the java API

  • 26 February 2021
  • 1 reply
  • 74 views

I'm evaluating mixpanel as a replacement for our current elasticsearch solution, and am trying to use the Java API to import last 6 months of data.

 

I can’t find a way to do this uśing the java api. Do I need to use the import-events http endpoints for this since the data is older than 5 days? I'm getting "Server refused to accept messages, they may be malformed" when I try to add the "time" property using this snippet:

ClientDelivery delivery = new ClientDelivery();
MessageBuilder messageBuilder = new MessageBuilder(mixPanelProjectToken);

JSONObject properties = new JSONObject();

properties.put("time", TimeUnit.MILLISECONDS.toSeconds(event.getEpochTime()));

JSONObject sendEvent = messageBuilder.event(event.getUserId(), event.getType().name(), properties);

delivery.addMessage(sendEvent);

MixpanelAPI mixpanel = new MixpanelAPI();
mixpanel.deliver(delivery);

 


1 reply

Hi,

I am also stuck with this. I have tried using the endpoint https://api.mixpanel.com/import#past-events using RestTemplate. Below is my code. I am getting “0” as response. Response Code is 200 no matter what.

 

public static void main(String... strings) {
File file = new File(FILE_PATH);
RestTemplate restTemplate = new RestTemplate();
try {
Files.lines(file.toPath()).forEach(line -> {
String[] arr = line.split(",");
Properties properties = new Properties(arr[0], arr[1]);
LinkedMultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("event", arr[3]);
params.add("properties", properties);
ResponseEntity<String> log = restTemplate.exchange("https://api.mixpanel.com/import#past-events",
HttpMethod.POST, new HttpEntity<LinkedMultiValueMap<String, Object>>(params,
createHeaders(API_SECRET, "")),
String.class);
System.out.print(log.getBody());
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

static HttpHeaders createHeaders(String username, String password) {
return new HttpHeaders() {
{
String auth = username + ":" + password;
byte[] encodedAuth = org.apache.commons.codec.binary.Base64
.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
String authHeader = "Basic " + new String(encodedAuth);
set("Authorization", authHeader);
set("Accept", "text/plain");
setContentType(MediaType.APPLICATION_FORM_URLENCODED);
}
};
}

Below is the Properties class:

public class Properties {

private String distinct_id;
private String token;
private long time;

public Properties(String distinct_id, String time) {
super();
this.distinct_id = distinct_id;
this.time = Long.valueOf(time.substring(0, 10));
this.token = PROJECT_TOKEN;
}
}

Below is a sample line from the input file:

a78870fdxxxxxxxxxxxxxx,1610966301494139,select_item,my_event_name

Reply


Mixpanel