Java Code Examples for org.apache.iceberg.types.TypeUtil#visit()

The following examples show how to use org.apache.iceberg.types.TypeUtil#visit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RandomData.java    From iceberg with Apache License 2.0 6 votes vote down vote up
public static Iterable<InternalRow> generateSpark(Schema schema, int numRecords, long seed) {
  return () -> new Iterator<InternalRow>() {
    private SparkRandomDataGenerator generator = new SparkRandomDataGenerator(seed);
    private int count = 0;

    @Override
    public boolean hasNext() {
      return count < numRecords;
    }

    @Override
    public InternalRow next() {
      if (count >= numRecords) {
        throw new NoSuchElementException();
      }
      count += 1;
      return (InternalRow) TypeUtil.visit(schema, generator);
    }
  };
}
 
Example 2
Source File: RandomData.java    From iceberg with Apache License 2.0 6 votes vote down vote up
private static Iterable<Record> newIterable(Supplier<RandomDataGenerator> newGenerator,
                                            Schema schema, int numRecords) {
  return () -> new Iterator<Record>() {
    private int count = 0;
    private RandomDataGenerator generator = newGenerator.get();

    @Override
    public boolean hasNext() {
      return count < numRecords;
    }

    @Override
    public Record next() {
      if (count >= numRecords) {
        throw new NoSuchElementException();
      }
      count += 1;
      return (Record) TypeUtil.visit(schema, generator);
    }
  };
}
 
Example 3
Source File: RandomData.java    From iceberg with Apache License 2.0 6 votes vote down vote up
private static Iterable<Row> generateData(Schema schema, int numRecords, Supplier<RandomRowGenerator> supplier) {
  return () -> new Iterator<Row>() {
    private final RandomRowGenerator generator = supplier.get();
    private int count = 0;

    @Override
    public boolean hasNext() {
      return count < numRecords;
    }

    @Override
    public Row next() {
      if (!hasNext()) {
        throw new NoSuchElementException();
      }
      ++count;
      return (Row) TypeUtil.visit(schema, generator);
    }
  };
}
 
Example 4
Source File: ORCSchemaUtil.java    From iceberg with Apache License 2.0 4 votes vote down vote up
/**
 * Generates mapping from field IDs to ORC qualified names. See {@link IdToOrcName} for details.
 */
public static Map<Integer, String> idToOrcName(Schema schema) {
  return TypeUtil.visit(schema, new IdToOrcName());
}
 
Example 5
Source File: Accessors.java    From iceberg with Apache License 2.0 4 votes vote down vote up
static Map<Integer, Accessor<StructLike>> forSchema(Schema schema) {
  return TypeUtil.visit(schema, new BuildPositionAccessors());
}
 
Example 6
Source File: PartitionKey.java    From iceberg with Apache License 2.0 4 votes vote down vote up
private static Map<Integer, Accessor<InternalRow>> buildAccessors(Schema schema) {
  return TypeUtil.visit(schema, new BuildPositionAccessors());
}
 
Example 7
Source File: Spark3Util.java    From iceberg with Apache License 2.0 4 votes vote down vote up
public static String describe(Schema schema) {
  return TypeUtil.visit(schema, DescribeSchemaVisitor.INSTANCE);
}
 
Example 8
Source File: Spark3Util.java    From iceberg with Apache License 2.0 4 votes vote down vote up
public static String describe(Type type) {
  return TypeUtil.visit(type, DescribeSchemaVisitor.INSTANCE);
}
 
Example 9
Source File: AvroSchemaUtil.java    From iceberg with Apache License 2.0 4 votes vote down vote up
public static Schema convert(org.apache.iceberg.Schema schema,
                             Map<Types.StructType, String> names) {
  return TypeUtil.visit(schema, new TypeToSchema(names));
}
 
Example 10
Source File: AvroSchemaUtil.java    From iceberg with Apache License 2.0 4 votes vote down vote up
public static Schema convert(Type type, Map<Types.StructType, String> names) {
  return TypeUtil.visit(type, new TypeToSchema(names));
}
 
Example 11
Source File: AvroSchemaUtil.java    From iceberg with Apache License 2.0 4 votes vote down vote up
public static Map<Type, Schema> convertTypes(Types.StructType type, String name) {
  TypeToSchema converter = new TypeToSchema(ImmutableMap.of(type, name));
  TypeUtil.visit(type, converter);
  return ImmutableMap.copyOf(converter.getConversionMap());
}
 
Example 12
Source File: SparkSchemaUtil.java    From iceberg with Apache License 2.0 2 votes vote down vote up
/**
 * Convert a {@link Schema} to a {@link DataType Spark type}.
 *
 * @param schema a Schema
 * @return the equivalent Spark type
 * @throws IllegalArgumentException if the type cannot be converted to Spark
 */
public static StructType convert(Schema schema) {
  return (StructType) TypeUtil.visit(schema, new TypeToSparkType());
}
 
Example 13
Source File: SparkSchemaUtil.java    From iceberg with Apache License 2.0 2 votes vote down vote up
/**
 * Convert a {@link Type} to a {@link DataType Spark type}.
 *
 * @param type a Type
 * @return the equivalent Spark type
 * @throws IllegalArgumentException if the type cannot be converted to Spark
 */
public static DataType convert(Type type) {
  return TypeUtil.visit(type, new TypeToSparkType());
}
 
Example 14
Source File: MappingUtil.java    From iceberg with Apache License 2.0 2 votes vote down vote up
/**
 * Create a name-based mapping for a schema.
 * <p>
 * The mapping returned by this method will use the schema's name for each field.
 *
 * @param schema a {@link Schema}
 * @return a {@link NameMapping} initialized with the schema's fields and names
 */
public static NameMapping create(Schema schema) {
  return new NameMapping(TypeUtil.visit(schema, CreateMapping.INSTANCE));
}
 
Example 15
Source File: IcebergUtils.java    From dremio-oss with Apache License 2.0 2 votes vote down vote up
/**
 *
 * @param schema iceberg schema
 * @return column name to integer ID mapping
 */
public static Map<String, Integer> getIcebergColumnNameToIDMap(Schema schema) {
  Map<String, Integer> schemaNameIDMap = TypeUtil.visit(Types.StructType.of(schema.columns()), new DremioIndexByName());
  return CaseInsensitiveMap.newImmutableMap(schemaNameIDMap);
}