irpas技术客

大数据基本开发(一)Hbase 2.4.4 配置eclipse开发环境_confsky_eclipse hbase

网络 1796

Hbase 2.4.4 配置eclipse开发环境附示例代码 eclipse 环境配置环境要求创建Java项目 示例代码参考链接

eclipse 环境配置 环境要求

Hadoop 3.2.2 Hbase 2.4.10 eclipse Java 1.8

创建Java项目 打开eclipsefile ->project选择Java目录里的Java Project,然后next输入项目名,将jre版本设置为1.8,然后选择finish.在项目目录下(非src目录下)新建文件夹conf,将hbase配置文件hbase-site.xml复制到该目录下,然后在项目名右键,选择build path->configure build path,在右侧点击add class folder,选中刚才新建的conf文件夹,然后ok,最后选择apply and close。打开上边的build path界面,点击右侧add external jars,选择hbase安装目录下的 lib文件夹里的jar包,如没有特别需求,全部选中,并且lib目录里还有五个子文件夹,里边的jar包也要选中。 完成后如下图,点击apply and close 示例代码 在上述项目内新建package 包 命名为example在example包内新建class类 BaseDemo,复制以下代码 package example; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor; import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.TableDescriptorBuilder; import org.apache.hadoop.hbase.util.Bytes; public class BaseDemo { public Connection getConnection() throws IOException { final Configuration configuration = HBaseConfiguration.create(); //必选参数 configuration.set("hbase.zookeeper.quorum","localhost");//localhost可改成自己的主机名 configuration.set("hbase.zookeeper.property.clientPort", "2181"); //其他可选参数 // configuration.set("hbase.client.scanner.timeout.period", "10000"); // configuration.set("hbase.hconnection.threads.max", "0"); // configuration.set("hbase.hconnection.threads.core", "0"); // configuration.set("hbase.rpc.timeout", "60000"); return ConnectionFactory.createConnection(configuration); } //查询表是否存在。默认使用default命名空间。其他命名空间需要传入 {namespace}:tableName public Boolean isTableExist(Connection connection,String tableName) throws IOException { final HBaseAdmin admin = (HBaseAdmin) connection.getAdmin(); return admin.tableExists(TableName.valueOf(tableName)); } public void createTable(Connection connection,String tableName,String... columnFamilys) throws IOException { final HBaseAdmin admin = (HBaseAdmin) connection.getAdmin(); if(columnFamilys.length<=0){ System.out.println("请设置列簇信息"); }else if (admin.tableExists(TableName.valueOf(tableName))) { System.out.println("表"+tableName+"已经存在。"); } else{ //2.0.0版本新的API。官网说明2.0.0版本有很多API已经过时,预计会在3.0版本后删除。如HTableDescriptor等 TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName)); List<ColumnFamilyDescriptor> families = new ArrayList<>(); for(String columnFamily : columnFamilys){ final ColumnFamilyDescriptorBuilder cfdBuilder = ColumnFamilyDescriptorBuilder.newBuilder(ColumnFamilyDescriptorBuilder.of(columnFamily)); final ColumnFamilyDescriptor cfd = cfdBuilder.setMaxVersions(10).setInMemory(true).setBlocksize(8 * 1024) .setScope(HConstants.REPLICATION_SCOPE_LOCAL).build(); families.add(cfd); } final TableDescriptor tableDescriptor = builder.setColumnFamilies(families).build(); admin.createTable(tableDescriptor); } } public void dropTable(Connection connection,String tableName) throws IOException { final HBaseAdmin admin = (HBaseAdmin) connection.getAdmin(); if(admin.tableExists(TableName.valueOf(tableName))){ admin.disableTable(TableName.valueOf(tableName)); admin.deleteTable(TableName.valueOf(tableName)); System.out.println("表"+tableName+"成功删除"); }else { System.out.println("表"+tableName+"不存在"); } } public void addRowData(Connection connection,String tableName,String rowkey, String columnFamily,String column,String value) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); //构建Put指令,rowkey必传 Put put = new Put(Bytes.toBytes(rowkey)); //指定版本时间戳 //put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column),System.currentTimeMillis(),Bytes.toBytes(value)); put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column),Bytes.toBytes(value)); //也可以传入一个List<Put> 插入多列数据 table.put(put); //表操作之后记得要close,关闭线程池 table.close(); } public void deleteMultiRow(Connection connection,String tableName,String... rows) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); List<Delete> deletes = new ArrayList<>(); for(String row: rows){ Delete delete = new Delete(Bytes.toBytes(row)); deletes.add(delete); } if(deletes.size()>0){ table.delete(deletes); } table.close(); System.out.println("表数据删除成功"); } public void getRowData(Connection connection,String tableName,String row) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(row)); final Result result = table.get(get); for (Cell cell : result.rawCells()) { System.out.println("rowkey:"+Bytes.toString(CellUtil.cloneRow(cell))); System.out.println(" 列簇:"+Bytes.toString(CellUtil.cloneFamily(cell))); System.out.println(" 列:"+Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.println(" 值:"+Bytes.toString(CellUtil.cloneValue(cell))); System.out.println(" 版本时间戳:"+cell.getTimestamp()); System.out.println("=========================="); } table.close(); } public void scanRowData(Connection connection,String tableName,String startRow,String stopRow) throws IOException{ Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); if(null != startRow && !"".equals(startRow)){ scan.withStartRow(Bytes.toBytes(startRow)); } if(null != stopRow && !"".equals(stopRow)){ //包含最后一行数据。 默认是左开右闭。 scan.withStopRow(Bytes.toBytes(stopRow),true); } ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { for (Cell cell : result.rawCells()) { System.out.println("rowkey:"+Bytes.toString(CellUtil.cloneRow(cell))); System.out.println(" 列簇:"+Bytes.toString(CellUtil.cloneFamily(cell))); System.out.println(" 列:"+Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.println(" 值:"+Bytes.toString(CellUtil.cloneValue(cell))); System.out.println(" 版本时间戳:"+cell.getTimestamp()); System.out.println("=========================="); } System.out.println("-----------------------"); } table.close(); } } 在example包下新建class DemoTest,如下代码 package example; import java.io.IOException; import org.apache.hadoop.hbase.client.Connection; public class DemoTest { public static void main(String [] arsg) throws IOException { BaseDemo baseDemo = new BaseDemo(); final Connection connection = baseDemo.getConnection(); String tableName = "mytable"; String basicCF="basicinfo"; String studyCF="studyinfo"; //建表 baseDemo.createTable(connection,tableName,new String[]{"basicinfo","studyinfo"}); //删除表 // baseDemo.dropTable(connection,tableName); //插入数据 baseDemo.addRowData(connection,tableName,"1001",basicCF,"age","10"); baseDemo.addRowData(connection,tableName,"1001",basicCF,"name","roy"); baseDemo.addRowData(connection,tableName,"1001",studyCF,"grade","100"); baseDemo.addRowData(connection,tableName,"1002",basicCF,"age","11"); baseDemo.addRowData(connection,tableName,"1002",basicCF,"name","yula"); baseDemo.addRowData(connection,tableName,"1002",studyCF,"grade","99"); baseDemo.addRowData(connection,tableName,"1003",basicCF,"age","12"); baseDemo.addRowData(connection,tableName,"1003",basicCF,"name","sophia"); baseDemo.addRowData(connection,tableName,"1003",studyCF,"grade","120"); //删除数据 // baseDemo.deleteMultiRow(connection,tableName,new String[]{"1003"}); //读取某一行数据 baseDemo.getRowData(connection,tableName,"1001"); //搜索数据 baseDemo.scanRowData(connection,tableName,null,null); connection.close(); } } 运行代码,可以得到以下结果

参考链接

https://blog.csdn.net/xiangxizhishi/article/details/77909716

https://blog.csdn.net/roykingw/article/details/119602919


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #Eclipse #HBase #大数据基本开发一Hbase #244 #配置eclipse开发环境