从ES中读取数据,并将数据输出到本地(本地执行)

从ES中读取数据,并将数据输出到本地(本地执行ES)


本程序总共需要两个文件,一个是job执行文件,另一个是对读取的数据进行处理的mapper文件。

因程序是maven程序,需要有一个pom文件,pom文件如下:

pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>ES2hadoop</artifactId>
    <version>1.0-SNAPSHOT</version>


    <repositories>
        <repository>
            <id>huaweicloudsdk</id>
            <url>https://mirrors.huaweicloud.com/repository/maven/huaweicloudsdk/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <!-- 阿里云 -->
        <repository>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>https://maven.aliyun.com/repository/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <properties>
        <lombok.version>1.18.14</lombok.version>

        <java.version>1.8</java.version>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>3.1.1-hw-ei-312005</version>
            <scope>provided</scope>
        </dependency>

<!--        <dependency>-->
<!--            <groupId>org.projectlombok</groupId>-->
<!--            <artifactId>lombok</artifactId>-->
<!--            <version>${lombok.version}</version>-->
<!--        </dependency>-->
<!--        &lt;!&ndash; Hive Dependency &ndash;&gt;-->
<!--        <dependency>-->
<!--            <groupId>org.apache.hive</groupId>-->
<!--            <artifactId>hive-exec</artifactId>-->
<!--            <version>2.3.4</version>-->
<!--            &lt;!&ndash;            <scope>provided</scope>&ndash;&gt;-->
<!--        </dependency>-->
<!--        <dependency>-->
<!--            <groupId>org.apache.hive</groupId>-->
<!--            <artifactId>hive-serde</artifactId>-->
<!--            <version>3.1.0-hw-ei-312005</version>-->
<!--            <scope>provided</scope>-->
<!--        </dependency>-->
<!--        <dependency>-->
<!--            <groupId>org.apache.hive.hcatalog</groupId>-->
<!--            <artifactId>hive-hcatalog-core</artifactId>-->
<!--            <version>3.1.0-hw-ei-312005</version>-->
<!--            &lt;!&ndash;            <scope>provided</scope>&ndash;&gt;-->
<!--        </dependency>-->

        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch-hadoop</artifactId>
            <version>7.16.2</version>
<!--            <version>5.5.2</version>-->
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.28</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>3.1.1-hw-ei-312005</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-mapreduce-client-core</artifactId>
            <version>3.1.1-hw-ei-312005</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <artifactSet>
                                <excludes>
                                    <exclude>com.google.code.findbugs:jsr305</exclude>
                                    <exclude>org.slf4j:*</exclude>
                                    <exclude>log4j:*</exclude>
                                    <exclude>org.apache.hadoop:*</exclude>

                                </excludes>
                            </artifactSet>
                            <filters>
                                <filter>
                                    <!-- Do not copy the signatures in the META-INF folder.
                                    Otherwise, this might cause SecurityExceptions when using the JAR. -->
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                            <transformers combine.children="append">
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer">
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
mapper程序如下:
package com.es_hadoop_example;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.elasticsearch.hadoop.mr.LinkedMapWritable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

class E2HMapper extends Mapper<Text, LinkedMapWritable, Text, LinkedMapWritable> {

    private static final Logger LOG = LoggerFactory.getLogger(E2HMapper.class);

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        super.setup(context);
    }

    /**
     * key :ES中document的id的值
     * value: 包含的就是一个document的内容
    */
    @Override
    protected void map(Text key, LinkedMapWritable value, Context context)
            throws IOException, InterruptedException {
        LOG.info("key {} value {}", key, value);
        context.write(key, value); //数据不做任何处理,直接输出
    }

    @Override
    protected void cleanup(Context context) throws IOException, InterruptedException {
        super.cleanup(context);
    }

}
job执行文件
package com.es_hadoop_example;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.elasticsearch.hadoop.mr.EsInputFormat;
import org.elasticsearch.hadoop.mr.LinkedMapWritable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @Description:  实现从es读取数据,并将读取的数据存放在本地,目前不对读取的数据做任何处理
* @Param:
* @return:
* @Author: zhangjiwei
* @Date: -
*/
public class E2HJob {
    private static Logger LOG = LoggerFactory.getLogger(E2HJob.class);

    public static void main(String[] args) {
        try {
            Configuration conf = new Configuration();

            //禁止speculative(推测执行)机制,该机制会启动多个相同task,使数据重复索引
            conf.setBoolean("mapreduce.map.speculative", false);
            conf.setBoolean("mapreduce.reduce.speculative", false);

            conf.set("es.nodes", "127.0.0.1:9200"); //ElasticSearch节点
            conf.set("es.resource", "ecommerce/product"); //ElaticSearch source: Index/Type
//            conf.set("es.resource", "user/_doc");
//            conf.set("es.resource", "kibana_sample_data_ecommerce/_doc"); //无法执行成功???

            /**
             * es.nodes.wan.only (默认为假)
            * 连接器是否用于 WAN 上云/受限环境中的 Elasticsearch 实例,例如 Amazon Web Services。
             * 在这种模式下,连接器禁用发现,并且仅通过在所有操作期间声明的连接es.nodes,包括读取和写入。
             * 请注意,在此模式下,性能受到很大 影响
            */
            conf.set("es.nodes.wan.only","true"); // 禁用网络中其他节点的自动发现.强制系统使用“es.nodes”属性,默认情况下会尝试连接到 localhost.

            /** 调试模式中可以看到下面这行代码会发生异常(Method threw 'java.lang.IllegalStateException' exception. Cannot evaluate org.apache.hadoop.mapreduce.Job.toString())
             * 这是因为job还未提交,但是该方法试图调用job.toString().
             * 不过没有关系,系统会自动处理这个异常
             */
            Job job = Job.getInstance(conf, "JOBE2H"); //构建job对象
            job.setJarByClass(E2HJob.class); //指定jar包运行主类
            job.setInputFormatClass(EsInputFormat.class); //指定输入格式的类
            job.setMapperClass(E2HMapper.class); //指定map类
            job.setMapOutputKeyClass(Text.class); //指定map输出的 key的类
            job.setMapOutputValueClass(LinkedMapWritable.class); //指定map输出的 value的类

            FileSystem fs = FileSystem.get(conf);
            Path outPath =new Path("D:\\test\\es_data");
            if(fs.exists(outPath)) {
                fs.delete(outPath, true);
            }
            FileOutputFormat.setOutputPath(job, outPath); //指定输出路径

            System.out.println(job.waitForCompletion(true));//打印执行结果,结果为true表明执行成功
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);

        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 使用Spark Streaming可以实时读取HDFS数据,并将其写入Elasticsearch。 具体步骤如下: 1. 创建Spark Streaming应用程序,并设置批处理间隔时间。 2. 创建一个DStream,从HDFS读取数据。 3. 对DStream进行处理,例如过滤、转换等操作。 4. 将处理后的数据写入Elasticsearch。 代码示例: ```python from pyspark import SparkContext from pyspark.streaming import StreamingContext from pyspark.streaming.kafka import KafkaUtils from elasticsearch import Elasticsearch # 创建SparkContext和StreamingContext sc = SparkContext(appName="HDFS to Elasticsearch") ssc = StreamingContext(sc, 10) # 从HDFS读取数据 lines = ssc.textFileStream("hdfs://localhost:900/data") # 对数据进行处理 words = lines.flatMap(lambda line: line.split(" ")) filtered_words = words.filter(lambda word: len(word) > 3) # 将数据写入Elasticsearch es = Elasticsearch() def send_to_es(rdd): for record in rdd.collect(): es.index(index="myindex", doc_type="mytype", body=record) filtered_words.foreachRDD(send_to_es) # 启动StreamingContext ssc.start() ssc.awaitTermination() ``` 这段代码从HDFS读取数据,对数据进行过滤,然后将处理后的数据写入Elasticsearch。其,send_to_es函数用于将RDD数据写入Elasticsearch。在实际使用,需要根据具体的需求进行修改。 ### 回答2: Spark Streaming是Apache Spark生态系统的一种强大的流处理框架。它提供了实时流数据处理的能力,并且可以无缝地与Hadoop生态系统集成。同时,Elasticsearch是一种分布式搜索引擎,它能够快速准确地处理大规模文本数据。 使用Spark Streaming实时读取HDFS数据并将其写入Elasticsearch,可以实现流数据的实时处理和导入到搜索引擎进行快速准确的查询和分析。具体实现步骤如下: 1. 创建一个Spark Streaming应用程序,并定义接收数据的源。在本例,可以使用Hadoop FileSystem API来读取HDFS文件系统特定路径下的文件。 2. 将Spark Streaming应用程序与Elasticsearch连接。这可以通过使用Elasticsearch-Hadoop库来实现。这个库提供了一些Spark RDD的API,可以让你从Spark RDD导入数据Elasticsearch。 3. 实现对于数据的转换,以便可以将数据写入到Elasticsearch。这个过程可能需要一些数据清洗和转换。在本例,可以使用Spark SQL来处理数据的转换。 4. 实现数据的写入到Elasticsearch。在这个过程,可以使用Hadoop API从Spark上下文读取数据,并将其写入到Elasticsearch。可以将数据写入到一个新的Elasticsearch索引或直接添加到现有的索引。 5. 启动Spark Streaming应用程序,并监视其处理实时流数据和写入Elasticsearch的性能和表现。 总的来说,用Spark Streaming实时读取HDFS数据并写入Elasticsearch是一种非常有效的处理流数据的方式。这种方法需要一定的技术知识和经验,但可以为企业和组织提供实时数据处理和搜索分析的能力,从而做出更明智的业务决策。 ### 回答3: Spark Streaming是通过流形式处理数据的一种框架,在不断增长的数据,它可以实时读取数据,同时进行实时分析和存储。HDFS是一个基于Hadoop的分布式文件系统,用于存储和处理大量数据Elasticsearch是一个实时分布式搜索与分析引擎,它可以快速处理大量数据。 我们可以通过使用Spark Streaming来实时读取HDFS数据并将其写入Elasticsearch。这个过程可以分为以下步骤: 1.创建Spark Streaming对象 在程序开始之前,需要定义一个Spark Streaming对象,它由StreamingContext来创建,通过指定时间间隔(batch interval)来控制进行实时处理的数据块大小。这里我们根据需求设置数据块大小为1秒,创建一个StreamingContext对象: val conf= new SparkConf() val ssc = new StreamingContext(conf, Seconds(1)) 2. 读取HDFS数据 通过Spark的FileInputDStream对象传递数据从HDFS读取数据, 我们通过DStream对象来表示一连串的RDDs,从而使数据可以在流处理被处理。例子是读取Hadoop文件系统当的某个文件夹 hdfs://localhost:9000/data val inputRDD = ssc.fileStream[LongWritable, Text, TextInputFormat](“hdfs://localhost:9000/data") 3. 对数据进行实时处理 我们需要继续对数据进行处理,以便将数据传递到Elasticsearch,这里开发者可以根据自己的需求定义数据处理的逻辑。处理完后,我们需要将数据打包成Elasticsearch所接受的JSON格式,例如: input.flatMap(x ⇒ x._2.toString.split(”\n”)).map(callYourProcessingLogicFunction).map(toJson).saveJsonToEs(“index/type”); 4. 将处理好的数据写入Elasticsearch 通过Spark对Elasticsearch的支持,可以利用DStream类的foreachRDD方法将数据写入到Elasticsearch。以下为代码示例: input.foreachRDD((rdd,time) => rdd.saveToEs(“index/type”) ) 这就是使用Spark Streaming实时读取HDFS数据并将其写入Elasticsearch的过程。需要注意的是,我们在处理大规模数据时,一定要注意数据的处理速度,否则我们将无法及时,准确地完成相关的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值