Commit 64bedc5a authored by liya.cookie's avatar liya.cookie
Browse files

refactor broadcast-table-raw-jdbc-example

parent 536b81f3
Loading
Loading
Loading
Loading
+0 −101
Original line number Diff line number Diff line
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.shardingsphere.example.common.jdbc.service;

import org.apache.shardingsphere.example.common.entity.Country;
import org.apache.shardingsphere.example.common.repository.CountryRepository;
import org.apache.shardingsphere.example.common.service.CommonService;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;

public class CountryServiceImpl implements CommonService {

    private final CountryRepository countryRepository;

    public CountryServiceImpl(final CountryRepository countryRepository) {
        this.countryRepository = countryRepository;
    }

    @Override
    public void initEnvironment() {
        countryRepository.createTableIfNotExists();
        countryRepository.truncateTable();
    }

    @Override
    public void cleanEnvironment() {
        countryRepository.dropTable();
    }

    @Override
    public void processSuccess() {
        System.out.println("-------------- Process Success Begin ---------------");
        List<String> countryCodes = insertData();
        printData();
        deleteData(countryCodes);
        printData();
        System.out.println("-------------- Process Success Finish --------------");
    }

    @Override
    public void processFailure() {
        System.out.println("-------------- Process Failure Begin ---------------");
        insertData();
        System.out.println("-------------- Process Failure Finish --------------");
        throw new RuntimeException("Exception occur for transaction test.");
    }

    @Override
    public void printData() {
        System.out.println("---------------------------- Print Country Data -------------------");
        for (Object each : countryRepository.selectAll()) {
            System.out.println(each);
        }
    }

    private void deleteData(final List<String> countryCodes) {
        System.out.println("---------------------------- Delete Data ----------------------------");
        for (String each: countryCodes) {
            countryRepository.delete(each);
        }
    }

    private List<String> insertData() {
        System.out.println("---------------------------- Insert Data ----------------------------");
        Set<String> result = new LinkedHashSet<>();
        for (Locale each : Locale.getAvailableLocales()) {
            if (result.contains(each.getCountry()) || each.getCountry().isEmpty()) {
                continue;
            }
            if (result.size() >= 20) {
                break;
            }
            result.add(each.getCountry());
            Country entity = new Country();
            entity.setName(each.getDisplayCountry(each));
            entity.setLanguage(each.getLanguage());
            entity.setCode(each.getCountry());
            countryRepository.insert(entity);
        }
        return new ArrayList<>(result);
    }
}
+3 −2
Original line number Diff line number Diff line
@@ -23,7 +23,8 @@ package org.apache.shardingsphere.example.broadcast.table.raw.jdbc;

import org.apache.shardingsphere.example.broadcast.table.raw.jdbc.config.ShardingDatabasesConfiguration;
import org.apache.shardingsphere.example.common.jdbc.repository.CountryRepositoryImpl;
import org.apache.shardingsphere.example.common.jdbc.service.CountryServiceImpl;
import org.apache.shardingsphere.example.common.jdbc.repository.SportsmanRepositoryImpl;
import org.apache.shardingsphere.example.common.jdbc.service.SportsmanServiceImpl;
import org.apache.shardingsphere.example.common.service.CommonService;

import javax.sql.DataSource;
@@ -40,6 +41,6 @@ public class JavaConfigurationExample {
    }

    private static CommonService getCountryService(final DataSource dataSource) {
        return new CountryServiceImpl(new CountryRepositoryImpl(dataSource));
        return new SportsmanServiceImpl(new CountryRepositoryImpl(dataSource), new SportsmanRepositoryImpl(dataSource));
    }
}
+3 −2
Original line number Diff line number Diff line
@@ -18,7 +18,8 @@
package org.apache.shardingsphere.example.broadcast.table.raw.jdbc;

import org.apache.shardingsphere.example.common.jdbc.repository.CountryRepositoryImpl;
import org.apache.shardingsphere.example.common.jdbc.service.CountryServiceImpl;
import org.apache.shardingsphere.example.common.jdbc.repository.SportsmanRepositoryImpl;
import org.apache.shardingsphere.example.common.jdbc.service.SportsmanServiceImpl;
import org.apache.shardingsphere.example.common.service.CommonService;
import org.apache.shardingsphere.shardingjdbc.api.yaml.YamlShardingDataSourceFactory;

@@ -42,6 +43,6 @@ public class YamlConfigurationExample {
    }

    private static CommonService getCountryService(final DataSource dataSource) {
        return new CountryServiceImpl(new CountryRepositoryImpl(dataSource));
        return new SportsmanServiceImpl(new CountryRepositoryImpl(dataSource), new SportsmanRepositoryImpl(dataSource));
    }
}
+11 −0
Original line number Diff line number Diff line
@@ -17,7 +17,10 @@

package org.apache.shardingsphere.example.broadcast.table.raw.jdbc.config;

import org.apache.shardingsphere.api.config.sharding.KeyGeneratorConfiguration;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.strategy.InlineShardingStrategyConfiguration;
import org.apache.shardingsphere.example.common.DataSourceUtil;
import org.apache.shardingsphere.example.config.ExampleConfiguration;
import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
@@ -33,10 +36,18 @@ public class ShardingDatabasesConfiguration implements ExampleConfiguration {
    @Override
    public DataSource getDataSource() throws SQLException {
        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        shardingRuleConfig.getTableRuleConfigs().add(getSportsmanTableRuleConfiguration());
        shardingRuleConfig.getBroadcastTables().add("t_country");
        shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("id", "demo_ds_${id % 2}"));
        return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, new Properties());
    }

    private TableRuleConfiguration getSportsmanTableRuleConfiguration() {
        TableRuleConfiguration result = new TableRuleConfiguration("t_sportsman");
        result.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "id", new Properties()));
        return result;
    }

    private static Map<String, DataSource> createDataSourceMap() {
        Map<String, DataSource> result = new HashMap<>();
        result.put("demo_ds_0", DataSourceUtil.createDataSource("demo_ds_0"));
+10 −0
Original line number Diff line number Diff line
@@ -11,6 +11,16 @@ dataSources:
    password:

shardingRule:
  tables:
    t_sportsman:
      actualDataNodes: ds_${0..1}.t_sportsman
      keyGenerator:
        type: SNOWFLAKE
        column: id
  defaultDatabaseStrategy:
    inline:
      shardingColumn: id
      algorithmExpression: ds_${id % 2}
  broadcastTables:
    - t_country

Loading