文档首页 > > 开发指南> 开发HBase Elasticsearch全文检索应用> 样例代码说明> 写入数据

写入数据

分享
更新时间:2020/06/17 GMT+08:00

功能简介

写入数据同插入数据,需要注意的是,写入的数据必须包含Elasticsearch中索引的所有字段 。字段不全,Elasticsearch索引数据异常,用户客户端需要根据自己的业务场景处理ESDocIndexException异常,可以重试,可以忽略或其他操作。

样例代码

样例代码如下,请将代码中的字符串“***replace_with_Chinese_characters***”替换为中文字符。

  public void putData() {
    LOG.info("Entering testPut.");

    Table table = null;
    try {
      // Instantiate an HTable object.
      table = conn.getTable(tableName);
      List<Put> puts = new ArrayList<Put>();
      // Instantiate a Put object.
      Put put = new Put(Bytes.toBytes("rowkey001"));
      put.addColumn(CF1, QUA_ARTICLE_CONTENT_CHINESE, Bytes.toBytes("***replace_with_Chinese_characters***"));
      put.addColumn(CF2, QUA_ARTICLE_CONTENT_English, Bytes.toBytes("how many apples in the market"));
      put.addColumn(CF1, QUA_ARTICLE_ID, Bytes.toBytes(1111l));
      put.addColumn(CF1, QUA_CHARACTER_NUM, Bytes.toBytes(1));
      short shortNum = 1;
      put.addColumn(CF1, QUA_PAGE_NUM, Bytes.toBytes(shortNum));
      char c = 'A';
      put.addColumn(CF1, QUA_ARTICLE_LEVEL, new byte[]{(byte)c});
      put.addColumn(CF1, QUA_RESEARCH_COST, Bytes.toBytes(111.11d));
      put.addColumn(CF1, QUA_ARTICLE_SCORE, Bytes.toBytes(80.5f));
      put.addColumn(CF1, QUA_AUTHOR_MALE, Bytes.toBytes(true));
      put.addColumn(CF1, QUA_WHATEVER, Bytes.toBytes("happy life and sweet love"));
      puts.add(put);

      put = new Put(Bytes.toBytes("rowkey002"));
      put.addColumn(CF1, QUA_ARTICLE_CONTENT_CHINESE, Bytes.toBytes("***replace_with_Chinese_characters***"));
      put.addColumn(CF2, QUA_ARTICLE_CONTENT_English, Bytes.toBytes("how many people in the swimming pool"));
      put.addColumn(CF1, QUA_ARTICLE_ID, Bytes.toBytes(2222l));
      put.addColumn(CF1, QUA_CHARACTER_NUM, Bytes.toBytes(2));
      shortNum = 2;
      put.addColumn(CF1, QUA_PAGE_NUM, Bytes.toBytes(shortNum));
      c = 'B';
      put.addColumn(CF1, QUA_ARTICLE_LEVEL, new byte[]{(byte)c});
      put.addColumn(CF1, QUA_RESEARCH_COST, Bytes.toBytes(222.22d));
      put.addColumn(CF1, QUA_ARTICLE_SCORE, Bytes.toBytes(170.5f));
      put.addColumn(CF1, QUA_AUTHOR_MALE, Bytes.toBytes(true));
      put.addColumn(CF1, QUA_WHATEVER, Bytes.toBytes("forever young"));
      puts.add(put);

      put = new Put(Bytes.toBytes("rowkey003"));
      put.addColumn(CF1, QUA_ARTICLE_CONTENT_CHINESE, Bytes.toBytes("***replace_with_Chinese_characters***"));
      put.addColumn(CF2, QUA_ARTICLE_CONTENT_English, Bytes.toBytes("we play video game in night"));
      put.addColumn(CF1, QUA_ARTICLE_ID, Bytes.toBytes(3333l));
      put.addColumn(CF1, QUA_CHARACTER_NUM, Bytes.toBytes(3));
      shortNum = 3;
      put.addColumn(CF1, QUA_PAGE_NUM, Bytes.toBytes(shortNum));
      c = 'C';
      put.addColumn(CF1, QUA_ARTICLE_LEVEL, new byte[]{(byte)c});
      put.addColumn(CF1, QUA_RESEARCH_COST, Bytes.toBytes(333.33d));
      put.addColumn(CF1, QUA_ARTICLE_SCORE, Bytes.toBytes(180.5f));
      put.addColumn(CF1, QUA_AUTHOR_MALE, Bytes.toBytes(false));
      put.addColumn(CF1, QUA_WHATEVER, Bytes.toBytes("wishes always with you"));
      puts.add(put);

      // Submit a put request.
      try {
        table.put(puts);
      }
      // if your put operation does not contain the all field in your schema, you will get ESDocIndexException.
      // ESDocIndexException means data/document indexing to ES failed, but remember data put in HBase success.
      // so here you handle this exception depends on your business( you can retry or ignore).
      catch (ESDocIndexException e) {
        //TODO
      }

      LOG.info("Put successfully.");
    } catch (IOException e) {
      LOG.error("Put failed ", e);
    } finally {
      if (table != null) {
        try {
          // Close the HTable object.
          table.close();
        } catch (IOException e) {
          LOG.error("Close table failed ", e);
        }
      }
    }
    LOG.info("Exiting testPut.");
  }
分享:

    相关文档

    相关产品

文档是否有解决您的问题?

提交成功!非常感谢您的反馈,我们会继续努力做到更好!
反馈提交失败,请稍后再试!

*必选

请至少选择或填写一项反馈信息

字符长度不能超过200

提交反馈 取消

如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨

智能客服提问云社区提问