方法1:
/** * 添加一行数据 * * @param tableName * 表名 * @param columnFamily * 列族名 * @param rowKey * 行键 * @param columnQualifier * 列限定符 * @param value * 值 */ public void AddRowData(String tableName, String columnFamily, String rowKey, String columnQualifier, String value) { tableName = HBASE_TABLE_NAMESPACE + ":" + tableName; try { _hTableAdmin = new HTable(_configuration, tableName); Put put = new Put(Bytes.toBytes(rowKey)); put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(columnQualifier), Bytes.toBytes(value)); // 当数据变动时,同步写WAL日志 put.setDurability(Durability.SYNC_WAL); _hTableAdmin.put(put); } catch (IOException e) { e.printStackTrace(); } finally { this.CloseHTableAdmin("AddRowData"); } }
方法2:
/** * 添加一行多限定符的数据 * * @param tableName * 表名 * @param columnFamily * 列族 * @param rowKey * 行键 * @param cqAndvalue * 列名(列限定符)和值的键值对 */ public void AddRowData(String tableName, String columnFamily, String rowKey, MapcqAndvalue) { if (cqAndvalue.isEmpty()) return; tableName = HBASE_TABLE_NAMESPACE + ":" + tableName; try { List puts = new ArrayList (); for (String cq : cqAndvalue.keySet()) { _hTableAdmin = new HTable(_configuration, tableName); Put put = new Put(Bytes.toBytes(rowKey)); put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(cq), Bytes.toBytes(cqAndvalue.get(cq).toString())); puts.add(put); } _hTableAdmin.put(puts); } catch (IOException e) { e.printStackTrace(); } finally { this.CloseHTableAdmin("AddRowData"); } }
调用方法1:
hbaseHelper.AddRowData(tableName, columnFamily, "3", "title", "四核I5 4570升4590/GTX750独显组装电脑主机 游戏台式DIY兼容整机"); hbaseHelper.AddRowData(tableName, columnFamily, "3", "introductions", "开学季,劲爆升级限量抢①免费升级ID-Cooling SE902 V3双铜管蓝光LED高端散热器,彻底告别高温、死机、重启②免费升级到第一品牌七彩虹GTX750 2G D5高端显卡③免费升级全球第一品牌华硕B85M-F全固态主板,独家EPU节能及数字稳定供电,送全国顺丰包邮!限量300台,即将抢完!"); hbaseHelper.AddRowData(tableName, columnFamily, "3", "price", 3699.00); hbaseHelper.AddRowData(tableName, columnFamily, "3", "real", 2899.00); hbaseHelper.AddRowData(tableName, columnFamily, "3", "freight", 0.00); hbaseHelper.AddRowData(tableName, columnFamily, "3", "place", "湖北武汉"); hbaseHelper.AddRowData(tableName, columnFamily, "3", "monthsell", 4469);
结果1:
2014-09-10 15:18:12,777 WARN [main] util.NativeCodeLoader (NativeCodeLoader.java:(62)) - Unable to load native-hadoop library for your platform... using builtin-java classes where applicable 2014-09-10 15:18:12,964 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT 2014-09-10 15:18:12,964 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:host.name=[...] 2014-09-10 15:18:12,964 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:java.version=1.7.0_60 2014-09-10 15:18:12,964 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:java.vendor=Oracle Corporation 2014-09-10 15:18:12,964 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:java.home=D:\JAVA\jre7 2014-09-10 15:18:12,964 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:java.class.path=[...] 2014-09-10 15:18:12,965 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:java.library.path=[...] 2014-09-10 15:18:12,965 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:java.io.tmpdir=[...] 2014-09-10 15:18:12,965 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:java.compiler= 2014-09-10 15:18:12,965 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:os.name=Windows 7 2014-09-10 15:18:12,965 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:os.arch=amd64 2014-09-10 15:18:12,965 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:os.version=6.1 2014-09-10 15:18:12,965 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:user.name=LFF 2014-09-10 15:18:12,965 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:user.home=[...] 2014-09-10 15:18:12,965 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:user.dir=[...] 2014-09-10 15:18:12,968 INFO [main] zookeeper.ZooKeeper (ZooKeeper.java: (438)) - Initiating client connection, connectString=192.168.129.130:2181 sessionTimeout=90000 watcher=hconnection-0x7b70ad95, quorum=192.168.129.130:2181, baseZNode=/hbase 2014-09-10 15:18:12,999 INFO [main] zookeeper.RecoverableZooKeeper (RecoverableZooKeeper.java: (120)) - Process identifier=hconnection-0x7b70ad95 connecting to ZooKeeper ensemble=192.168.129.130:2181 2014-09-10 15:18:12,999 INFO [main-SendThread(192.168.129.130:2181)] zookeeper.ClientCnxn (ClientCnxn.java:logStartConnect(975)) - Opening socket connection to server 192.168.129.130/192.168.129.130:2181. Will not attempt to authenticate using SASL (unknown error) 2014-09-10 15:18:13,001 INFO [main-SendThread(192.168.129.130:2181)] zookeeper.ClientCnxn (ClientCnxn.java:primeConnection(852)) - Socket connection established to 192.168.129.130/192.168.129.130:2181, initiating session 2014-09-10 15:18:13,007 INFO [main-SendThread(192.168.129.130:2181)] zookeeper.ClientCnxn (ClientCnxn.java:onConnected(1235)) - Session establishment complete on server 192.168.129.130/192.168.129.130:2181, sessionid = 0x1485d39e21a0015, negotiated timeout = 40000 HBase Namespace Exists[true]! ExistsHBaseNamespace(...):关闭与HBase的连接! CreateHBaseNamespace(...):关闭与HBase的连接! AddRowData(...):关闭与HTable的连接! AddRowData(...):关闭与HTable的连接! AddRowData(...):关闭与HTable的连接! AddRowData(...):关闭与HTable的连接! AddRowData(...):关闭与HTable的连接! AddRowData(...):关闭与HTable的连接! AddRowData(...):关闭与HTable的连接!
调用方法2:
Mapmap = new HashMap (); map.put("title", "Lenovo/联想 IdeaPad Y400 Y400N-IFI(T) i5游戏本笔记本电脑750M"); map.put("introductions", "彪悍的Y星人 加SSD性能更强劲"); map.put("price", 5351.00); map.put("real", 4649.00); map.put("freight", 0); map.put("place", "北京"); map.put("monthsell", 1329); hbaseHelper.AddRowData(tableName, columnFamily, "2", map);
结果2:
2014-09-10 15:05:20,212 WARN [main] util.NativeCodeLoader (NativeCodeLoader.java:(62)) - Unable to load native-hadoop library for your platform... using builtin-java classes where applicable 2014-09-10 15:05:20,401 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT 2014-09-10 15:05:20,401 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:host.name=[...] 2014-09-10 15:05:20,401 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:java.version=1.7.0_60 2014-09-10 15:05:20,401 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:java.vendor=Oracle Corporation 2014-09-10 15:05:20,401 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:java.home=[...] 2014-09-10 15:05:20,401 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:java.class.path=[...] 2014-09-10 15:05:20,402 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:java.library.path=[...] 2014-09-10 15:05:20,402 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:java.io.tmpdir=[...] 2014-09-10 15:05:20,402 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:java.compiler= 2014-09-10 15:05:20,402 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:os.name=Windows 7 2014-09-10 15:05:20,402 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:os.arch=amd64 2014-09-10 15:05:20,402 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:os.version=6.1 2014-09-10 15:05:20,402 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:user.name=[...] 2014-09-10 15:05:20,402 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:user.home=[...] 2014-09-10 15:05:20,403 INFO [main] zookeeper.ZooKeeper (Environment.java:logEnv(100)) - Client environment:user.dir=[...] 2014-09-10 15:05:20,405 INFO [main] zookeeper.ZooKeeper (ZooKeeper.java: (438)) - Initiating client connection, connectString=192.168.129.130:2181 sessionTimeout=90000 watcher=hconnection-0x265a7006, quorum=192.168.129.130:2181, baseZNode=/hbase 2014-09-10 15:05:20,437 INFO [main] zookeeper.RecoverableZooKeeper (RecoverableZooKeeper.java: (120)) - Process identifier=hconnection-0x265a7006 connecting to ZooKeeper ensemble=192.168.129.130:2181 2014-09-10 15:05:20,437 INFO [main-SendThread(192.168.129.130:2181)] zookeeper.ClientCnxn (ClientCnxn.java:logStartConnect(975)) - Opening socket connection to server 192.168.129.130/192.168.129.130:2181. Will not attempt to authenticate using SASL (unknown error) 2014-09-10 15:05:20,439 INFO [main-SendThread(192.168.129.130:2181)] zookeeper.ClientCnxn (ClientCnxn.java:primeConnection(852)) - Socket connection established to 192.168.129.130/192.168.129.130:2181, initiating session 2014-09-10 15:05:20,445 INFO [main-SendThread(192.168.129.130:2181)] zookeeper.ClientCnxn (ClientCnxn.java:onConnected(1235)) - Session establishment complete on server 192.168.129.130/192.168.129.130:2181, sessionid = 0x1485d39e21a0014, negotiated timeout = 40000 HBase Namespace Exists[true]! ExistsHBaseNamespace(...):关闭与HBase的连接! CreateHBaseNamespace(...):关闭与HBase的连接! AddRowData(...):关闭与HTable的连接!
对比看结果显示的,基本了解了C端和S端的沟通情况了!