当前位置:   article > 正文

phoenix创建映射表和创建索引、删除索引、重建索引_phoenix删除索引

phoenix删除索引

导读:

默认情况下,直接在hbase中创建的表,通过phoenix是查看不到的。如果需要在phoenix中操作直接在hbase中创建的表,则需要在phoenix中进行表的映射。映射方式有两种:视图映射和表映射。

0.创建hbase映射表:

映射表一定要和hbase中表的字段一一对应,首先pk是主键,也可以用rowkey来表示,剩下的就是各个列,不要忘了用“列族”.“具体列”这样的形式表示。

  1. create table "device_data_test08"
  2. ("pk" varchar primary key,"data"."deviceID" varchar,
  3. "data"."deviceTime" varchar,"data"."modelID" varchar,
  4. "data"."processState" varchar,"data"."subDevice" varchar,
  5. "data"."test08" varchar,"data"."abc1234567" varchar);

1.创建索引:

两个双引号,第一个是索引的名称,第二个是原表的名称

这边的索引字段为:deviceID

Include括号里面是包含要返回的列是哪些。

  1. CREATE INDEX "index_device_data_test08" ON
  2. "device_data_test08"("data"."deviceID")
  3. INCLUDE
  4. ("data"."deviceTime","data"."modelID","data"."processState","data"."subDevice","data"."test08");

 2.删除索引

drop index "index_device_data_test08" on "device_data_test08";

3.重建索引

ALTER INDEX IF EXISTS "idx_spc_test08" on "device_data_test08" REBUILD;

有可能重建索引失败,失败的原因可能是表的数据量太大,或者生产环境中正在进行该表的操作。多尝试几次即可。如果还是失败,建议删除索引,然后重新创建索引。 

  1. test08lil1634894735885
  2. test08\x00lil\x001634894735885\x001\x00test08lil1634894735885
  3. 建立映射表:
  4. create table "device_data_test08" ("pk" varchar primary key,"data"."deviceID" varchar,"data"."deviceTime" varchar,"data"."modelID" varchar,"data"."processState" varchar,"data"."subDevice" varchar,
  5. "data"."test08" varchar,"data"."abc1234567" varchar);
  6. create table "device_data_test01" ("pk" varchar primary key,"data"."1" varchar,"data"."deviceID" varchar,"data"."deviceTime" varchar,"data"."modelID" varchar,"data"."processState" varchar,"data"."subDevice" varchar);
  7. 建立索引表:
  8. CREATE INDEX "index_device_data_test08" ON "device_data_test08"("data"."deviceID")INCLUDE("data"."deviceTime","data"."modelID","data"."processState","data"."subDevice","data"."test08");
  9. CREATE INDEX "idx_spc_test01" ON "device_data_test01"("data"."modelID","data"."deviceID","data"."deviceTime","data"."processState")INCLUDE("data"."subDevice","data"."1","data"."abc1234567");
  10. 删除索引:
  11. drop index "index_device_data_test08" on "device_data_test08";
  12. drop index "index_device_data_test01" on "device_data_test01";
  13. 增加列:
  14. alter table "device_data_test02" add "data"."abc1234567" varchar
  15. 删除列:
  16. alter table "harve_role" drop column +列名字,如:createAt,name;
  17. 重建索引
  18. ALTER INDEX IF EXISTS "idx_spc_test08" on "device_data_test08" REBUILD;
  19. explain select "modelID" from "device_data_test08" where "modelID" = 'test08' and "deviceID" = 'lil' and "deviceTime" = '1634867582045' and "processState" = '1';
  20. 索引表改名称:
  21. drop 'index_device_data_test01'
  22. hbase(main):017:0> disable 'index_device_data_test02'
  23. hbase(main):018:0> snapshot 'index_device_data_test02','index_device_data_test02Snapshot'
  24. hbase(main):019:0> clone_snapshot 'index_device_data_test02Snapshot','idx_spc_test02'
  25. hbase(main):020:0> delete_snapshot 'index_device_data_test02Snapshot'
  26. hbase(main):021:0> drop 'index_device_data_test02'
  27. 查找最大rowkey
  28. select max("rowkey") from "device_data_28UTB9" where
  29. to_char(CONVERT_TZ(to_date(SUBSTR("deviceTime",1,10),'s'), 'UTC', 'Asia/Shanghai'),'yyyy-MM-dd')
  30. = '2021-11-07' and "deviceID" = 'VWY3FL14M4'
  31. 查找固定设备
  32. select * from "device_data_28UTB9" where
  33. to_char(CONVERT_TZ(to_date(SUBSTR("deviceTime",1,10),'s'), 'UTC', 'Asia/Shanghai'),'yyyy-MM-dd')
  34. = '2021-11-07' and "deviceID" = 'VWY3FL14M4' limit 10;
  35. 计数值计量值控制图、工序能力分析,异常报警,支持手工录入和自动采集

补充:

  1. 视图映射:
  2. Phoenix创建的视图是只读的,所以只能用来做查询,无法通过视图对源数据进行修改等操作
  3. # hbase shell 进入hbase命令行
  4. hbase shell
  5. # 创建hbase表
  6. create 'test','name','company'
  7. # 插入数据
  8. put 'test','001','name:firstname','zhangsan1'
  9. put 'test','001','name:lastname','zhangsan2'
  10. put 'test','001','company:name','数加'
  11. put 'test','001','company:address','合肥'
  12. upsert into TEST values('002','xiaohu','xiaoxiao','数加','合肥');
  13. # 在phoenix创建视图, primary key 对应到hbase中的rowkey
  14. create view "test"(
  15. empid varchar primary key,
  16. "name"."firstname" varchar,
  17. "name"."lastname" varchar,
  18. "company"."name" varchar,
  19. "company"."address" varchar
  20. );
  21. CREATE view "students" (
  22. id VARCHAR NOT NULL PRIMARY KEY,
  23. "info"."name" VARCHAR,
  24. "info"."age" VARCHAR,
  25. "info"."gender" VARCHAR ,
  26. "info"."clazz" VARCHAR
  27. ) column_encoded_bytes=0;
  28. # 在phoenix查询数据,表名通过双引号引起来
  29. select * from "test";
  30. # 删除视图
  31. drop view "test";
  1. 3.2、表映射
  2. 使用Apache Phoenix创建对HBase的表映射,有两类:
  3. 1) 当HBase中已经存在表时,可以以类似创建视图的方式创建关联表,只需要将create view改为create table即可。
  4. 2) 当HBase中不存在表时,可以直接使用create table指令创建需要的表,并且在创建指令中可以根据需要对HBase表结构进行显示的说明。
  5. 1)种情况下,如在之前的基础上已经存在了test表,则表映射的语句如下:
  6. create table "test" (
  7. empid varchar primary key,
  8. "name"."firstname" varchar,
  9. "name"."lastname"varchar,
  10. "company"."name" varchar,
  11. "company"."address" varchar
  12. )column_encoded_bytes=0;
  13. upsert into "students" values('150011000100','xiaohu','24','男','理科三班');
  14. upsert into "test" values('1001','xiaohu','xiaoxiao','数加','合肥');
  15. CREATE table "students" (
  16. id VARCHAR NOT NULL PRIMARY KEY,
  17. "info"."name" VARCHAR,
  18. "info"."age" VARCHAR,
  19. "info"."gender" VARCHAR ,
  20. "info"."clazz" VARCHAR
  21. ) column_encoded_bytes=0;
  22. upsert into "students" values('150011000100','xiaohu','24','男','理科三班');
  23. CREATE table "score" (
  24. id VARCHAR NOT NULL PRIMARY KEY,
  25. "info"."score_dan" VARCHAR
  26. ) column_encoded_bytes=0;
  27. 使用create table创建的关联表,如果对表进行了修改,源数据也会改变,同时如果关联表被删除,源表也会被删除。但是视图就不会,如果删除视图,源数据不会发生改变。
  1. 一、重建索引步骤:
  2. 1)从现有的EAP数据库中拿到所有模型的列表,modelID匹配modelName
  3. 2)进入phoenix命令行
  4. 3)看模型的更新与创建时间,如果模型与现有的索引不一致,先删掉现有索引
  5. drop index "idx_spc_6CYPIO" on "device_data_6CYPIO";
  6. (4) 新建索引,语句如下,字段自行替换(include之前的四个字段不可替换,通用的)
  7. include内的字段通过model_ID进行查询,SQL语句如下:select * from device_data where model_ID ='';
  8. create index "idx_spc_6CYPIO" on "device_data_6CYPIO"("data"."modelID","data"."deviceID","data"."deviceTime","data"."processState") include
  9. (
  10. "data"."subDevice"
  11. ,"data"."siteCode"
  12. ,"data"."equipNum"
  13. ,"data"."userName"
  14. ,"data"."productType"
  15. ,"data"."identification"
  16. ,"data"."qualityStatus"
  17. ,"data"."testOrderNum"
  18. ,"data"."completeQty"
  19. ,"data"."JSGDKW01"
  20. ,"data"."JSGDTD01"
  21. ,"data"."JSGDST01"
  22. ,"data"."JSGDPF01"
  23. ,"data"."JSGDSV01"
  24. ,"data"."JSGDTPTM"
  25. ,"data"."JSGDXHCS01"
  26. ,"data"."STATUS"
  27. ,"data"."STEP"
  28. ,"data"."CURRENT"
  29. ,"data"."VOLTAGE"
  30. ,"data"."CAPACITY"
  31. ,"data"."ENERGY"
  32. ,"data"."STIME"
  33. ,"data"."DTIME"
  34. ,"data"."TEMP"
  35. ,"data"."VACUUM"
  36. ,"data"."DEVICETYPE"
  37. ,"data"."MESDEVICEID");
  38. (5) 等待执行成功即可。
  39. 二、检查需要重建索引集合
  40. 1)SQL查询:根据李璐上次重建索引时间,凡是创建时间或者更新时间大于李璐上次重建时间,都需要重新创建。
  41. 2)SQL语句如下:select * from device_model where update_time >'2022-04-26 22:25:00' or create_time > '2022-04-26 22:25:00';
  42. 3)SQL结果如下,model_id(通过mdoel_id就可以查询具体的data_id字段,即include包括的字段),一共需要重建29个索引:
  43. 189538
  44. 28UTB9
  45. .........
  46. 例如:
  47. 新建索引表:8K4PNX
  48. drop view "device_data_8K4PNX";
  49. create index "idx_spc_8K4PNX" on "device_data_8K4PNX"("data"."modelID","data"."deviceID","data"."deviceTime","data"."processState") include
  50. (
  51. "data"."ZSDP_JZSJ"
  52. ,"data"."ZSDP_CZSJ"
  53. ,"data"."ZSDP_JQWZ"
  54. ,"data"."ZSDP_DPSJ"
  55. ,"data"."ZSDP_KZGCF_MIN"
  56. ,"data"."ZSDP_KZGCF_MAX"
  57. ,"data"."ZSDP_KFGCF_MIN"
  58. ,"data"."ZSDP_KFGCF_MAX"
  59. ,"data"."ZSDP_KZFCZ_MIN"
  60. ,"data"."ZSDP_KZFCZ_MAX"
  61. ,"data"."ZSDP_KFFCZ_MIN"
  62. ,"data"."ZSDP_KFFCZ_MAX"
  63. ,"data"."ZSDP_GSGCF_MAX"
  64. ,"data"."ZSDP_GSGCF_MIN"
  65. ,"data"."ZSDP_GSFCZ_MAX"
  66. ,"data"."ZSDP_GSFCZ_MIN"
  67. ,"data"."ZSDP_GDGCF_MAX"
  68. ,"data"."ZSDP_GDGCF_MIN"
  69. ,"data"."ZSDP_GDFCZ_MAX"
  70. ,"data"."ZSDP_GDFCZ_MIN"
  71. ,"data"."ZSDP_GMZL"
  72. ,"data"."ZSDP_SZRYYL"
  73. ,"data"."ZSDP_RYYL"
  74. ,"data"."ZSDP_SZRYSJ"
  75. ,"data"."ZSDP_DXTLSJ"
  76. ,"data"."ZSDP_SZSRYWD"
  77. ,"data"."ZSDP_SZXRYWD"
  78. ,"data"."ZSDP_SRYWD"
  79. ,"data"."ZSDP_XRYWD"
  80. ,"data"."MQEF_ANODE"
  81. ,"data"."MQEF_CATHODE"
  82. ,"data"."MQEF_ANODE_LH"
  83. ,"data"."MQEF_CATHODE_LH"
  84. ,"data"."SEPARATOR"
  85. ,"data"."TAPE1"
  86. ,"data"."CELL"
  87. ,"data"."ZSDP_JQH"
  88. ,"data"."ZSDP_CZRY"
  89. ,"data"."ZSDP_BFCCJG"
  90. ,"data"."ZSRY_RYJGPD"
  91. ,"data"."siteCode"
  92. ,"data"."equipNum"
  93. ,"data"."userName"
  94. ,"data"."productType"
  95. ,"data"."identification"
  96. ,"data"."qualityStatus"
  97. ,"data"."testOrderNum"
  98. ,"data"."completeQty"
  99. ,"data"."materialLotCode");
  100. ------------
  101. 新建视图:
  102. CREATE VIEW "device_data_8K4PNX"(
  103. "rowkey" varchar primary key
  104. ,"data"."modelID"varchar
  105. ,"data"."deviceID"varchar
  106. ,"data"."deviceTime"varchar
  107. ,"data"."ZSDP_JZSJ"varchar
  108. ,"data"."ZSDP_CZSJ"varchar
  109. ,"data"."ZSDP_JQWZ"varchar
  110. ,"data"."ZSDP_DPSJ"varchar
  111. ,"data"."ZSDP_KZGCF_MIN"varchar
  112. ,"data"."ZSDP_KZGCF_MAX"varchar
  113. ,"data"."ZSDP_KFGCF_MIN"varchar
  114. ,"data"."ZSDP_KFGCF_MAX"varchar
  115. ,"data"."ZSDP_KZFCZ_MIN"varchar
  116. ,"data"."ZSDP_KZFCZ_MAX"varchar
  117. ,"data"."ZSDP_KFFCZ_MIN"varchar
  118. ,"data"."ZSDP_KFFCZ_MAX"varchar
  119. ,"data"."ZSDP_GSGCF_MAX"varchar
  120. ,"data"."ZSDP_GSGCF_MIN"varchar
  121. ,"data"."ZSDP_GSFCZ_MAX"varchar
  122. ,"data"."ZSDP_GSFCZ_MIN"varchar
  123. ,"data"."ZSDP_GDGCF_MAX"varchar
  124. ,"data"."ZSDP_GDGCF_MIN"varchar
  125. ,"data"."ZSDP_GDFCZ_MAX"varchar
  126. ,"data"."ZSDP_GDFCZ_MIN"varchar
  127. ,"data"."ZSDP_GMZL"varchar
  128. ,"data"."ZSDP_SZRYYL"varchar
  129. ,"data"."ZSDP_RYYL"varchar
  130. ,"data"."ZSDP_SZRYSJ"varchar
  131. ,"data"."ZSDP_DXTLSJ"varchar
  132. ,"data"."ZSDP_SZSRYWD"varchar
  133. ,"data"."ZSDP_SZXRYWD"varchar
  134. ,"data"."ZSDP_SRYWD"varchar
  135. ,"data"."ZSDP_XRYWD"varchar
  136. ,"data"."MQEF_ANODE"varchar
  137. ,"data"."MQEF_CATHODE"varchar
  138. ,"data"."MQEF_ANODE_LH"varchar
  139. ,"data"."MQEF_CATHODE_LH"varchar
  140. ,"data"."SEPARATOR"varchar
  141. ,"data"."TAPE1"varchar
  142. ,"data"."CELL"varchar
  143. ,"data"."ZSDP_JQH"varchar
  144. ,"data"."ZSDP_CZRY"varchar
  145. ,"data"."ZSDP_BFCCJG"varchar
  146. ,"data"."ZSRY_RYJGPD"varchar
  147. ,"data"."siteCode"varchar
  148. ,"data"."equipNum"varchar
  149. ,"data"."userName"varchar
  150. ,"data"."productType"varchar
  151. ,"data"."identification"varchar
  152. ,"data"."qualityStatus"varchar
  153. ,"data"."testOrderNum"varchar
  154. ,"data"."completeQty"varchar
  155. ,"data"."materialLotCode"varchar
  156. ,"data"."processState"varchar
  157. )column_encoded_bytes=0;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/656723
推荐阅读
相关标签
  

闽ICP备14008679号