《HBase伪分布式集群安装及代码测试 - 数据库服务器.docx》由会员分享,可在线阅读,更多相关《HBase伪分布式集群安装及代码测试 - 数据库服务器.docx(22页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、HBase伪分布式集群安装及代码测试-数据库服务器-最新IT资讯_电脑知识大全_网络安全教程-次元立方网exportHBASE_HOME=/home/developer/app/hbase-1.2.0-cdh5.7.1exportPATH=$PATH:$HBASE_HOME/bin$source.bashrc3、编辑hbase-env.sh文件$cdapp/hbase-1.2.0-cdh5.7.1/conf/$vimhbase-env.shexportJAVA_HOME=/home/developer/app/jdk1.7.0_79exportHBASE_CLASSPATH=/home/dev
2、eloper/app/hadoop-2.6.0-cdh5.7.1/etc/hadoop4、编辑hbase-site.xml文件$vimhbase-site.xmlconfigurationpropertynamehbase.rootdir/namevaluehdfs:/localhost:9000/hbase/value/propertypropertynamehbase.cluster.distributed/namevaluetrue/value/propertypropertynamehbase.tmp.dir/namevalue/home/developer/app/hbase-1.2
3、.0-cdh5.7.1/tmp/value/propertypropertynamedfs.replication/namevalue1/value/propertypropertynamehbase.zookeeper.quorum/namevaluelocalhost/value/propertypropertynamehbase.zookeeper.property.clientPort/namevalue2222/value/property/configuration5、启动$start-hbase.sh6、关闭$stop-hbase.sh7、WebUI:/localhost:600
4、108、解决HBase中SLF4J的jar包与Hadoop冲突问题描绘:SLF4J:ClasspathcontainsmultipleSLF4Jbindings.SLF4J:Foundbindinginjar:file:/home/developer/app/hbase-1.2.0-cdh5.7.1/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.classSLF4J:Foundbindinginjar:file:/home/developer/app/hadoop-2.6.0-cdh5.7.1/share/hado
5、op/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.classSLF4J:See:/slf4j.org/codes.#multiple_bindingsforanexplanation.SLF4J:Actualbindingisoftypeorg.slf4j.impl.Log4jLoggerFactory解决方法:$cdapp/hbase-1.2.0-cdh5.7.1/lib/$rmslf4j-log4j12-1.7.5.jar二、代码测试1、maven依靠repositoriesrepository
6、idcloudera/idurls:/repository.cloudera/artifactory/cloudera-repos/url/repository/repositoriesdependenciesdependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion3.8.1/versionscopetest/scope/dependencydependencygroupIdorg.apache.hadoop/groupIdartifactIdhadoop-common/artifactIdversion2.6.0-cdh5
7、.7.1/version/dependencydependencygroupIdorg.apache.hadoop/groupIdartifactIdhadoop-hdfs/artifactIdversion2.6.0-cdh5.7.1/version/dependencydependencygroupIdorg.apache.hbase/groupIdartifactIdhbase-client/artifactIdversion1.2.0-cdh5.7.1/version/dependency/dependencies2、测试代码packagecom.hbase.demo;importja
8、va.io.IOException;publicclassHBaseTestprivatestaticfinalStringTABLE_NAME=studentsprivatestaticfinalStringFAMILY_COL_SCORE=scoreprivatestaticfinalStringFAMILY_COL_INFO=infoprivateConfigurationconf;publicstaticvoidmain(Stringargs)HBaseTesttest=newHBaseTest();test.init();test.createTable();test.insertD
9、ata();test.scanTable();test.queryByRowKey();/test.deleteRow();/test.deleteFamily();/test.deleteTable();*初始化hbase的配置信息,可以以将hbase的配置文件hbase-site.xml引入项目,则不需要在代码中填写配置信息publicvoidinit()conf=HBaseConfiguration.create();conf.set(hbase.zookeeper.quorum,localhostconf.set(hbase.zookeeper.property.clientPort,
10、2222*创立表publicvoidcreateTable()Connectionconn=null;tryconn=ConnectionFactory.createConnection(conf);HBaseAdminadmin=(HBaseAdmin)conn.getAdmin();HTableDescriptordesc=newHTableDescriptor(TableName.valueOf(TABLE_NAME);desc.addFamily(newHColumnDescriptor(FAMILY_COL_SCORE);desc.addFamily(newHColumnDescri
11、ptor(FAMILY_COL_INFO);if(admin.tableExists(TABLE_NAME)System.out.println(table+TABLE_NAME+isexists!System.exit(0);elseadmin.createTable(desc);System.out.println(table+TABLE_NAME+createdsuccessfully.catch(IOExceptione)e.printStackTrace();finallyif(conn!=null)tryconn.close();catch(IOExceptione)e.print
12、StackTrace();*在指定表中插入数据publicvoidinsertData()Connectionconn=null;HTabletable=null;tryconn=ConnectionFactory.createConnection(conf);table=(HTable)conn.getTable(TableName.valueOf(TABLE_NAME);ListPutputs=newArrayListPut/添加数据,一个Put代表一行,构造函数传入的是RowKeyPutput1=newPut(Bytes.toBytes(001:carl);put1.addColumn(
13、Bytes.toBytes(FAMILY_COL_SCORE),Bytes.toBytes(english),Bytes.toBytes(97);put1.addColumn(Bytes.toBytes(FAMILY_COL_SCORE),Bytes.toBytes(math),Bytes.toBytes(99);put1.addColumn(Bytes.toBytes(FAMILY_COL_INFO),Bytes.toBytes(weight),Bytes.toBytes(130);puts.add(put1);Putput2=newPut(Bytes.toBytes(002:sophie)
14、;put2.addColumn(Bytes.toBytes(FAMILY_COL_SCORE),Bytes.toBytes(english),Bytes.toBytes(100);put2.addColumn(Bytes.toBytes(FAMILY_COL_SCORE),Bytes.toBytes(math),Bytes.toBytes(92);put2.addColumn(Bytes.toBytes(FAMILY_COL_INFO),Bytes.toBytes(weight),Bytes.toBytes(102);puts.add(put2);/将数据参加表table.put(puts);
15、catch(Exceptione)e.printStackTrace();finallyif(table!=null)trytable.close();catch(IOExceptione)e.printStackTrace();if(conn!=null)tryconn.close();catch(IOExceptione)e.printStackTrace();*在指定表中查询所有数据(全表扫描)publicvoidscanTable()Connectionconn=null;HTabletable=null;ResultScannerscann=null;tryconn=Connecti
16、onFactory.createConnection(conf);table=(HTable)conn.getTable(TableName.valueOf(TABLE_NAME);scann=table.getScanner(newScan();for(Resultrs:scann)System.out.println(该行数据的RowKey为:+newString(rs.getRow();for(Cellcell:rs.rawCells()System.out.println(列族:+newString(CellUtil.cloneFamily(cell)+列修饰符:+newString(
17、CellUtil.cloneQualifier(cell)+值:+newString(CellUtil.cloneValue(cell)+时间戳:+cell.getTimestamp();System.out.println(-catch(Exceptione)e.printStackTrace();finallyif(table!=null)trytable.close();catch(IOExceptione)e.printStackTrace();if(conn!=null)tryconn.close();catch(IOExceptione)e.printStackTrace();*通
18、过rowkey在指定表中查询一行数据publicvoidqueryByRowKey()Connectionconn=null;HTabletable=null;tryconn=ConnectionFactory.createConnection(conf);table=(HTable)conn.getTable(TableName.valueOf(TABLE_NAME);Getget=newGet(001:carl.getBytes();/get.setMaxVersions(2);/get.addColumn(FAMILY_COL_SCORE.getBytes(),english.getBy
19、tes();Resultrs=table.get(get);System.out.println(表+TABLE_NAME+中RowKey为001:carl的行数据如下for(Cellcell:rs.rawCells()System.out.println(列族:+newString(CellUtil.cloneFamily(cell)+列修饰符:+newString(CellUtil.cloneQualifier(cell)+值:+newString(CellUtil.cloneValue(cell)+时间戳:+cell.getTimestamp();catch(Exceptione)e.p
20、rintStackTrace();finallyif(table!=null)trytable.close();catch(IOExceptione)e.printStackTrace();if(conn!=null)tryconn.close();catch(IOExceptione)e.printStackTrace();*删除表中指定RowKey的行publicvoiddeleteRow()Connectionconn=null;HTabletable=null;tryconn=ConnectionFactory.createConnection(conf);table=(HTable)
21、conn.getTable(TableName.valueOf(TABLE_NAME);table.delete(newDelete(001:carl.getBytes();catch(Exceptione)e.printStackTrace();finallytryif(table!=null)table.close();if(conn!=null)conn.close();catch(Exceptione)e.printStackTrace();*删除指定名称的列族publicvoiddeleteFamily()Connectionconn=null;HBaseAdminadmin=nul
22、l;tryconn=ConnectionFactory.createConnection(conf);admin=(HBaseAdmin)conn.getAdmin();admin.deleteColumn(TABLE_NAME.getBytes(),FAMILY_COL_INFO);catch(Exceptione)e.printStackTrace();finallytryif(null!=conn)conn.close();catch(Exceptione)e.printStackTrace();*删除指定表publicvoiddeleteTable()Connectionconn=nu
23、ll;HBaseAdminadmin=null;tryconn=ConnectionFactory.createConnection(conf);admin=(HBaseAdmin)conn.getAdmin();/在删除一张表前,要先使其失效admin.disableTable(TABLE_NAME);admin.deleteTable(TABLE_NAME);catch(Exceptione)e.printStackTrace();finallytryif(conn!=null)conn.close();catch(Exceptione)e.printStackTrace();3、Eclipse运行结果4、HBase的shell终端查看结果