Lindorm 實踐 | 5分鐘入門 Lindorm SearchIndex
一. 引言
云原生多模數(shù)據(jù)庫Lindorm,支持海量數(shù)據(jù)的低成本存儲和彈性按需付費,提供寬表、時序、文件等多種數(shù)據(jù)模型,兼容HBase、Cassandra、OpenTSDB等多種開源標準接口,是互聯(lián)網(wǎng)、IoT、車聯(lián)網(wǎng)、廣告、社交、監(jiān)控、游戲、風控等場景的首選數(shù)據(jù)庫,也是為阿里巴巴核心業(yè)務提供關鍵支撐的數(shù)據(jù)庫之一。關于Lindorm的更多介紹,可以參考 云原生多模數(shù)據(jù)庫Lindorm。
Lindorm對外提供統(tǒng)一的標準SQL入口,可以讓開發(fā)人員快速上手,輕松運維海量數(shù)據(jù)。SearchIndex是Lindorm寬表的二級索引,主要用來幫助業(yè)務實現(xiàn)快速的檢索分析。本篇文章介紹如何通過簡單的SQL接口操作SearchIndex。
二. SQL操作SearchIndex
開通全文索引
當您購買Lindorm寬表引擎后,可以在數(shù)據(jù)庫實例控制臺開通“全文索引”功能,如下圖:

下載SQL工具
進入數(shù)據(jù)庫實例控制臺,在“數(shù)據(jù)庫連接”中下載SQL工具:Lindorm-cli。同時,為了能夠讓本機可以訪問集群,需要“開通外網(wǎng)地址”,并將本機ip加入到白名單中。


連接集群
將上一步下載的Lindorm-cli在本機解壓后,直接執(zhí)行如下命令:
./lindorm-cli -url jdbc:lindorm:table:url=http://ld-xxxx-proxy-lindorm-pub.lindorm.rds.aliyuncs.com:30060 -username xxx -password xxx其中的公網(wǎng)連接地址、用戶名和密碼可以在數(shù)據(jù)庫控制臺“數(shù)據(jù)庫連接”中獲取,參見上個章節(jié)中的截圖。
備注:需要提前配置好白名單。
執(zhí)行SQL語句
創(chuàng)建數(shù)據(jù)庫
create schema testSchema;創(chuàng)建表
use testSchema;
// 創(chuàng)建一個表,主鍵為id,非主鍵為name,age,address
create table if not exists testTable(id varchar, name varchar, age bigint, address varchar, constraint primary key (id));創(chuàng)建SearchIndex
// 對表的非主鍵列建索引,其中address是一個分詞字段,使用IK分詞器
create search index if not exists testIdx on testTable(name,age,address(type=text,analyzer=ik)) with (indexState=ACTIVE);寫入測試數(shù)據(jù)
upsert into testTable(id,name,age,address) values ('1', '張三', 25, '北京市朝陽區(qū)綠地中心c座');
upsert into testTable(id,name,age,address) values ('2', '李四', 30, '上科路張江人工智能島2號樓');
upsert into testTable(id,name,age,address) values ('3', '王五', 28, '深圳市南山區(qū)科苑南路3331號');
upsert into testTable(id,name,age,address) values ('4', '趙六', 36, '杭州市余杭區(qū)文一西路969號');查詢
select name from testtable where address = '朝陽區(qū)' limit 10;
select * from testtable where age > 30 and address = '杭州' limit 10;
select name from testtable where name = '王五' limit 10;對于非主鍵的查詢,一般會消耗非常多的系統(tǒng)資源,為了系統(tǒng)穩(wěn)定,這樣的查詢默認會被拒絕掉。如果基于這些非主鍵創(chuàng)建了二級索引,則可以高效的執(zhí)行。
更多SQL語法,可參考語法手冊。
三. 總結(jié)
SearchIndex是Lindorm寬表的二級索引,當您想加速檢索分析的速度時,只需要對表創(chuàng)建索引即可,寫入的數(shù)據(jù)會自動構(gòu)建索引,查詢時服務端會自動路由到索引,整個使用過程對業(yè)務代碼0侵入。
SearchIndex在阿里內(nèi)部以及公有云上有著豐富的應用場景,如果您在面對訂單查詢、物流軌跡、大數(shù)據(jù)畫像、車聯(lián)網(wǎng)等場景的業(yè)務架構(gòu)設計,可以嘗試將Lindorm SearchIndex應用到架構(gòu)中,將會帶來開發(fā)和存儲成本的極大收益。
Lindorm的案例匯總,可進一步參考解決方案。
歡迎掃碼加入釘釘群,相關的技術專家將為您解答所有疑惑。
