Java Code Examples for org.apache.kudu.Type#UNIXTIME_MICROS

The following examples show how to use org.apache.kudu.Type#UNIXTIME_MICROS . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KuduToBigQuery.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
@Override
public TableRow apply(RowResult input) {
  TableRow outputTableRow = new TableRow();
  Schema kuduSchema = input.getSchema();

  for (int i = 0; i < kuduSchema.getColumnCount(); i++) {
    String columnName = kuduSchema.getColumnByIndex(i).getName();

    if (kuduSchema.getColumnByIndex(i).getType() == Type.UNIXTIME_MICROS) {
      outputTableRow.set(columnName, convertNanoSecondsToMilliSeconds(input, i));
    } else {
      // For other datatypes return the object value
      outputTableRow.set(columnName, input.getObject(i));
    }
  }
  return outputTableRow;
}
 
Example 2
Source File: AbstractKuduProcessor.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a NiFi DataType to it's equivalent Kudu Type.
 */
private Type toKuduType(DataType nifiType) {
    switch (nifiType.getFieldType()) {
        case BOOLEAN:
            return Type.BOOL;
        case BYTE:
            return Type.INT8;
        case SHORT:
            return Type.INT16;
        case INT:
            return Type.INT32;
        case LONG:
            return Type.INT64;
        case FLOAT:
            return Type.FLOAT;
        case DOUBLE:
            return Type.DOUBLE;
        case DECIMAL:
            return Type.DECIMAL;
        case TIMESTAMP:
            return Type.UNIXTIME_MICROS;
        case CHAR:
        case STRING:
            return Type.STRING;
        default:
            throw new IllegalArgumentException(String.format("unsupported type %s", nifiType));
    }
}
 
Example 3
Source File: KuduTypeUtils.java    From bahir-flink with Apache License 2.0 4 votes vote down vote up
@Override
public Type visit(TimestampType timestampType) {
    return Type.UNIXTIME_MICROS;
}
 
Example 4
Source File: KuduRecordConverter.java    From datacollector with Apache License 2.0 4 votes vote down vote up
private void recordToRow(Record record, PartialRow row, String fieldName, String column, int operation) throws OnRecordErrorException {
  Field.Type type = columnsToFieldTypes.get(column);
  ColumnSchema columnSchema = schema.getColumn(column);
  if (record.has(fieldName)) {
    Field field = record.get(fieldName);
    if (field.getValue() == null) {
      if (!columnSchema.isNullable()) {
        throw new OnRecordErrorException(record, Errors.KUDU_06, column, fieldName);
      }
      row.setNull(column);
    } else {
      try {
        switch (type) {
          case BOOLEAN:
            row.addBoolean(column, field.getValueAsBoolean());
            break;
          case BYTE:
            row.addByte(column, field.getValueAsByte());
            break;
          case SHORT:
            row.addShort(column, field.getValueAsShort());
            break;
          case INTEGER:
            row.addInt(column, field.getValueAsInteger());
            break;
          case LONG:
            if (columnSchema.getType() == Type.UNIXTIME_MICROS) {
              // Convert millisecond to microsecond
              row.addLong(column, field.getValueAsLong() * 1000);
            } else {
              row.addLong(column, field.getValueAsLong());
            }
            break;
          case FLOAT:
            row.addFloat(column, field.getValueAsFloat());
            break;
          case DOUBLE:
            row.addDouble(column, field.getValueAsDouble());
            break;
          case STRING:
            row.addString(column, field.getValueAsString());
            break;
          case BYTE_ARRAY:
            row.addBinary(column, field.getValueAsByteArray());
            break;
          case DECIMAL:
            row.addDecimal(column, field.getValueAsDecimal());
            break;
          default:
            throw new OnRecordErrorException(record, Errors.KUDU_04, fieldName, type.name());
        }
      } catch (IllegalArgumentException e) {
        throw new OnRecordErrorException(record, Errors.KUDU_09, fieldName, type.name(), e.toString(), e);
      }
    }
  } else {
    // SDC-5816.  do not null out columns in UPDATE or UPSERT mode.
    // if the columns are not specified - they should not be changed.
    if (operation == KuduOperationType.INSERT.code) {
      if (!columnSchema.isNullable()) {
        throw new OnRecordErrorException(record, Errors.KUDU_06, column, fieldName);
      }
      row.setNull(column);
    }
  }
}