更新时间:2021-03-18 GMT+08:00
创建数据表开启Elasticsearch索引
功能简介
建表功能同创建表,在此基础上,表属性配置Elasticsearch中的索引开启的字段,请参见HBase Elasticsearch schema说明。
样例代码
public void createTable() { LOG.info("Entering testCreateTable."); // Specify the table descriptor. HTableDescriptor htd = new HTableDescriptor(tableName); // Set the column family name to info. HColumnDescriptor hcd = new HColumnDescriptor(CF1); // Set data encoding methods. HBase provides DIFF,FAST_DIFF,PREFIX // and PREFIX_TREE hcd.setDataBlockEncoding(DataBlockEncoding.FAST_DIFF); // Set compression methods, HBase provides two default compression // methods:GZ and SNAPPY // GZ has the highest compression rate,but low compression and // decompression efficiency,fit for cold data // SNAPPY has low compression rate, but high compression and // decompression efficiency,fit for hot data. // it is advised to use SANPPY hcd.setCompressionType(Compression.Algorithm.SNAPPY); HColumnDescriptor hcd2 = new HColumnDescriptor(CF2); hcd2.setDataBlockEncoding(DataBlockEncoding.FAST_DIFF); hcd2.setCompressionType(Compression.Algorithm.SNAPPY); htd.addFamily(hcd); htd.addFamily(hcd2); //add HBase ES schema String ESJsonSchema = "[" + "{\"name\":\"contentCh\",\"type\":\"text\",\"hbaseQualifier\":\"cf1:contentCh\",\"analyzer\":\"ik_smart\"}," + "{\"name\":\"contentEng\",\"type\":\"text\",\"hbaseQualifier\":\"cf2:contentEng\"}," + "{\"name\":\"id\",\"type\":\"long\",\"hbaseQualifier\":\"cf1:id\"}," + "{\"name\":\"charNum\",\"type\":\"integer\",\"hbaseQualifier\":\"cf1:charNum\"}," + "{\"name\":\"pageNum\",\"type\":\"short\",\"hbaseQualifier\":\"cf1:pageNum\"}," + "{\"name\":\"level\",\"type\":\"byte\",\"hbaseQualifier\":\"cf1:level\"}," + "{\"name\":\"researchCost\",\"type\":\"double\",\"hbaseQualifier\":\"cf1:researchCost\"}," + "{\"name\":\"score\",\"type\":\"float\",\"hbaseQualifier\":\"cf1:score\"}," + "{\"name\":\"male\",\"type\":\"boolean\",\"hbaseQualifier\":\"cf1:male\"}" + "]"; htd.setValue(HBaseESConst.HBASE_INDEX_ES_ENABLED, "true"); htd.setValue(HBaseESConst.HBASE_INDEX_ES_ENDPOINT, ESClusterHosts);//(1) htd.setValue(HBaseESConst.HBASE_INDEX_ES_INDEXNAME, ES_INDEX_NAME);//(2) htd.setValue(HBaseESConst.HBASE_INDEX_ES_SCHEMA, ESJsonSchema);//(3) Admin admin = null; try { // Instantiate an Admin object. admin = conn.getAdmin(); if (!admin.tableExists(tableName)) { LOG.info("Creating table..."); admin.createTable(htd); LOG.info(admin.getClusterStatus()); LOG.info(admin.listNamespaceDescriptors()); LOG.info("Table created successfully."); } else { LOG.warn("table already exists"); } } catch (IOException e) { LOG.error("Create table failed.", e); } finally { if (admin != null) { try { // Close the Admin object. admin.close(); } catch (IOException e) { LOG.error("Failed to close admin ", e); } } } LOG.info("Exiting testCreateTable."); }
说明:
(1)从云搜索服务console界面,查看的云搜索服务(Elasticsearch搜索引擎)集群访问地址。
(2)用户自定义索引名称。
(3)HBase字段和Elasticsearch字段映射信息。
父主题: 样例代码说明