Kudu C++ client API
schema.h
1 //
2 // Licensed to the Apache Software Foundation (ASF) under one
3 // or more contributor license agreements. See the NOTICE file
4 // distributed with this work for additional information
5 // regarding copyright ownership. The ASF licenses this file
6 // to you under the Apache License, Version 2.0 (the
7 // "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing,
13 // software distributed under the License is distributed on an
14 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, either express or implied. See the License for the
16 // specific language governing permissions and limitations
17 // under the License.
18 #ifndef KUDU_CLIENT_SCHEMA_H
19 #define KUDU_CLIENT_SCHEMA_H
20 
21 #include <string>
22 #include <vector>
23 
24 #include "kudu/client/value.h"
25 #include "kudu/util/kudu_export.h"
26 
27 namespace kudu {
28 
29 class ColumnSchema;
30 class KuduPartialRow;
31 class Schema;
32 class TestWorkload;
33 
34 namespace tools {
35 class RemoteKsckMaster;
36 class ReplicaDumper;
37 }
38 
39 namespace client {
40 
41 namespace internal {
42 class GetTableSchemaRpc;
43 class LookupRpc;
44 class MetaCacheEntry;
45 class WriteRpc;
46 } // namespace internal
47 
48 class KuduClient;
49 class KuduSchema;
50 class KuduSchemaBuilder;
51 class KuduWriteOperation;
52 
54 class KUDU_EXPORT KuduColumnStorageAttributes {
55  public:
57  enum EncodingType {
58  AUTO_ENCODING = 0,
59  PLAIN_ENCODING = 1,
60  PREFIX_ENCODING = 2,
61  RLE = 4,
62  DICT_ENCODING = 5,
63  BIT_SHUFFLE = 6,
64 
67  GROUP_VARINT = 3
68  };
69 
72  DEFAULT_COMPRESSION = 0,
73  NO_COMPRESSION = 1,
74  SNAPPY = 2,
75  LZ4 = 3,
76  ZLIB = 4,
77  };
78 
79 
92  EncodingType encoding = AUTO_ENCODING,
93  CompressionType compression = DEFAULT_COMPRESSION,
94  int32_t block_size = 0)
95  ATTRIBUTE_DEPRECATED("this constructor will be private in a future release")
96  : encoding_(encoding),
97  compression_(compression),
98  block_size_(block_size) {
99  }
100 
102  const EncodingType encoding() const {
103  return encoding_;
104  }
105 
107  const CompressionType compression() const {
108  return compression_;
109  }
110 
112  std::string ToString() const;
113 
114  private:
115  EncodingType encoding_;
116  CompressionType compression_;
117  int32_t block_size_;
118 };
119 
121 class KUDU_EXPORT KuduColumnSchema {
122  public:
124  enum DataType {
125  INT8 = 0,
126  INT16 = 1,
127  INT32 = 2,
128  INT64 = 3,
129  STRING = 4,
130  BOOL = 5,
131  FLOAT = 6,
132  DOUBLE = 7,
133  BINARY = 8,
134  UNIXTIME_MICROS = 9,
135  TIMESTAMP = UNIXTIME_MICROS
136  };
137 
141  static std::string DataTypeToString(DataType type);
142 
159  KuduColumnSchema(const std::string &name,
160  DataType type,
161  bool is_nullable = false,
162  const void* default_value = NULL,
164  ATTRIBUTE_DEPRECATED("use KuduSchemaBuilder instead");
165 
170  KuduColumnSchema(const KuduColumnSchema& other);
171  ~KuduColumnSchema();
172 
178  KuduColumnSchema& operator=(const KuduColumnSchema& other);
179 
184  void CopyFrom(const KuduColumnSchema& other);
185 
191  bool Equals(const KuduColumnSchema& other) const;
192 
199  const std::string& name() const;
200 
202  DataType type() const;
203 
205  bool is_nullable() const;
207 
208  private:
209  friend class KuduColumnSpec;
210  friend class KuduSchema;
211  friend class KuduSchemaBuilder;
212  // KuduTableAlterer::Data needs to be a friend. Friending the parent class
213  // is transitive to nested classes. See http://tiny.cloudera.com/jwtui
214  friend class KuduTableAlterer;
215 
217 
218  // Owned.
219  ColumnSchema* col_;
220 };
221 
230 class KUDU_EXPORT KuduColumnSpec {
231  public:
243  KuduColumnSpec* Default(KuduValue* value);
244 
250  KuduColumnSpec* Compression(KuduColumnStorageAttributes::CompressionType compression);
251 
259  KuduColumnSpec* Encoding(KuduColumnStorageAttributes::EncodingType encoding);
260 
280  KuduColumnSpec* BlockSize(int32_t block_size);
281 
294  KuduColumnSpec* PrimaryKey();
295 
301  KuduColumnSpec* NotNull();
302 
308  KuduColumnSpec* Nullable();
309 
317  KuduColumnSpec* Type(KuduColumnSchema::DataType type);
319 
329  KuduColumnSpec* RemoveDefault();
330 
336  KuduColumnSpec* RenameTo(const std::string& new_name);
338 
339  private:
340  class KUDU_NO_EXPORT Data;
341  friend class KuduSchemaBuilder;
342  friend class KuduTableAlterer;
343 
344  // This class should always be owned and deleted by one of its friends,
345  // not the user.
346  ~KuduColumnSpec();
347 
348  explicit KuduColumnSpec(const std::string& col_name);
349 
350  Status ToColumnSchema(KuduColumnSchema* col) const;
351 
352  // Owned.
353  Data* data_;
354 };
355 
377 class KUDU_EXPORT KuduSchemaBuilder {
378  public:
381 
388  KuduColumnSpec* AddColumn(const std::string& name);
389 
397  KuduSchemaBuilder* SetPrimaryKey(const std::vector<std::string>& key_col_names);
398 
408  Status Build(KuduSchema* schema);
409 
410  private:
411  class KUDU_NO_EXPORT Data;
412  // Owned.
413  Data* data_;
414 };
415 
417 class KUDU_EXPORT KuduSchema {
418  public:
419  KuduSchema();
420 
425  KuduSchema(const KuduSchema& other);
426  ~KuduSchema();
427 
434  KuduSchema& operator=(const KuduSchema& other);
435  void CopyFrom(const KuduSchema& other);
437 
447  Status Reset(const std::vector<KuduColumnSchema>& columns, int key_columns)
448  ATTRIBUTE_DEPRECATED("this method will be removed in a future release")
449  WARN_UNUSED_RESULT;
450 
457  bool Equals(const KuduSchema& other) const;
458 
462  KuduColumnSchema Column(size_t idx) const;
463 
465  size_t num_columns() const;
466 
475  void GetPrimaryKeyColumnIndexes(std::vector<int>* indexes) const;
476 
484  KuduPartialRow* NewRow() const;
485 
486  private:
487  friend class KuduClient;
488  friend class KuduScanner;
489  friend class KuduScanToken;
490  friend class KuduScanTokenBuilder;
491  friend class KuduSchemaBuilder;
492  friend class KuduTable;
493  friend class KuduTableCreator;
494  friend class KuduWriteOperation;
495  friend class ScanConfiguration;
496  friend class internal::GetTableSchemaRpc;
497  friend class internal::LookupRpc;
498  friend class internal::MetaCacheEntry;
499  friend class internal::WriteRpc;
500  friend class tools::RemoteKsckMaster;
501  friend class tools::ReplicaDumper;
502 
503  friend KuduSchema KuduSchemaFromSchema(const Schema& schema);
504 
505 
506  // For use by kudu tests.
507  explicit KuduSchema(const Schema& schema);
508 
509  // Private since we don't want users to rely on the first N columns
510  // being the keys.
511  size_t num_key_columns() const;
512 
513  // Owned.
514  Schema* schema_;
515 };
516 
517 } // namespace client
518 } // namespace kudu
519 #endif // KUDU_CLIENT_SCHEMA_H
A representation of a table&#39;s schema.
Definition: schema.h:417
A representation of an operation&#39;s outcome.
Definition: status.h:130
A constant cell value with a specific type.
Definition: value.h:33
Definition: callbacks.h:28
Representation of column storage attributes.
Definition: schema.h:54
Builder API for specifying or altering a column within a table schema.
Definition: schema.h:230
Representation of the column schema.
Definition: schema.h:121
const EncodingType encoding() const
Definition: schema.h:102
Builds scan tokens for a table.
Definition: client.h:2118
Alters an existing table based on the provided steps.
Definition: client.h:1005
KuduColumnStorageAttributes(EncodingType encoding=AUTO_ENCODING, CompressionType compression=DEFAULT_COMPRESSION, int32_t block_size=0) ATTRIBUTE_DEPRECATED("this const ructor will be private in a future release")
Definition: schema.h:91
A handle for a connection to a cluster.
Definition: client.h:283
A representation of a table on a particular cluster.
Definition: client.h:850
This class is a representation of a single scan.
Definition: client.h:1682
A single-row write operation to be sent to a Kudu table.
Definition: write_op.h:56
CompressionType
Column compression types.
Definition: schema.h:71
Builder API for constructing a KuduSchema object.
Definition: schema.h:377
DataType
Supported data types for columns.
Definition: schema.h:124
A scan descriptor limited to a single physical contiguous location.
Definition: client.h:2058
A helper class to create a new table with the desired options.
Definition: client.h:634
A row which may only contain values for a subset of the columns.
Definition: partial_row.h:61
const CompressionType compression() const
Definition: schema.h:107
EncodingType
Column encoding types.
Definition: schema.h:57