com.clickhouse.client.api.data_formats.NativeFormatReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of client-v2 Show documentation
Show all versions of client-v2 Show documentation
New client api for ClickHouse
package com.clickhouse.client.api.data_formats;
import com.clickhouse.client.api.data_formats.internal.AbstractBinaryFormatReader;
import com.clickhouse.client.api.data_formats.internal.BinaryStreamReader;
import com.clickhouse.client.api.query.QuerySettings;
import com.clickhouse.data.ClickHouseColumn;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* For the backward compatibility server will not send TZ id in column type. Client should send version to a server
* to get the correct column type.
* (see: https://github.com/ClickHouse/ClickHouse/issues/38209)
*/
public class NativeFormatReader extends AbstractBinaryFormatReader {
private Block currentBlock;
private int blockRowIndex;
public NativeFormatReader(InputStream inputStream) {
this(inputStream, null);
}
public NativeFormatReader(InputStream inputStream, QuerySettings settings) {
super(inputStream, settings, null);
readNextRecord();
}
@Override
public boolean readRecord(Map record) throws IOException {
if (currentBlock == null || blockRowIndex >= currentBlock.getnRows()) {
if (!readBlock()) {
return false;
}
}
currentBlock.fillRecord(blockRowIndex, record);
blockRowIndex++;
return true;
}
private boolean readBlock() throws IOException {
int nColumns;
try {
nColumns = BinaryStreamReader.readVarInt(input);
} catch (EOFException e) {
endReached();
return false;
}
int nRows = BinaryStreamReader.readVarInt(input);
List names = new ArrayList<>(nColumns);
List types = new ArrayList<>(nColumns);
currentBlock = new Block(names, types, nRows);
for (int i = 0; i < nColumns; i++) {
ClickHouseColumn column = ClickHouseColumn.of(BinaryStreamReader.readString(input),
BinaryStreamReader.readString(input));
names.add(column.getColumnName());
types.add(column.getDataType().name());
List