Search in sources :

Example 11 with DataByteArray

use of org.apache.pig.data.DataByteArray in project phoenix by apache.

the class PhoenixHBaseStorerIT method testStoreWithBinaryDataTypes.

/**
     * Test storage of DataByteArray columns to Phoenix
     * Maps the DataByteArray with the target PhoenixDataType and persists in HBase. 
    * @throws Exception
     */
@Test
public void testStoreWithBinaryDataTypes() throws Exception {
    final String tableName = "TABLE3";
    final Statement stmt = conn.createStatement();
    stmt.execute("CREATE TABLE " + tableName + " (col1 BIGINT NOT NULL, col2 INTEGER , col3 FLOAT, col4 DOUBLE , col5 TINYINT , " + "  col6 BOOLEAN , col7 VARBINARY CONSTRAINT my_pk PRIMARY KEY (col1))");
    final Data data = Storage.resetData(pigServer);
    final Collection<Tuple> list = Lists.newArrayList();
    int rows = 10;
    for (int i = 1; i <= rows; i++) {
        Tuple t = tupleFactory.newTuple();
        t.append(i);
        t.append(new DataByteArray(Bytes.toBytes(i * 5)));
        t.append(new DataByteArray(Bytes.toBytes(i * 10.0F)));
        t.append(new DataByteArray(Bytes.toBytes(i * 15.0D)));
        t.append(new DataByteArray(Bytes.toBytes(i)));
        t.append(new DataByteArray(Bytes.toBytes(i % 2 == 0)));
        t.append(new DataByteArray(Bytes.toBytes(i)));
        list.add(t);
    }
    data.set("in", "col1:int,col2:bytearray,col3:bytearray,col4:bytearray,col5:bytearray,col6:bytearray,col7:bytearray ", list);
    pigServer.setBatchOn();
    pigServer.registerQuery("A = LOAD 'in' USING mock.Storage();");
    pigServer.registerQuery("Store A into 'hbase://" + tableName + "' using " + PhoenixHBaseStorage.class.getName() + "('" + zkQuorum + "', '-batchSize 1000');");
    if (pigServer.executeBatch().get(0).getStatus() != JOB_STATUS.COMPLETED) {
        throw new RuntimeException("Job failed", pigServer.executeBatch().get(0).getException());
    }
    final ResultSet rs = stmt.executeQuery(String.format("SELECT col1 , col2 , col3 , col4 , col5 , col6, col7  FROM %s ORDER BY col1", tableName));
    int count = 0;
    for (int i = 1; i <= rows; i++) {
        assertTrue(rs.next());
        assertEquals(i, rs.getInt(1));
        assertEquals(i * 5, rs.getInt(2));
        assertEquals(i * 10.0F, rs.getFloat(3), 0.0);
        assertEquals(i * 15.0D, rs.getInt(4), 0.0);
        assertEquals(i, rs.getInt(5));
        assertEquals(i % 2 == 0, rs.getBoolean(6));
        assertArrayEquals(Bytes.toBytes(i), rs.getBytes(7));
        count++;
    }
    assertEquals(rows, count);
}
Also used : Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Data(org.apache.pig.builtin.mock.Storage.Data) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple) Test(org.junit.Test)

Aggregations

DataByteArray (org.apache.pig.data.DataByteArray)11 Tuple (org.apache.pig.data.Tuple)8 IOException (java.io.IOException)4 Map (java.util.Map)4 Timestamp (java.sql.Timestamp)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 DateTime (org.joda.time.DateTime)3 Date (java.sql.Date)2 UUID (java.util.UUID)2 Type (org.apache.hive.hcatalog.data.schema.HCatFieldSchema.Type)2 DataType (org.apache.pig.data.DataType)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 BasicDBObject (com.mongodb.BasicDBObject)1 DBRef (com.mongodb.DBRef)1 InterruptedException (java.lang.InterruptedException)1 BigDecimal (java.math.BigDecimal)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1