-
Notifications
You must be signed in to change notification settings - Fork 534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NullPointerException when inserting Cap'N'Proto encoded data with ClickHouseClient #1475
Comments
@ladislavmacoun Thanks for opening the issue. Can you provide a sample of data so we can reproduce. |
@mzitnik Sure, here is simple example of inserting Cap'N'Proto to Clickhouse Let's create a simple capnproto schema @0xe7a7d8bbc2f70135;
struct Message {
id @0 :UInt64;
data @1 :Text;
} {
"id": 1475,
"data": "Hello, clickhouse-java!"
} We can now encode this data to capnp using the capnp tool1 capnp convert json:binary schema.capnp Message < example.json > encoded.bin Make sure it's properly encoded
Now, prepare Clickhouse DB and table (
Let's create Table for the data curl -X POST 'http://localhost:8123/' \
--data-binary "CREATE TABLE Message (id UInt64, data String) ENGINE = MergeTree() ORDER BY id;" And insert the encoded capnp data
Now, we should now be able to query the data curl 'http://localhost:8123/' --data-binary "SELECT * FROM Message format Vertical;"
Row 1:
──────
id: 1475
data: Hello, clickhouse-java! Here is an example Java class to which loads this data import com.clickhouse.client.*;
import com.clickhouse.data.ClickHouseDataStreamFactory;
import com.clickhouse.data.ClickHouseFormat;
import com.clickhouse.data.ClickHousePipedOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class ClickhouseCapnpInsertExample {
private static final String TABLE_NAME = "Message";
private static final ClickHouseNode server = ClickHouseNode.of("http://localhost:8123");
public static long insert(List<Record> records) throws ClickHouseException {
if (records.isEmpty()) {
return 0;
}
try (ClickHouseClient client = ClickHouseClient.newInstance(ClickHouseProtocol.HTTP)) {
ClickHouseRequest.Mutation request = client
.read(server)
.write()
.table(TABLE_NAME)
.format(ClickHouseFormat.CapnProto)
.set("format_schema", "schema.capnp:Message");
ClickHouseConfig config = request.getConfig();
CompletableFuture<ClickHouseResponse> future;
// back-pressuring is not supported, you can adjust the first two arguments
try (ClickHousePipedOutputStream stream = ClickHouseDataStreamFactory.getInstance()
.createPipedOutputStream(config, (Runnable) null)) {
// in async mode, which is default, execution happens in a worker thread
future = request.data(stream.getInputStream()).execute();
records.stream()
.filter(Objects::nonNull)
.forEach(record -> {
try {
stream.write(record.payload());
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
// response should be always closed
try (ClickHouseResponse response = future.get()) {
ClickHouseResponseSummary summary = response.getSummary();
return summary.getWrittenRows();
}
} catch (IOException | InterruptedException | ExecutionException e) {
throw ClickHouseException.of(e, server);
}
}
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Missing encoded binary data path");
return;
}
String filePath = args[0];
try {
byte[] data = Files.readAllBytes(Paths.get(filePath));
long insertedRows = 0;
try {
insertedRows = insert(new ArrayList<>(Collections.singleton(new Record(data))));
} catch (ClickHouseException e) {
throw new RuntimeException("Error while inserting CapNProto data to clickhouse:", e);
}
System.out.println("Successfully inserted " + insertedRows + " rows.");
} catch (IOException e) {
System.out.println("An error occurred while reading the file: " + e.getMessage());
}
}
public record Record(byte[] payload) {
}
} with these maven deps
When run with the encoded.bin data as an argument, you should get this exception
|
Hi @mzitnik, it's been a few months since the last update, and I was wondering if there are any new developments. Alternatively, could you suggest any other approaches to address this issue? |
Describe the bug
When attempting to insert Cap'N'Proto binary encoded data using the edited example, a
NullPointerException
is encountered.The specific error message is:
Steps to reproduce
Expected behaviour
Using the same code snippet with RowBinary encoded data works as expected, returning the server response.
Code example
Configuration
Environment
It looks like the problem is at the
ClickHouseDataStreamFactory
, where any other binary format from RowBinary will return null processor.I would appriciate any assistance addressing this problem or offer any potential solutions or workarounds, thank you.
The text was updated successfully, but these errors were encountered: