Search in sources :

Example 16 with PhoenixConnection

use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.

the class CSVCommonsLoaderIT method testCSVUpsertWithInvalidNumericalData_StrictMode.

// Ensure that strict mode also causes the import to stop if a data type on a single
// row is not correct
@Test
public void testCSVUpsertWithInvalidNumericalData_StrictMode() throws Exception {
    CSVParser parser = null;
    PhoenixConnection conn = null;
    try {
        String stockTableName = generateUniqueName();
        // Create table
        String statements = "CREATE TABLE IF NOT EXISTS " + stockTableName + "(SYMBOL VARCHAR NOT NULL PRIMARY KEY, COMPANY_ID BIGINT);";
        conn = DriverManager.getConnection(getUrl()).unwrap(PhoenixConnection.class);
        PhoenixRuntime.executeStatements(conn, new StringReader(statements), null);
        // Upsert CSV file in strict mode
        CSVCommonsLoader csvUtil = new CSVCommonsLoader(conn, stockTableName, Arrays.asList("SYMBOL", "COMPANY_ID"), true);
        try {
            csvUtil.upsert(new StringReader(STOCK_CSV_VALUES));
            fail("Running an upsert with data that can't be upserted in strict mode " + "should throw an exception");
        } catch (IllegalDataException e) {
        // Expected
        }
    } finally {
        if (parser != null)
            parser.close();
        if (conn != null)
            conn.close();
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) CSVParser(org.apache.commons.csv.CSVParser) StringReader(java.io.StringReader) CSVCommonsLoader(org.apache.phoenix.util.CSVCommonsLoader) IllegalDataException(org.apache.phoenix.schema.IllegalDataException) Test(org.junit.Test)

Example 17 with PhoenixConnection

use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.

the class CSVCommonsLoaderIT method testCSVCommonsUpsert_WithArray.

@Test
public void testCSVCommonsUpsert_WithArray() throws Exception {
    CSVParser parser = null;
    PhoenixConnection conn = null;
    try {
        // Create table
        String statements = "CREATE TABLE IF NOT EXISTS ARRAY_TABLE " + "(ID BIGINT NOT NULL PRIMARY KEY, VALARRAY INTEGER ARRAY);";
        conn = DriverManager.getConnection(getUrl()).unwrap(PhoenixConnection.class);
        PhoenixRuntime.executeStatements(conn, new StringReader(statements), null);
        // Upsert CSV file
        CSVCommonsLoader csvUtil = new CSVCommonsLoader(conn, "ARRAY_TABLE", ImmutableList.<String>of(), true, ',', '"', null, "!");
        csvUtil.upsert(new StringReader("ID,VALARRAY\n" + "1,2!3!4\n"));
        // Compare Phoenix ResultSet with CSV file content
        PreparedStatement statement = conn.prepareStatement("SELECT ID, VALARRAY FROM ARRAY_TABLE");
        ResultSet phoenixResultSet = statement.executeQuery();
        assertTrue(phoenixResultSet.next());
        assertEquals(1L, phoenixResultSet.getLong(1));
        assertEquals(PArrayDataType.instantiatePhoenixArray(PInteger.INSTANCE, new Integer[] { 2, 3, 4 }), phoenixResultSet.getArray(2));
        assertFalse(phoenixResultSet.next());
    } finally {
        if (parser != null)
            parser.close();
        if (conn != null)
            conn.close();
    }
}
Also used : PInteger(org.apache.phoenix.schema.types.PInteger) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) CSVParser(org.apache.commons.csv.CSVParser) StringReader(java.io.StringReader) CSVCommonsLoader(org.apache.phoenix.util.CSVCommonsLoader) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 18 with PhoenixConnection

use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.

the class CSVCommonsLoaderIT method testCSVUpsertWithBogusColumnStrict.

@Test
public void testCSVUpsertWithBogusColumnStrict() throws Exception {
    CSVParser parser = null;
    PhoenixConnection conn = null;
    try {
        String stockTableName = generateUniqueName();
        // Create table
        String statements = "CREATE TABLE IF NOT EXISTS " + stockTableName + "(SYMBOL VARCHAR NOT NULL PRIMARY KEY, COMPANY VARCHAR);";
        conn = DriverManager.getConnection(getUrl()).unwrap(PhoenixConnection.class);
        PhoenixRuntime.executeStatements(conn, new StringReader(statements), null);
        // Upsert CSV file
        CSVCommonsLoader csvUtil = new CSVCommonsLoader(conn, stockTableName, Arrays.asList(STOCK_COLUMNS_WITH_BOGUS), true);
        try {
            csvUtil.upsert(new StringReader(STOCK_CSV_VALUES));
            fail();
        } catch (SQLException e) {
            assertTrue(e.getMessage(), e.getMessage().contains("ERROR 504 (42703): Undefined column. columnName=" + stockTableName + ".BOGUS"));
        }
    } finally {
        if (parser != null)
            parser.close();
        if (conn != null)
            conn.close();
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) SQLException(java.sql.SQLException) CSVParser(org.apache.commons.csv.CSVParser) StringReader(java.io.StringReader) CSVCommonsLoader(org.apache.phoenix.util.CSVCommonsLoader) Test(org.junit.Test)

Example 19 with PhoenixConnection

use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.

the class CSVCommonsLoaderIT method testCSVUpsertWithColumns.

@Test
public void testCSVUpsertWithColumns() throws Exception {
    CSVParser parser = null;
    PhoenixConnection conn = null;
    try {
        String stockTableName = generateUniqueName();
        // Create table
        String statements = "CREATE TABLE IF NOT EXISTS " + stockTableName + "(SYMBOL VARCHAR NOT NULL PRIMARY KEY, COMPANY VARCHAR);";
        conn = DriverManager.getConnection(getUrl()).unwrap(PhoenixConnection.class);
        PhoenixRuntime.executeStatements(conn, new StringReader(statements), null);
        // Upsert CSV file
        CSVCommonsLoader csvUtil = new CSVCommonsLoader(conn, stockTableName, Arrays.<String>asList(STOCK_COLUMNS), true);
        // no header
        csvUtil.upsert(new StringReader(STOCK_CSV_VALUES));
        // Compare Phoenix ResultSet with CSV file content
        PreparedStatement statement = conn.prepareStatement("SELECT SYMBOL, COMPANY FROM " + stockTableName);
        ResultSet phoenixResultSet = statement.executeQuery();
        parser = new CSVParser(new StringReader(STOCK_CSV_VALUES), csvUtil.getFormat());
        for (CSVRecord record : parser) {
            assertTrue(phoenixResultSet.next());
            int i = 0;
            for (String value : record) {
                assertEquals(value, phoenixResultSet.getString(i + 1));
                i++;
            }
        }
        assertFalse(phoenixResultSet.next());
    } finally {
        if (parser != null)
            parser.close();
        if (conn != null)
            conn.close();
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) CSVParser(org.apache.commons.csv.CSVParser) StringReader(java.io.StringReader) CSVCommonsLoader(org.apache.phoenix.util.CSVCommonsLoader) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) CSVRecord(org.apache.commons.csv.CSVRecord) Test(org.junit.Test)

Example 20 with PhoenixConnection

use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.

the class ImmutableTablePropertiesIT method testImmutableProperty.

@Test
public void testImmutableProperty() throws Exception {
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    String immutableDataTableFullName = SchemaUtil.getTableName("", generateUniqueName());
    String mutableDataTableFullName = SchemaUtil.getTableName("", generateUniqueName());
    try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
        Statement stmt = conn.createStatement();
        // create table with immutable table property set to true
        String ddl = "CREATE TABLE  " + immutableDataTableFullName + "  (a_string varchar not null, col1 integer" + "  CONSTRAINT pk PRIMARY KEY (a_string)) IMMUTABLE_ROWS=true";
        stmt.execute(ddl);
        // create table with immutable table property set to false
        ddl = "CREATE TABLE  " + mutableDataTableFullName + "  (a_string varchar not null, col1 integer" + "  CONSTRAINT pk PRIMARY KEY (a_string))  IMMUTABLE_ROWS=false";
        stmt.execute(ddl);
        PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
        PTable immutableTable = phxConn.getTable(new PTableKey(null, immutableDataTableFullName));
        assertTrue("IMMUTABLE_ROWS should be set to true", immutableTable.isImmutableRows());
        PTable mutableTable = phxConn.getTable(new PTableKey(null, mutableDataTableFullName));
        assertFalse("IMMUTABLE_ROWS should be set to false", mutableTable.isImmutableRows());
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Statement(java.sql.Statement) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Properties(java.util.Properties) PTableKey(org.apache.phoenix.schema.PTableKey) PTable(org.apache.phoenix.schema.PTable) Test(org.junit.Test)

Aggregations

PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)216 Test (org.junit.Test)111 Connection (java.sql.Connection)57 Properties (java.util.Properties)52 PTable (org.apache.phoenix.schema.PTable)52 Scan (org.apache.hadoop.hbase.client.Scan)51 PhoenixPreparedStatement (org.apache.phoenix.jdbc.PhoenixPreparedStatement)48 BaseConnectionlessQueryTest (org.apache.phoenix.query.BaseConnectionlessQueryTest)47 PTableKey (org.apache.phoenix.schema.PTableKey)43 ResultSet (java.sql.ResultSet)41 PreparedStatement (java.sql.PreparedStatement)40 SQLException (java.sql.SQLException)40 Filter (org.apache.hadoop.hbase.filter.Filter)29 SkipScanFilter (org.apache.phoenix.filter.SkipScanFilter)29 RowKeyComparisonFilter (org.apache.phoenix.filter.RowKeyComparisonFilter)28 TestUtil.multiEncodedKVFilter (org.apache.phoenix.util.TestUtil.multiEncodedKVFilter)28 TestUtil.singleKVFilter (org.apache.phoenix.util.TestUtil.singleKVFilter)28 Statement (java.sql.Statement)19 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)17 PColumn (org.apache.phoenix.schema.PColumn)17