org.apache.storm.utils.Utils.toByteArray()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(7.5k)|赞(0)|评价(0)|浏览(170)

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

Utils.toByteArray介绍

暂无

代码示例

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

/**
 * Deserialize ByteBuffer to String.
 * @param byteBuffer input ByteBuffer
 * @return deserialized string
 */
public static String deserializeString(ByteBuffer byteBuffer) {
  if (byteBuffer.hasArray()) {
    int base = byteBuffer.arrayOffset();
    return new String(byteBuffer.array(), base + byteBuffer.position(), byteBuffer.remaining(),
      StandardCharsets.UTF_8);
  } else {
    return new String(Utils.toByteArray(byteBuffer), StandardCharsets.UTF_8);
  }
}

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

@Override
public List<Object> deserialize(ByteBuffer ser) {
  String data = new String(Utils.toByteArray(ser), StandardCharsets.UTF_8);
  List<String> parts = org.apache.storm.sql.runtime.utils.Utils.split(data, delimiter);
  Preconditions.checkArgument(parts.size() == fieldNames.size(), "Invalid schema");
  ArrayList<Object> list = new ArrayList<>(fieldNames.size());
  list.addAll(parts);
  return list;
}

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

@Override
public List<Object> deserialize(ByteBuffer ser) {
  ObjectMapper mapper = new ObjectMapper();
  try {
    @SuppressWarnings("unchecked")
    HashMap<String, Object> map = mapper.readValue(Utils.toByteArray(ser), HashMap.class);
    ArrayList<Object> list = new ArrayList<>(fields.size());
    for (String f : fields) {
      list.add(map.get(f));
    }
    return list;
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

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

public List<Object> deserialize(ByteBuffer ser) {
  // Maintain backward compatibility for 0.10
  byte[] b = Utils.toByteArray(ser);
  return Utils.tuple(new Object[]{ b });
}

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

@Override
public Iterable<List<Object>> deserialize(ByteBuffer ser) {
  return asList(Utils.tuple(Utils.toByteArray(ser)));
}

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

@Override
public List<Object> deserialize(ByteBuffer ser) {
  try {
    Schema schema = schemas.getSchema(schemaString);
    DatumReader<GenericRecord> reader = new GenericDatumReader<>(schema);
    BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(Utils.toByteArray(ser), null);
    GenericRecord record = reader.read(null, decoder);
    ArrayList<Object> list = new ArrayList<>(fieldNames.size());
    for (String field : fieldNames) {
      Object value = record.get(field);
      // Avro strings are stored using a special Avro Utf8 type instead of using Java primitives
      list.add(SerdeUtils.convertAvroUtf8(value));
    }
    return list;
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

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

private List<IWorkerHook> deserializeWorkerHooks() {
  List<IWorkerHook> myHookList = new ArrayList<>();
  if (topology.is_set_worker_hooks()) {
    for (ByteBuffer hook : topology.get_worker_hooks()) {
      byte[] hookBytes = Utils.toByteArray(hook);
      IWorkerHook hookObject = Utils.javaDeserialize(hookBytes, IWorkerHook.class);
      myHookList.add(hookObject);
    }
  }
  return myHookList;
}

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

@Override
public List<Object> deserialize(ByteBuffer ser) {
  try {
    String data = new String(Utils.toByteArray(ser), StandardCharsets.UTF_8);
    CSVParser parser = CSVParser.parse(data, CSVFormat.RFC4180);
    CSVRecord record = parser.getRecords().get(0);
    Preconditions.checkArgument(record.size() == fieldNames.size(), "Invalid schema");
    ArrayList<Object> list = new ArrayList<>(fieldNames.size());
    for (int i = 0; i < record.size(); i++) {
      list.add(record.get(i));
    }
    return list;
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

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

@Test
 public void testJsonSerializer() {
  final List<String> fields = Lists.newArrayList("ID", "val");
  List<Object> o = Lists.<Object> newArrayList(1, "2");
  JsonSerializer s = new JsonSerializer(fields);
  ByteBuffer buf = s.write(o, null);
  byte[] b = Utils.toByteArray(buf);
  assertEquals("{\"ID\":1,\"val\":\"2\"}", new String(b));
 }
}

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

@Override
 public byte[] serialize(String s, ByteBuffer b) {
  return Utils.toByteArray(b);
 }
}

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

public static String deserializeString(ByteBuffer string) {
  if (string.hasArray()) {
    int base = string.arrayOffset();
    return new String(string.array(), base + string.position(), string.remaining(), UTF8_CHARSET);
  } else {
    return new String(Utils.toByteArray(string), UTF8_CHARSET);
  }
}

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

public List<Object> deserialize(ByteBuffer ser) {
  // Maintain backward compatibility for 0.10
  byte[] b = Utils.toByteArray(ser);
  return tuple(new Object[]{b});
}

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

@Override
public List<Object> deserialize(ByteBuffer ser) {
 String data = new String(Utils.toByteArray(ser), StandardCharsets.UTF_8);
 List<String> parts = org.apache.storm.sql.runtime.utils.Utils.split(data, delimiter);
 Preconditions.checkArgument(parts.size() == fieldNames.size(), "Invalid schema");
 ArrayList<Object> list = new ArrayList<>(fieldNames.size());
 list.addAll(parts);
 return list;
}

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

@Override
public List<Object> deserialize(ByteBuffer ser) {
 ObjectMapper mapper = new ObjectMapper();
 try {
  @SuppressWarnings("unchecked")
  HashMap<String, Object> map = mapper.readValue(Utils.toByteArray(ser), HashMap.class);
  ArrayList<Object> list = new ArrayList<>(fields.size());
  for (String f : fields) {
   list.add(map.get(f));
  }
  return list;
 } catch (IOException e) {
  throw new RuntimeException(e);
 }
}

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

@Override
public Iterable<List<Object>> deserialize(ByteBuffer ser) {
 return asList(tuple(Utils.toByteArray(ser)));
}

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

@Override
public List<Object> deserialize(ByteBuffer ser) {
 try {
  Schema schema = schemas.getSchema(schemaString);
  DatumReader<GenericRecord> reader = new GenericDatumReader<>(schema);
  BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(Utils.toByteArray(ser), null);
  GenericRecord record = reader.read(null, decoder);
  ArrayList<Object> list = new ArrayList<>(fieldNames.size());
  for (String field : fieldNames) {
   Object value = record.get(field);
   // Avro strings are stored using a special Avro Utf8 type instead of using Java primitives
   list.add(SerdeUtils.convertAvroUtf8(value));
  }
  return list;
 } catch (IOException e) {
  throw new RuntimeException(e);
 }
}

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

@Override
public List<Object> deserialize(ByteBuffer ser) {
 try {
  String data = new String(Utils.toByteArray(ser), StandardCharsets.UTF_8);
  CSVParser parser = CSVParser.parse(data, CSVFormat.RFC4180);
  CSVRecord record = parser.getRecords().get(0);
  Preconditions.checkArgument(record.size() == fieldNames.size(), "Invalid schema");
  ArrayList<Object> list = new ArrayList<>(fieldNames.size());
  for (int i = 0; i < record.size(); i++) {
   list.add(record.get(i));
  }
  return list;
 } catch (IOException e) {
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: JiuzhouSec/nightwatch

@Override
public List<Object> deserialize(ByteBuffer ser) {
  String jsonStr = null;
  if (ser.hasArray()) {
    int base = ser.arrayOffset();
    jsonStr = new String(ser.array(), base + ser.position(), ser.remaining());
  } else {
    jsonStr = new String(Utils.toByteArray(ser), UTF8_CHARSET);
  }
  JSONObject jsonObject = JSONObject.fromObject(jsonStr);
  Values values = new Values();
  for (String outputField : outputFields) {
    if("jsonBody".equals(outputField)) {
      values.add(jsonStr);
    } else {
      if(!jsonObject.containsKey(outputField)) {
        JSONObject rcMsgpara = JSONObject.fromObject(jsonObject.get("rc_msg_para"));
        values.add(rcMsgpara.get(outputField));
      } else {
        values.add(jsonObject.get(outputField));
      }
    }
  }
  return values;
}

相关文章

Utils类方法