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

The following examples show how to use org.apache.kudu.client.PartialRow#addFloat() . 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: RowHelper.java    From presto with Apache License 2.0 5 votes vote down vote up
public static void copyPrimaryKey(Schema schema, RowResult from, PartialRow to)
{
    for (int i = 0; i < schema.getPrimaryKeyColumnCount(); i++) {
        switch (schema.getColumnByIndex(i).getType()) {
            case STRING:
                to.addStringUtf8(i, from.getString(i).getBytes(StandardCharsets.UTF_8));
                break;
            case INT64:
            case UNIXTIME_MICROS:
                to.addLong(i, from.getLong(i));
                break;
            case INT32:
                to.addInt(i, from.getInt(i));
                break;
            case INT16:
                to.addShort(i, from.getShort(i));
                break;
            case INT8:
                to.addByte(i, from.getByte(i));
                break;
            case DOUBLE:
                to.addDouble(i, from.getDouble(i));
                break;
            case FLOAT:
                to.addFloat(i, from.getFloat(i));
                break;
            case BOOL:
                to.addBoolean(i, from.getBoolean(i));
                break;
            case BINARY:
                to.addBinary(i, from.getBinary(i));
                break;
            default:
                throw new IllegalStateException("Unknown type " + schema.getColumnByIndex(i).getType()
                        + " for column " + schema.getColumnByIndex(i).getName());
        }
    }
}
 
Example 2
Source File: RowHelper.java    From presto with Apache License 2.0 5 votes vote down vote up
public static void copyPrimaryKey(Schema schema, PartialRow from, PartialRow to)
{
    for (int i = 0; i < schema.getPrimaryKeyColumnCount(); i++) {
        switch (schema.getColumnByIndex(i).getType()) {
            case STRING:
                to.addStringUtf8(i, from.getString(i).getBytes(StandardCharsets.UTF_8));
                break;
            case INT64:
            case UNIXTIME_MICROS:
                to.addLong(i, from.getLong(i));
                break;
            case INT32:
                to.addInt(i, from.getInt(i));
                break;
            case INT16:
                to.addShort(i, from.getShort(i));
                break;
            case INT8:
                to.addByte(i, from.getByte(i));
                break;
            case DOUBLE:
                to.addDouble(i, from.getDouble(i));
                break;
            case FLOAT:
                to.addFloat(i, from.getFloat(i));
                break;
            case BOOL:
                to.addBoolean(i, from.getBoolean(i));
                break;
            case BINARY:
                to.addBinary(i, from.getBinary(i));
                break;
            default:
                throw new IllegalStateException("Unknown type " + schema.getColumnByIndex(i).getType()
                        + " for column " + schema.getColumnByIndex(i).getName());
        }
    }
}
 
Example 3
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 4
Source File: RowHelper.java    From presto-kudu with Apache License 2.0 5 votes vote down vote up
public static void copyPrimaryKey(Schema schema, RowResult from, PartialRow to) {
    for (int i = 0; i < schema.getPrimaryKeyColumnCount(); i++) {
        switch (schema.getColumnByIndex(i).getType()) {
            case STRING:
                to.addStringUtf8(i, from.getString(i).getBytes(Charsets.UTF_8));
                break;
            case INT64:
            case UNIXTIME_MICROS:
                to.addLong(i, from.getLong(i));
                break;
            case INT32:
                to.addInt(i, from.getInt(i));
                break;
            case INT16:
                to.addShort(i, from.getShort(i));
                break;
            case INT8:
                to.addByte(i, from.getByte(i));
                break;
            case DOUBLE:
                to.addDouble(i, from.getDouble(i));
                break;
            case FLOAT:
                to.addFloat(i, from.getFloat(i));
                break;
            case BOOL:
                to.addBoolean(i, from.getBoolean(i));
                break;
            case BINARY:
                to.addBinary(i, from.getBinary(i));
                break;
            default:
                throw new IllegalStateException("Unknown type " + schema.getColumnByIndex(i).getType()
                        + " for column " + schema.getColumnByIndex(i).getName());
        }
    }
}
 
Example 5
Source File: RowHelper.java    From presto-kudu with Apache License 2.0 5 votes vote down vote up
public static void copyPrimaryKey(Schema schema, PartialRow from, PartialRow to) {
    for (int i = 0; i < schema.getPrimaryKeyColumnCount(); i++) {
        switch (schema.getColumnByIndex(i).getType()) {
            case STRING:
                to.addStringUtf8(i, from.getString(i).getBytes(Charsets.UTF_8));
                break;
            case INT64:
            case UNIXTIME_MICROS:
                to.addLong(i, from.getLong(i));
                break;
            case INT32:
                to.addInt(i, from.getInt(i));
                break;
            case INT16:
                to.addShort(i, from.getShort(i));
                break;
            case INT8:
                to.addByte(i, from.getByte(i));
                break;
            case DOUBLE:
                to.addDouble(i, from.getDouble(i));
                break;
            case FLOAT:
                to.addFloat(i, from.getFloat(i));
                break;
            case BOOL:
                to.addBoolean(i, from.getBoolean(i));
                break;
            case BINARY:
                to.addBinary(i, from.getBinary(i));
                break;
            default:
                throw new IllegalStateException("Unknown type " + schema.getColumnByIndex(i).getType()
                        + " for column " + schema.getColumnByIndex(i).getName());
        }
    }
}
 
Example 6
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 7
Source File: KuduTableProperties.java    From presto with Apache License 2.0 4 votes vote down vote up
private static void setColumnValue(PartialRow partialRow, int idx, Object obj, Type type, String name)
{
    Number n;

    switch (type) {
        case STRING:
            if (obj instanceof String) {
                partialRow.addString(idx, (String) obj);
            }
            else {
                handleInvalidValue(name, type, obj);
            }
            break;
        case INT64:
            n = toNumber(obj, type, name);
            partialRow.addLong(idx, n.longValue());
            break;
        case INT32:
            n = toNumber(obj, type, name);
            partialRow.addInt(idx, n.intValue());
            break;
        case INT16:
            n = toNumber(obj, type, name);
            partialRow.addShort(idx, n.shortValue());
            break;
        case INT8:
            n = toNumber(obj, type, name);
            partialRow.addByte(idx, n.byteValue());
            break;
        case DOUBLE:
            n = toNumber(obj, type, name);
            partialRow.addDouble(idx, n.doubleValue());
            break;
        case FLOAT:
            n = toNumber(obj, type, name);
            partialRow.addFloat(idx, n.floatValue());
            break;
        case UNIXTIME_MICROS:
            long l = toUnixTimeMicros(obj, type, name);
            partialRow.addLong(idx, l);
            break;
        case BOOL:
            boolean b = toBoolean(obj, type, name);
            partialRow.addBoolean(idx, b);
            break;
        case BINARY:
            byte[] bytes = toByteArray(obj, type, name);
            partialRow.addBinary(idx, bytes);
            break;
        default:
            handleInvalidValue(name, type, obj);
            break;
    }
}
 
Example 8
Source File: DafPutKudu.java    From daf-kylo with GNU Affero General Public License v3.0 4 votes vote down vote up
private void insert(KuduTable kuduTable, Operation operation, Record record, List<String> fieldNames){
    PartialRow row = operation.getRow();
    Schema colSchema = kuduTable.getSchema();

    for (String colName : fieldNames) {
        int colIdx = this.getColumnIndex(colSchema, colName);
        if (colIdx != -1) {
            Type colType = colSchema.getColumnByIndex(colIdx).getType();

            switch (colType.getDataType()) {
                case BOOL:
                    row.addBoolean(colIdx, record.getAsBoolean(colName));
                    break;
                case FLOAT:
                    row.addFloat(colIdx, record.getAsFloat(colName));
                    break;
                case DOUBLE:
                    row.addDouble(colIdx, record.getAsDouble(colName));
                    break;
                case BINARY:
                    row.addBinary(colIdx, record.getAsString(colName).getBytes());
                    break;
                case INT8:
                case INT16:
                    short temp = (short)record.getAsInt(colName).intValue();
                    row.addShort(colIdx, temp);
                case INT32:
                    row.addInt(colIdx, record.getAsInt(colName));
                    break;
                case INT64:
                    row.addLong(colIdx, record.getAsLong(colName));
                    break;
                case STRING:
                    row.addString(colIdx, record.getAsString(colName));
                    break;
                default:
                    throw new IllegalStateException(String.format("unknown column type %s", colType));
            }
        }
    }
}
 
Example 9
Source File: KuduTableProperties.java    From presto-kudu with Apache License 2.0 4 votes vote down vote up
private static void setColumnValue(PartialRow partialRow, int idx, Object obj, Type type, String name) {
    Number n;

    switch (type) {
        case STRING:
            if (obj instanceof String) {
                partialRow.addString(idx, (String) obj);
            } else {
                handleInvalidValue(name, type, obj);
            }
            break;
        case INT64:
            n = toNumber(obj, type, name);
            partialRow.addLong(idx, n.longValue());
            break;
        case INT32:
            n = toNumber(obj, type, name);
            partialRow.addInt(idx, n.intValue());
            break;
        case INT16:
            n = toNumber(obj, type, name);
            partialRow.addShort(idx, n.shortValue());
            break;
        case INT8:
            n = toNumber(obj, type, name);
            partialRow.addByte(idx, n.byteValue());
            break;
        case DOUBLE:
            n = toNumber(obj, type, name);
            partialRow.addDouble(idx, n.doubleValue());
            break;
        case FLOAT:
            n = toNumber(obj, type, name);
            partialRow.addFloat(idx, n.floatValue());
            break;
        case UNIXTIME_MICROS:
            long l = toUnixTimeMicros(obj, type, name);
            partialRow.addLong(idx, l);
            break;
        case BOOL:
            boolean b = toBoolean(obj, type, name);
            partialRow.addBoolean(idx, b);
            break;
        case BINARY:
            byte[] bytes = toByteArray(obj, type, name);
            partialRow.addBinary(idx, bytes);
            break;
        default:
            handleInvalidValue(name, type, obj);
            break;
    }
}
 
Example 10
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 11
Source File: ITestKuduLookupService.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Before
public void init() throws Exception {
    testRunner = TestRunners.newTestRunner(SampleProcessor.class);
    testRunner.setValidateExpressionUsage(false);
    final String tableName = "table1";

    KuduClient client =  harness.getClient();
    List<ColumnSchema> columns = new ArrayList<>();
    columns.add(new ColumnSchema.ColumnSchemaBuilder("string", Type.STRING).key(true).build());
    columns.add(new ColumnSchema.ColumnSchemaBuilder("binary", Type.BINARY).build());
    columns.add(new ColumnSchema.ColumnSchemaBuilder("bool", Type.BOOL).build());
    columns.add(new ColumnSchema
            .ColumnSchemaBuilder("decimal", Type.DECIMAL)
            .typeAttributes(DecimalUtil.typeAttributes(DecimalUtil.MAX_DECIMAL64_PRECISION, 1))
            .build()
    );
    columns.add(new ColumnSchema.ColumnSchemaBuilder("double", Type.DOUBLE).build());
    columns.add(new ColumnSchema.ColumnSchemaBuilder("float", Type.FLOAT).build());
    columns.add(new ColumnSchema.ColumnSchemaBuilder("int8", Type.INT8).build());
    columns.add(new ColumnSchema.ColumnSchemaBuilder("int16", Type.INT16).build());
    columns.add(new ColumnSchema.ColumnSchemaBuilder("int32", Type.INT32).build());
    columns.add(new ColumnSchema.ColumnSchemaBuilder("int64", Type.INT64).build());
    columns.add(new ColumnSchema.ColumnSchemaBuilder("unixtime_micros", Type.UNIXTIME_MICROS).build());
    columns.add(new ColumnSchema.ColumnSchemaBuilder("varchar_3", Type.VARCHAR).typeAttributes(
            new ColumnTypeAttributes.ColumnTypeAttributesBuilder().length(3).build()
    ).build());
    Schema schema = new Schema(columns);

    CreateTableOptions opts = new CreateTableOptions().setRangePartitionColumns(Collections.singletonList("string"));
    client.createTable(tableName, schema, opts);

    KuduTable table = client.openTable(tableName);
    KuduSession session = client.newSession();

    Insert insert = table.newInsert();
    PartialRow row = insert.getRow();
    row.addString("string", "string1");
    row.addBinary("binary", "binary1".getBytes());
    row.addBoolean("bool",true);
    row.addDecimal("decimal", BigDecimal.valueOf(0.1));
    row.addDouble("double",0.2);
    row.addFloat("float",0.3f);
    row.addByte("int8", (byte) 1);
    row.addShort("int16", (short) 2);
    row.addInt("int32",3);
    row.addLong("int64",4L);
    row.addTimestamp("unixtime_micros", new Timestamp(nowMillis));
    row.addVarchar("varchar_3", "SFO");
    session.apply(insert);

    insert = table.newInsert();
    row = insert.getRow();
    row.addString("string", "string2");
    row.addBinary("binary", "binary2".getBytes());
    row.addBoolean("bool",false);
    row.addDecimal("decimal", BigDecimal.valueOf(0.1));
    row.addDouble("double",1.2);
    row.addFloat("float",1.3f);
    row.addByte("int8", (byte) 11);
    row.addShort("int16", (short) 12);
    row.addInt("int32",13);
    row.addLong("int64",14L);
    row.addTimestamp("unixtime_micros", new Timestamp(nowMillis+(1000L * 60 * 60 * 24 * 365))); //+ 1 year
    row.addVarchar("varchar_3", "SJC");
    session.apply(insert);

    session.close();

    kuduLookupService = new KuduLookupService();
    testRunner.addControllerService("kuduLookupService", kuduLookupService);
    testRunner.setProperty(kuduLookupService, KuduLookupService.KUDU_MASTERS, "testLocalHost:7051");
    testRunner.setProperty(kuduLookupService, KuduLookupService.KUDU_REPLICA_SELECTION, KuduLookupService.LEADER_ONLY);
    testRunner.setProperty(kuduLookupService, KuduLookupService.TABLE_NAME, tableName);
    kuduLookupService.kuduClient = client;
}
 
Example 12
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));
                }
            }
        }
    }
}