Java Code Examples for org.apache.kudu.client.PartialRow#setNull()

The following examples show how to use org.apache.kudu.client.PartialRow#setNull() . 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: KuduPageSink.java    From presto-kudu with Apache License 2.0 5 votes vote down vote up
private void appendColumn(PartialRow row, Page page, int position, int channel, int destChannel) {
    Block block = page.getBlock(channel);
    Type type = columnTypes.get(destChannel);
    if (block.isNull(position)) {
        row.setNull(destChannel);
    } else if (TIMESTAMP.equals(type)) {
        row.addLong(destChannel, type.getLong(block, position) * 1000);
    } else if (REAL.equals(type)) {
        row.addFloat(destChannel, intBitsToFloat((int) type.getLong(block, position)));
    } else if (BIGINT.equals(type)) {
        row.addLong(destChannel, type.getLong(block, position));
    } else if (INTEGER.equals(type)) {
        row.addInt(destChannel, (int) type.getLong(block, position));
    } else if (SMALLINT.equals(type)) {
        row.addShort(destChannel, (short) type.getLong(block, position));
    } else if (TINYINT.equals(type)) {
        row.addByte(destChannel, (byte) type.getLong(block, position));
    } else if (BOOLEAN.equals(type)) {
        row.addBoolean(destChannel, type.getBoolean(block, position));
    } else if (DOUBLE.equals(type)) {
        row.addDouble(destChannel, type.getDouble(block, position));
    } else if (isVarcharType(type)) {
        Type originalType = originalColumnTypes.get(destChannel);
        if (DATE.equals(originalType)) {
            SqlDate date = (SqlDate) originalType.getObjectValue(connectorSession, block, position);
            LocalDateTime ldt = LocalDateTime.ofEpochSecond(TimeUnit.DAYS.toSeconds(date.getDays()), 0, ZoneOffset.UTC);
            byte[] bytes = ldt.format(DateTimeFormatter.ISO_LOCAL_DATE).getBytes(Charsets.UTF_8);
            row.addStringUtf8(destChannel, bytes);
        } else {
            row.addString(destChannel, type.getSlice(block, position).toStringUtf8());
        }
    } else if (VARBINARY.equals(type)) {
        row.addBinary(destChannel, type.getSlice(block, position).toByteBuffer());
    } else if (type instanceof DecimalType) {
        SqlDecimal sqlDecimal = (SqlDecimal) type.getObjectValue(connectorSession, block, position);
        row.addDecimal(destChannel, sqlDecimal.toBigDecimal());
    } else {
        throw new UnsupportedOperationException("Type is not supported: " + type);
    }
}
 
Example 2
Source File: KuduPageSink.java    From presto with Apache License 2.0 4 votes vote down vote up
private void appendColumn(PartialRow row, Page page, int position, int channel, int destChannel)
{
    Block block = page.getBlock(channel);
    Type type = columnTypes.get(destChannel);
    if (block.isNull(position)) {
        row.setNull(destChannel);
    }
    else if (TIMESTAMP.equals(type)) {
        row.addLong(destChannel, type.getLong(block, position) * 1000);
    }
    else if (REAL.equals(type)) {
        row.addFloat(destChannel, intBitsToFloat(toIntExact(type.getLong(block, position))));
    }
    else if (BIGINT.equals(type)) {
        row.addLong(destChannel, type.getLong(block, position));
    }
    else if (INTEGER.equals(type)) {
        row.addInt(destChannel, toIntExact(type.getLong(block, position)));
    }
    else if (SMALLINT.equals(type)) {
        row.addShort(destChannel, Shorts.checkedCast(type.getLong(block, position)));
    }
    else if (TINYINT.equals(type)) {
        row.addByte(destChannel, SignedBytes.checkedCast(type.getLong(block, position)));
    }
    else if (BOOLEAN.equals(type)) {
        row.addBoolean(destChannel, type.getBoolean(block, position));
    }
    else if (DOUBLE.equals(type)) {
        row.addDouble(destChannel, type.getDouble(block, position));
    }
    else if (isVarcharType(type)) {
        Type originalType = originalColumnTypes.get(destChannel);
        if (DATE.equals(originalType)) {
            SqlDate date = (SqlDate) originalType.getObjectValue(connectorSession, block, position);
            LocalDateTime ldt = LocalDateTime.ofEpochSecond(TimeUnit.DAYS.toSeconds(date.getDays()), 0, ZoneOffset.UTC);
            byte[] bytes = ldt.format(DateTimeFormatter.ISO_LOCAL_DATE).getBytes(StandardCharsets.UTF_8);
            row.addStringUtf8(destChannel, bytes);
        }
        else {
            row.addString(destChannel, type.getSlice(block, position).toStringUtf8());
        }
    }
    else if (VARBINARY.equals(type)) {
        row.addBinary(destChannel, type.getSlice(block, position).toByteBuffer());
    }
    else if (type instanceof DecimalType) {
        SqlDecimal sqlDecimal = (SqlDecimal) type.getObjectValue(connectorSession, block, position);
        row.addDecimal(destChannel, sqlDecimal.toBigDecimal());
    }
    else {
        throw new UnsupportedOperationException("Type is not supported: " + type);
    }
}
 
Example 3
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);
    }
  }
}
 
Example 4
Source File: AbstractKuduProcessor.java    From nifi with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
protected void buildPartialRow(Schema schema, PartialRow row, Record record, List<String> fieldNames, Boolean ignoreNull, Boolean lowercaseFields) {
    for (String recordFieldName : fieldNames) {
        String colName = recordFieldName;
        if (lowercaseFields) {
            colName = colName.toLowerCase();
        }
        int colIdx = this.getColumnIndex(schema, colName);
        if (colIdx != -1) {
            ColumnSchema colSchema = schema.getColumnByIndex(colIdx);
            Type colType = colSchema.getType();

            if (record.getValue(recordFieldName) == null) {
                if (schema.getColumnByIndex(colIdx).isKey()) {
                    throw new IllegalArgumentException(String.format("Can't set primary key column %s to null ", colName));
                } else if(!schema.getColumnByIndex(colIdx).isNullable()) {
                    throw new IllegalArgumentException(String.format("Can't set column %s to null ", colName));
                }

                if (!ignoreNull) {
                    row.setNull(colName);
                }
            } else {
                Object value = record.getValue(recordFieldName);
                switch (colType) {
                    case BOOL:
                        row.addBoolean(colIdx, DataTypeUtils.toBoolean(value, recordFieldName));
                        break;
                    case INT8:
                        row.addByte(colIdx, DataTypeUtils.toByte(value, recordFieldName));
                        break;
                    case INT16:
                        row.addShort(colIdx,  DataTypeUtils.toShort(value, recordFieldName));
                        break;
                    case INT32:
                        row.addInt(colIdx,  DataTypeUtils.toInteger(value, recordFieldName));
                        break;
                    case INT64:
                        row.addLong(colIdx,  DataTypeUtils.toLong(value, recordFieldName));
                        break;
                    case UNIXTIME_MICROS:
                        DataType fieldType = record.getSchema().getDataType(recordFieldName).get();
                        Timestamp timestamp = DataTypeUtils.toTimestamp(record.getValue(recordFieldName),
                                () -> DataTypeUtils.getDateFormat(fieldType.getFormat()), recordFieldName);
                        row.addTimestamp(colIdx, timestamp);
                        break;
                    case STRING:
                        row.addString(colIdx, DataTypeUtils.toString(value, recordFieldName));
                        break;
                    case BINARY:
                        row.addBinary(colIdx, DataTypeUtils.toString(value, recordFieldName).getBytes());
                        break;
                    case FLOAT:
                        row.addFloat(colIdx, DataTypeUtils.toFloat(value, recordFieldName));
                        break;
                    case DOUBLE:
                        row.addDouble(colIdx, DataTypeUtils.toDouble(value, recordFieldName));
                        break;
                    case DECIMAL:
                        row.addDecimal(colIdx, new BigDecimal(DataTypeUtils.toString(value, recordFieldName)));
                        break;
                    case VARCHAR:
                        row.addVarchar(colIdx, DataTypeUtils.toString(value, recordFieldName));
                        break;
                    default:
                        throw new IllegalStateException(String.format("unknown column type %s", colType));
                }
            }
        }
    }
}