org.apache.pig.backend.hadoop.hbase.HBaseBinaryConverter Maven / Gradle / Ivy
/* 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.pig.backend.hadoop.hbase;
import java.io.IOException;
import java.util.Map;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.pig.LoadStoreCaster;
import org.apache.pig.ResourceSchema.ResourceFieldSchema;
import org.apache.pig.backend.executionengine.ExecException;
import org.apache.pig.data.DataBag;
import org.apache.pig.data.DataByteArray;
import org.apache.pig.data.Tuple;
public class HBaseBinaryConverter implements LoadStoreCaster {
@Override
public String bytesToCharArray(byte[] b) throws IOException {
return Bytes.toString(b);
}
@Override
public Double bytesToDouble(byte[] b) throws IOException {
if (Bytes.SIZEOF_DOUBLE > b.length){
return Bytes.toDouble(Bytes.padHead(b, Bytes.SIZEOF_DOUBLE - b.length));
} else {
return Bytes.toDouble(Bytes.head(b, Bytes.SIZEOF_DOUBLE));
}
}
@Override
public Float bytesToFloat(byte[] b) throws IOException {
if (Bytes.SIZEOF_FLOAT > b.length){
return Bytes.toFloat(Bytes.padHead(b, Bytes.SIZEOF_FLOAT - b.length));
} else {
return Bytes.toFloat(Bytes.head(b, Bytes.SIZEOF_FLOAT));
}
}
@Override
public Integer bytesToInteger(byte[] b) throws IOException {
if (Bytes.SIZEOF_INT > b.length){
return Bytes.toInt(Bytes.padHead(b, Bytes.SIZEOF_INT - b.length));
} else {
return Bytes.toInt(Bytes.head(b, Bytes.SIZEOF_INT));
}
}
@Override
public Long bytesToLong(byte[] b) throws IOException {
if (Bytes.SIZEOF_LONG > b.length){
return Bytes.toLong(Bytes.padHead(b, Bytes.SIZEOF_LONG - b.length));
} else {
return Bytes.toLong(Bytes.head(b, Bytes.SIZEOF_LONG));
}
}
@Override
public Map bytesToMap(byte[] b) throws IOException {
return bytesToMap(b, null);
}
/**
* NOT IMPLEMENTED
*/
@Override
public Map bytesToMap(byte[] b, ResourceFieldSchema fieldSchema) throws IOException {
throw new ExecException("Can't generate a Map from byte[]");
}
/**
* NOT IMPLEMENTED
*/
@Override
public Tuple bytesToTuple(byte[] b, ResourceFieldSchema fieldSchema) throws IOException {
throw new ExecException("Can't generate a Tuple from byte[]");
}
/**
* NOT IMPLEMENTED
*/
@Override
public DataBag bytesToBag(byte[] b, ResourceFieldSchema fieldSchema) throws IOException {
throw new ExecException("Can't generate DataBags from byte[]");
}
/**
* NOT IMPLEMENTED
*/
@Override
public byte[] toBytes(DataBag bag) throws IOException {
throw new ExecException("Cant' generate bytes from DataBag");
}
@Override
public byte[] toBytes(String s) throws IOException {
return Bytes.toBytes(s);
}
@Override
public byte[] toBytes(Double d) throws IOException {
return Bytes.toBytes(d);
}
@Override
public byte[] toBytes(Float f) throws IOException {
return Bytes.toBytes(f);
}
@Override
public byte[] toBytes(Integer i) throws IOException {
return Bytes.toBytes(i);
}
@Override
public byte[] toBytes(Long l) throws IOException {
return Bytes.toBytes(l);
}
/**
* NOT IMPLEMENTED
*/
@Override
public byte[] toBytes(Map m) throws IOException {
throw new IOException("Can't generate bytes from Map");
}
/**
* NOT IMPLEMENTED
*/
@Override
public byte[] toBytes(Tuple t) throws IOException {
throw new IOException("Can't generate bytes from Tuple");
}
@Override
public byte[] toBytes(DataByteArray a) throws IOException {
return a.get();
}
}