阅读由Spark Redis保存的数据,使用Java。

huangapple go评论75阅读模式
英文:

Read data saved by spark redis using Java

问题

使用 [spark-redis][1] 将 Dataset 保存到 Redis
然后我使用 [Spring data redis][2] 读取这些数据

我保存到 Redis 的对象

    @Getter
    @Setter
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    @RedisHash("collaborative_filtering")
    public class RatingResult implements Serializable {
    	private static final long serialVersionUID = 8755574422193819444L;
    
    	@Id
    	private String id;
    
    	@Indexed
    	private int user;
    
    	@Indexed
    	private String product;
    
    	private double productN;
    	private double rating;
    	private float prediction;
    
    	public static RatingResult convert(Row row) {
    		int user = row.getAs("user");
    		String product = row.getAs("product");
    		double productN = row.getAs("productN");
    		double rating = row.getAs("rating");
    		float prediction = row.getAs("prediction");
    		String id = user + product;
    
    		return RatingResult.builder().id(id).user(user).product(product).productN(productN).rating(rating)
    				.prediction(prediction).build();
    	}
    
    }

使用 spark-redis 进行对象保存

    JavaRDD<RatingResult> result = ...
    ...
    sparkSession.createDataFrame(result, RatingResult.class).write().format("org.apache.spark.sql.redis")
    			.option("table", "collaborative_filtering").mode(SaveMode.Overwrite).save();

Repository

    @Repository
    public interface RatingResultRepository extends JpaRepository<RatingResult, String> {
    
    }

我无法使用 Spring data redis 读取通过 spark-redis 保存在 Redis 中的数据因为结构数据由于 spark-redis 和 spring data redis 的不同而不同我使用命令进行检查:`redis-cli -p 6379 keys \*``redis-cli hgetall $key`,spark-redis 和 spring data redis 创建的键的值不同

那么如何使用 Java 或者 Java 中的任何库来读取已经保存的这些数据呢

  [1]: https://github.com/RedisLabs/spark-redis/blob/master/doc/java.md
  [2]: https://www.baeldung.com/spring-data-redis-tutorial
英文:

I using spark-redis to save Dataset to Redis.
Then I read this data by using Spring data redis:

This object I save to redis:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
@RedisHash(&quot;collaborative_filtering&quot;)
public class RatingResult implements Serializable {
private static final long serialVersionUID = 8755574422193819444L;
@Id
private String id;
@Indexed
private int user;
@Indexed
private String product;
private double productN;
private double rating;
private float prediction;
public static RatingResult convert(Row row) {
int user = row.getAs(&quot;user&quot;);
String product = row.getAs(&quot;product&quot;);
double productN = row.getAs(&quot;productN&quot;);
double rating = row.getAs(&quot;rating&quot;);
float prediction = row.getAs(&quot;prediction&quot;);
String id = user + product;
return RatingResult.builder().id(id).user(user).product(product).productN(productN).rating(rating)
.prediction(prediction).build();
}
}

Save object by using spark-redis:

JavaRDD&lt;RatingResult&gt; result = ...
...
sparkSession.createDataFrame(result, RatingResult.class).write().format(&quot;org.apache.spark.sql.redis&quot;)
.option(&quot;table&quot;, &quot;collaborative_filtering&quot;).mode(SaveMode.Overwrite).save();

Repository:

@Repository
public interface RatingResultRepository extends JpaRepository&lt;RatingResult, String&gt; {
}

I can't read this data have been saved in Redis by using Spring data redis because structure data saved by spark-redis and spring data redis not same (I checked value of keys created by spark-redis and spring data redis are different by using command: redis-cli -p 6379 keys \* and redis-cli hgetall $key)

So how to read this data have been saved using Java or by any library in Java?

答案1

得分: 1

以下对我有效。

从Spark-Redis中写入数据。

我在这里使用Scala,但与您在Java中所做的基本相同。我唯一更改的是我添加了.option("key.column", "id")来指定哈希ID。

    val ratingResult = new RatingResult("1", 1, "product1", 2.0, 3.0, 4)

    val result: JavaRDD[RatingResult] = spark.sparkContext.parallelize(Seq(ratingResult)).toJavaRDD()
    spark
      .createDataFrame(result, classOf[RatingResult])
      .write
      .format("org.apache.spark.sql.redis")
      .option("key.column", "id")
      .option("table", "collaborative_filtering")
      .mode(SaveMode.Overwrite)
      .save()

在spring-data-redis中,我有以下内容:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
@RedisHash("collaborative_filtering")
public class RatingResult implements Serializable {
    private static final long serialVersionUID = 8755574422193819444L;

    @Id
    private String id;

    @Indexed
    private int user;

    @Indexed
    private String product;

    private double productN;
    private double rating;
    private float prediction;

    @Override
    public String toString() {
        return "RatingResult{" +
                "id='" + id + '\'' +
                ", user=" + user +
                ", product='" + product + '\'' +
                ", productN=" + productN +
                ", rating=" + rating +
                ", prediction=" + prediction +
                '}';
    }
}

我使用CrudRepository而不是JPA:

@Repository
public interface RatingResultRepository extends CrudRepository<RatingResult, String> {

}

查询:

     RatingResult found = ratingResultRepository.findById("1").get();
     System.out.println("found = " + found);

输出:

found = RatingResult{id='null', user=1, product='product1', productN=2.0, rating=3.0, prediction=4.0}

您可能注意到,id字段未填充,因为spark-redis存储具有哈希ID,而不是作为哈希属性。

英文:

The following works for me.

Writing data from spark-redis.

I use Scala here, but it's essentially the same as you do in Java. The only thing I changed is I added a .option(&quot;key.column&quot;, &quot;id&quot;) to specify the hash id.

    val ratingResult = new RatingResult(&quot;1&quot;, 1, &quot;product1&quot;, 2.0, 3.0, 4)

    val result: JavaRDD[RatingResult] = spark.sparkContext.parallelize(Seq(ratingResult)).toJavaRDD()
    spark
      .createDataFrame(result, classOf[RatingResult])
      .write
      .format(&quot;org.apache.spark.sql.redis&quot;)
      .option(&quot;key.column&quot;, &quot;id&quot;)
      .option(&quot;table&quot;, &quot;collaborative_filtering&quot;)
      .mode(SaveMode.Overwrite)
      .save()

In spring-data-redis I have the following:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
@RedisHash(&quot;collaborative_filtering&quot;)
public class RatingResult implements Serializable {
    private static final long serialVersionUID = 8755574422193819444L;

    @Id
    private String id;

    @Indexed
    private int user;

    @Indexed
    private String product;

    private double productN;
    private double rating;
    private float prediction;

    @Override
    public String toString() {
        return &quot;RatingResult{&quot; +
                &quot;id=&#39;&quot; + id + &#39;\&#39;&#39; +
                &quot;, user=&quot; + user +
                &quot;, product=&#39;&quot; + product + &#39;\&#39;&#39; +
                &quot;, productN=&quot; + productN +
                &quot;, rating=&quot; + rating +
                &quot;, prediction=&quot; + prediction +
                &#39;}&#39;;
    }
}

I use CrudRepository instead of JPA:

@Repository
public interface RatingResultRepository extends CrudRepository&lt;RatingResult, String&gt; {

}

Querying:

     RatingResult found = ratingResultRepository.findById(&quot;1&quot;).get();
     System.out.println(&quot;found = &quot; + found);

The output:

found = RatingResult{id=&#39;null&#39;, user=1, product=&#39;product1&#39;, productN=2.0, rating=3.0, prediction=4.0}

You may notice that the id field was not populated because the spark-redis stored has a hash id and not as a hash attribute.

huangapple
  • 本文由 发表于 2020年9月1日 18:39:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63686047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定