com.datastax.driver.core.utils.Bytes Java Examples

The following examples show how to use com.datastax.driver.core.utils.Bytes. 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: TestSelect.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test(groups = {CASSANDRA, PROFILE_SPECIFIC_TESTS})
public void testAllDataTypes()
{
    // NOTE: DECIMAL is treated like DOUBLE
    QueryResult query = query(format(
            "SELECT a, b, bl, bo, d, do, dt, f, fr, i, integer, l, m, s, si, t, ti, ts, tu, u, v, vari FROM %s.%s.%s",
            CONNECTOR_NAME, KEY_SPACE, CASSANDRA_ALL_TYPES.getName()));

    assertThat(query)
            .hasColumns(VARCHAR, BIGINT, VARBINARY, BOOLEAN, DOUBLE, DOUBLE, DATE, REAL, VARCHAR, VARCHAR,
                    INTEGER, VARCHAR, VARCHAR, VARCHAR, SMALLINT, VARCHAR, TINYINT, TIMESTAMP, VARCHAR, VARCHAR,
                    VARCHAR, VARCHAR)
            .containsOnly(
                    row("\0", Long.MIN_VALUE, Bytes.fromHexString("0x00").array(), false, 0f, Double.MIN_VALUE, Date.valueOf("1970-01-02"),
                            Float.MIN_VALUE, "[0]", "0.0.0.0", Integer.MIN_VALUE, "[0]", "{\"\\u0000\":-2147483648,\"a\":0}",
                            "[0]", Short.MIN_VALUE, "\0", Byte.MIN_VALUE, Timestamp.valueOf(LocalDateTime.of(1970, 1, 1, 0, 0)),
                            "d2177dd0-eaa2-11de-a572-001b779c76e3", "01234567-0123-0123-0123-0123456789ab",
                            "\0", String.valueOf(Long.MIN_VALUE)),
                    row("the quick brown fox jumped over the lazy dog", 9223372036854775807L, "01234".getBytes(UTF_8),
                            true, new Double("99999999999999999999999999999999999999"), Double.MAX_VALUE, Date.valueOf("9999-12-31"),
                            Float.MAX_VALUE, "[4,5,6,7]", "255.255.255.255", Integer.MAX_VALUE, "[4,5,6]",
                            "{\"a\":1,\"b\":2}", "[4,5,6]", Short.MAX_VALUE, "this is a text value", Byte.MAX_VALUE, Timestamp.valueOf(LocalDateTime.of(9999, 12, 31, 23, 59, 59)),
                            "d2177dd0-eaa2-11de-a572-001b779c76e3", "01234567-0123-0123-0123-0123456789ab",
                            "abc", String.valueOf(Long.MAX_VALUE)),
                    row("def", null, null, null, null, null, null, null, null, null, null,
                            null, null, null, null, null, null, null, null, null, null, null));
}
 
Example #2
Source File: CassandraType.java    From presto with Apache License 2.0 5 votes vote down vote up
private static String objectToJson(Object cassandraValue, DataType dataType)
{
    CassandraType cassandraType = toCassandraType(dataType.getName())
            .orElseThrow(() -> new IllegalStateException("Unsupported type: " + dataType));

    switch (cassandraType) {
        case ASCII:
        case TEXT:
        case VARCHAR:
        case UUID:
        case TIMEUUID:
        case TIMESTAMP:
        case DATE:
        case INET:
        case VARINT:
            return quoteStringLiteralForJson(cassandraValue.toString());

        case BLOB:
        case CUSTOM:
            return quoteStringLiteralForJson(Bytes.toHexString((ByteBuffer) cassandraValue));

        case SMALLINT:
        case TINYINT:
        case INT:
        case BIGINT:
        case COUNTER:
        case BOOLEAN:
        case DOUBLE:
        case FLOAT:
        case DECIMAL:
            return cassandraValue.toString();
        case LIST:
        case SET:
            return buildArrayValue((Collection<?>) cassandraValue, getOnlyElement(dataType.getTypeArguments()));
        case MAP:
            return buildMapValue((Map<?, ?>) cassandraValue, dataType.getTypeArguments().get(0), dataType.getTypeArguments().get(1));
        default:
            throw new IllegalStateException("Unsupported type: " + cassandraType);
    }
}
 
Example #3
Source File: DeviceDAOImpl.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
public DeviceDriverStateHolder loadDriverState(Device device) {
   Preconditions.checkNotNull(device, "device cannot be null");
   Preconditions.checkNotNull(device.getId(), "device must have an id");

   BoundStatement bound = new BoundStatement(loadState);
   Row r;
   try(Context ctxt = loadDriverStateTimer.time()) {
      r = session.execute(bound.bind(device.getId())).one();
   }

   if(r == null) {
      return null;
   }

   Map<String,String> encoded = r.getMap(NonEntityColumns.ATTRIBUTES, String.class, String.class);
   AttributeMap attributes = AttributeMap.newMap();
   for(Map.Entry<String, String> entry: encoded.entrySet()) {
      AttributeKey<?> key = key(entry.getKey());
      if(key == null) {
         continue;
      }
      Object value = deserialize(key, entry.getValue());
      // this shouldn't be necessary...
      attributes.add(key.coerceToValue(value));
   }


   Map<String,Object> variables = new HashMap<>();
   ByteBuffer buf = r.getBytes(NonEntityColumns.VARIABLES);
   if (buf != null) {
      variables = SerializationUtils.deserialize(Bytes.getArray(buf));
   }

   return new DeviceDriverStateHolder(attributes, variables);
}
 
Example #4
Source File: DatastaxBackend.java    From heroic with Apache License 2.0 5 votes vote down vote up
@Override
public AsyncFuture<List<String>> serializeKeyToHex(final BackendKey key) {
    final MetricsRowKey rowKey = new MetricsRowKey(key.getSeries(), key.getBase());

    return connection.doto(c -> async.resolved(
        ImmutableList.of(Bytes.toHexString(c.schema.rowKey().serialize(rowKey)))));
}
 
Example #5
Source File: DatastaxBackend.java    From heroic with Apache License 2.0 5 votes vote down vote up
@Override
public AsyncFuture<List<BackendKey>> deserializeKeyFromHex(String key) {
    return connection.doto(c -> {
        final MetricsRowKey rowKey = c.schema.rowKey().deserialize(Bytes.fromHexString(key));
        return async.resolved(
            ImmutableList.of(new BackendKey(rowKey.getSeries(), rowKey.getBase())));
    });
}
 
Example #6
Source File: CorruptionTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public static byte[] get(byte[] key)
{
    BoundStatement boundStatement = new BoundStatement(getStatement);
    boundStatement.setBytes(0, ByteBuffer.wrap(key));

    final com.datastax.driver.core.ResultSet resultSet =  session.execute(boundStatement);
    final Row row = resultSet.one();
    if (row != null)
    {
        final ByteBuffer byteBuf = row.getBytes("value");
        return Bytes.getArray(byteBuf);
    }

    return null;
}
 
Example #7
Source File: CassandraType.java    From presto with Apache License 2.0 4 votes vote down vote up
public String getColumnValueForCql(Row row, int position)
{
    if (row.isNull(position)) {
        return null;
    }

    switch (this) {
        case ASCII:
        case TEXT:
        case VARCHAR:
            return quoteStringLiteral(row.getString(position));
        case INT:
            return Integer.toString(row.getInt(position));
        case SMALLINT:
            return Short.toString(row.getShort(position));
        case TINYINT:
            return Byte.toString(row.getByte(position));
        case BIGINT:
        case COUNTER:
            return Long.toString(row.getLong(position));
        case BOOLEAN:
            return Boolean.toString(row.getBool(position));
        case DOUBLE:
            return Double.toString(row.getDouble(position));
        case FLOAT:
            return Float.toString(row.getFloat(position));
        case DECIMAL:
            return row.getDecimal(position).toString();
        case UUID:
        case TIMEUUID:
            return row.getUUID(position).toString();
        case TIMESTAMP:
            return Long.toString(row.getTimestamp(position).getTime());
        case DATE:
            return row.getDate(position).toString();
        case INET:
            return quoteStringLiteral(toAddrString(row.getInet(position)));
        case VARINT:
            return row.getVarint(position).toString();
        case BLOB:
        case CUSTOM:
            return Bytes.toHexString(row.getBytesUnsafe(position));
        default:
            throw new IllegalStateException("Handling of type " + this + " is not implemented");
    }
}
 
Example #8
Source File: TestCassandraConnector.java    From presto with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetRecords()
{
    ConnectorTableHandle tableHandle = getTableHandle(table);
    ConnectorTableMetadata tableMetadata = metadata.getTableMetadata(SESSION, tableHandle);
    List<ColumnHandle> columnHandles = ImmutableList.copyOf(metadata.getColumnHandles(SESSION, tableHandle).values());
    Map<String, Integer> columnIndex = indexColumns(columnHandles);

    ConnectorTransactionHandle transaction = CassandraTransactionHandle.INSTANCE;

    tableHandle = metadata.applyFilter(SESSION, tableHandle, Constraint.alwaysTrue()).get().getHandle();

    List<ConnectorSplit> splits = getAllSplits(splitManager.getSplits(transaction, SESSION, tableHandle, UNGROUPED_SCHEDULING));

    long rowNumber = 0;
    for (ConnectorSplit split : splits) {
        CassandraSplit cassandraSplit = (CassandraSplit) split;

        long completedBytes = 0;
        try (RecordCursor cursor = recordSetProvider.getRecordSet(transaction, SESSION, cassandraSplit, tableHandle, columnHandles).cursor()) {
            while (cursor.advanceNextPosition()) {
                try {
                    assertReadFields(cursor, tableMetadata.getColumns());
                }
                catch (RuntimeException e) {
                    throw new RuntimeException("row " + rowNumber, e);
                }

                rowNumber++;

                String keyValue = cursor.getSlice(columnIndex.get("key")).toStringUtf8();
                assertTrue(keyValue.startsWith("key "));
                int rowId = Integer.parseInt(keyValue.substring(4));

                assertEquals(keyValue, "key " + rowId);

                assertEquals(Bytes.toHexString(cursor.getSlice(columnIndex.get("typebytes")).getBytes()), format("0x%08X", rowId));

                // VARINT is returned as a string
                assertEquals(cursor.getSlice(columnIndex.get("typeinteger")).toStringUtf8(), String.valueOf(rowId));

                assertEquals(cursor.getLong(columnIndex.get("typelong")), 1000 + rowId);

                assertEquals(cursor.getSlice(columnIndex.get("typeuuid")).toStringUtf8(), format("00000000-0000-0000-0000-%012d", rowId));

                assertEquals(cursor.getSlice(columnIndex.get("typetimestamp")).toStringUtf8(), Long.valueOf(DATE.getTime()).toString());

                long newCompletedBytes = cursor.getCompletedBytes();
                assertTrue(newCompletedBytes >= completedBytes);
                completedBytes = newCompletedBytes;
            }
        }
    }
    assertEquals(rowNumber, 9);
}
 
Example #9
Source File: DeviceDAOImpl.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
@Override
protected void populateEntity(Row row, Device entity) {
   entity.setAccount(row.getUUID(DeviceEntityColumns.ACCOUNT_ID));
   entity.setAddress(row.getString(DeviceEntityColumns.DRIVER_ADDRESS));
   entity.setProtocolAddress(row.getString(DeviceEntityColumns.PROTOCOL_ADDRESS));

   String hubId = row.getString(DeviceEntityColumns.HUB_ID2);
   if(hubId == null) {
      ColumnRepairMetrics.incHubIdCounter();
      hubId = row.getString(DeviceEntityColumns.HUB_ID);
   }

   entity.setHubId(hubId);
   entity.setProtocol(row.getString(DeviceEntityColumns.PROTOCOL_NAME));
   entity.setProtocolid(row.getString(DeviceEntityColumns.PROTOCOL_ID));

   String version = row.getString(DeviceEntityColumns.DRIVER_VERSION2);
   if(version == null) {
      ColumnRepairMetrics.incDriverVersionCounter();
      version = row.getString(DeviceEntityColumns.DRIVER_VERSION);
   }

   DriverId driverId = new DriverId(row.getString(DeviceEntityColumns.DRIVER_NAME), version == null ? Version.UNVERSIONED : Version.fromRepresentation(version));
   entity.setDriverId(driverId);
   entity.setState(row.getString(DeviceEntityColumns.STATE));
   entity.setPlace(row.getUUID(DeviceEntityColumns.PLACE_ID));
   entity.setCaps(row.getSet(DeviceEntityColumns.CAPS, String.class));
   entity.setDevtypehint(row.getString(DeviceEntityColumns.DEVTYPEHINT));
   entity.setName(row.getString(DeviceEntityColumns.NAME));

   entity.setVendor(row.getString(DeviceEntityColumns.VENDOR));
   entity.setModel(row.getString(DeviceEntityColumns.MODEL));
   entity.setProductId(row.getString(DeviceEntityColumns.PRODUCTID));
   entity.setSubprotocol(row.getString(DeviceEntityColumns.SUBPROTOCOL));
   ByteBuffer buf = row.getBytes(DeviceEntityColumns.PROTOCOL_ATTRS);
   if (buf != null) {
      entity.setProtocolAttributes(bytesToProtocolAttributes(Bytes.getArray(buf)));
   }
   entity.setDegradedCode(row.getString(DeviceEntityColumns.DEGRADED));
   entity.setHubLocal(row.getBool(DeviceEntityColumns.HUBLOCAL));
}
 
Example #10
Source File: BytesBlobCodec.java    From cassandra-jdbc-driver with Apache License 2.0 4 votes vote down vote up
@Override
public String format(byte[] value) {
    if (value == null)
        return "NULL";
    return Bytes.toHexString(value);
}
 
Example #11
Source File: BytesBlobCodec.java    From cassandra-jdbc-driver with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] parse(String value) {
    return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")
            ? null : Bytes.fromHexString(value).array();
}
 
Example #12
Source File: CassandraCluster.java    From monasca-persister with Apache License 2.0 4 votes vote down vote up
private void loadMetricIdCache(ExecutorService executor) {
  final AtomicInteger tasks = new AtomicInteger(0);
  logger.info("Found token ranges: " + cluster.getMetadata().getTokenRanges().size());
  for (TokenRange range : cluster.getMetadata().getTokenRanges()) {
    List<BoundStatement> queries = rangeQuery(retrieveMetricIdStmt, range);
    for (BoundStatement query : queries) {
      tasks.incrementAndGet();
      logger.info("adding a metric id reading task, total: " + tasks.get());

      ResultSetFuture future = metricsSession.executeAsync(query);

      Futures.addCallback(future, new FutureCallback<ResultSet>() {
        @Override
        public void onSuccess(ResultSet result) {
          for (Row row : result) {
            String id = Bytes.toHexString(row.getBytes(METRIC_ID));
            if (id != null) {
              //remove '0x'
              metricIdCache.put(id.substring(2), Boolean.TRUE);
            }
          }

          tasks.decrementAndGet();

          logger.info("completed a metric id read task. Remaining tasks: " + tasks.get());
        }

        @Override
        public void onFailure(Throwable t) {
          logger.error("Failed to execute query to load metric id cache.", t);

          tasks.decrementAndGet();

          logger.info("Failed a metric id read task. Remaining tasks: " + tasks.get());
        }
      }, executor);

    }
  }

  while (tasks.get() > 0) {
    logger.debug("waiting for more metric id load tasks: " + tasks.get());

    try {
      Thread.sleep(3000);
    } catch (InterruptedException e) {
      logger.warn("load metric cache was interrupted", e);
    }
  }

  logger.info("loaded metric id cache from database: " + metricIdCache.size());
}
 
Example #13
Source File: Serializer.java    From cumulusrdf with Apache License 2.0 2 votes vote down vote up
/**
 * Deserializes the object from the given ByteBuffer.
 * 
 * @param serialized The serialized object.
 * @return The deserialized object.
 */
public T deserialize(final ByteBuffer serialized) {
	return deserializeInternal(Bytes.getArray(serialized));
}