org.apache.kafka.connect.data.Struct.getString()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(8.3k)|赞(0)|评价(0)|浏览(224)

本文整理了Java中org.apache.kafka.connect.data.Struct.getString()方法的一些代码示例,展示了Struct.getString()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Struct.getString()方法的具体详情如下:
包路径:org.apache.kafka.connect.data.Struct
类名称:Struct
方法名:getString

Struct.getString介绍

[英]Equivalent to calling #get(String) and casting the result to a String.
[中]相当于调用#get(String)并将结果转换为字符串。

代码示例

代码示例来源:origin: debezium/debezium

/**
   * Obtain the operation for the given source record.
   *
   * @param record the source record; may not be null
   * @return the operation, or null if no valid operation was found in the record
   */
  public static Operation operationFor(SourceRecord record) {
    Struct value = (Struct) record.value();
    Field opField = value.schema().field(FieldName.OPERATION);
    if (opField != null) {
      return Operation.forCode(value.getString(opField.name()));
    }
    return null;
  }
}

代码示例来源:origin: debezium/debezium

protected void assertSourceInfo(SourceRecord record) {
  assertTrue(record.value() instanceof Struct);
  Struct source = ((Struct) record.value()).getStruct("source");
  assertNotNull(source.getString("db"));
  assertNotNull(source.getString("schema"));
  assertNotNull(source.getString("table"));
}

代码示例来源:origin: debezium/debezium

protected void assertSourceInfo(SourceRecord record, String db, String schema, String table) {
  assertTrue(record.value() instanceof Struct);
  Struct source = ((Struct) record.value()).getStruct("source");
  assertEquals(db, source.getString("db"));
  assertEquals(schema, source.getString("schema"));
  assertEquals(table, source.getString("table"));
}

代码示例来源:origin: debezium/debezium

protected String getAffectedDatabase(SourceRecord record) {
  Struct value = (Struct) record.value();
  if (value != null) {
    Field dbField = value.schema().field(HistoryRecord.Fields.DATABASE_NAME);
    if (dbField != null) {
      return value.getString(dbField.name());
    }
  }
  return null;
}

代码示例来源:origin: debezium/debezium

protected String getAffectedDatabase(SourceRecord record) {
    Struct envelope = (Struct) record.value();
    Field dbField = envelope.schema().field(HistoryRecord.Fields.DATABASE_NAME);
    if (dbField != null) {
      return envelope.getString(dbField.name());
    }
    return null;
  }
}

代码示例来源:origin: debezium/debezium

protected void verifyOperation(SourceRecord record, Operation expected) {
  Struct value = (Struct) record.value();
  assertThat(value.getString(Envelope.FieldName.OPERATION)).isEqualTo(expected.code());
}

代码示例来源:origin: debezium/debezium

@Test
public void testHandleDeleteRewrite() {
  try (final UnwrapFromEnvelope<SourceRecord> transform = new UnwrapFromEnvelope<>()) {
    final Map<String, String> props = new HashMap<>();
    props.put(HANDLE_DELETES, "rewrite");
    transform.configure(props);
    final SourceRecord deleteRecord = createDeleteRecord();
    final SourceRecord unwrapped = transform.apply(deleteRecord);
    assertThat(((Struct)unwrapped.value()).getString("__deleted")).isEqualTo("true");
  }
}

代码示例来源:origin: debezium/debezium

@Test
  public void connectorIsPresent() {
    assertThat(source.source().getString(SourceInfo.DEBEZIUM_CONNECTOR_KEY)).isEqualTo(Module.name());
  }
}

代码示例来源:origin: debezium/debezium

@Test
  public void connectorIsPresent() {
    assertThat(source.struct().getString(SourceInfo.DEBEZIUM_CONNECTOR_KEY)).isEqualTo(Module.name());
  }
}

代码示例来源:origin: debezium/debezium

@Test
  public void connectorIsPresent() {
    assertThat(source.offsetStructForEvent("rs", null).getString(SourceInfo.DEBEZIUM_CONNECTOR_KEY)).isEqualTo(Module.name());
  }
}

代码示例来源:origin: debezium/debezium

@Test
public void versionIsPresent() {
  assertThat(source.struct().getString(SourceInfo.DEBEZIUM_VERSION_KEY)).isEqualTo(Module.version());
}

代码示例来源:origin: debezium/debezium

@Test
public void versionIsPresent() {
  assertThat(source.offsetStructForEvent("rs", null).getString(SourceInfo.DEBEZIUM_VERSION_KEY)).isEqualTo(Module.version());
}

代码示例来源:origin: debezium/debezium

@Test
public void versionIsPresent() {
  assertThat(source.source().getString(SourceInfo.DEBEZIUM_VERSION_KEY)).isEqualTo(Module.version());
}

代码示例来源:origin: debezium/debezium

@Test
public void shouldExcludeFieldsForDeleteEvent() throws InterruptedException {
  // given
  CollectionId collectionId = new CollectionId("rs0", "dbA", "c1");
  ObjectId objId = new ObjectId();
  Document obj = new Document("_id", objId);
  Filters filters = build.excludeFields("*.c1.name,*.c1.active").createFilters();
  List<SourceRecord> produced = new ArrayList<>();
  RecordMakers recordMakers = new RecordMakers(filters, source, topicSelector, produced::add, true);
  // when
  recordMakers.forCollection(collectionId).recordEvent(createEvent(obj, "d"), 1002);
  // then
  Struct value = getValue(produced);
  String json = value.getString(AFTER);
  if (json == null) {
    json = value.getString(PATCH);
  }
  assertThat(json).isNull();
}

代码示例来源:origin: debezium/debezium

@Test
public void shouldRenameFieldsForDeleteEvent() throws InterruptedException {
  // given
  CollectionId collectionId = new CollectionId("rs0", "dbA", "c1");
  ObjectId objId = new ObjectId();
  Document obj = new Document("_id", objId);
  Filters filters = build.renameFields("*.c1.name:new_name,*.c1.active:new_active").createFilters();
  List<SourceRecord> produced = new ArrayList<>();
  RecordMakers recordMakers = new RecordMakers(filters, source, topicSelector, produced::add, true);
  // when
  recordMakers.forCollection(collectionId).recordEvent(createEvent(obj, "d"), 1002);
  // then
  Struct value = getValue(produced);
  String json = value.getString(AFTER);
  if (json == null) {
    json = value.getString(PATCH);
  }
  assertThat(json).isNull();
}

代码示例来源:origin: debezium/debezium

@Test
public void versionIsPresent() {
  sourceWith(offset(100, 5, true));
  assertThat(source.struct().getString(SourceInfo.DEBEZIUM_VERSION_KEY)).isEqualTo(Module.version());
}

代码示例来源:origin: debezium/debezium

@Test
public void connectorIsPresent() {
  sourceWith(offset(100, 5, true));
  assertThat(source.struct().getString(SourceInfo.DEBEZIUM_CONNECTOR_KEY)).isEqualTo(Module.name());
}

代码示例来源:origin: debezium/debezium

@Test
public void testHandleCreateRewrite() {
  try (final UnwrapFromEnvelope<SourceRecord> transform = new UnwrapFromEnvelope<>()) {
    final Map<String, String> props = new HashMap<>();
    props.put(HANDLE_DELETES, "rewrite");
    props.put(OPERATION_HEADER, "true");
    transform.configure(props);
    final SourceRecord createRecord = createCreateRecord();
    final SourceRecord unwrapped = transform.apply(createRecord);
    assertThat(((Struct)unwrapped.value()).getString("__deleted")).isEqualTo("false");
    assertThat(unwrapped.headers()).hasSize(1);
    String headerValue = getSourceRecordHeaderByKey(unwrapped, transform.DEBEZIUM_OPERATION_HEADER_KEY);
    assertThat(headerValue).isEqualTo(Envelope.Operation.CREATE.code());
  }
}

代码示例来源:origin: debezium/debezium

@Test
public void shouldReturnOffsetForUnusedReplicaName() {
  assertThat(source.hasOffset(REPLICA_SET_NAME)).isEqualTo(false);
  Map<String, ?> offset = source.lastOffset(REPLICA_SET_NAME);
  assertThat(offset.get(SourceInfo.TIMESTAMP)).isEqualTo(0);
  assertThat(offset.get(SourceInfo.ORDER)).isEqualTo(0);
  assertThat(offset.get(SourceInfo.OPERATION_ID)).isNull();
  BsonTimestamp ts = source.lastOffsetTimestamp(REPLICA_SET_NAME);
  assertThat(ts.getTime()).isEqualTo(0);
  assertThat(ts.getInc()).isEqualTo(0);
  Struct struct = source.lastOffsetStruct(REPLICA_SET_NAME,new CollectionId(REPLICA_SET_NAME,"dbA","collectA"));
  assertThat(struct.getInt32(SourceInfo.TIMESTAMP)).isEqualTo(0);
  assertThat(struct.getInt32(SourceInfo.ORDER)).isEqualTo(0);
  assertThat(struct.getInt64(SourceInfo.OPERATION_ID)).isNull();
  assertThat(struct.getString(SourceInfo.NAMESPACE)).isEqualTo("dbA.collectA");
  assertThat(struct.getString(SourceInfo.REPLICA_SET_NAME)).isEqualTo(REPLICA_SET_NAME);
  assertThat(struct.getString(SourceInfo.SERVER_NAME)).isEqualTo("serverX");
  assertThat(struct.getBoolean(SourceInfo.INITIAL_SYNC)).isNull();
  assertThat(source.hasOffset(REPLICA_SET_NAME)).isEqualTo(false);
}

代码示例来源:origin: debezium/debezium

/**
 * Verify that the given {@link SourceRecord} is a {@link Operation#READ READ} record.
 *
 * @param record the source record; may not be null
 */
public static void isValidRead(SourceRecord record) {
  assertThat(record.key()).isNotNull();
  assertThat(record.keySchema()).isNotNull();
  assertThat(record.valueSchema()).isNotNull();
  Struct value = (Struct) record.value();
  assertThat(value).isNotNull();
  assertThat(value.getString(FieldName.OPERATION)).isEqualTo(Operation.READ.code());
  assertThat(value.get(FieldName.AFTER)).isNotNull();
  assertThat(value.get(FieldName.BEFORE)).isNull();
}

相关文章