Elasticsearch+hbase 实现hbase中数据的快速查询(二)

本文介绍了如何利用Elasticsearch 5.x版本进行HBase数据的快速查询。通过在Elasticsearch中进行CRUD操作,并结合官方文档,创建了一个工具类来实现这一目标。查询过程主要包括从ES获取索引,然后用这些索引来查找HBase中的对应数据。
摘要由CSDN通过智能技术生成

接下来是Elasticsearch (版本5.x)中数据的CRUD 操作,为此,根据ES官网上的资料总结了一个工具类.
具体如下:
(1)maven 添加依赖
(2)工具类代码:

public class ESClientUtils {
   
    protected static Logger logger = Logger.getLogger(ESClientUtils.class);
    protected static TransportClient client;
    private static String es_node1_host = "";    
    private static String es_node1_port = "";

    static {

        String configName = "/hbase_elasticsearch.properties";
        PropertiesLoader propertiesLoader = new PropertiesLoader(configName);
        try {
            es_node1_host = propertiesLoader.getProperty("es_node1_host");
            es_node1_port = propertiesLoader.getProperty("es_node1_port");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**   
     * @Title: setUp   
     * @Description: TODO(创建client连接)   
     * @param: @throws Exception      
     * @return: void      
     * @throws   
     */
    @Before
    public static void setUp() throws Exception {

        Settings esSettings = Settings.builder()
                .put("cluster.name", "utan-es") //设置ES实例的名称
                .put("client.transport.sniff", true) //自动嗅探整个集群的状态,把集群中其他ES节点的ip添加到本地的客户端列表中
                .build();

        /**
         * 这里的连接方式指的是没有安装x-pack插件,如果安装了x-pack则参考{@link ElasticsearchXPackClient}
         * 1. java客户端的方式是以tcp协议在9300端口上进行通信
         * 2. http客户端的方式是以http协议在9200端口上进行通信
         */
        /*client = new PreBuiltTransportClient(esSettings.EMPTY)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.0.0.12"), Integer.parseInt("9300")));*/

        client = new PreBuiltTransportClient(esSettings.EMPTY)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(es_node1_host), Integer.parseInt(es_node1_port)));

        System.out.println("ElasticsearchClient 连接成功");
    }


    public ESClientUtils() throws Exception{
        setUp();
    }

    @After
    public static void closeClient() throws Exception {
        if (client != null) {
            client.close();
        }

    }

    /**   
     * @Title: getQueryResponse   
     * @Description: TODO(查询数据,精确查询)   
     * @param: @throws Exception      
     * @return: void      
     * @throws   
     */
    public static GetResponse getQueryResponse(String indexName, String type, String id) throws Exception{
        setUp();
        // 搜索数据
        GetResponse response = client.prepareGet(indexName, type, id).execute().actionGet();
        // 输出结果
        System.out.println(response.getSourceAsString());
        // 关闭client
        closeClient();
        return response;
    }

    /**   
     * @Title: getQueryResSource   
     * @Description: TODO(查询数据,返回Source,与getQueryResponse搭配使用)   
     * @param</
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值