6. Inner Join

data[2018-12-23]
MySql Join方式

6. Inner Join_第1张图片
Join.jpg

目地,了解数据库join的结果,为大数据环境下实现做准备

准备数据

userId locationId
u1          UT
u2          GA
u3          CA
u4          CA
u5          GA
===============================================
transactionId productId userId quantity amount
t1                  p3      u1      1   300
t2                  p1      u2      1   100
t3                  p1      u1      1   100
t4                  p2      u2      1   10
t5                  p4      u4      1   9
t6                  p1      u1      1   100
t7                  p4      u1      1   9
t8                  p4      u5      2   40
public class Users {
    private String userId;
    private String locationId;
    ... get/set
    public Users(String userId, String locationId) {
        this.userId = userId;
        this.locationId = locationId;
    }
}
public class Transactions {
    private String transactionId;
    private String productId;
    private String userId;
    private String quantity;
    private String amount;
    ... get/set
    public Transactions(String transactionId, String productId, String userId, String quantity, String amount) {
        this.transactionId = transactionId;
        this.productId = productId;
        this.userId = userId;
        this.quantity = quantity;
        this.amount = amount;
    }
}

junit准备数据

 public static List usersList;
    public static List transactionsList;

    @Before
    public void setUp() {
        /*users*/
        usersList = new ArrayList<>();
        usersList.add(new Users("u1", "UT"));
        usersList.add(new Users("u2", "GA"));
        usersList.add(new Users("u3", "CA"));
        usersList.add(new Users("u4", "CA"));
        usersList.add(new Users("u5", "GA"));


        /*transactions*/
        transactionsList = new ArrayList<>();
        transactionsList.add(new Transactions("t1", "p3", "u1", "1", "300"));
        transactionsList.add(new Transactions("t2", "p1", "u2", "1", "100"));
        transactionsList.add(new Transactions("t3", "p1", "u1", "1", "100"));
        transactionsList.add(new Transactions("t4", "p2", "u2", "1", "10"));
        transactionsList.add(new Transactions("t5", "p4", "u4", "1", "9"));
        transactionsList.add(new Transactions("t6", "p1", "u1", "1", "100"));
        transactionsList.add(new Transactions("t7", "p4", "u1", "1", "9"));
        transactionsList.add(new Transactions("t8", "p4", "u5", "2", "40"));
    }

UsersTransactionsuserId做内连接

java实现,使用list每次遍历或者使用Map辅助

使用list类似于全表扫描,使用Map类似于走主键或者索引

    @Test
    public void testLeftJoin() {
        /*users left join transactions*/
        System.out.println("===========Left Join Result============");
        for (Transactions transactions : transactionsList) {
            for (Users users : usersList) {
                if (Objects.equals(users.getUserId(), transactions.getUserId())) {
                    System.out.println(transactions.getProductId() + " " + users.getLocationId());
                }
            }
        }
        System.out.println("===============End  Join===============");
    }

    @Test
    public void testLeftJoinUseMap() {
        Map usersMap = new HashMap<>();
        usersList.forEach(users -> {
            usersMap.put(users.getUserId(), users);
        });

        /*users left join transactions*/
        System.out.println("===========Left Join Result============");
        for (Transactions transactions : transactionsList) {
            Users users = usersMap.get(transactions.getUserId());
            if (users != null) {
                System.out.println(transactions.getProductId() + " " + users.getLocationId());
            }
        }
        System.out.println("===============End  Join===============");
    }

    @After
    public void tearDown() {
        System.out.println(Users.printHeader());
        for (Users users : usersList) {
            System.out.println(users);
        }

        System.out.println("=============================");
        System.out.println(Transactions.printHeader());
        for (Transactions transactions : transactionsList) {
            System.out.println(transactions);
        }
    }

你可能感兴趣的:(6. Inner Join)