Spark 读 Elasticsearch

没什么好说的,直接粘贴代码吧

public class MySparkReadEs implements Serializable {

    private transient JavaSparkContext javaSparkContext = null;

    private transient SparkConf sparkConf = null;

    private transient Configuration esConf = null;

    private String esSource = "post/post";

    private String esNodes = "192.168.1.235,192.168.1.236";

    private static final Log LOG = LogFactory.getLog(MySparkReadEs.class);

    private MySparkReadEs() {
    }

    private MySparkReadEs(String esSource, String esNodes) {
        this.esSource = esSource;
        this.esNodes = esNodes;
    }

    //初始化Spark context
    private void initSparkContext() {
        this.sparkConf = new SparkConf().setMaster("local[2]").setAppName("my spark rdd");
        this.javaSparkContext = new JavaSparkContext(this.sparkConf);
    }

    //初始化查询Es的配置文件
    private void initEsConfig(String urn) {

        this.esConf = new Configuration((Configuration) HBaseConfiguration.create());
        //指定读取的索引名称
        this.esConf.set("es.resource", this.esSource);
        //指定es节点
        this.esConf.set("es.nodes", this.esNodes);
        //加入ES查询条件
        this.esConf.set("es.query", buildQueryESCondition(urn));
    }

    //构建查询Es的条件
    private String buildQueryESCondition(String urn) {

        //支持lucence的写法 里面可以继续添加其他字段
        StringBuilder sb = new StringBuilder();

        sb.append("postUrn:").append("(").append(urn).append(")");

        //查询条件的构造
        ImmutableMap<String, ImmutableMap<String, ImmutableMap<String, String>>> conditionMap = ImmutableMap.of("query", ImmutableMap.of("query_string", ImmutableMap.of("query", sb.toString())));

        //SPARK查询ES的查询条件
        LOG.info("SPARK 查询 ES 的条件为:" + JSON.toJSONString(conditionMap));

        return JSON.toJSONString(conditionMap);
    }

    //开始任务
    private void startJob() {

        List<String> extensionCollect = this.javaSparkContext.newAPIHadoopRDD(this.esConf, EsInputFormat.class, NullWritable.class, MapWritable.class).map(new Function<Tuple2<NullWritable, MapWritable>, String>() {
            @Override
            public String call(Tuple2<NullWritable, MapWritable> v1) throws Exception {
                MapWritable mapWritable = v1._2();
                
                Map<String, Object> o = (Map<String, Object>) WritableUtils.fromWritable(mapWritable);
                
                String extension = (String) o.get("extension");
                return extension;
            }
        }).collect();

        for (String extension : extensionCollect) {
            LOG.info("Extension : " + extension);
        }
    }

    //主方法
    public static void main(String[] args) {
        String esNodes = "192.168.1.235,192.168.1.236";
        String esSource = "post/post";
        String userurn = "3136949-bf30c1da6f8b47d3d93a5c4f75194447";
        MySparkReadEs mySparkRdd = new MySparkReadEs(esSource, esNodes);
        //初始化 spark context
        mySparkRdd.initSparkContext();
        //初始化 es 配置
        mySparkRdd.initEsConfig(userurn);
        //开始作业
        mySparkRdd.startJob();
    }
}


转载于:https://my.oschina.net/momisabuilder/blog/605956

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值