HBase table Put 插入数据
2016-06-27 14:02
363 查看
HBase 版本: 1.2.1
package com.feng.hbase; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.util.Bytes; public class PutData { public static void main(String[] args) { String talbeName = "test1"; String columnFamily = "cf"; String attribute = "attribute" + Math.random(); String rowkey = "row-" + Math.random(); String data = String.valueOf(Math.random()); Connection connection = null; Table table = null; try { Configuration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", "localhost"); connection = ConnectionFactory.createConnection(config); // 获取表对象 table = connection.getTable(TableName.valueOf(talbeName)); if (table != null) { Put put = new Put(Bytes.toBytes(rowkey)); put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(attribute), Bytes.toBytes(data)); table.put(put); put = new Put(Bytes.toBytes(rowkey)); put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(attribute + "-01"), Bytes.toBytes(data)); table.put(put); System.out.println(talbeName + " create success!"); } else { System.out.println("There is not " + talbeName); } } catch (Exception e) { e.printStackTrace(); } finally { if (table != null) { try { table.close(); } catch (IOException e) { e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
相关文章推荐
- Java中HttpServletResponse响应中文出现乱码问题
- Android使用VideoView播放网络视频,获取网络视频缩略图
- cmake 学习小结
- JNDI学习总结
- Linux之进程创建
- express3.0的ejs模板改成html
- Codeforces 444C DZY Love Colors Solution
- Qt学习六:控件三:单选按钮、复选框、列表控件、树控件
- C#生成验证码
- release程序加载调用ffmpeg动态库崩溃
- Visual Studio Ultimate 2012 激活密钥
- Swift学习(四)常量&变量&基础数据类型
- LXC 虚拟化技术
- idea编译报错:未结束的字符串文字;非法的表达式;未结束的字符串字面值
- 单片机结构体内存的分配
- 31. Next Permutation (java 字典序生成下一个排列)
- SQL SERVER 2005 DBCC PAGE命令说明
- C++ UNICODE下CString 转换为string
- 平台树型部件获取或设置水平/垂直网格线
- 树莓派vnc远程桌面