public static Iterator<Statement> toStatements(Iterator<Row> iterator) throws RDFParseException, RDFHandlerException, IOException { if (!iterator.hasNext()) { return Collections.emptyIterator(); } Set<Statement> ret = new HashSet<Statement>(); RDFParser rdfParser = Rio.createParser(RDFFormat.BINARY); StatementCollector collector = new StatementCollector(ret); rdfParser.setRDFHandler(collector); while (iterator.hasNext()) { toStatements( rdfParser, Bytes.getArray(iterator.next().getBytes( DATA_TABLE_ATTRIBUTE_3))); } return Collections.unmodifiableSet(ret).iterator(); }
/** * Return a String equivalent of an Object. Useful for writing SQL. * @param val the object to String-ify * @return the String value */ public static String getStringValue(Object val) { if (val == null) return "NULL"; if (val instanceof String) return "'" + val.toString().replaceAll("'", "''") + "'"; // double any quotes if (val instanceof Number) return ""+val; if (val instanceof ByteBuffer) return "'" + Bytes.toHexString((ByteBuffer)val).substring(2) + "'"; // substring(2) is to remove the "0x" at front if (val instanceof Date) return "'" + (new Timestamp(((Date)val).getTime())).toString() + "'"; // Boolean, and anything else return val.toString(); }
@Override public String[] convertEntry(CredDAO.Data cd) { String[] columns = new String[5]; columns[0] = cd.id; columns[1] = String.valueOf(cd.type); DateFormat df = new SimpleDateFormat(DATE_FORMAT); columns[2] = df.format(cd.expires); columns[3] = Bytes.toHexString(cd.cred); columns[4] = (cd.ns==null)?"":cd.ns; return columns; }
/** * Overwrites the file given by the pair "desc" and "ext" witht the data in * the "data" ByteArray. * * @throws IOException if an exception occurs when communicating to the * database */ public void writeFile(DfsPackDescription desc, PackExt ext, ByteBuffer data) throws IOException { try { Statement stmt = QueryBuilder.insertInto(keyspace, DATA_TABLE_NAME) .value("name", desc.getFileName(ext)) .value("data", Bytes.toHexString(data)); session.execute(stmt); } catch (RuntimeException e) { e.printStackTrace(); throw new IOException(e); } }
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; }
public ResourceEntity read(GettableData data) { ResourceEntity resourceEntity = new ResourceEntity(); resourceEntity.setId(data.getString("id")); resourceEntity.setDeploymentId(data.getString("deployment_id")); resourceEntity.setName(data.getString("name")); resourceEntity.setBytes(Bytes.getArray(data.getBytes("content"))); return resourceEntity; }
@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))))); }
@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()))); }); }
/** * Transforms the given row in a byte array containing term identifiers. * * @param row the row. * @return a byte array containing term identifiers. */ private byte[][] asByteArray(final Row row) { final byte[] s = Bytes.getArray(row.getBytesUnsafe(0)); final byte[] p = Bytes.getArray(row.getBytesUnsafe(1)); final byte[] o = Bytes.getArray(row.getBytesUnsafe(2)); final ByteBuffer c = row.getBytesUnsafe(3); return (c != null) ? new byte[][] {s, p, o} : new byte[][] {s, p, o, Bytes.getArray(c)}; }
/** * Generate new id using <code>endpointKeyHash</code> and <code>lastModifyTime</code>. * * @return id */ public String generateId() { String id = null; if (endpointKeyHash != null) { StringBuilder builder = new StringBuilder(Bytes.toHexString(endpointKeyHash)); builder.append(CassandraModelConstants.KEY_DELIMITER).append(lastModifyTime.getTime()); id = builder.toString(); } return id; }
/** * Id consist of endpoint key hash and last modify time, we get these data and assign values on * fields <code>endpointKeyHash</code> and <code>lastModifyTime</code>. * * @param id is id to parsing */ public void parseStringId(String id) { String[] ids = parseId(id); if (ids != null && ids.length == 2) { endpointKeyHash = Bytes.fromHexString(ids[0]); lastModifyTime = new Date(Long.valueOf(ids[1])); } }
/** * This method convert ByteBuffer object to byte array. * * @return the byte array or null */ public static byte[] getBytes(ByteBuffer byteBuffer) { byte[] array = null; if (byteBuffer != null) { array = Bytes.getArray(byteBuffer); } return array; }
/** * This method convert ByteBuffer object to string representation, * if endpointKeyHash eq null, than return null. * * @return the String representation of endpoint key hash */ public static String convertKeyHashToString(ByteBuffer endpointKeyHash) { String id = null; if (endpointKeyHash != null) { id = Bytes.toHexString(endpointKeyHash); } return id; }
/** * Convert key hash to string. * * @param endpointKeyHash is key hash to convert * @return string */ public static String convertKeyHashToString(byte[] endpointKeyHash) { String id = null; if (endpointKeyHash != null) { id = Bytes.toHexString(endpointKeyHash); } return id; }
/** * This method convert string representation of endpoint key hash to ByteBuffer object * if id eq null, than return null. * * @return the ByteBuffer object */ public static ByteBuffer convertStringToKeyHash(String id) { ByteBuffer endpointKeyHash = null; if (id != null && id.length() != 0) { endpointKeyHash = Bytes.fromHexString(id); } return endpointKeyHash; }
@Test public void testBytesToStringConversation() { byte[] array = new byte[]{-16, 7, 51, -98, -75, -19, -82, 119, -51, 122, -125, -14, 22, 44, -28, -56, 26, 111, 115, 2}; String hash = Bytes.toHexString(array); LOG.info("---> hash is {}", hash); byte[] converted = Bytes.fromHexString(hash).array(); Assert.assertArrayEquals(array, converted); Assert.assertEquals(hash, Bytes.toHexString(converted)); }
@Override public String format(byte[] value) { if (value == null) return "NULL"; return Bytes.toHexString(value); }
@Override public byte[] parse(String value) { return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : Bytes.fromHexString(value).array(); }
@Test public void testGetRecords() throws Exception { 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; List<ConnectorTableLayoutResult> layouts = metadata.getTableLayouts(SESSION, tableHandle, Constraint.alwaysTrue(), Optional.empty()); ConnectorTableLayoutHandle layout = getOnlyElement(layouts).getTableLayout().getHandle(); List<ConnectorSplit> splits = getAllSplits(splitManager.getSplits(transaction, SESSION, layout)); long rowNumber = 0; for (ConnectorSplit split : splits) { CassandraSplit cassandraSplit = (CassandraSplit) split; long completedBytes = 0; try (RecordCursor cursor = recordSetProvider.getRecordSet(transaction, SESSION, cassandraSplit, 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, String.format("key %d", rowId)); assertEquals(Bytes.toHexString(cursor.getSlice(columnIndex.get("typebytes")).getBytes()), String.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(), String.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); }
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()); }
/** * 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)); }