public static PartialRow toRangeBoundToPartialRow(Schema schema, RangePartitionDefinition definition, RangeBoundValue boundValue) { PartialRow partialRow = new PartialRow(schema); if (boundValue != null) { List<Integer> rangeColumns = definition.getColumns().stream() .map(schema::getColumnIndex).collect(toImmutableList()); if (rangeColumns.size() != boundValue.getValues().size()) { throw new IllegalStateException("Expected " + rangeColumns.size() + " range columns, but got " + boundValue.getValues().size()); } for (int i = 0; i < rangeColumns.size(); i++) { Object obj = boundValue.getValues().get(i); int idx = rangeColumns.get(i); ColumnSchema columnSchema = schema.getColumnByIndex(idx); setColumnValue(partialRow, idx, obj, columnSchema.getType(), columnSchema.getName()); } } return partialRow; }
@Override public Map<String, ColumnHandle> getColumnHandles(ConnectorSession session, ConnectorTableHandle connectorTableHandle) { KuduTableHandle tableHandle = (KuduTableHandle) connectorTableHandle; Schema schema = clientSession.getTableSchema(tableHandle); ImmutableMap.Builder<String, ColumnHandle> columnHandles = ImmutableMap.builder(); for (int ordinal = 0; ordinal < schema.getColumnCount(); ordinal++) { ColumnSchema col = schema.getColumnByIndex(ordinal); String name = col.getName(); Type type = TypeHelper.fromKuduColumn(col); KuduColumnHandle columnHandle = new KuduColumnHandle(name, ordinal, type); columnHandles.put(name, columnHandle); } return columnHandles.build(); }
private static Object toValue(Schema schema, PartialRow bound, Integer idx) { Type type = schema.getColumnByIndex(idx).getType(); switch (type) { case UNIXTIME_MICROS: long millis = bound.getLong(idx) / 1000; return ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC).print(millis); case STRING: return bound.getString(idx); case INT64: return bound.getLong(idx); case INT32: return bound.getInt(idx); case INT16: return bound.getShort(idx); case INT8: return (short) bound.getByte(idx); case BOOL: return bound.getBoolean(idx); case BINARY: return bound.getBinaryCopy(idx); default: throw new IllegalStateException("Unhandled type " + type + " for range partition"); } }
public static PartitionDesign getPartitionDesign(KuduTable table) { Schema schema = table.getSchema(); PartitionDesign partitionDesign = new PartitionDesign(); PartitionSchema partitionSchema = table.getPartitionSchema(); List<HashPartitionDefinition> hashPartitions = partitionSchema.getHashBucketSchemas().stream() .map(hashBucketSchema -> { HashPartitionDefinition hash = new HashPartitionDefinition(); List<String> cols = hashBucketSchema.getColumnIds().stream() .map(idx -> schema.getColumnByIndex(idx).getName()).collect(toImmutableList()); hash.setColumns(cols); hash.setBuckets(hashBucketSchema.getNumBuckets()); return hash; }).collect(toImmutableList()); partitionDesign.setHash(hashPartitions); List<Integer> rangeColumns = partitionSchema.getRangeSchema().getColumns(); if (!rangeColumns.isEmpty()) { RangePartitionDefinition definition = new RangePartitionDefinition(); definition.setColumns(rangeColumns.stream() .map(i -> schema.getColumns().get(i).getName()) .collect(ImmutableList.toImmutableList())); partitionDesign.setRange(definition); } return partitionDesign; }
@Override public Status insert(String table, String key, Map<String, ByteIterator> values) { Insert insert = this.kuduTable.newInsert(); PartialRow row = insert.getRow(); row.addString(KEY, key); for (int i = 1; i < schema.getColumnCount(); i++) { row.addStringUtf8(i, values.get(schema.getColumnByIndex(i).getName()).toArray()); } apply(insert); return Status.OK; }
@Override public Status update(String table, String key, Map<String, ByteIterator> values) { Update update = this.kuduTable.newUpdate(); PartialRow row = update.getRow(); row.addString(KEY, key); for (int i = 1; i < schema.getColumnCount(); i++) { String columnName = schema.getColumnByIndex(i).getName(); ByteIterator b = values.get(columnName); if (b != null) { row.addStringUtf8(columnName, b.toArray()); } } apply(update); return Status.OK; }
public static void copyPrimaryKey(Schema schema, RowResult from, PartialRow to) switch (schema.getColumnByIndex(i).getType()) { case STRING: to.addStringUtf8(i, from.getString(i).getBytes(StandardCharsets.UTF_8)); break; default: throw new IllegalStateException("Unknown type " + schema.getColumnByIndex(i).getType() + " for column " + schema.getColumnByIndex(i).getName());
public static void copyPrimaryKey(Schema schema, PartialRow from, PartialRow to) switch (schema.getColumnByIndex(i).getType()) { case STRING: to.addStringUtf8(i, from.getString(i).getBytes(StandardCharsets.UTF_8)); break; default: throw new IllegalStateException("Unknown type " + schema.getColumnByIndex(i).getType() + " for column " + schema.getColumnByIndex(i).getName());
ColumnSchema column = schema.getColumnByIndex(0); KuduPredicate.ComparisonOp predicateOp = recordcount == 1 ? EQUAL : GREATER_EQUAL; KuduPredicate predicate = KuduPredicate.newComparisonPredicate(column, predicateOp, startkey);
int colIdx = this.getColumnIndex(schema, colName); if (colIdx != -1) { ColumnSchema colSchema = schema.getColumnByIndex(colIdx); Type colType = colSchema.getType();
ColumnSchema col = schema.getColumnByIndex(i);
for (TupleDomain.ColumnDomain<ColumnHandle> columnDomain : constraintSummary.getColumnDomains().get()) { int position = ((KuduColumnHandle) columnDomain.getColumn()).getOrdinalPosition(); ColumnSchema columnSchema = schema.getColumnByIndex(position); Domain domain = columnDomain.getDomain(); if (domain.isNone()) {
/** * Set the specified column to null * @param columnIndex the column's index in the schema * @throws IllegalArgumentException if the column doesn't exist or cannot be set to null * @throws IllegalStateException if the row was already applied */ public void setNull(int columnIndex) { setNull(this.schema.getColumnByIndex(columnIndex)); }
/** * Get if the specified column has been set * @param columnIndex Column index in the schema * @return true if the column has been set * @throws IndexOutOfBoundsException if the column doesn't exist */ public boolean isSet(int columnIndex) { checkColumnExists(schema.getColumnByIndex(columnIndex)); return this.columnsBitSet.get(columnIndex); }
/** * Get the type of a column in this result. * @param columnIndex column index in the schema * @return a type * @throws IndexOutOfBoundsException if the column doesn't exist */ public Type getColumnType(int columnIndex) { return this.schema.getColumnByIndex(columnIndex).getType(); }
/** * Get if the specified column is NULL * @param columnIndex Column index in the schema * @return true if the column cell is null and the column is nullable, * false otherwise * @throws IndexOutOfBoundsException if the column doesn't exist */ public boolean isNull(int columnIndex) { checkColumnExists(schema.getColumnByIndex(columnIndex)); return schema.getColumnByIndex(columnIndex).isNullable() && isSetToNull(columnIndex); }
private void checkType(int columnIndex, Type expectedType) { ColumnSchema columnSchema = schema.getColumnByIndex(columnIndex); Type columnType = columnSchema.getType(); if (!columnType.equals(expectedType)) { throw new IllegalArgumentException("Column (name: " + columnSchema.getName() + ", index: " + columnIndex + ") is of type " + columnType.getName() + " but was requested as a type " + expectedType.getName()); } }
/** * Get the specified column's integer * @param columnIndex Column index in the schema * @return an integer * @throws IllegalArgumentException if the column is null, is unset, * or if the type doesn't match the column's type * @throws IndexOutOfBoundsException if the column doesn't exist */ public int getInt(int columnIndex) { checkColumn(schema.getColumnByIndex(columnIndex), Type.INT32); checkValue(columnIndex); return Bytes.getInt(rowAlloc, schema.getColumnOffset(columnIndex)); }
/** * Add an long for the specified column. * @param columnIndex the column's index in the schema * @param val value to add * @throws IllegalArgumentException if the value doesn't match the column's type * @throws IllegalStateException if the row was already applied * @throws IndexOutOfBoundsException if the column doesn't exist */ public void addLong(int columnIndex, long val) { checkNotFrozen(); checkColumn(schema.getColumnByIndex(columnIndex), Type.INT64, Type.UNIXTIME_MICROS); Bytes.setLong(rowAlloc, val, getPositionInRowAllocAndSetBitSet(columnIndex)); }
private AsyncKuduScanner getScanner(int start, int exclusiveEnd, List<String> columnNames) { PartialRow lowerBound = schema.newPartialRow(); lowerBound.addInt(schema.getColumnByIndex(0).getName(), start); PartialRow upperBound = schema.newPartialRow(); upperBound.addInt(schema.getColumnByIndex(0).getName(), exclusiveEnd); return client.newScannerBuilder(table) .lowerBound(lowerBound) .exclusiveUpperBound(upperBound) .setProjectedColumnNames(columnNames) .build(); }