Java Code Examples for org.apache.cassandra.db.marshal.BytesType#instance()

The following examples show how to use org.apache.cassandra.db.marshal.BytesType#instance() . 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: BytesConversionFcts.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public static Function makeFromBlobFunction(final AbstractType<?> toType)
{
    final String name = "blobas" + toType.asCQL3Type();
    return new AbstractFunction(name, toType, BytesType.instance)
    {
        public ByteBuffer execute(List<ByteBuffer> parameters) throws InvalidRequestException
        {
            ByteBuffer val = parameters.get(0);
            try
            {
                if (val != null)
                    toType.validate(val);
                return val;
            }
            catch (MarshalException e)
            {
                throw new InvalidRequestException(String.format("In call to function %s, value 0x%s is not a valid binary representation for type %s",
                                                                name, ByteBufferUtil.bytesToHex(val), toType.asCQL3Type()));
            }
        }
    };
}
 
Example 2
Source File: LazyCassandraUtils.java    From Hive-Cassandra with Apache License 2.0 6 votes vote down vote up
public static AbstractType getCassandraType(PrimitiveObjectInspector oi) {
  switch (oi.getPrimitiveCategory()) {
  case BOOLEAN:
    return BooleanType.instance;
  case INT:
    return Int32Type.instance;
  case LONG:
    return LongType.instance;
  case FLOAT:
    return FloatType.instance;
  case DOUBLE:
    return DoubleType.instance;
  case STRING:
    return UTF8Type.instance;
  case BYTE:
  case SHORT:
  case BINARY:
    return BytesType.instance;
  case TIMESTAMP:
    return DateType.instance;
  default:
    throw new RuntimeException("Hive internal error.");

  }
}
 
Example 3
Source File: CassandraTypeConverterTest.java    From debezium-incubator with Apache License 2.0 5 votes vote down vote up
@Test
public void testBlob() {
    DataType blobType = DataType.blob();
    AbstractType<?> convertedType = CassandraTypeConverter.convert(blobType);

    BytesType expectedType = BytesType.instance;

    Assert.assertEquals(expectedType, convertedType);
}
 
Example 4
Source File: BytesConversionFcts.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public static Function makeToBlobFunction(AbstractType<?> fromType)
{
    String name = fromType.asCQL3Type() + "asblob";
    return new AbstractFunction(name, BytesType.instance, fromType)
    {
        public ByteBuffer execute(List<ByteBuffer> parameters)
        {
            return parameters.get(0);
        }
    };
}
 
Example 5
Source File: SSTableImport.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Get key validator for column family
 * @param columnFamily column family instance
 * @return key validator for given column family
 */
private AbstractType<?> getKeyValidator(ColumnFamily columnFamily) {
    // this is a fix to support backward compatibility
    // which allows to skip the current key validator
    // please, take a look onto CASSANDRA-7498 for more details
    if ("true".equals(System.getProperty("skip.key.validator", "false"))) {
        return BytesType.instance;
    }
    return columnFamily.metadata().getKeyValidator();
}
 
Example 6
Source File: CompressedRandomAccessReaderTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void test6791() throws IOException, ConfigurationException
{
    File f = File.createTempFile("compressed6791_", "3");
    String filename = f.getAbsolutePath();
    try
    {

        MetadataCollector sstableMetadataCollector = new MetadataCollector(new SimpleDenseCellNameType(BytesType.instance));
        CompressedSequentialWriter writer = new CompressedSequentialWriter(f, filename + ".metadata", new CompressionParameters(SnappyCompressor.instance, 32, Collections.<String, String>emptyMap()), sstableMetadataCollector);

        for (int i = 0; i < 20; i++)
            writer.write("x".getBytes());

        FileMark mark = writer.mark();
        // write enough garbage to create new chunks:
        for (int i = 0; i < 40; ++i)
            writer.write("y".getBytes());

        writer.resetAndTruncate(mark);

        for (int i = 0; i < 20; i++)
            writer.write("x".getBytes());
        writer.close();

        CompressedRandomAccessReader reader = CompressedRandomAccessReader.open(filename, new CompressionMetadata(filename + ".metadata", f.length(), true));
        String res = reader.readLine();
        assertEquals(res, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        assertEquals(40, res.length());
    }
    finally
    {
        // cleanup
        if (f.exists())
            f.delete();
        File metadata = new File(filename+ ".metadata");
            if (metadata.exists())
                metadata.delete();
    }
}
 
Example 7
Source File: JsonOutputFormat.java    From aegisthus with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private AbstractType<ByteBuffer> getConverter(Configuration conf, String key) {
    String converterType = conf.get(key);
    if (Strings.isNullOrEmpty(key)) {
        return BytesType.instance;
    }

    try {
        return (AbstractType<ByteBuffer>) TypeParser.parse(converterType);
    } catch (SyntaxException | ConfigurationException e) {
        throw Throwables.propagate(e);
    }
}
 
Example 8
Source File: CrunchBulkRecordWriter.java    From hdfs2cass with Apache License 2.0 5 votes vote down vote up
private void prepareWriter() {
  String columnFamily = CrunchConfigHelper.getOutputColumnFamily(conf);
  String keyspace = ConfigHelper.getOutputKeyspace(conf);

  if (outputdir == null) {
    // dir must be named by ks/cf for the loader
    outputdir = Paths.get(getOutputLocation(), keyspace, columnFamily).toFile();
    outputdir.mkdirs();
  }

  if (writer == null) {
    AbstractType<?> subcomparator = null;

    if (cfType == CFType.SUPER)
      subcomparator = BytesType.instance;

    this.writer = new SSTableSimpleWriter(
        outputdir, ConfigHelper.getOutputPartitioner(conf),
        keyspace, columnFamily,
        BytesType.instance, subcomparator);

    ExternalSSTableLoaderClient externalClient = new ExternalSSTableLoaderClient(
        ConfigHelper.getOutputInitialAddress(conf),
        ConfigHelper.getOutputRpcPort(conf),
        ConfigHelper.getOutputKeyspaceUserName(conf),
        ConfigHelper.getOutputKeyspacePassword(conf));

    this.loader = new SSTableLoader(outputdir, externalClient,
        new OutputHandler.SystemOutput(true, true));
  }
}
 
Example 9
Source File: AbstractByteOrderedPartitioner.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public AbstractType<?> getTokenValidator()
{
    return BytesType.instance;
}
 
Example 10
Source File: ColumnMapperBlob.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
/**
 * Builds a new {@link ColumnMapperBlob}.
 */
@JsonCreator
public ColumnMapperBlob() {
    super(new AbstractType<?>[]{AsciiType.instance, UTF8Type.instance, BytesType.instance}, new AbstractType[]{});
}
 
Example 11
Source File: HexBytes.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public HexBytes(String name, GeneratorConfig config)
{
    super(BytesType.instance, config, name, ByteBuffer.class);
    bytes = new byte[(int) sizeDistribution.maxValue()];
}
 
Example 12
Source File: Bytes.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public Bytes(String name, GeneratorConfig config)
{
    super(BytesType.instance, config, name, ByteBuffer.class);
    bytes = new byte[(int) sizeDistribution.maxValue()];
}
 
Example 13
Source File: HiveCassandraStandardColumnInputFormat.java    From Hive-Cassandra with Apache License 2.0 4 votes vote down vote up
@Override
public RecordReader<BytesWritable, MapWritable> getRecordReader(InputSplit split,
    JobConf jobConf, final Reporter reporter) throws IOException {
  HiveCassandraStandardSplit cassandraSplit = (HiveCassandraStandardSplit) split;

  List<String> columns = AbstractColumnSerDe.parseColumnMapping(cassandraSplit.getColumnMapping());
  isTransposed = AbstractColumnSerDe.isTransposed(columns);


  List<Integer> readColIDs = ColumnProjectionUtils.getReadColumnIDs(jobConf);

  if (columns.size() < readColIDs.size()) {
    throw new IOException("Cannot read more columns than the given table contains.");
  }

  org.apache.cassandra.hadoop.ColumnFamilySplit cfSplit = cassandraSplit.getSplit();
  Job job = new Job(jobConf);

  TaskAttemptContext tac = new TaskAttemptContext(job.getConfiguration(), new TaskAttemptID()) {
    @Override
    public void progress() {
      reporter.progress();
    }
  };

  SlicePredicate predicate = new SlicePredicate();

  if (isTransposed || readColIDs.size() == columns.size() || readColIDs.size() == 0) {
    SliceRange range = new SliceRange();
    AbstractType comparator = BytesType.instance;

    String comparatorType = jobConf.get(AbstractColumnSerDe.CASSANDRA_SLICE_PREDICATE_RANGE_COMPARATOR);
    if (comparatorType != null && !comparatorType.equals("")) {
      try {
        comparator = TypeParser.parse(comparatorType);
      } catch (Exception ex) {
        throw new IOException("Comparator class not found.");
      }
    }

    String sliceStart = jobConf.get(AbstractColumnSerDe.CASSANDRA_SLICE_PREDICATE_RANGE_START);
    String sliceEnd = jobConf.get(AbstractColumnSerDe.CASSANDRA_SLICE_PREDICATE_RANGE_FINISH);
    String reversed = jobConf.get(AbstractColumnSerDe.CASSANDRA_SLICE_PREDICATE_RANGE_REVERSED);

    range.setStart(comparator.fromString(sliceStart == null ? "" : sliceStart));
    range.setFinish(comparator.fromString(sliceEnd == null ? "" : sliceEnd));
    range.setReversed(reversed == null ? false : reversed.equals("true"));
    range.setCount(cassandraSplit.getSlicePredicateSize());
    predicate.setSlice_range(range);
  } else {
    int iKey = columns.indexOf(AbstractColumnSerDe.CASSANDRA_KEY_COLUMN);
    predicate.setColumn_names(getColumnNames(iKey, columns, readColIDs));
  }


  try {
    ConfigHelper.setInputColumnFamily(tac.getConfiguration(),
        cassandraSplit.getKeyspace(), cassandraSplit.getColumnFamily());

    ConfigHelper.setInputSlicePredicate(tac.getConfiguration(), predicate);
    ConfigHelper.setRangeBatchSize(tac.getConfiguration(), cassandraSplit.getRangeBatchSize());
    ConfigHelper.setInputRpcPort(tac.getConfiguration(), cassandraSplit.getPort() + "");
    ConfigHelper.setInputInitialAddress(tac.getConfiguration(), cassandraSplit.getHost());
    ConfigHelper.setInputPartitioner(tac.getConfiguration(), cassandraSplit.getPartitioner());
    // Set Split Size
    ConfigHelper.setInputSplitSize(tac.getConfiguration(), cassandraSplit.getSplitSize());

    CassandraHiveRecordReader rr = null;

    if(isTransposed && tac.getConfiguration().getBoolean(AbstractColumnSerDe.CASSANDRA_ENABLE_WIDEROW_ITERATOR, true)) {
      rr = new CassandraHiveRecordReader(new ColumnFamilyWideRowRecordReader(), isTransposed);
    } else {
      rr = new CassandraHiveRecordReader(new ColumnFamilyRecordReader(), isTransposed);
    }
    rr.initialize(cfSplit, tac);

    return rr;

  } catch (Exception ie) {
    throw new IOException(ie);
  }
}