Example #1
0
        public static void CreateSearchIndex(OTSClient otsClient)
        {
            Console.WriteLine("\n Start Create searchindex...");

            CreateSearchIndexRequest request      = new CreateSearchIndexRequest(TableName, IndexName);
            List <FieldSchema>       FieldSchemas = new List <FieldSchema>()
            {
                new FieldSchema(Keyword_type_col, FieldType.KEYWORD)
                {
                    index = true, EnableSortAndAgg = true
                },
                new FieldSchema(Long_type_col, FieldType.LONG)
                {
                    index = true, EnableSortAndAgg = true
                },
                new FieldSchema(Text_type_col, FieldType.TEXT)
                {
                    index = true
                }
            };

            request.IndexSchame = new IndexSchema()
            {
                FieldSchemas = FieldSchemas
            };

            CreateSearchIndexResponse response = otsClient.CreateSearchIndex(request);

            Console.WriteLine("Searchindex is created: " + IndexName);
        }
        /// <summary>
        /// 创建一个多元索引,包含Keyword_type_col、Long_type_col、Text_type_col三个属性列,类型分别设置为不分词字符串(KEYWORD),整型(LONG),分词字符串(TEXT)
        /// </summary>
        /// <param name="otsClient"></param>
        public static void CreateSearchIndexWithIndexSort(OTSClient otsClient)
        {
            Console.WriteLine("\n Start Create searchindex with indexSort...");

            //指定表名和索引名
            CreateSearchIndexRequest request      = new CreateSearchIndexRequest(TableName, IndexName);
            List <FieldSchema>       FieldSchemas = new List <FieldSchema>()
            {
                new FieldSchema(Keyword_type_col, FieldType.KEYWORD) //设置字段名和字段类型
                {
                    index            = true,                         //开启索引
                    EnableSortAndAgg = true                          //开启排序和统计功能
                },
                new FieldSchema(Long_type_col, FieldType.LONG)
                {
                    index = true, EnableSortAndAgg = true
                },
                new FieldSchema(Text_type_col, FieldType.TEXT)
                {
                    index = true
                }
            };

            request.IndexSchame = new IndexSchema()
            {
                FieldSchemas = FieldSchemas,
                IndexSort    = new Sort(new List <ISorter>()
                {
                    new FieldSort(Long_type_col, SortOrder.ASC)
                })
            };

            CreateSearchIndexResponse response = otsClient.CreateSearchIndex(request);

            Console.WriteLine("Searchindex is created: " + IndexName);
        }
Example #3
0
 /// <summary>
 /// CreateSearchIndex的异步形式。
 /// </summary>
 /// <param name="request"></param>
 /// <returns></returns>
 public Task <CreateSearchIndexResponse> CreateSearchIndexAsync(CreateSearchIndexRequest request)
 {
     return(CallAsync <CreateSearchIndexRequest, CreateSearchIndexResponse>("/CreateSearchIndex", request));
 }
Example #4
0
 public CreateSearchIndexResponse CreateSearchIndex(CreateSearchIndexRequest request)
 {
     return(GetResponseFromAsyncTask(CreateSearchIndexAsync(request)));
 }
Example #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="type"></param>
        protected static string SetTable(Type type)
        {
            #region 初始化缓存
            if (!tableNames.ContainsKey(type))
            {
                foreach (var attr in type.CustomAttributes)
                {
                    if (attr.AttributeType == typeof(System.ComponentModel.DataAnnotations.Schema.TableAttribute))
                    {
                        tableNames.TryAdd(type, attr.ConstructorArguments[0].Value.ToString());
                        break;
                    }
                }
            }
            #endregion

            var name = tableNames.ContainsKey(type) ? tableNames[type] : type.Name.ToLower();

            #region 分析表结构
            var pkSchema = new PrimaryKeySchema();
            foreach (PropertyInfo info in rft.GetPropertyList(type))
            {
                if (info.GetCustomAttribute(typeof(System.ComponentModel.DataAnnotations.KeyAttribute)) == null)
                {
                    continue;
                }
                else if (info.PropertyType == typeof(int))
                {
                    pkSchema.Add(info.Name, ColumnValueType.Integer);
                }
                else if (info.PropertyType == typeof(string))
                {
                    pkSchema.Add(info.Name, ColumnValueType.String);
                }
                else if (info.PropertyType == typeof(byte[]))
                {
                    pkSchema.Add(info.Name, ColumnValueType.Binary);
                }
                else
                {
                    throw new Exception("不支持当前类型的主键");
                }
            }
            ;
            #endregion

            #region 生成表结构
            if (tableKey.ContainsKey(name))
            {
                #region 对比已有结构
                tableKey[name].ForEach(key =>
                {
                    pkSchema.ForEach(tmp =>
                    {
                        if (key.Item1 == tmp.Item1 && (key.Item2 != tmp.Item2 || key.Item3 != tmp.Item3))
                        {
                            log.Fatal("Table structure is inconsistent, table name:" + name);
                            return;
                        }
                    });
                });
                #endregion
            }
            else
            {
                #region 创建新表
                //通过表名和主键列的schema创建一个tableMeta
                var client    = new OTSClient(OtsConfig);
                var tableMeta = new TableMeta(name, pkSchema);
                // 设定预留读吞吐量为0,预留写吞吐量为0
                var reservedThroughput = new CapacityUnit(0, 0);

                try
                {
                    // 构造CreateTableRequest对象
                    var reqCreateTable = new CreateTableRequest(tableMeta, reservedThroughput);
                    // 调用client的CreateTable接口,如果没有抛出异常,则说明成功,否则失败
                    client.CreateTable(reqCreateTable);

                    // 生成查询索引
                    var reqCreateSearchIndex = new CreateSearchIndexRequest(name, name);
                    var fieldSchemas         = new List <FieldSchema>();
                    foreach (PropertyInfo info in rft.GetPropertyList(type))
                    {
                        if (info.GetCustomAttribute(typeof(System.ComponentModel.DataAnnotations.CompareAttribute)) == null)
                        {
                            continue;
                        }
                        else if (info.PropertyType == typeof(int) || info.PropertyType == typeof(long))
                        {
                            fieldSchemas.Add(new FieldSchema(info.Name, FieldType.LONG)
                            {
                                index = true
                            });
                        }
                        else if (info.PropertyType == typeof(string))
                        {
                            fieldSchemas.Add(new FieldSchema(info.Name, FieldType.TEXT)
                            {
                                index = true
                            });
                        }
                        else if (info.PropertyType == typeof(double))
                        {
                            fieldSchemas.Add(new FieldSchema(info.Name, FieldType.DOUBLE)
                            {
                                index = true
                            });
                        }
                        else if (info.PropertyType == typeof(bool))
                        {
                            fieldSchemas.Add(new FieldSchema(info.Name, FieldType.BOOLEAN)
                            {
                                index = true
                            });
                        }
                    }
                    ;
                    if (fieldSchemas.Count > 0)
                    {
                        reqCreateSearchIndex.IndexSchame = new IndexSchema()
                        {
                            FieldSchemas = fieldSchemas
                        };
                        client.CreateSearchIndex(reqCreateSearchIndex);
                        log.Info("Create table succeeded, table name:" + name);
                    }
                }
                // 处理异常
                catch (Exception ex)
                {
                    Console.WriteLine("Create table failed, exception:{0}", ex.Message);
                }
                #endregion
            }
            #endregion

            return(name);
        }