Advertisement
bakhridinova

Untitled

May 14th, 2024 (edited)
899
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.74 KB | None | 0 0
  1. package biz.svyatoslav.learning.bigdata.elasticsearch;
  2.  
  3. import org.apache.spark.sql.Dataset;
  4. import org.apache.spark.sql.Row;
  5. import org.apache.spark.sql.SaveMode;
  6. import org.apache.spark.sql.SparkSession;
  7. import org.elasticsearch.hadoop.cfg.ConfigurationOptions;
  8.  
  9. public class SparkJavaElasticStreamTest {
  10.     public static void main(String[] args) {
  11.         try {
  12.             System.out.println("Connecting to Elasticsearch...");
  13.             SparkSession spark = SparkSession.builder()
  14.                 .config(ConfigurationOptions.ES_NODES, "127.0.0.1")
  15.                 .config(ConfigurationOptions.ES_PORT, "9200")
  16.                 .appName("StreamingElastic")
  17.                 .master("local[*]")
  18.                 .getOrCreate();
  19.  
  20.             System.out.println("Preparing simple data...");
  21.             var staticDataFrame = spark.read()
  22.                 .option("header", "true")
  23.                 .csv("src/main/resources/test")
  24.                 .schema();
  25.  
  26.             Dataset<Row> df = spark.read().format("csv")
  27.                 .option("header", "true")
  28.                 .schema(staticDataFrame)
  29.                 .load("src/main/resources/test");
  30.  
  31.             String esIndex = "receipt_restaurants/data";
  32.  
  33.             System.out.println("Writing simple data...");
  34.             df.write()
  35.                 .format("org.elasticsearch.spark.sql")
  36.                 .option("es.resource", esIndex)
  37.                 .mode(SaveMode.Append)
  38.                 .save();
  39.  
  40.             spark.stop();
  41.             System.out.println("Done. Visit http://localhost:9200/receipt_restaurants and http://localhost:9200/receipt_restaurants/_search?pretty to see the result.");
  42.  
  43.         } catch (Exception e) {
  44.             e.printStackTrace();
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement