HBase操作、java压缩、解压、Base64



import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;


import com.zrsf.dzfp.util.SystemGlobals;


@SuppressWarnings({"unchecked","unused"})
public class HBaseUtil {


private static HTablePool pool = null;
public static Configuration configuration = null;
private static String tableName = null;
static {
tableName = SystemGlobals.getContextProperty("tableName");
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.property.clientPort", SystemGlobals.getContextProperty("clientPort"));
configuration.set("hbase.zookeeper.quorum", SystemGlobals.getContextProperty("quorum"));// 设置zookeeper的主机,建议使用单数
configuration.set("hbase.master", SystemGlobals.getContextProperty("master"));// 设置hbase的master主机名和端口
pool = new HTablePool(configuration, 1000);
}


public static void copyFile(byte[] bytes, String filename)
throws IOException {
FileOutputStream outf = new FileOutputStream(new File(filename));
BufferedOutputStream bufferout = new BufferedOutputStream(outf);
bufferout.write(bytes);
bufferout.flush();
bufferout.close();
}




/**
* 解压

* @param compressed
* @return
*/
public static byte[] decompress(byte[] compressed) {
if (compressed == null)
return null;
ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
ZipInputStream zin = null;
byte[] decompressed;
try {
out = new ByteArrayOutputStream();
in = new ByteArrayInputStream(compressed);
zin = new ZipInputStream(in);
ZipEntry entry = zin.getNextEntry();
byte[] buffer = new byte[1024];
int offset = -1;
while ((offset = zin.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
decompressed = out.toByteArray();
} catch (IOException e) {
decompressed = null;
} finally {
if (zin != null) {
try {
zin.close();
} catch (IOException e) {
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
return decompressed;
}


/**
* 压缩

* 这里需要特别注意的是,如果你想把压缩后的byte[]保存到字符串中, 不能直接使用new
* String(byte)或者byte.toString(), 因为这样转换之后容量是增加的。 同样的道理,如果是字符串的话,也不能直接使用new
* String().getBytes()获取byte[]传入到decompress中进行解压缩。 如果保存压缩后的二进制,可以使用new
* sun.misc.BASE64Encoder().encodeBuffer(byte[] b)将其转换为字符串。 同样解压缩的时候首先使用new
* BASE64Decoder().decodeBuffer 方法将字符串转换为字节,然后解压就可以了。

* @param str
* @return
*/
public static byte[] compress(byte[] bytes) {
if (bytes == null)
return null;
byte[] compressed;
ByteArrayOutputStream out = null;
ZipOutputStream zout = null;
try {
out = new ByteArrayOutputStream();
zout = new ZipOutputStream(out);
zout.putNextEntry(new ZipEntry("0"));
zout.write(bytes);
zout.closeEntry();
compressed = out.toByteArray();
} catch (IOException e) {
compressed = null;
} finally {
if (zout != null) {
try {
zout.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
return compressed;
}


public static String getStr(File file) throws IOException {
FileInputStream fis = new FileInputStream(file);
byte[] bs = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len = -1;
while ((len = fis.read(bs)) != -1) {
bos.write(bs, 0, len);
}
bs = bos.toByteArray();
if (fis != null) {
fis.close();
}
if (bos != null) {
bos.close();
bos.flush();
}
return Base64Util.base64encoder(compress(bs));
}


public static void insertHbaseData(Map map, String rowKey) throws Exception {
HTableInterface table = pool.getTable(tableName);
// 一个PUT代表一行数据,再NEW一个PUT表示第二行数据,每行一个唯一的ROWKEY,此处rowkey为put构造方法中传入的值
Put put = new Put(rowKey.getBytes());
if (map != null) {
Set keys = map.entrySet();
if (keys != null) {
Iterator iterator = keys.iterator();
while (iterator.hasNext()) {
Map.Entry<String, Object> entry = (Entry) iterator.next();
String columnKey = entry.getKey().toUpperCase();
String columnValue = (String) entry.getValue();
// 向表中制定列中插入数据columnValue.getBytes()
put.add(columnKey.getBytes(), null,
(columnValue == null ? "" : columnValue)
.getBytes());
}
}
}
table.put(put);
}


public static Map<String, String> getHBaseData(String rowKey) throws IOException {
Map<String, String> returnMap = new HashMap<String, String>();
HTableInterface table = pool.getTable(tableName);
Get scan = new Get(rowKey.getBytes());// 根据hadoopMapTypeRowKey查询
Result r = table.get(scan);
for (KeyValue keyValue : r.raw()) {
returnMap.put(new String(keyValue.getFamily()), new String(keyValue
.getValue()));
}
return returnMap;
}


public static void deleteHbaseDataByRowKeyAndTableName(
String... hadoopMapTypeRowKeys) throws Exception {
HTable table = new HTable(configuration, tableName);
List list = new ArrayList();
if (hadoopMapTypeRowKeys != null) {
for (String hadoopMapTypeRowKey : hadoopMapTypeRowKeys) {
Delete delete = new Delete(hadoopMapTypeRowKey.getBytes());
list.add(delete);
}
}
table.delete(list);
}

}




import java.io.IOException;


import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


public class Base64Util {
/**
* 解码
* @param requestString
* @return
* @throws IOException
*/
public static byte[] base64Decoder(String requestString) throws IOException{
return new BASE64Decoder().decodeBuffer(requestString);
}


/*
* 编码
*/
public static String base64encoder(byte[] bytes) throws IOException{
BASE64Encoder enc = new BASE64Encoder();
String encStr =enc.encode(bytes);
return encStr;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值