Enabling an Index in Elasticsearch When Creating a Table
Function Description
Follow instructions in Creating a Table to create a table. To configure table properties, follow instructions in HBase Elasticsearch Schema to configure the field to enable an index in Elasticsearch.
Sample Code
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.");
} Note:
(1) Indicates the CSS (Elasticsearch engine) cluster access address obtained from the CSS management console.
(2) Indicates the user-defined index name.
(3) Indicates the field mapping between HBase and Elasticsearch.
Last Article: Creating the Configuration Object
Next Article: Writing Data
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.