public static void main(String[] args) throws IOException { Configuration conf = HBaseConfiguration.create(); HBaseHelper helper = HBaseHelper.getHelper(conf); helper.dropTable("testtable"); helper.createTable("testtable", "colfam1", "colfam2", "colfam3", "colfam4"); System.out.println("Adding rows to table..."); helper.fillTable("testtable", 1, 10, 2, "colfam1", "colfam2", "colfam3", "colfam4"); HTable table = new HTable(conf, "testtable"); // vv FamilyFilterExample Filter filter1 = new FamilyFilter( CompareFilter.CompareOp .LESS, // co FamilyFilterExample-1-Filter Create filter, while specifying the // comparison operator and comparator. new BinaryComparator(Bytes.toBytes("colfam3"))); Scan scan = new Scan(); scan.setFilter(filter1); ResultScanner scanner = table.getScanner( scan); // co FamilyFilterExample-2-Scan Scan over table while applying the filter. // ^^ FamilyFilterExample System.out.println("Scanning table... "); // vv FamilyFilterExample for (Result result : scanner) { System.out.println(result); } scanner.close(); Get get1 = new Get(Bytes.toBytes("row-5")); get1.setFilter(filter1); Result result1 = table.get(get1); // co FamilyFilterExample-3-Get Get a row while applying the same filter. System.out.println("Result of get(): " + result1); Filter filter2 = new FamilyFilter( CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("colfam3"))); Get get2 = new Get( Bytes.toBytes( "row-5")); // co FamilyFilterExample-4-Mismatch Create a filter on one column family // while trying to retrieve another. get2.addFamily(Bytes.toBytes("colfam1")); get2.setFilter(filter2); Result result2 = table.get( get2); // co FamilyFilterExample-5-Get2 Get the same row while applying the new filter, // this will return "NONE". System.out.println("Result of get(): " + result2); // ^^ FamilyFilterExample }
public static void main(String[] args) throws IOException { Configuration conf = HBaseConfiguration.create(); HBaseHelper helper = HBaseHelper.getHelper(conf); helper.dropTable("testtable"); helper.createTable("testtable", "colfam1", "colfam2"); System.out.println("Adding rows to table..."); helper.fillTable("testtable", 1, 10, 10, "colfam1", "colfam2"); Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(TableName.valueOf("testtable")); // vv SingleColumnValueFilterExample SingleColumnValueFilter filter = new SingleColumnValueFilter( Bytes.toBytes("colfam1"), Bytes.toBytes("col-5"), CompareFilter.CompareOp.NOT_EQUAL, new SubstringComparator("val-5")); filter.setFilterIfMissing(true); Scan scan = new Scan(); scan.setFilter(filter); ResultScanner scanner = table.getScanner(scan); // ^^ SingleColumnValueFilterExample System.out.println("Results of scan:"); // vv SingleColumnValueFilterExample for (Result result : scanner) { for (Cell cell : result.rawCells()) { System.out.println( "Cell: " + cell + ", Value: " + Bytes.toString( cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } } scanner.close(); Get get = new Get(Bytes.toBytes("row-6")); get.setFilter(filter); Result result = table.get(get); System.out.println("Result of get: "); for (Cell cell : result.rawCells()) { System.out.println( "Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } // ^^ SingleColumnValueFilterExample }
public static void main(String[] args) throws IOException { Configuration conf = HBaseConfiguration.create(); HBaseHelper helper = HBaseHelper.getHelper(conf); helper.dropTable("testtable"); helper.createTable("testtable", "colfam1"); Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(TableName.valueOf("testtable")); List<Put> puts = new ArrayList<Put>(); // vv PutListErrorExample2 Put put1 = new Put(Bytes.toBytes("row1")); put1.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"), Bytes.toBytes("val1")); puts.add(put1); Put put2 = new Put(Bytes.toBytes("row2")); put2.addColumn(Bytes.toBytes("BOGUS"), Bytes.toBytes("qual1"), Bytes.toBytes("val2")); puts.add(put2); Put put3 = new Put(Bytes.toBytes("row2")); put3.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2"), Bytes.toBytes("val3")); puts.add(put3); /*[*/ Put put4 = new Put(Bytes.toBytes("row2")); puts.add( put4); /*]*/ // co PutListErrorExample2-1-AddErrorPut Add put with no content at all to // list. /*[*/ try { /*]*/ table.put(puts); /*[*/ } catch (Exception e) { System.err.println("Error: " + e); // table.flushCommits(); // todo: FIX! /*]*/ // co PutListErrorExample2-2-Catch Catch local exception and commit queued updates. /*[*/ } /*]*/ // ^^ PutListErrorExample2 table.close(); connection.close(); helper.close(); }
public static void main(String[] args) throws IOException, InterruptedException { Configuration conf = HBaseConfiguration.create(); HBaseHelper helper = HBaseHelper.getHelper(conf); helper.dropTable("testtable"); Connection connection = ConnectionFactory.createConnection(conf); Admin admin = connection.getAdmin(); // vv CreateTableWithNamespaceExample /*[*/ NamespaceDescriptor namespace = NamespaceDescriptor.create("testspace").build(); admin.createNamespace(namespace); /*]*/ TableName tableName = TableName.valueOf("testspace", "testtable"); HTableDescriptor desc = new HTableDescriptor(tableName); HColumnDescriptor coldef = new HColumnDescriptor(Bytes.toBytes("colfam1")); desc.addFamily(coldef); admin.createTable(desc); // ^^ CreateTableWithNamespaceExample boolean avail = admin.isTableAvailable(tableName); System.out.println("Table available: " + avail); }
public static void main(String[] args) throws IOException { Configuration conf = HBaseConfiguration.create(); conf.set( "hbase.zookeeper.quorum", "master-1.internal.larsgeorge.com," + "master-2.internal.larsgeorge.com,master-3.internal.larsgeorge.com"); HBaseHelper helper = HBaseHelper.getHelper(conf); helper.dropTable("testtable"); helper.createTable("testtable", "colfam1"); System.out.println("Adding rows to table..."); helper.fillTable("testtable", 1, 5, 1, "colfam1"); System.out.println("Table before the operations:"); helper.dump("testtable"); Connection connection = ConnectionFactory.createConnection(conf); TableName tableName = TableName.valueOf("testtable"); Table table = connection.getTable(tableName); // vv ScanConsistencyExample1 Scan scan = new Scan(); scan.setCaching( 1); // co ScanConsistencyExample1-1-ConfScan Configure scan to iterate over each row // separately. ResultScanner scanner = table.getScanner(scan); // ^^ ScanConsistencyExample1 System.out.println("Starting scan, reading one row..."); // vv ScanConsistencyExample1 Result result = scanner.next(); helper.dumpResult(result); // ^^ ScanConsistencyExample1 System.out.println("Applying mutations..."); // vv ScanConsistencyExample1 Put put = new Put(Bytes.toBytes("row-3")); put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("col-1"), Bytes.toBytes("val-999")); table.put(put); // co ScanConsistencyExample1-2-Put Update a later row with a new value. Delete delete = new Delete(Bytes.toBytes("row-4")); table.delete( delete); // co ScanConsistencyExample1-3-Delete Remove an entire row, that is located at the // end of the scan. // ^^ ScanConsistencyExample1 System.out.println("Resuming original scan..."); // vv ScanConsistencyExample1 for (Result result2 : scanner) { helper.dumpResult( result2); // co ScanConsistencyExample1-4-Scan Scan the rest of the table to see if the // mutations are visible. } scanner.close(); // ^^ ScanConsistencyExample1 System.out.println("Print table under new scanner..."); // vv ScanConsistencyExample1 helper.dump( "testtable"); // co ScanConsistencyExample1-5-Dump Print the entire table again, with a new // scanner instance. // ^^ ScanConsistencyExample1 table.close(); connection.close(); helper.close(); }