当前位置: 首页>>代码示例>>Java>>正文


Java Schema.createRecord方法代码示例

本文整理汇总了Java中org.apache.avro.Schema.createRecord方法的典型用法代码示例。如果您正苦于以下问题:Java Schema.createRecord方法的具体用法?Java Schema.createRecord怎么用?Java Schema.createRecord使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.avro.Schema的用法示例。


在下文中一共展示了Schema.createRecord方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testValidateSchemaPairAllFields

import org.apache.avro.Schema; //导入方法依赖的package包/类
@Test
public void testValidateSchemaPairAllFields() throws Exception {
  final List<Schema.Field> readerFields = list(
      new Schema.Field("oldfield1", INT_SCHEMA, null, null),
      new Schema.Field("oldfield2", STRING_SCHEMA, null, null));
  final Schema reader = Schema.createRecord(readerFields);
  final SchemaCompatibility.SchemaPairCompatibility expectedResult =
      new SchemaCompatibility.SchemaPairCompatibility(
          SchemaCompatibility.SchemaCompatibilityResult.compatible(),
          reader,
          WRITER_SCHEMA,
          SchemaCompatibility.READER_WRITER_COMPATIBLE_MESSAGE);

  // Test with all fields.
  assertEquals(expectedResult, checkReaderWriterCompatibility(reader, WRITER_SCHEMA));
}
 
开发者ID:HotelsDotCom,项目名称:avro-compatibility,代码行数:17,代码来源:TestSchemaCompatibility.java

示例2: testValidateSchemaNewFieldWithDefault

import org.apache.avro.Schema; //导入方法依赖的package包/类
@Test
public void testValidateSchemaNewFieldWithDefault() throws Exception {
  final List<Schema.Field> readerFields = list(
      new Schema.Field("oldfield1", INT_SCHEMA, null, null),
      new Schema.Field("newfield1", INT_SCHEMA, null, 42));
  final Schema reader = Schema.createRecord(readerFields);
  final SchemaCompatibility.SchemaPairCompatibility expectedResult =
      new SchemaCompatibility.SchemaPairCompatibility(
          SchemaCompatibility.SchemaCompatibilityResult.compatible(),
          reader,
          WRITER_SCHEMA,
          SchemaCompatibility.READER_WRITER_COMPATIBLE_MESSAGE);

  // Test new field with default value.
  assertEquals(expectedResult, checkReaderWriterCompatibility(reader, WRITER_SCHEMA));
}
 
开发者ID:HotelsDotCom,项目名称:avro-compatibility,代码行数:17,代码来源:TestSchemaCompatibility.java

示例3: testValidateSchemaNewField

import org.apache.avro.Schema; //导入方法依赖的package包/类
@Test
public void testValidateSchemaNewField() throws Exception {
  final List<Schema.Field> readerFields = list(
      new Schema.Field("oldfield1", INT_SCHEMA, null, null),
      new Schema.Field("newfield1", INT_SCHEMA, null, null));
  final Schema reader = Schema.createRecord(readerFields);
  SchemaPairCompatibility compatibility = checkReaderWriterCompatibility(reader, WRITER_SCHEMA);

  // Test new field without default value.
  assertEquals(SchemaCompatibility.SchemaCompatibilityType.INCOMPATIBLE, compatibility.getType());
  assertEquals(SchemaCompatibility.SchemaCompatibilityResult.incompatible(SchemaIncompatibilityType.READER_FIELD_MISSING_DEFAULT_VALUE, reader, WRITER_SCHEMA, "newfield1", Arrays.asList("", "fields", "1")), compatibility.getResult());
  assertEquals(String.format("Data encoded using writer schema:%n%s%n"
      + "will or may fail to decode using reader schema:%n%s%n",
      WRITER_SCHEMA.toString(true),
      reader.toString(true)), compatibility.getDescription());
  assertEquals(reader, compatibility.getReader());
  assertEquals(WRITER_SCHEMA, compatibility.getWriter());
}
 
开发者ID:HotelsDotCom,项目名称:avro-compatibility,代码行数:19,代码来源:TestSchemaCompatibility.java

示例4: testParquetRecordsNotSupported

import org.apache.avro.Schema; //导入方法依赖的package包/类
public void testParquetRecordsNotSupported() throws IOException, SQLException {
  String[] argv = {};
  final int TOTAL_RECORDS = 1;

  Schema schema =  Schema.createRecord("nestedrecord", null, null, false);
  schema.setFields(Lists.newArrayList(buildField("myint",
      Schema.Type.INT)));
  GenericRecord record = new GenericData.Record(schema);
  record.put("myint", 100);
  // DB type is not used so can be anything:
  ColumnGenerator gen = colGenerator(record, schema, null, "VARCHAR(64)");
  createParquetFile(0, TOTAL_RECORDS,  gen);
  createTable(gen);
  try {
    runExport(getArgv(true, 10, 10, newStrArray(argv, "-m", "" + 1)));
    fail("Parquet records can not be exported.");
  } catch (Exception e) {
    // expected
    assertTrue(true);
  }
}
 
开发者ID:aliyun,项目名称:aliyun-maxcompute-data-collectors,代码行数:22,代码来源:TestParquetExport.java

示例5: testAvroRecordsNotSupported

import org.apache.avro.Schema; //导入方法依赖的package包/类
public void testAvroRecordsNotSupported() throws IOException, SQLException {
  String[] argv = {};
  final int TOTAL_RECORDS = 1;

  Schema schema =  Schema.createRecord("nestedrecord", null, null, false);
  schema.setFields(Lists.newArrayList(buildAvroField("myint",
      Schema.Type.INT)));
  GenericRecord record = new GenericData.Record(schema);
  record.put("myint", 100);
  // DB type is not used so can be anything:
  ColumnGenerator gen = colGenerator(record, schema, null, "VARCHAR(64)");
  createAvroFile(0, TOTAL_RECORDS,  gen);
  createTable(gen);
  try {
    runExport(getArgv(true, 10, 10, newStrArray(argv, "-m", "" + 1)));
    fail("Avro records can not be exported.");
  } catch (Exception e) {
    // expected
    assertTrue(true);
  }
}
 
开发者ID:aliyun,项目名称:aliyun-maxcompute-data-collectors,代码行数:22,代码来源:TestAvroExport.java

示例6: createAvroRecordSchema

import org.apache.avro.Schema; //导入方法依赖的package包/类
/**
 * Creates the avro record schema
 * 
 * @param recordName - Name of record.
 * @param columnNames - Column/field names
 * @param schemas - Avro schema for each field/column
 * @return - Avro Record schema
 */
public static Schema createAvroRecordSchema(String recordName, String[] columnNames,
    Schema[] schemas) {
  Schema record = Schema.createRecord(recordName, null, null, false);
  // create fields
  List<Schema.Field> fields = new ArrayList<>();
  // String field
  for (int i = 0; i < columnNames.length; i++) {
    String fieldName = columnNames[i];
    Schema fieldType = schemas[i];
    Schema.Field field = new Schema.Field(fieldName, fieldType, null, null,
        ParquetColumnConverterDescriptor.DEFAULT_ORDER);
    fields.add(field);
  }
  record.setFields(fields);
  return record;
}
 
开发者ID:ampool,项目名称:monarch,代码行数:25,代码来源:ParquetUtils.java

示例7: buildSchema

import org.apache.avro.Schema; //导入方法依赖的package包/类
private Schema buildSchema(ColumnGenerator... extraCols) {
  List<Field> fields = new ArrayList<Field>();
  fields.add(buildField("id", Schema.Type.INT));
  fields.add(buildField("msg", Schema.Type.STRING));
  int colNum = 0;
  for (ColumnGenerator gen : extraCols) {
    if (gen.getColumnParquetSchema() != null) {
      fields.add(buildParquetField(forIdx(colNum++),
          gen.getColumnParquetSchema()));
    }
  }
  Schema schema = Schema.createRecord("myschema", null, null, false);
  schema.setFields(fields);
  return schema;
}
 
开发者ID:aliyun,项目名称:aliyun-maxcompute-data-collectors,代码行数:16,代码来源:TestParquetExport.java

示例8: testValidateSchemaPairMissingField

import org.apache.avro.Schema; //导入方法依赖的package包/类
@Test
public void testValidateSchemaPairMissingField() throws Exception {
  final List<Schema.Field> readerFields = list(
      new Schema.Field("oldfield1", INT_SCHEMA, null, null));
  final Schema reader = Schema.createRecord(readerFields);
  final SchemaCompatibility.SchemaPairCompatibility expectedResult =
      new SchemaCompatibility.SchemaPairCompatibility(
          SchemaCompatibility.SchemaCompatibilityResult.compatible(),
          reader,
          WRITER_SCHEMA,
          SchemaCompatibility.READER_WRITER_COMPATIBLE_MESSAGE);

  // Test omitting a field.
  assertEquals(expectedResult, checkReaderWriterCompatibility(reader, WRITER_SCHEMA));
}
 
开发者ID:HotelsDotCom,项目名称:avro-compatibility,代码行数:16,代码来源:TestSchemaCompatibility.java

示例9: testValidateSchemaPairMissingSecondField

import org.apache.avro.Schema; //导入方法依赖的package包/类
@Test
public void testValidateSchemaPairMissingSecondField() throws Exception {
  final List<Schema.Field> readerFields = list(
      new Schema.Field("oldfield2", STRING_SCHEMA, null, null));
  final Schema reader = Schema.createRecord(readerFields);
  final SchemaCompatibility.SchemaPairCompatibility expectedResult =
      new SchemaCompatibility.SchemaPairCompatibility(
          SchemaCompatibility.SchemaCompatibilityResult.compatible(),
          reader,
          WRITER_SCHEMA,
          SchemaCompatibility.READER_WRITER_COMPATIBLE_MESSAGE);

  // Test omitting other field.
  assertEquals(expectedResult, checkReaderWriterCompatibility(reader, WRITER_SCHEMA));
}
 
开发者ID:HotelsDotCom,项目名称:avro-compatibility,代码行数:16,代码来源:TestSchemaCompatibility.java

示例10: AvroRecordSchema

import org.apache.avro.Schema; //导入方法依赖的package包/类
public AvroRecordSchema( final StructContainerField schema ) throws IOException{
  this.schema = schema;

  List<Schema.Field> childList = new ArrayList<Schema.Field>();
  for( String key : schema.getKeys() ){
    childList.add( new Schema.Field( key , AvroSchemaFactory.getAvroSchema( schema.get( key ) ) , null , null ) );
  }
  avroSchema = Schema.createRecord( childList );
}
 
开发者ID:yahoojapan,项目名称:dataplatform-schema-lib,代码行数:10,代码来源:AvroRecordSchema.java

示例11: generate

import org.apache.avro.Schema; //导入方法依赖的package包/类
public Schema generate(String schemaNameOverride) throws IOException {
  ClassWriter classWriter = new ClassWriter(options, connManager,
      tableName, null);
  Map<String, Integer> columnTypes = classWriter.getColumnTypes();
  String[] columnNames = classWriter.getColumnNames(columnTypes);

  List<Field> fields = new ArrayList<Field>();
  for (String columnName : columnNames) {
    String cleanedCol = AvroUtil.toAvroIdentifier(ClassWriter.toJavaIdentifier(columnName));
    int sqlType = columnTypes.get(columnName);
    Schema avroSchema = toAvroSchema(sqlType, columnName);
    Field field = new Field(cleanedCol, avroSchema, null,  NullNode.getInstance());
    field.addProp("columnName", columnName);
    field.addProp("sqlType", Integer.toString(sqlType));
    fields.add(field);
  }

  TableClassName tableClassName = new TableClassName(options);
  String shortClassName = tableClassName.getShortClassForTable(tableName);
  String avroTableName = (tableName == null ? TableClassName.QUERY_RESULT : tableName);
  String avroName = schemaNameOverride != null ? schemaNameOverride :
      (shortClassName == null ? avroTableName : shortClassName);
  String avroNamespace = tableClassName.getPackageForTable();

  String doc = "Sqoop import of " + avroTableName;
  Schema schema = Schema.createRecord(avroName, doc, avroNamespace, false);
  schema.setFields(fields);
  schema.addProp("tableName", avroTableName);
  return schema;
}
 
开发者ID:aliyun,项目名称:aliyun-maxcompute-data-collectors,代码行数:31,代码来源:AvroSchemaGenerator.java

示例12: buildAvroSchema

import org.apache.avro.Schema; //导入方法依赖的package包/类
private Schema buildAvroSchema(ColumnGenerator... extraCols) {
  List<Field> fields = new ArrayList<Field>();
  fields.add(buildAvroField("id", Schema.Type.INT));
  fields.add(buildAvroField("msg", Schema.Type.STRING));
  int colNum = 0;
  for (ColumnGenerator gen : extraCols) {
    if (gen.getColumnAvroSchema() != null) {
      fields.add(buildAvroField(forIdx(colNum++),
          gen.getColumnAvroSchema()));
    }
  }
  Schema schema = Schema.createRecord("myschema", null, null, false);
  schema.setFields(fields);
  return schema;
}
 
开发者ID:aliyun,项目名称:aliyun-maxcompute-data-collectors,代码行数:16,代码来源:TestAvroExport.java

示例13: computeSchema

import org.apache.avro.Schema; //导入方法依赖的package包/类
private Schema computeSchema(String streamName, SamzaSqlRelMessage relMessage) {
  List<Schema.Field> keyFields = new ArrayList<>();
  List<String> fieldNames = relMessage.getFieldNames();
  List<Object> values = relMessage.getFieldValues();

  for (int index = 0; index < fieldNames.size(); index++) {
    if (fieldNames.get(index).equals(SamzaSqlRelMessage.KEY_NAME) || values.get(index) == null) {
      continue;
    }

    Object value = values.get(index);
    Schema avroType;
    if (value instanceof GenericData.Record) {
      avroType = ((GenericData.Record) value).getSchema();
    } else {
      avroType = ReflectData.get().getSchema(value.getClass());
    }
    keyFields.add(new Schema.Field(fieldNames.get(index), avroType, "", null));
  }

  Schema ks = Schema.createRecord(streamName, "", streamName + "_namespace", false);
  ks.setFields(keyFields);
  String schemaStr = ks.toString();
  Schema schema;
  // See whether we have a schema object corresponding to the schemaValue and reuse it.
  // CachedSchemaRegistryClient doesn't like if we recreate schema objects.
  if (_schemas.containsKey(schemaStr)) {
    schema = _schemas.get(schemaStr);
  } else {
    schema = Schema.parse(schemaStr);
    _schemas.put(schemaStr, schema);
  }

  return schema;
}
 
开发者ID:srinipunuru,项目名称:samza-sql-tools,代码行数:36,代码来源:AvroSchemaGenRelConverter.java

示例14: createAvroSchema

import org.apache.avro.Schema; //导入方法依赖的package包/类
private Schema createAvroSchema() {
  Schema avroSchema = Schema.createRecord("Doc", "adoc", null, false);
  List<Field> fields = new ArrayList<Field>();
  fields.add(new Field("id", Schema.create(Type.STRING), null, null));
  fields.add(new Field("user_friends_count",
                       createOptional(Schema.create(Type.INT)),
                       null, null));
  fields.add(new Field("user_location",
                       createOptional(Schema.create(Type.STRING)),
                       null, null));
  fields.add(new Field("user_description",
                       createOptional(Schema.create(Type.STRING)),
                       null, null));
  fields.add(new Field("user_statuses_count",
                       createOptional(Schema.create(Type.INT)),
                       null, null));
  fields.add(new Field("user_followers_count",
                       createOptional(Schema.create(Type.INT)),
                       null, null));
  fields.add(new Field("user_name",
                       createOptional(Schema.create(Type.STRING)),
                       null, null));
  fields.add(new Field("user_screen_name",
                       createOptional(Schema.create(Type.STRING)),
                       null, null));
  fields.add(new Field("created_at",
                       createOptional(Schema.create(Type.STRING)),
                       null, null));
  fields.add(new Field("text",
                       createOptional(Schema.create(Type.STRING)),
                       null, null));
  fields.add(new Field("retweet_count",
                       createOptional(Schema.create(Type.LONG)),
                       null, null));
  fields.add(new Field("retweeted",
                       createOptional(Schema.create(Type.BOOLEAN)),
                       null, null));
  fields.add(new Field("in_reply_to_user_id",
                       createOptional(Schema.create(Type.LONG)),
                       null, null));
  fields.add(new Field("source",
                       createOptional(Schema.create(Type.STRING)),
                       null, null));
  fields.add(new Field("in_reply_to_status_id",
                       createOptional(Schema.create(Type.LONG)),
                       null, null));
  fields.add(new Field("media_url_https",
                       createOptional(Schema.create(Type.STRING)),
                       null, null));
  fields.add(new Field("expanded_url",
                       createOptional(Schema.create(Type.STRING)),
                       null, null));
  avroSchema.setFields(fields);
  return avroSchema;
}
 
开发者ID:moueimei,项目名称:flume-release-1.7.0,代码行数:56,代码来源:TwitterSource.java

示例15: createAvroFile

import org.apache.avro.Schema; //导入方法依赖的package包/类
public void createAvroFile(File file, String codec, boolean useSchemaUrl,
                           boolean useStaticSchemaUrl) throws IOException {
  // serialize a few events using the reflection-based avro serializer
  OutputStream out = new FileOutputStream(file);

  Context ctx = new Context();
  if (codec != null) {
    ctx.put("compressionCodec", codec);
  }

  Schema schema = Schema.createRecord("myrecord", null, null, false);
  schema.setFields(Arrays.asList(new Schema.Field[]{
      new Schema.Field("message", Schema.create(Schema.Type.STRING), null, null)
  }));
  GenericRecordBuilder recordBuilder = new GenericRecordBuilder(schema);
  File schemaFile = null;
  if (useSchemaUrl || useStaticSchemaUrl) {
    schemaFile = File.createTempFile(getClass().getSimpleName(), ".avsc");
    Files.write(schema.toString(), schemaFile, Charsets.UTF_8);
  }

  if (useStaticSchemaUrl) {
    ctx.put(AvroEventSerializerConfigurationConstants.STATIC_SCHEMA_URL,
            schemaFile.toURI().toURL().toExternalForm());
  }

  EventSerializer.Builder builder = new AvroEventSerializer.Builder();
  EventSerializer serializer = builder.build(ctx, out);

  serializer.afterCreate();
  for (int i = 0; i < 3; i++) {
    GenericRecord record = recordBuilder.set("message", "Hello " + i).build();
    Event event = EventBuilder.withBody(serializeAvro(record, schema));
    if (schemaFile == null && !useSchemaUrl) {
      event.getHeaders().put(AvroEventSerializer.AVRO_SCHEMA_LITERAL_HEADER,
          schema.toString());
    } else if (useSchemaUrl) {
      event.getHeaders().put(AvroEventSerializer.AVRO_SCHEMA_URL_HEADER,
          schemaFile.toURI().toURL().toExternalForm());
    }
    serializer.write(event);
  }
  serializer.flush();
  serializer.beforeClose();
  out.flush();
  out.close();
  if (schemaFile != null ) {
    schemaFile.delete();
  }

}
 
开发者ID:moueimei,项目名称:flume-release-1.7.0,代码行数:52,代码来源:TestAvroEventSerializer.java


注:本文中的org.apache.avro.Schema.createRecord方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。