github.com/tsuna/gohbase@v0.0.0-20250731002811-4ffcadfba63e/hrpc/create.go (about) 1 // Copyright (C) 2015 The GoHBase Authors. All rights reserved. 2 // This file is part of GoHBase. 3 // Use of this source code is governed by the Apache License 2.0 4 // that can be found in the COPYING file. 5 6 package hrpc 7 8 import ( 9 "context" 10 11 "github.com/tsuna/gohbase/pb" 12 "google.golang.org/protobuf/proto" 13 ) 14 15 // CreateTable represents a CreateTable HBase call 16 type CreateTable struct { 17 base 18 19 attributes map[string]string 20 families map[string]map[string]string 21 splitKeys [][]byte 22 } 23 24 var defaultFamiliesAttributes = map[string]string{ 25 "BLOOMFILTER": "ROW", 26 "VERSIONS": "3", 27 "IN_MEMORY": "false", 28 "KEEP_DELETED_CELLS": "false", 29 "DATA_BLOCK_ENCODING": "FAST_DIFF", 30 "TTL": "2147483647", 31 "COMPRESSION": "NONE", 32 "MIN_VERSIONS": "0", 33 "BLOCKCACHE": "true", 34 "BLOCKSIZE": "65536", 35 "REPLICATION_SCOPE": "0", 36 } 37 38 // NewCreateTable creates a new CreateTable request that will create the given 39 // table in HBase. 'families' is a map of column family name to its attributes. 40 // For use by the admin client. 41 func NewCreateTable(ctx context.Context, table []byte, 42 families map[string]map[string]string, 43 options ...func(*CreateTable)) *CreateTable { 44 ct := &CreateTable{ 45 base: base{ 46 table: table, 47 ctx: ctx, 48 resultch: make(chan RPCResult, 1), 49 }, 50 families: make(map[string]map[string]string, len(families)), 51 } 52 for _, option := range options { 53 option(ct) 54 } 55 for family, attrs := range families { 56 ct.families[family] = make(map[string]string, len(defaultFamiliesAttributes)) 57 for k, dv := range defaultFamiliesAttributes { 58 if v, ok := attrs[k]; ok { 59 ct.families[family][k] = v 60 } else { 61 ct.families[family][k] = dv 62 } 63 } 64 } 65 return ct 66 } 67 68 // SplitKeys will return an option that will set the split keys for the created table 69 func SplitKeys(sk [][]byte) func(*CreateTable) { 70 return func(ct *CreateTable) { 71 ct.splitKeys = sk 72 } 73 } 74 75 // TableAttributes will return an option that will set attributes on the created table 76 func TableAttributes(attrs map[string]string) func(*CreateTable) { 77 return func(ct *CreateTable) { 78 ct.attributes = attrs 79 } 80 } 81 82 // Name returns the name of this RPC call. 83 func (ct *CreateTable) Name() string { 84 return "CreateTable" 85 } 86 87 // Description returns the description of this RPC call. 88 func (ct *CreateTable) Description() string { 89 return ct.Name() 90 } 91 92 // ToProto converts the RPC into a protobuf message 93 func (ct *CreateTable) ToProto() proto.Message { 94 pbAttributes := make([]*pb.BytesBytesPair, 0, len(ct.attributes)) 95 for k, v := range ct.attributes { 96 pbAttributes = append(pbAttributes, &pb.BytesBytesPair{ 97 First: []byte(k), 98 Second: []byte(v), 99 }) 100 } 101 102 pbFamilies := make([]*pb.ColumnFamilySchema, 0, len(ct.families)) 103 for family, attrs := range ct.families { 104 f := &pb.ColumnFamilySchema{ 105 Name: []byte(family), 106 Attributes: make([]*pb.BytesBytesPair, 0, len(attrs)), 107 } 108 for k, v := range attrs { 109 f.Attributes = append(f.Attributes, &pb.BytesBytesPair{ 110 First: []byte(k), 111 Second: []byte(v), 112 }) 113 } 114 pbFamilies = append(pbFamilies, f) 115 } 116 117 return &pb.CreateTableRequest{ 118 TableSchema: &pb.TableSchema{ 119 TableName: &pb.TableName{ 120 // TODO: handle namespaces 121 Namespace: []byte("default"), 122 Qualifier: ct.table, 123 }, 124 Attributes: pbAttributes, 125 ColumnFamilies: pbFamilies, 126 }, 127 SplitKeys: ct.splitKeys, 128 } 129 } 130 131 // NewResponse creates an empty protobuf message to read the response of this 132 // RPC. 133 func (ct *CreateTable) NewResponse() proto.Message { 134 return &pb.CreateTableResponse{} 135 }