java api地址:Java API | Apache Paimon
import com.yfw.mapper.User;
import org.apache.paimon.data.BinaryString;
import org.apache.paimon.data.InternalRow;
import org.apache.paimon.predicate.Predicate;
import org.apache.paimon.predicate.PredicateBuilder;
import org.apache.paimon.reader.RecordReader;
import org.apache.paimon.schema.TableSchema;
import org.apache.paimon.table.Table;
import org.apache.paimon.table.source.ReadBuilder;
import org.apache.paimon.table.source.Split;
import org.apache.paimon.table.source.TableRead;
import org.apache.paimon.types.*;
import java.lang.reflect.Field;
import java.util.List;
public class ReadTable {
public static void main(String[] args) throws Exception {
// 1. Create a ReadBuilder and push filter (`withFilter`)
// and projection (`withProjection`) if necessary
Table table = GetTable.getTable();
// 过滤条件
PredicateBuilder builder =
new PredicateBuilder(RowType.of(DataTypes.STRING(), DataTypes.INT()));
Predicate notNull = builder.isNotNull(0);
Predicate greaterOrEqual = builder.greaterOrEqual(1, 12);
int[] projection = new int[]{0, 1};
ReadBuilder readBuilder =
table.newReadBuilder()
.withProjection(projection);
//.withFilter(Lists.newArrayList(notNull, greaterOrEqual));
// 2. Plan splits in 'Coordinator' (or named 'Driver')
List<Split> splits = readBuilder.newScan().plan().splits();
// 3. Distribute these splits to different tasks
// 4. Read a split in task
TableRead read = readBuilder.newRead();
RecordReader<InternalRow> reader = read.createReader(splits);
TableSchema tableSchema = GetTable.getTableSchema();
reader.forEachRemaining(x -> {
try {
print(tableSchema, x);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
});
}
public static void print(TableSchema tableSchema, InternalRow row) throws InstantiationException, IllegalAccessException, NoSuchFieldException {
List<DataField> fields = tableSchema.fields();
User t = User.class.newInstance();
for (int i = 0; i < fields.size(); i++) {// 遍历每一个列
DataField dataField = fields.get(i);
String columnLabel = dataField.name();
int id = dataField.id();
DataType type = dataField.type();
Object val = null;
if (type instanceof VarCharType) {
BinaryString string = row.getString(id);
val = string.toString();
}
if (type instanceof IntType) {
val = row.getInt(id);
}
// 6.2使用反射,给对象的相应属性赋值
Field field = User.class.getDeclaredField(columnLabel);
field.setAccessible(true);
field.set(t, val);
}
System.out.println(t.getF1());
}
}
public class User {
private String f0;
private Integer f1;
public String getF0() {
return f0;
}
public void setF0(String f0) {
this.f0 = f0;
}
public Integer getF1() {
return f1;
}
public void setF1(Integer f1) {
this.f1 = f1;
}
}