コード例 #1
0
  @Test
  public void chekGetAllRowsWithEmptyParam() {

    SQLSourceHelper sqlSourceHelper = new SQLSourceHelper(context, "Source Name");
    assertEquals(
        new ArrayList<String>(), sqlSourceHelper.getAllRows(new ArrayList<List<Object>>()));
  }
コード例 #2
0
  @Test
  public void checkStatusFileCorrectlyCreated() {

    SQLSourceHelper sqlSourceHelper = new SQLSourceHelper(context, "Source Name");
    // sqlSourceHelper.setCurrentIndex(10);

    sqlSourceHelper.updateStatusFile();

    File file = new File("/tmp/flume/statusFileName.txt");
    assertEquals(true, file.exists());
    if (file.exists()) {
      file.delete();
      file.getParentFile().delete();
    }
  }
コード例 #3
0
  @Test
  public void checkStatusFileCorrectlyUpdated() throws Exception {

    File file = File.createTempFile("statusFileName", ".txt");

    when(context.getString("status.file.path", "/var/lib/flume")).thenReturn(file.getParent());
    when(context.getString("status.file.name")).thenReturn(file.getName());

    SQLSourceHelper sqlSourceHelper = new SQLSourceHelper(context, "Source Name");
    sqlSourceHelper.setCurrentIndex(10);

    sqlSourceHelper.updateStatusFile();

    SQLSourceHelper sqlSourceHelper2 = new SQLSourceHelper(context, "Source Name");
    assertEquals(10L, sqlSourceHelper2.getCurrentIndex());
  }
コード例 #4
0
  /** Connect to database using hibernate */
  public void establishSession() {

    LOG.info("Opening hibernate session");

    serviceRegistry =
        new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
    factory = config.buildSessionFactory(serviceRegistry);
    session = factory.openSession();
    session.setCacheMode(CacheMode.IGNORE);

    session.setDefaultReadOnly(sqlSourceHelper.isReadOnlySession());
  }
コード例 #5
0
  @SuppressWarnings("deprecation")
  @Test
  public void chekGetAllRows() {

    SQLSourceHelper sqlSourceHelper = new SQLSourceHelper(context, "Source Name");
    List<List<Object>> queryResult = new ArrayList<List<Object>>(2);
    List<String[]> expectedResult = new ArrayList<String[]>(2);
    String string1 = "string1";
    String string2 = "string2";
    int int1 = 1;
    int int2 = 2;
    Date date1 = new Date(115, 0, 1);
    Date date2 = new Date(115, 1, 2);

    List<Object> row1 = new ArrayList<Object>(3);
    String[] expectedRow1 = new String[3];
    row1.add(string1);
    expectedRow1[0] = string1;
    row1.add(int1);
    expectedRow1[1] = Integer.toString(int1);
    row1.add(date1);
    expectedRow1[2] = date1.toString();
    queryResult.add(row1);
    expectedResult.add(expectedRow1);

    List<Object> row2 = new ArrayList<Object>(3);
    String[] expectedRow2 = new String[3];
    row2.add(string2);
    expectedRow2[0] = string2;
    row2.add(int2);
    expectedRow2[1] = Integer.toString(int2);
    row2.add(date2);
    expectedRow2[2] = date2.toString();
    queryResult.add(row2);
    expectedResult.add(expectedRow2);

    assertArrayEquals(expectedResult.get(0), sqlSourceHelper.getAllRows(queryResult).get(0));
    assertArrayEquals(expectedResult.get(1), sqlSourceHelper.getAllRows(queryResult).get(1));
  }
コード例 #6
0
  /**
   * Constructor to initialize hibernate configuration parameters
   *
   * @param sqlSourceHelper Contains the configuration parameters from flume config file
   */
  public HibernateHelper(SQLSourceHelper sqlSourceHelper) {

    this.sqlSourceHelper = sqlSourceHelper;
    Context context = sqlSourceHelper.getContext();

    Map<String, String> hibernateProperties = context.getSubProperties("hibernate.");
    Iterator<Map.Entry<String, String>> it = hibernateProperties.entrySet().iterator();

    config = new Configuration();
    Map.Entry<String, String> e;

    while (it.hasNext()) {
      e = it.next();
      config.setProperty("hibernate." + e.getKey(), e.getValue());
    }
  }
コード例 #7
0
 @Test
 public void getCustomQuery() {
   when(context.getString("custom.query")).thenReturn("SELECT column FROM table");
   SQLSourceHelper sqlSourceHelper = new SQLSourceHelper(context, "Source Name");
   assertEquals("SELECT column FROM table", sqlSourceHelper.getQuery());
 }
コード例 #8
0
 @Test
 public void getQuery() {
   SQLSourceHelper sqlSourceHelper = new SQLSourceHelper(context, "Source Name");
   assertEquals("SELECT * FROM table", sqlSourceHelper.getQuery());
 }
コード例 #9
0
 @Test
 public void getBatchSize() {
   SQLSourceHelper sqlSourceHelper = new SQLSourceHelper(context, "Source Name");
   assertEquals(100, sqlSourceHelper.getBatchSize());
 }
コード例 #10
0
 @Test
 public void getRunQueryDelay() {
   SQLSourceHelper sqlSourceHelper = new SQLSourceHelper(context, "Source Name");
   assertEquals(10000, sqlSourceHelper.getRunQueryDelay());
 }
コード例 #11
0
 @Test
 public void getPassword() {
   SQLSourceHelper sqlSourceHelper = new SQLSourceHelper(context, "Source Name");
   assertEquals("password", sqlSourceHelper.getPassword());
 }
コード例 #12
0
 @Test
 public void getUser() {
   SQLSourceHelper sqlSourceHelper = new SQLSourceHelper(context, "Source Name");
   assertEquals("user", sqlSourceHelper.getUser());
 }
コード例 #13
0
 @Test
 public void setCurrentIndex() {
   SQLSourceHelper sqlSourceHelper = new SQLSourceHelper(context, "Source Name");
   sqlSourceHelper.setCurrentIndex(10);
   assertEquals(10, sqlSourceHelper.getCurrentIndex());
 }
コード例 #14
0
  @Test
  public void getConnectionURL() {

    SQLSourceHelper sqlSourceHelper = new SQLSourceHelper(context, "Source Name");
    assertEquals("jdbc:mysql://host:3306/database", sqlSourceHelper.getConnectionURL());
  }
コード例 #15
0
  /**
   * Execute the selection query in the database
   *
   * @return The query result. Each Object is a cell content.
   *     <p>The cell contents use database types (date,int,string...), keep in mind in case of
   *     future conversions/castings.
   * @throws InterruptedException
   */
  @SuppressWarnings("unchecked")
  public List<List<Object>> executeQuery() throws InterruptedException {

    List<List<Object>> rowsList = new ArrayList<List<Object>>();
    Query query;

    if (!session.isConnected()) {
      resetConnection();
    }

    if (sqlSourceHelper.isCustomQuerySet()) {

      query = session.createSQLQuery(sqlSourceHelper.buildQuery());

      if (sqlSourceHelper.getMaxRows() != 0) {
        query = query.setMaxResults(sqlSourceHelper.getMaxRows());
      }
    } else {
      query =
          session
              .createSQLQuery(sqlSourceHelper.getQuery())
              .setFirstResult(Integer.parseInt(sqlSourceHelper.getCurrentIndex()));

      if (sqlSourceHelper.getMaxRows() != 0) {
        query = query.setMaxResults(sqlSourceHelper.getMaxRows());
      }
    }

    try {
      rowsList =
          query
              .setFetchSize(sqlSourceHelper.getMaxRows())
              .setResultTransformer(Transformers.TO_LIST)
              .list();
    } catch (Exception e) {
      resetConnection();
    }

    if (!rowsList.isEmpty()) {
      if (sqlSourceHelper.isCustomQuerySet()) {
        sqlSourceHelper.setCurrentIndex(rowsList.get(rowsList.size() - 1).get(0).toString());
      } else {
        sqlSourceHelper.setCurrentIndex(
            Integer.toString(
                (Integer.parseInt(sqlSourceHelper.getCurrentIndex()) + rowsList.size())));
      }
    }

    return rowsList;
  }