org.apache.storm.tuple.Tuple.getBinary()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(6.2k)|赞(0)|评价(0)|浏览(84)

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

Tuple.getBinary介绍

[英]Returns the byte array at position i in the tuple.
[中]返回元组中位置i处的字节数组。

代码示例

代码示例来源:origin: apache/storm

@Override
  protected String getTupleValue(Tuple t, int idx) {
    return new String(t.getBinary(idx));
  }
}

代码示例来源:origin: apache/metron

@Override
 public byte[] get(Tuple tuple) {
  return tuple.getBinary(position);
 }
}

代码示例来源:origin: apache/metron

@Override
public Map<String, byte[]> getValues(Tuple tuple, JSONObject message) {
 Map<String, byte[]> values = new HashMap<>();
 values.put(column, tuple.getBinary(0));
 return values;
}

代码示例来源:origin: apache/metron

@Override
 public JSONObject get(Tuple tuple) {
  String s = null;
  try {
   s =  new String(tuple.getBinary(position), Charsets.UTF_8);
   return (JSONObject) parser.get().parse(s);
  } catch (Exception e) {
   throw new IllegalStateException("Unable to parse " + s + " due to " + e.getMessage(), e);
  }
 }
}

代码示例来源:origin: apache/metron

@Override
public JSONObject generateMessage(Tuple tuple) {
 JSONObject message = null;
 if (messageFieldName == null) {
  byte[] data = tuple.getBinary(0);
  try {
   message = (JSONObject) parser.parse(new String(data, "UTF8"));
   message.put(getClass().getSimpleName().toLowerCase() + ".splitter.begin.ts", "" + System.currentTimeMillis());
  } catch (ParseException | UnsupportedEncodingException e) {
   e.printStackTrace();
  }
 } else {
  message = (JSONObject) tuple.getValueByField(messageFieldName);
  message.put(getClass().getSimpleName().toLowerCase() + ".splitter.begin.ts", "" + System.currentTimeMillis());
 }
 return message;
}

代码示例来源:origin: apache/metron

public Tuple toTuple(byte[] record) {
  Tuple ret = mock(Tuple.class);
  when(ret.getStringByField("topic")).thenReturn(sensorType);
  when(ret.getBinary(eq(0))).thenReturn(record);
  return ret;
 }
}

代码示例来源:origin: apache/metron

byte[] keyObj = t.getBinary(KEY_INDEX);
String keyStr = null;
try {

代码示例来源:origin: apache/metron

@Test
public void jsonFromPositionShouldReturnJSON() {
 Tuple tuple = mock(Tuple.class);
 when(tuple.getBinary(1)).thenReturn("{\"field\":\"value\"}".getBytes(UTF_8));
 JSONObject expected = new JSONObject();
 expected.put("field", "value");
 MessageGetStrategy messageGetStrategy = MessageGetters.JSON_FROM_POSITION.get("1");
 assertEquals(expected, messageGetStrategy.get(tuple));
}

代码示例来源:origin: apache/metron

@Test
public void defaultJSONFromPositionShouldReturnJSON() {
 Tuple tuple = mock(Tuple.class);
 when(tuple.getBinary(0)).thenReturn("{\"field\":\"value\"}".getBytes(UTF_8));
 JSONObject expected = new JSONObject();
 expected.put("field", "value");
 MessageGetStrategy messageGetStrategy = MessageGetters.DEFAULT_JSON_FROM_POSITION.get();
 assertEquals(expected, messageGetStrategy.get(tuple));
}

代码示例来源:origin: apache/metron

@Test
public void defaultBytesFromPositionShouldReturnBytes() {
 Tuple tuple = mock(Tuple.class);
 when(tuple.getBinary(0)).thenReturn("bytes".getBytes(UTF_8));
 MessageGetStrategy messageGetStrategy = MessageGetters.DEFAULT_BYTES_FROM_POSITION.get();
 assertEquals("bytes", new String((byte[]) messageGetStrategy.get(tuple), UTF_8));
}

代码示例来源:origin: apache/metron

@Test
public void bytesFromPositionShouldReturnBytes() {
 Tuple tuple = mock(Tuple.class);
 when(tuple.getBinary(1)).thenReturn("bytes".getBytes(UTF_8));
 MessageGetStrategy messageGetStrategy = MessageGetters.BYTES_FROM_POSITION.get("1");
 assertEquals("bytes", new String((byte[]) messageGetStrategy.get(tuple), UTF_8));
}

代码示例来源:origin: apache/metron

@Test
public void jsonFromPositionShouldThrowException() {
 exception.expect(IllegalStateException.class);
 Tuple tuple = mock(Tuple.class);
 when(tuple.getBinary(1)).thenReturn("{\"field\":".getBytes(UTF_8));
 MessageGetStrategy messageGetStrategy = MessageGetters.JSON_FROM_POSITION.get("1");
 messageGetStrategy.get(tuple);
}

代码示例来源:origin: apache/metron

private static Tuple createTuple(Map<String, Object> kafkaFields, String metadata) throws Exception {
 List<Map.Entry<String, Object>> fields = new ArrayList<>();
 for(Map.Entry<String, Object> kv : kafkaFields.entrySet()) {
  fields.add(kv);
 }
 Tuple t = mock(Tuple.class);
 Fields f = mock(Fields.class);
 when(f.size()).thenReturn(fields.size()+2);
 for(int i = 0;i < fields.size();++i) {
  when(f.get(eq(i + 2))).thenReturn(fields.get(i).getKey());
  when(t.getValue(eq(i + 2))).thenReturn(fields.get(i).getValue());
 }
 when(t.getFields()).thenReturn(f);
 when(t.getBinary(eq(MetadataUtil.KEY_INDEX))).thenReturn(metadata.getBytes());
 return t;
}

代码示例来源:origin: apache/metron

/**
 * If an invalid message is sent to indexing, the message should be handled as an error
 * and the topology should continue processing.
 */
@Test
public void testMessageInvalid() throws Exception {
 FakeClock clock = new FakeClock();
 // setup the bolt
 BulkMessageWriterBolt<IndexingConfigurations> bolt = new BulkMessageWriterBolt<IndexingConfigurations>(
   "zookeeperUrl", "INDEXING")
     .withBulkMessageWriter(bulkMessageWriter)
     .withMessageGetter(MessageGetters.JSON_FROM_POSITION.name())
     .withMessageGetterField("message");
 bolt.setCuratorFramework(client);
 bolt.setZKCache(cache);
 bolt.getConfigurations().updateSensorIndexingConfig(sensorType, new FileInputStream(sampleSensorIndexingConfigPath));
 // initialize the bolt
 bolt.declareOutputFields(declarer);
 Map stormConf = new HashMap();
 bolt.prepare(stormConf, topologyContext, outputCollector, clock);
 // execute a tuple that contains an invalid message
 byte[] invalidJSON = "this is not valid JSON".getBytes();
 when(tuple.getBinary(0)).thenReturn(invalidJSON);
 bolt.execute(tuple);
 // the tuple should be handled as an error and ack'd
 verify(outputCollector, times(1)).emit(eq(Constants.ERROR_STREAM), any());
 verify(outputCollector, times(1)).ack(tuple);
}

代码示例来源:origin: org.apache.storm/storm-core

@Override
  protected String getTupleValue(Tuple t, int idx) {
   return new String(t.getBinary(idx));
  }
}

代码示例来源:origin: apache/metron

key = enrichmentSplitterBolt.getKey(tuple, sampleMessage);
Assert.assertEquals(guid, key);
when(tuple.getBinary(0)).thenReturn(sampleMessageString.getBytes());
JSONObject generatedMessage = enrichmentSplitterBolt.generateMessage(tuple);
removeTimingFields(generatedMessage);

相关文章