Java Examples

Here are some examples of how to use the Konbert API with Java. These examples demonstrate different conversion scenarios and options.

Convert JSON file to PostgreSQL

This example shows how to convert a JSON file to PostgreSQL format using the Konbert API.

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import okhttp3.*;
public class KonbertApiExample {
private static final String API_KEY = "<your_api_key>";
private static final String API_ENDPOINT = "https://konbert.com/api/v1/convert";
private static final String INPUT_FILE_PATH = "<input_file_path>";
private static final String OUTPUT_FILE_PATH = "<output_file_path>";
public static void main(String[] args) {
try {
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("input[file]", "file.json",
RequestBody.create(MediaType.parse("application/json"), new File(INPUT_FILE_PATH)))
.addFormDataPart("input[format]", "json")
.addFormDataPart("input[options][flatten_objects]", "true")
.addFormDataPart("output[format]", "sql")
.addFormDataPart("output[options][create_table]", "true")
.addFormDataPart("output[options][syntax]", "postgres")
.addFormDataPart("output[options][table_name]", "mytable")
.addFormDataPart("sync", "true")
.build();
Request request = new Request.Builder()
.url(API_ENDPOINT)
.header("Authorization", "Bearer " + API_KEY)
.post(requestBody)
.build();
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) {
try (FileOutputStream fos = new FileOutputStream(OUTPUT_FILE_PATH)) {
fos.write(response.body().bytes());
}
System.out.println("File converted successfully");
} else {
System.out.println("Failed to convert file: " + response.body().string());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

Convert CSV URL to Parquet

This example demonstrates how to convert a CSV file from a URL to Parquet format.

import java.io.FileOutputStream;
import java.io.IOException;
import okhttp3.*;
import org.json.JSONObject;
public class KonbertApiExample {
private static final String API_KEY = "<your_api_key>";
private static final String API_ENDPOINT = "https://konbert.com/api/v1/convert";
private static final String OUTPUT_FILE_PATH = "<output_file_path>";
public static void main(String[] args) {
try {
OkHttpClient client = new OkHttpClient();
JSONObject payload = new JSONObject()
.put("input", new JSONObject()
.put("http", new JSONObject()
.put("url", "https://example.com/data.csv"))
.put("format", "csv")
.put("options", new JSONObject()
.put("delimiter", ",")))
.put("output", new JSONObject()
.put("format", "parquet"))
.put("sync", true);
RequestBody body = RequestBody.create(
MediaType.parse("application/json"), payload.toString());
Request request = new Request.Builder()
.url(API_ENDPOINT)
.header("Authorization", "Bearer " + API_KEY)
.header("Content-Type", "application/json")
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) {
try (FileOutputStream fos = new FileOutputStream(OUTPUT_FILE_PATH)) {
fos.write(response.body().bytes());
}
System.out.println("File converted successfully");
} else {
System.out.println("Failed to convert file: " + response.body().string());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}