在编译java文件的时候我只用到了下面三个jar包:Hadoop-core-1.0.2.jar,hbase-0.94.0.jar,zookeeper-3.4.3.jar,java代码如下:
- package org.fansy.date905;
- import java.io.IOException;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.hbase.HBaseConfiguration;
- import org.apache.hadoop.hbase.HColumnDescriptor;
- import org.apache.hadoop.hbase.HTableDescriptor;
- import org.apache.hadoop.hbase.client.HBaseAdmin;
- import org.apache.hadoop.hbase.client.HTable;
- import org.apache.hadoop.hbase.client.Put;
- public class TableOperation {
- /**
- * include all table operation like scan,input,create table,drop table, and so on
- * date:15:12
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- // createTable("tabledemo","name");
- int status=Integer.parseInt(args[0]);
- if(status==1){
- createTable(args[1],args[2]);
- }else if(status==2){
- deleteTable(args[1]);
- }
- }
- /*
- * create a table and its family
- */
- public static void createTable(String tablename ,String family) throws IOException{
- Configuration conf=HBaseConfiguration.create();
- HBaseAdmin admin=new HBaseAdmin(conf);
- HTableDescriptor tableDesc=new HTableDescriptor(tablename);
- tableDesc.addFamily(new HColumnDescriptor(family));
- admin.createTable(tableDesc);
- admin.close();
- }
- /*
- * add data to table ,include row ,family,column,value,tablename
- */
- public static void addData(String tablename,String family,String qualifier,String row,String value)throws IOException {
- Configuration conf=HBaseConfiguration.create();
- HTable table=new HTable(conf,tablename);
- Put putrow=new Put(row.getBytes());
- putrow.add(family.getBytes(), qualifier.getBytes(), value.getBytes());
- table.put(putrow);
- table.close();
- }
- /*
- * drop a table by the tablename given
- */
- public static void deleteTable(String tablename)throws IOException{
- Configuration conf=HBaseConfiguration.create();
- HBaseAdmin admin=new HBaseAdmin(conf);
- admin.disableTable(tablename);
- admin.deleteTable(tablename);
- admin.close();
- }
- }
命令行的测试结果如下:
- fansy@fansyPC:~/hadoop-1.0.2$ bin/hadoop TableOperation 1 testdemo name
- Warning: $HADOOP_HOME is deprecated.
- 12/09/29 15:28:03 INFO zookeeper.ZooKeeper: Client environment:zookeeper.version=3.4.3-1240972, built on 02/06/2012 10:48 GMT
- 12/09/29 15:28:03 INFO zookeeper.ZooKeeper: Client environment:host.name=fansyPC
- 12/09/29 15:28:03 INFO zookeeper.ZooKeeper: Client environment:java.version=1.7.0_07
- 12/09/29 15:28:03 INFO zookeeper.ZooKeeper: Client environment:java.vendor=Oracle Corporation
- 12/09/29 15:28:03 INFO zookeeper.ZooKeeper: Client environment:java.home=/home/fansy/jdk1.7.0_07/jre
- 12/09/29 15:28:03 INFO zookeeper.ZooKeeper: Client environment:java.library.path=/home/fansy/hadoop-1.0.2/libexec/../lib/native/Linux-amd64-64
- 12/09/29 15:28:03 INFO zookeeper.ZooKeeper: Client environment:java.io.tmpdir=/tmp
- 12/09/29 15:28:03 INFO zookeeper.ZooKeeper: Client environment:java.compiler=<NA>
- 12/09/29 15:28:03 INFO zookeeper.ZooKeeper: Client environment:os.name=Linux
- 12/09/29 15:28:03 INFO zookeeper.ZooKeeper: Client environment:os.arch=amd64
- 12/09/29 15:28:03 INFO zookeeper.ZooKeeper: Client environment:os.version=3.2.0-31-generic
- 12/09/29 15:28:03 INFO zookeeper.ZooKeeper: Client environment:user.name=fansy
- 12/09/29 15:28:03 INFO zookeeper.ZooKeeper: Client environment:user.home=/home/fansy
- 12/09/29 15:28:03 INFO zookeeper.ZooKeeper: Client environment:user.dir=/home/fansy/hadoop-1.0.2
- 12/09/29 15:28:03 INFO zookeeper.ZooKeeper: Initiating client connection, connectString=localhost:2181 sessionTimeout=180000 watcher=hconnection
- 12/09/29 15:28:04 INFO zookeeper.ClientCnxn: Opening socket connection to server /127.0.0.1:2181
- 12/09/29 15:28:04 INFO client.ZooKeeperSaslClient: Client will not SASL-authenticate because the default JAAS configuration section 'Client' could not be found. If you are not using SASL, you may ignore this. On the other hand, if you expected SASL to work, please fix your JAAS configuration.
- 12/09/29 15:28:04 INFO zookeeper.RecoverableZooKeeper: The identifier of this process is 18076@bwa108
- 12/09/29 15:28:04 INFO zookeeper.ClientCnxn: Socket connection established to localhost/127.0.0.1:2181, initiating session
- 12/09/29 15:28:04 INFO zookeeper.ClientCnxn: Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x13a10cf8247000e, negotiated timeout = 180000
- 12/09/29 15:28:05 INFO client.HConnectionManager$HConnectionImplementation: Closed zookeeper sessionid=0x13a10cf8247000e
- 12/09/29 15:28:05 INFO zookeeper.ZooKeeper: Session: 0x13a10cf8247000e closed
- create table:testdemo done
这样基本的命令行操作hbase就完成了。
