java api读取本地paimon表

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;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值