Hi,
I have an object list of Users and I want to implement top() and filter()
methods on the object list. Let me explain you the whole scenario:
1. I have User object list named "usersList". I fill it during record set.
User user = new User();
user.setUserName(record.getValue("username").toString());
user.setPassword(record.getValue("password").toString());
usersList.add(user);
2. I successfully implement first() and take() methods like this, they are
giving results:
JavaRDD<User> rdd = context.parallelize(usersList);
/*Getting First User*/
User firstUsr = (User) rdd.rdd().first();
System.out.println("First User: "+firstUsr.getUserName());
/*Getting Take 2 Users*/
User[] takeUsr =
(User[])rdd.rdd().retag(User.class).take(2);
for(int ctusr=0 ; ctusr<takeUsr.length;ctusr++ ){
System.out.println("User from take list:
"+takeUsr[ctusr].getUserName());
}
3. I want to implement the top() in the same way. I tried it but it requires
two arguements(num, Ordering<User> ord). I donot understand how to
implement Ordering. I tried two possibilities:
(i) User[] topUsr = (User[])rdd.rdd().retag(User.class).top(1, null);
(ii) User[] topUsr = (User[])rdd.rdd().retag(User.class).top(1, new
Ordering<User>() {
@Override
public Some tryCompare(User user, User t1) {
return null;
}
@Override
public int compare(User usr1, User usr2) {
usr1 = new User();
usr2 = new User();
for(int u=0 ; u < usersList.size(); u++){
usr1 = (User)usersList.get(u);
usr2 = (User)usersList.get(u);
}
String userName1 = usr1.getUserName();
String userName2 = usr2.getUserName();
System.out.println("userName1: "+userName1);
//ascending order
return userName1.compareTo(userName2);
//descending order
//return userName2.compareTo(userName1);
}
@Override
public boolean lteq(User user, User t1) {
return false;
}
@Override
public boolean gteq(User user, User t1) {
return false;
}
@Override
public boolean lt(User user, User t1) {
return false;
}
@Override
public boolean gt(User user, User t1) {
return false;
}
@Override
public boolean equiv(User user, User t1) {
return false;
}
@Override
public User max(User user, User t1) {
return null;
}
@Override
public User min(User user, User t1) {
return null;
}
@Override
public Ordering<User> reverse() {
return null;
}
@Override
public <U> Ordering<U> on(Function1<U, User> function1)
{
return null;
}
@Override
public Ops mkOrderingOps(User user) {
return null;
}
}
);
BOTH ARE GIVING ME NULLPOINTER EXCEPTION. Kindly guide me how to properly
implement it?
4. I also want to implement the filter() in same way.
User[] filterUsr =
(User[])rdd.rdd().retag(User.class).filter(Function1<User, Object>);
but could not write Function1 for it. I suppose that filter out the user
whose name is "Bob". How it can be possible by using object list and User
objects? Can you give me code sample for Function1?
I really appreciate your help and guidance.
Thanks,
Hafsa
--
View this message in context:
http://apache-spark-user-list.1001560.n3.nabble.com/How-to-implement-top-and-filter-on-object-List-for-JavaRDD-tp23669.html
Sent from the Apache Spark User List mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]