org.apache.hadoop.io.Text.decode()方法的使用及代码示例

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

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

Text.decode介绍

[英]Converts the provided byte array to a String using the UTF-8 encoding. If the input is malformed, replace by a default value.
[中]使用UTF-8编码将提供的字节数组转换为字符串。如果输入格式不正确,请替换为默认值。

代码示例

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

/**
 * Convert a UTF-8 byte array to String.
 *
 * @param bytes
 *          The byte[] containing the UTF-8 String.
 * @param start
 *          The start position inside the bytes.
 * @param length
 *          The length of the data, starting from "start"
 * @return The unicode String
 */
public static String convertToString(byte[] bytes, int start, int length) {
 try {
  return Text.decode(bytes, start, length);
 } catch (CharacterCodingException e) {
  return null;
 }
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

/** 
 * Convert text back to string
 * @see java.lang.Object#toString()
 */
@Override
public String toString() {
 try {
  return decode(bytes, 0, length);
 } catch (CharacterCodingException e) { 
  throw new RuntimeException("Should not have happened " , e); 
 }
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

/**
 * Converts the provided byte array to a String using the
 * UTF-8 encoding. If the input is malformed,
 * replace by a default value.
 */
public static String decode(byte[] utf8) throws CharacterCodingException {
 return decode(ByteBuffer.wrap(utf8), true);
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

/**
 * Converts the provided byte array to a String using the
 * UTF-8 encoding. If <code>replace</code> is true, then
 * malformed input is replaced with the
 * substitution character, which is U+FFFD. Otherwise the
 * method throws a MalformedInputException.
 */
public static String decode(byte[] utf8, int start, int length, boolean replace) 
 throws CharacterCodingException {
 return decode(ByteBuffer.wrap(utf8, start, length), replace);
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

public static String decode(byte[] utf8, int start, int length) 
 throws CharacterCodingException {
 return decode(ByteBuffer.wrap(utf8, start, length), true);
}

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

public void logExceptionMessage(byte[] bytes, int bytesStart, int bytesLength, String dataType) {
 try {
  if (LOG.isDebugEnabled()) {
   String byteData = Text.decode(bytes, bytesStart, bytesLength);
   LOG.debug("Data not in the " + dataType
     + " data type range so converted to null. Given data is :" +
         byteData, new Exception("For debugging purposes"));
  }
 } catch (CharacterCodingException e1) {
  LOG.debug("Data not in the " + dataType + " data type range so converted to null.", e1);
 }
}

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

@Override public byte[] getBytes(Text writable) {
 //@TODO  There is no reason to decode then encode the string to bytes really
 //@FIXME this issue with CTRL-CHAR ^0 added by Text at the end of string and Json serd does not like that.
 try {
  return Text.decode(writable.getBytes(), 0, writable.getLength()).getBytes(Charset.forName("UTF-8"));
 } catch (CharacterCodingException e) {
  throw new RuntimeException(e);
 }
}

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

@Override
protected long doGetField(byte[] bytes, int start, int length) throws ParseException {
 Date date = null;
 try {
  String decoded = Text.decode(bytes, start, length);
  date = format.parse(decoded);
 } catch (CharacterCodingException e) {
  throw new ParseException(e.getMessage(), 0);
 }
 calendar.setTime(date);
 return calendar.get(Calendar.DAY_OF_WEEK);
}

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

public void logExceptionMessage(ByteArrayRef bytes, int start, int length, String dataType) {
 try {
  if(LOG.isDebugEnabled()) {
   String byteData = Text.decode(bytes.getData(), start, length);
   LOG.debug("Data not in the " + dataType
     + " data type range so converted to null. Given data is :" +
         byteData, new Exception("For debugging purposes"));
  }
 } catch (CharacterCodingException e1) {
  LOG.debug("Data not in the " + dataType + " data type range so converted to null.", e1);
 }
}

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

@Override
protected long doGetField(byte[] bytes, int start, int length) throws ParseException {
 Date date = null;
 try {
  date = format.parse(Text.decode(bytes, start, length));
 } catch (CharacterCodingException e) {
  throw new ParseException(e.getMessage(), 0);
 }
 calendar.setTime(date);
 return calendar.getTimeInMillis() / 1000;
}

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

@Override
 protected long doGetField(byte[] bytes, int start, int length) throws ParseException {
  Date date = null;
  try {
   String decoded = Text.decode(bytes, start, length);
   date = format.parse(decoded);
  } catch (CharacterCodingException e) {
   throw new ParseException(e.getMessage(), 0);
  }
  calendar.setTime(date);
  return calendar.get(Calendar.WEEK_OF_YEAR);
 }
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

/** Read a UTF8 encoded string with a maximum size
 */
public static String readString(DataInput in, int maxLength)
  throws IOException {
 int length = WritableUtils.readVIntInRange(in, 0, maxLength);
 byte [] bytes = new byte[length];
 in.readFully(bytes, 0, length);
 return decode(bytes);
}

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

@Override
protected long doGetField(byte[] bytes, int start, int length) throws ParseException {
 Date date = null;
 try {
  String decoded = Text.decode(bytes, start, length);
  date = format.parse(decoded);
 } catch (CharacterCodingException e) {
  throw new ParseException(e.getMessage(), 0);
 }
 calendar.setTime(date);
 return calendar.get(Calendar.WEEK_OF_YEAR);
}

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

@Override
 protected long doGetField(byte[] bytes, int start, int length) throws ParseException {
  Date date = null;
  try {
   date = format.parse(Text.decode(bytes, start, length));
  } catch (CharacterCodingException e) {
   throw new ParseException(e.getMessage(), 0);
  }
  calendar.setTime(date);
  return calendar.getTimeInMillis() / 1000;
 }
}

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

@Override
protected long doGetField(byte[] bytes, int start, int length) throws ParseException {
 Date date = null;
 try {
  String decoded = Text.decode(bytes, start, length);
  date = format.parse(decoded);
 } catch (CharacterCodingException e) {
  throw new ParseException(e.getMessage(), 0);
 }
 calendar.setTime(date);
 return calendar.get(Calendar.DAY_OF_WEEK);
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

/**
 * Read a String as a VInt n, followed by n Bytes in Text format.
 * 
 * @param in
 *          The input stream.
 * @return The string
 * @throws IOException
 */
public static String readString(DataInput in) throws IOException {
 int length = readVInt(in);
 if (length == -1) return null;
 byte[] buffer = new byte[length];
 in.readFully(buffer);
 return Text.decode(buffer);
}

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

private Timestamp decodeTime(byte[] time) {
 try {
  return new Timestamp(dateFormat.parse(Text.decode(time)).getTime());
 } catch (Exception e) {
  throw new RuntimeException(e);
 }
}

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

@Override
public void init(ByteArrayRef bytes, int start, int length) {
 String byteData = null;
 if (!LazyUtils.isNumberMaybe(bytes.getData(), start, length)) {
  isNull = true;
  return;
 }
 try {
  byteData = Text.decode(bytes.getData(), start, length);
  data.set(Double.parseDouble(byteData));
  isNull = false;
 } catch (NumberFormatException e) {
  isNull = true;
  LOG.debug("Data not in the Double data type range so converted to null. Given data is :"
    + byteData, e);
 } catch (CharacterCodingException e) {
  isNull = true;
  LOG.debug("Data not in the Double data type range so converted to null.", e);
 }
}

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

@Override
public void init(ByteArrayRef bytes, int start, int length) {
 String s = null;
 try {
  s = Text.decode(bytes.getData(), start, length);
  data.set(HiveIntervalDayTime.valueOf(s));
  isNull = false;
 } catch (Exception e) {
  isNull = true;
  logExceptionMessage(bytes, start, length, "INTERVAL_DAY_TIME");
 }
}

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

@Override
public void init(ByteArrayRef bytes, int start, int length) {
 String s = null;
 try {
  s = Text.decode(bytes.getData(), start, length);
  data.set(HiveIntervalYearMonth.valueOf(s));
  isNull = false;
 } catch (Exception e) {
  isNull = true;
  logExceptionMessage(bytes, start, length, "INTERVAL_YEAR_MONTH");
 }
}

相关文章