使用maven+eclipse构建HAOOP环境,HBASE JAVA API操作部分

使用maven+eclipse构建HAOOP环境,HBASE JAVA API操作部分
代码
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;
import org.apache.hadoop.hbase.util.Bytes;


public class HbaseTest {
          
        private static Configuration conf=null;
      static {
      //Configuration hbase_config= new Configuration();
      conf = HBaseConfiguration.create(); 
      conf.set("hbase.zookeeper.quorum", "192.168.168.20");
      conf.set("hbase.zookeeper.property.clientPort", "2181");  
      //conf=HBaseConfiguration.create(hbase_config);  
      conf.addResource("classpath:/hbase/hbase-site.xml");  
      }
      /*
      * 创建一张表
      */
     
      public static void createTable(String tableName,String[] familys) throws IOException{
      HBaseAdmin admin= new HBaseAdmin(conf);
      if (admin.tableExists(tableName)){
      System.out.println("tableName exists!");
      }else{
      HTableDescriptor tabledesc=new HTableDescriptor(tableName);
      for (int i=0;i       tabledesc.addFamily(new HColumnDescriptor(familys[i]));
      }
      admin.createTable(tabledesc);
      System.out.println(tableName + " table created!");
      }
      }
   /*
    * 添加记录
    */
      public static void addData(String rowKey,String tableName,String[] column1,String[] value1,
      String[] column2,String[] value2) throws IOException{
     
      Put put =new Put(Bytes.toBytes(rowKey));
      HTable htable= new HTable(conf,Bytes.toBytes(tableName));
      HColumnDescriptor[] colFamilys=htable.getTableDescriptor().getColumnFamilies();
      for (int i=0;i       {
      String familyName=colFamilys[i].getNameAsString();
      if (familyName.equals(" article")){ // article列族
      for (int j=0;j       {
      put.add(Bytes.toBytes(familyName),Bytes.toBytes(column1[j]),Bytes.toBytes(value1[j]));
      }
      }
      if (familyName.equals(" author")){ // author列族
      for (int j=0;j       {
      put.add(Bytes.toBytes(familyName),Bytes.toBytes(column2[j]),Bytes.toBytes(value2[j]));
      }
      }
      }
      htable.put(put);
      System.out.println("add data sucess!");
      }
      public static void main(String[] args) throws IOException {
      //创建表
      String tableName = "blog";  
            String[] family = { "article", "author" };  
            Hbase.createTable(tableName, family);
     
      //添加数据 
            String[] column1 = { "title", "content", "tag" };  
            String[] value1 = {  
                    "Head First HBase",  
                    "HBase is the Hadoop database. Use it when you need random, realtime read/write access to your Big Data.",  
                    "Hadoop,HBase,NoSQL" };  
            String[] column2 = { "name", "nickname" };  
            String[] value2 = { "nicholas", "lee" };  
            addData("rowkey1", "blog", column1, value1, column2, value2);  
            addData("rowkey2", "blog", column1, value1, column2, value2);  
            addData("rowkey3", "blog", column1, value1, column2, value2);
     
      }


}
hbast-site.xml 配置
 
   
     hbase.zookeeper.property.clientPort  
      2181  
     
     
     hbase.zookeeper.quorum  
      192.168.168.20  
     
   
 hbase.defaults.for.version  
 0.94.7  
   
   



目录结构



控制台输出

14/10/29 09:43:19 INFO zookeeper.ZooKeeper: Client environment:zookeeper.version=3.4.5-1392090, built on 09/30/2012 17:52 GMT
14/10/29 09:43:19 INFO zookeeper.ZooKeeper: Client environment:host.name=msxit1497.prc.manyue.tech
14/10/29 09:43:19 INFO zookeeper.ZooKeeper: Client environment:java.version=1.6.0_10-rc2
14/10/29 09:43:19 INFO zookeeper.ZooKeeper: Client environment:java.vendor=Sun Microsystems Inc.
14/10/29 09:43:19 INFO zookeeper.ZooKeeper: Client environment:java.home=D:\java\jre6
14/10/29 09:43:19 INFO zookeeper.ZooKeeper: Client environment:java.class.path=E:\workspace\myHadoop\target\classes;E:\workspace\hadoop1.2\lib\hbase-0.94.7-security.jar;E:\workspace\hadoop1.2\lib\hbase-0.94.7-security-tests.jar;C:\Documents and Settings\cczhang\.m2\repository\org\apache\hadoop\hadoop-core\1.1.2\hadoop-core-1.1.2.jar;C:\Documents and Settings\cczhang\.m2\repository\commons-cli\commons-cli\1.2\commons-cli-1.2.jar;C:\Documents and Settings\cczhang\.m2\repository\xmlenc\xmlenc\0.52\xmlenc-0.52.jar;C:\Documents and Settings\cczhang\.m2\repository\com\sun\jersey\jersey-core\1.8\jersey-core-1.8.jar;C:\Documents and Settings\cczhang\.m2\repository\com\sun\jersey\jersey-json\1.8\jersey-json-1.8.jar;C:\Documents and Settings\cczhang\.m2\repository\org\codehaus\jettison\jettison\1.1\jettison-1.1.jar;C:\Documents and Settings\cczhang\.m2\repository\com\sun\xml\bind\jaxb-impl\2.2.3-1\jaxb-impl-2.2.3-1.jar;C:\Documents and Settings\cczhang\.m2\repository\com\sun\jersey\jersey-server\1.8\jersey-server-1.8.jar;C:\Documents and Settings\cczhang\.m2\repository\asm\asm\3.1\asm-3.1.jar;C:\Documents and Settings\cczhang\.m2\repository\commons-io\commons-io\2.1\commons-io-2.1.jar;C:\Documents and Settings\cczhang\.m2\repository\commons-httpclient\commons-httpclient\3.0.1\commons-httpclient-3.0.1.jar;C:\Documents and Settings\cczhang\.m2\repository\commons-codec\commons-codec\1.4\commons-codec-1.4.jar;C:\Documents and Settings\cczhang\.m2\repository\org\apache\commons\commons-math\2.1\commons-math-2.1.jar;C:\Documents and Settings\cczhang\.m2\repository\commons-configuration\commons-configuration\1.6\commons-configuration-1.6.jar;C:\Documents and Settings\cczhang\.m2\repository\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar;C:\Documents and Settings\cczhang\.m2\repository\commons-digester\commons-digester\1.8\commons-digester-1.8.jar;C:\Documents and Settings\cczhang\.m2\repository\commons-beanutils\commons-beanutils\1.7.0\commons-beanutils-1.7.0.jar;C:\Documents and Settings\cczhang\.m2\repository\commons-beanutils\commons-beanutils-core\1.8.0\commons-beanutils-core-1.8.0.jar;C:\Documents and Settings\cczhang\.m2\repository\commons-net\commons-net\1.4.1\commons-net-1.4.1.jar;C:\Documents and Settings\cczhang\.m2\repository\org\mortbay\jetty\jetty\6.1.26\jetty-6.1.26.jar;C:\Documents and Settings\cczhang\.m2\repository\org\mortbay\jetty\servlet-api\2.5-20081211\servlet-api-2.5-20081211.jar;C:\Documents and Settings\cczhang\.m2\repository\org\mortbay\jetty\jetty-util\6.1.26\jetty-util-6.1.26.jar;C:\Documents and Settings\cczhang\.m2\repository\tomcat\jasper-runtime\5.5.12\jasper-runtime-5.5.12.jar;C:\Documents and Settings\cczhang\.m2\repository\tomcat\jasper-compiler\5.5.12\jasper-compiler-5.5.12.jar;C:\Documents and Settings\cczhang\.m2\repository\org\mortbay\jetty\jsp-api-2.1\6.1.14\jsp-api-2.1-6.1.14.jar;C:\Documents and Settings\cczhang\.m2\repository\org\mortbay\jetty\jsp-2.1\6.1.14\jsp-2.1-6.1.14.jar;C:\Documents and Settings\cczhang\.m2\repository\ant\ant\1.6.5\ant-1.6.5.jar;C:\Documents and Settings\cczhang\.m2\repository\commons-el\commons-el\1.0\commons-el-1.0.jar;C:\Documents and Settings\cczhang\.m2\repository\net\java\dev\jets3t\jets3t\0.6.1\jets3t-0.6.1.jar;C:\Documents and Settings\cczhang\.m2\repository\hsqldb\hsqldb\1.8.0.10\hsqldb-1.8.0.10.jar;C:\Documents and Settings\cczhang\.m2\repository\oro\oro\2.0.8\oro-2.0.8.jar;C:\Documents and Settings\cczhang\.m2\repository\org\eclipse\jdt\core\3.1.1\core-3.1.1.jar;C:\Documents and Settings\cczhang\.m2\repository\org\codehaus\jackson\jackson-mapper-asl\1.8.8\jackson-mapper-asl-1.8.8.jar;C:\Documents and Settings\cczhang\.m2\repository\org\apache\hbase\hbase\0.94.7\hbase-0.94.7.jar;C:\Documents and Settings\cczhang\.m2\repository\com\yammer\metrics\metrics-core\2.1.2\metrics-core-2.1.2.jar;C:\Documents and Settings\cczhang\.m2\repository\com\google\guava\guava\11.0.2\guava-11.0.2.jar;C:\Documents and Settings\cczhang\.m2\repository\com\google\code\findbugs\jsr305\1.3.9\jsr305-1.3.9.jar;C:\Documents and Settings\cczhang\.m2\repository\com\github\stephenc\high-scale-lib\high-scale-lib\1.1.1\high-scale-lib-1.1.1.jar;C:\Documents and Settings\cczhang\.m2\repository\commons-lang\commons-lang\2.5\commons-lang-2.5.jar;C:\Documents and Settings\cczhang\.m2\repository\commons-logging\commons-logging\1.1.1\commons-logging-1.1.1.jar;C:\Documents and Settings\cczhang\.m2\repository\log4j\log4j\1.2.16\log4j-1.2.16.jar;C:\Documents and Settings\cczhang\.m2\repository\org\apache\avro\avro\1.5.3\avro-1.5.3.jar;C:\Documents and Settings\cczhang\.m2\repository\org\xerial\snappy\snappy-java\1.0.3.2\snappy-java-1.0.3.2.jar;C:\Documents and Settings\cczhang\.m2\repository\org\apache\avro\avro-ipc\1.5.3\avro-ipc-1.5.3.jar;C:\Documents and Settings\cczhang\.m2\repository\org\jboss\netty\netty\3.2.4.Final\netty-3.2.4.Final.jar;C:\Documents and Settings\cczhang\.m2\repository\org\apache\velocity\velocity\1.7\velocity-1.7.jar;C:\Documents and Settings\cczhang\.m2\repository\org\apache\zookeeper\zookeeper\3.4.5\zookeeper-3.4.5.jar;C:\Documents and Settings\cczhang\.m2\repository\org\apache\thrift\libthrift\0.8.0\libthrift-0.8.0.jar;C:\Documents and Settings\cczhang\.m2\repository\org\apache\httpcomponents\httpclient\4.1.2\httpclient-4.1.2.jar;C:\Documents and Settings\cczhang\.m2\repository\org\apache\httpcomponents\httpcore\4.1.3\httpcore-4.1.3.jar;C:\Documents and Settings\cczhang\.m2\repository\org\jruby\jruby-complete\1.6.5\jruby-complete-1.6.5.jar;C:\Documents and Settings\cczhang\.m2\repository\org\mortbay\jetty\servlet-api-2.5\6.1.14\servlet-api-2.5-6.1.14.jar;C:\Documents and Settings\cczhang\.m2\repository\org\codehaus\jackson\jackson-core-asl\1.8.8\jackson-core-asl-1.8.8.jar;C:\Documents and Settings\cczhang\.m2\repository\org\codehaus\jackson\jackson-jaxrs\1.8.8\jackson-jaxrs-1.8.8.jar;C:\Documents and Settings\cczhang\.m2\repository\org\codehaus\jackson\jackson-xc\1.8.8\jackson-xc-1.8.8.jar;C:\Documents and Settings\cczhang\.m2\repository\org\slf4j\slf4j-api\1.4.3\slf4j-api-1.4.3.jar;C:\Documents and Settings\cczhang\.m2\repository\org\slf4j\slf4j-log4j12\1.4.3\slf4j-log4j12-1.4.3.jar;C:\Documents and Settings\cczhang\.m2\repository\org\jamon\jamon-runtime\2.3.1\jamon-runtime-2.3.1.jar;C:\Documents and Settings\cczhang\.m2\repository\com\google\protobuf\protobuf-java\2.4.0a\protobuf-java-2.4.0a.jar;C:\Documents and Settings\cczhang\.m2\repository\javax\xml\bind\jaxb-api\2.1\jaxb-api-2.1.jar;C:\Documents and Settings\cczhang\.m2\repository\javax\activation\activation\1.1\activation-1.1.jar;C:\Documents and Settings\cczhang\.m2\repository\stax\stax-api\1.0.1\stax-api-1.0.1.jar
14/10/29 09:43:19 INFO zookeeper.ZooKeeper: Client environment:java.library.path=D:\java\jre6\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;D:/Java/jre6/bin/client;D:/Java/jre6/bin;D:/Java/jre6/lib/i386;C:\Program Files\Common Files\NetSarang;D:\oracle\ora92\bin;C:\Program Files\Oracle\jre\1.3.1\bin;C:\Program Files\Oracle\jre\1.1.8\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\SinoVoice\jTTS 5.0 Desktop\Bin;D:\java\jdk1.6.0_10\bin;E:\toolkit\maven3\bin;E:\eclipse;
14/10/29 09:43:19 INFO zookeeper.ZooKeeper: Client environment:java.io.tmpdir=C:\DOCUME~1\cczhang\LOCALS~1\Temp\
14/10/29 09:43:19 INFO zookeeper.ZooKeeper: Client environment:java.compiler=
14/10/29 09:43:19 INFO zookeeper.ZooKeeper: Client environment:os.name=Windows XP
14/10/29 09:43:19 INFO zookeeper.ZooKeeper: Client environment:os.arch=x86
14/10/29 09:43:19 INFO zookeeper.ZooKeeper: Client environment:os.version=5.1
14/10/29 09:43:19 INFO zookeeper.ZooKeeper: Client environment:user.name=cczhang
14/10/29 09:43:19 INFO zookeeper.ZooKeeper: Client environment:user.home=C:\Documents and Settings\cczhang
14/10/29 09:43:19 INFO zookeeper.ZooKeeper: Client environment:user.dir=E:\workspace\myHadoop
14/10/29 09:43:19 INFO zookeeper.ZooKeeper: Initiating client connection, connectString=192.168.168.20:2181 sessionTimeout=180000 watcher=hconnection
14/10/29 09:43:19 INFO zookeeper.ClientCnxn: Opening socket connection to server master/192.168.168.20:2181. Will not attempt to authenticate using SASL (无法定位登录配置)
14/10/29 09:43:19 INFO zookeeper.RecoverableZooKeeper: The identifier of this process is 6048@msxit1497
14/10/29 09:43:19 INFO zookeeper.ClientCnxn: Socket connection established to master/192.168.168.20:2181, initiating session
14/10/29 09:43:19 INFO zookeeper.ClientCnxn: Session establishment complete on server master/192.168.168.20:2181, sessionid = 0x14956144b0b0017, negotiated timeout = 40000
14/10/29 09:43:19 INFO zookeeper.ZooKeeper: Initiating client connection, connectString=192.168.168.20:2181 sessionTimeout=180000 watcher=catalogtracker-on-org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation@1016632
14/10/29 09:43:19 INFO zookeeper.ClientCnxn: Opening socket connection to server master/192.168.168.20:2181. Will not attempt to authenticate using SASL (无法定位登录配置)
14/10/29 09:43:19 INFO zookeeper.ClientCnxn: Socket connection established to master/192.168.168.20:2181, initiating session
14/10/29 09:43:19 INFO zookeeper.ClientCnxn: Session establishment complete on server master/192.168.168.20:2181, sessionid = 0x14956144b0b0018, negotiated timeout = 40000
14/10/29 09:43:19 INFO zookeeper.RecoverableZooKeeper: The identifier of this process is 6048@msxit1497
14/10/29 09:43:19 INFO zookeeper.ClientCnxn: EventThread shut down
14/10/29 09:43:19 INFO zookeeper.ZooKeeper: Session: 0x14956144b0b0018 closed
blogtable created!

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26495863/viewspace-1312682/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/26495863/viewspace-1312682/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值