當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。