|
| 1 | +# Google Cloud Java Emulator for Bigtable |
| 2 | + |
| 3 | +A Java wrapper for the [Cloud Bigtable][cloud-bigtable] emulator. This |
| 4 | +wrapper bundles the native Bigtable emulator and exposes a simple Java |
| 5 | +interface to ease writing tests. Please note that this wrapper is under |
| 6 | +heavy development and APIs may change in the future. |
| 7 | + |
| 8 | +[](http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/master.html) |
| 9 | +[](https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-bigtable-emulator.svg) |
| 10 | +[](https://www.codacy.com/app/mziccard/google-cloud-java) |
| 11 | + |
| 12 | +## Quickstart |
| 13 | + |
| 14 | +[//]: # ({x-version-update-start:google-cloud-bom:released}) |
| 15 | +If you are using Maven, add this to your pom.xml file |
| 16 | +```xml |
| 17 | +<dependencyManagement> |
| 18 | + <dependencies> |
| 19 | + <dependency> |
| 20 | + <groupId>com.google.cloud</groupId> |
| 21 | + <artifactId>google-cloud-bom</artifactId> |
| 22 | + <version>0.70.0-alpha</version> |
| 23 | + <type>pom</type> |
| 24 | + <scope>import</scope> |
| 25 | + </dependency> |
| 26 | + </dependencies> |
| 27 | +</dependencyManagement> |
| 28 | + |
| 29 | +<dependencies> |
| 30 | + <dependency> |
| 31 | + <groupId>com.google.cloud</groupId> |
| 32 | + <artifactId>google-cloud-bigtable</artifactId> |
| 33 | + </dependency> |
| 34 | + |
| 35 | + <dependency> |
| 36 | + <groupId>com.google.cloud</groupId> |
| 37 | + <artifactId>google-cloud-bigtable-admin</artifactId> |
| 38 | + </dependency> |
| 39 | + |
| 40 | + <dependency> |
| 41 | + <groupId>com.google.cloud</groupId> |
| 42 | + <artifactId>google-cloud-bigtable-emulator</artifactId> |
| 43 | + <scope>test</scope> |
| 44 | + </dependency> |
| 45 | + |
| 46 | + <dependency> |
| 47 | + <groupId>junit</groupId> |
| 48 | + <artifactId>junit</artifactId> |
| 49 | + <version>4.12</version> |
| 50 | + <scope>test</scope> |
| 51 | + </dependency> |
| 52 | +</dependencies>``` |
| 53 | + |
| 54 | +If you are using Gradle, add this to your dependencies |
| 55 | +```Groovy |
| 56 | +compile 'com.google.cloud:google-cloud-bigtable:0.70.0-alpha' |
| 57 | +compile 'com.google.cloud:google-cloud-bigtable-admin:0.70.0-alpha' |
| 58 | +testCompile 'com.google.cloud:google-cloud-bigtable-emulator:0.70.0-alpha' |
| 59 | +testCompile 'junit:junit:4.12' |
| 60 | +``` |
| 61 | +If you are using SBT, add this to your dependencies |
| 62 | +```Scala |
| 63 | +libraryDependencies += "com.google.cloud" % "google-cloud-bigtable" % "0.70.0-alpha" |
| 64 | +libraryDependencies += "com.google.cloud" % "google-cloud-bigtable-admin" % "0.70.0-alpha" |
| 65 | +libraryDependencies += "com.google.cloud" % "google-cloud-bigtable-emulator" % "0.70.0-alpha" % Test |
| 66 | +libraryDependencies += "junit" % "junit" % "4.12" % Test |
| 67 | +``` |
| 68 | +[//]: # ({x-version-update-end}) |
| 69 | + |
| 70 | +## Getting Started |
| 71 | + |
| 72 | +Here is a code snippet showing a simple JUnit test. Add the following imports |
| 73 | +at the top of your file: |
| 74 | + |
| 75 | +```java |
| 76 | +import com.google.api.core.ApiFuture; |
| 77 | +import com.google.api.gax.core.NoCredentialsProvider; |
| 78 | +import com.google.api.gax.grpc.GrpcTransportChannel; |
| 79 | +import com.google.api.gax.rpc.FixedTransportChannelProvider; |
| 80 | +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; |
| 81 | +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings; |
| 82 | +import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; |
| 83 | +import com.google.cloud.bigtable.data.v2.BigtableDataClient; |
| 84 | +import com.google.cloud.bigtable.data.v2.BigtableDataSettings; |
| 85 | +import com.google.cloud.bigtable.data.v2.models.Row; |
| 86 | +import com.google.cloud.bigtable.data.v2.models.RowMutation; |
| 87 | +import com.google.cloud.bigtable.emulator.v2.BigtableEmulatorRule; |
| 88 | +import java.io.IOException; |
| 89 | +import java.util.concurrent.ExecutionException; |
| 90 | +import org.junit.Assert; |
| 91 | +import org.junit.Before; |
| 92 | +import org.junit.Rule; |
| 93 | +import org.junit.Test; |
| 94 | +import org.junit.runner.RunWith; |
| 95 | +import org.junit.runners.JUnit4; |
| 96 | +``` |
| 97 | + |
| 98 | +Then, to make a query to Bigtable, use the following code: |
| 99 | +```java |
| 100 | +@RunWith(JUnit4.class) |
| 101 | +public class ExampleTest { |
| 102 | + // Initialize the emulator Rule |
| 103 | + @Rule |
| 104 | + public final BigtableEmulatorRule bigtableEmulator = BigtableEmulatorRule.create(); |
| 105 | + |
| 106 | + // Clients that will be connected to the emulator |
| 107 | + private BigtableTableAdminClient tableAdminClient; |
| 108 | + private BigtableDataClient dataClient; |
| 109 | + |
| 110 | + @Before |
| 111 | + public void setUp() throws IOException { |
| 112 | + // Initialize the clients to connect to the emulator |
| 113 | + BigtableTableAdminSettings.Builder tableAdminSettings = BigtableTableAdminSettings.newBuilder() |
| 114 | + .setInstanceName(com.google.bigtable.admin.v2.InstanceName.of("fake-project", "fake-instance")); |
| 115 | + tableAdminSettings.stubSettings() |
| 116 | + .setCredentialsProvider(NoCredentialsProvider.create()) |
| 117 | + .setTransportChannelProvider( |
| 118 | + FixedTransportChannelProvider.create( |
| 119 | + GrpcTransportChannel.create(bigtableEmulator.getAdminChannel()) |
| 120 | + ) |
| 121 | + ); |
| 122 | + tableAdminClient = BigtableTableAdminClient.create(tableAdminSettings.build()); |
| 123 | + |
| 124 | + BigtableDataSettings.Builder dataSettings = BigtableDataSettings.newBuilder() |
| 125 | + .setInstanceName(com.google.cloud.bigtable.data.v2.models.InstanceName.of("fake-project", "fake-instance")) |
| 126 | + .setCredentialsProvider(NoCredentialsProvider.create()) |
| 127 | + .setTransportChannelProvider( |
| 128 | + FixedTransportChannelProvider.create( |
| 129 | + GrpcTransportChannel.create(bigtableEmulator.getDataChannel()) |
| 130 | + ) |
| 131 | + ); |
| 132 | + dataClient = BigtableDataClient.create(dataSettings.build()); |
| 133 | + |
| 134 | + // Create a test table that can be used in tests |
| 135 | + tableAdminClient.createTable( |
| 136 | + CreateTableRequest.of("fake-table") |
| 137 | + .addFamily("cf") |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testWriteRead() throws ExecutionException, InterruptedException { |
| 143 | + ApiFuture<Void> mutateFuture = dataClient.mutateRowAsync( |
| 144 | + RowMutation.create("fake-table", "fake-key") |
| 145 | + .setCell("cf", "col", "value") |
| 146 | + ); |
| 147 | + |
| 148 | + mutateFuture.get(); |
| 149 | + |
| 150 | + ApiFuture<Row> rowFuture = dataClient.readRowAsync("fake-table", "fake-key"); |
| 151 | + |
| 152 | + Assert.assertEquals("value", rowFuture.get().getCells().get(0).getValue().toStringUtf8()); |
| 153 | + } |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +## Java Versions |
| 158 | + |
| 159 | +Java 7 or above is required for using this emulator. |
| 160 | + |
| 161 | +## Versioning |
| 162 | + |
| 163 | +This library follows [Semantic Versioning](http://semver.org/). |
| 164 | + |
| 165 | +It is currently in major version zero (`0.y.z`), which means that anything may |
| 166 | +change at any time and the public API should not be considered stable. |
| 167 | + |
| 168 | +## Contributing |
| 169 | + |
| 170 | +Contributions to this library are always welcome and highly encouraged. |
| 171 | + |
| 172 | +See [CONTRIBUTING] for more information on how to get started and [DEVELOPING] for a layout of the |
| 173 | +codebase. |
| 174 | + |
| 175 | +## License |
| 176 | + |
| 177 | +Apache 2.0 - See [LICENSE] for more information. |
| 178 | + |
| 179 | +[CONTRIBUTING]:https://github.com/googleapis/google-cloud-java/blob/master/CONTRIBUTING.md |
| 180 | +[LICENSE]: https://github.com/googleapis/google-cloud-java/blob/master/LICENSE |
| 181 | +[cloud-bigtable]: https://cloud.google.com/bigtable/ |
| 182 | + |
0 commit comments